]> git.sesse.net Git - movit/blob - util.cpp
Allow data files to be fetched somewhere else than the current directory.
[movit] / util.cpp
1 #include <stdio.h>
2 #include <math.h>
3 #include <string.h>
4 #include <assert.h>
5 #include <GL/glew.h>
6
7 #include "util.h"
8 #include "init.h"
9
10 extern std::string *movit_data_directory;
11
12 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b)
13 {
14         if (h < 0.0f) {
15                 h += 2.0f * M_PI;
16         }
17         float c = v * s;
18         float hp = (h * 180.0 / M_PI) / 60.0;
19         float x = c * (1 - fabs(fmod(hp, 2.0f) - 1.0f));
20
21         if (hp >= 0 && hp < 1) {
22                 *r = c;
23                 *g = x;
24                 *b = 0.0f;
25         } else if (hp >= 1 && hp < 2) {
26                 *r = x;
27                 *g = c;
28                 *b = 0.0f;
29         } else if (hp >= 2 && hp < 3) {
30                 *r = 0.0f;
31                 *g = c;
32                 *b = x;
33         } else if (hp >= 3 && hp < 4) {
34                 *r = 0.0f;
35                 *g = x;
36                 *b = c;
37         } else if (hp >= 4 && hp < 5) {
38                 *r = x;
39                 *g = 0.0f;
40                 *b = c;
41         } else {
42                 *r = c;
43                 *g = 0.0f;
44                 *b = x;
45         }
46
47         float m = v - c;
48         *r += m;
49         *g += m;
50         *b += m;
51 }
52
53 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b)
54 {
55         float ref_r, ref_g, ref_b;
56         hsv2rgb(h, s, v, r, g, b);
57         hsv2rgb(h, 0.0f, v, &ref_r, &ref_g, &ref_b);
58         float lum = 0.2126 * *r + 0.7152 * *g + 0.0722 * *b;
59         float ref_lum = 0.2126 * ref_r + 0.7152 * ref_g + 0.0722 * ref_b;
60         if (lum > 1e-3) {
61                 float fac = ref_lum / lum;
62                 *r *= fac;
63                 *g *= fac;
64                 *b *= fac;
65         }
66 }
67
68 std::string read_file(const std::string &filename)
69 {
70         const std::string full_pathname = *movit_data_directory + "/" + filename;
71
72         static char buf[131072];
73         FILE *fp = fopen(full_pathname.c_str(), "r");
74         if (fp == NULL) {
75                 perror(full_pathname.c_str());
76                 exit(1);
77         }
78
79         int len = fread(buf, 1, sizeof(buf), fp);
80         fclose(fp);
81
82         return std::string(buf, len);
83 }
84
85 GLuint compile_shader(const std::string &shader_src, GLenum type)
86 {
87         GLuint obj = glCreateShader(type);
88         const GLchar* source[] = { shader_src.data() };
89         const GLint length[] = { (GLint)shader_src.size() };
90         glShaderSource(obj, 1, source, length);
91         glCompileShader(obj);
92
93         GLchar info_log[4096];
94         GLsizei log_length = sizeof(info_log) - 1;
95         glGetShaderInfoLog(obj, log_length, &log_length, info_log);
96         info_log[log_length] = 0; 
97         if (strlen(info_log) > 0) {
98                 printf("shader compile log: %s\n", info_log);
99         }
100
101         GLint status;
102         glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
103         if (status == GL_FALSE) {
104                 exit(1);
105         }
106
107         return obj;
108 }
109
110 void print_3x3_matrix(const Eigen::Matrix3d& m)
111 {
112         printf("%6.4f %6.4f %6.4f\n", m(0,0), m(0,1), m(0,2));
113         printf("%6.4f %6.4f %6.4f\n", m(1,0), m(1,1), m(1,2));
114         printf("%6.4f %6.4f %6.4f\n", m(2,0), m(2,1), m(2,2));
115         printf("\n");
116 }
117
118 std::string output_glsl_mat3(const std::string &name, const Eigen::Matrix3d &m)
119 {
120         char buf[1024];
121         sprintf(buf,
122                 "const mat3 %s = mat3(\n"
123                 "    %.8f, %.8f, %.8f,\n"
124                 "    %.8f, %.8f, %.8f,\n"
125                 "    %.8f, %.8f, %.8f);\n\n",
126                 name.c_str(),
127                 m(0,0), m(1,0), m(2,0),
128                 m(0,1), m(1,1), m(2,1),
129                 m(0,2), m(1,2), m(2,2));
130         return buf;
131 }
132
133 void combine_two_samples(float w1, float w2, float *offset, float *total_weight, float *sum_sq_error)
134 {
135         assert(movit_initialized);
136         assert(w1 * w2 >= 0.0f);  // Should not have differing signs.
137         float z;  // Just a shorter name for offset.
138         if (fabs(w1 + w2) < 1e-6) {
139                 z = 0.5f;
140         } else {
141                 z = w2 / (w1 + w2);
142         }
143
144         // Round to the minimum number of bits we have measured earlier.
145         // The card will do this for us anyway, but if we know what the real z
146         // is, we can pick a better total_weight below.
147         z = lrintf(z / movit_texel_subpixel_precision) * movit_texel_subpixel_precision;
148         
149         // Choose total weight w so that we minimize total squared error
150         // for the effective weights:
151         //
152         //   e = (w(1-z) - a)² + (wz - b)²
153         //
154         // Differentiating by w and setting equal to zero:
155         //
156         //   2(w(1-z) - a)(1-z) + 2(wz - b)z = 0
157         //   w(1-z)² - a(1-z) + wz² - bz = 0
158         //   w((1-z)² + z²) = a(1-z) + bz
159         //   w = (a(1-z) + bz) / ((1-z)² + z²)
160         //
161         // If z had infinite precision, this would simply reduce to w = w1 + w2.
162         *total_weight = (w1 * (1 - z) + w2 * z) / (z * z + (1 - z) * (1 - z));
163         *offset = z;
164
165         if (sum_sq_error != NULL) {
166                 float err1 = *total_weight * (1 - z) - w1;
167                 float err2 = *total_weight * z - w2;
168                 *sum_sq_error = err1 * err1 + err2 * err2;
169         }
170
171         assert(*offset >= 0.0f);
172         assert(*offset <= 1.0f);
173 }