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