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