12 #if defined(__DARWIN__)
13 #include <OpenGL/OpenGL.h>
24 extern string *movit_data_directory;
26 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b)
32 float hp = (h * 180.0 / M_PI) / 60.0;
33 float x = c * (1 - fabs(fmod(hp, 2.0f) - 1.0f));
35 if (hp >= 0 && hp < 1) {
39 } else if (hp >= 1 && hp < 2) {
43 } else if (hp >= 2 && hp < 3) {
47 } else if (hp >= 3 && hp < 4) {
51 } else if (hp >= 4 && hp < 5) {
67 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b)
69 float ref_r, ref_g, ref_b;
70 hsv2rgb(h, s, v, r, g, b);
71 hsv2rgb(h, 0.0f, v, &ref_r, &ref_g, &ref_b);
72 float lum = 0.2126 * *r + 0.7152 * *g + 0.0722 * *b;
73 float ref_lum = 0.2126 * ref_r + 0.7152 * ref_g + 0.0722 * ref_b;
75 float fac = ref_lum / lum;
82 string read_file(const string &filename)
84 const string full_pathname = *movit_data_directory + "/" + filename;
86 static char buf[131072];
87 FILE *fp = fopen(full_pathname.c_str(), "r");
89 perror(full_pathname.c_str());
93 int len = fread(buf, 1, sizeof(buf), fp);
96 return string(buf, len);
99 GLuint compile_shader(const string &shader_src, GLenum type)
101 GLuint obj = glCreateShader(type);
102 const GLchar* source[] = { shader_src.data() };
103 const GLint length[] = { (GLint)shader_src.size() };
104 glShaderSource(obj, 1, source, length);
105 glCompileShader(obj);
107 GLchar info_log[4096];
108 GLsizei log_length = sizeof(info_log) - 1;
109 glGetShaderInfoLog(obj, log_length, &log_length, info_log);
110 info_log[log_length] = 0;
111 if (strlen(info_log) > 0) {
112 fprintf(stderr, "Shader compile log: %s\n", info_log);
116 glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
117 if (status == GL_FALSE) {
124 void print_3x3_matrix(const Eigen::Matrix3d& m)
126 printf("%6.4f %6.4f %6.4f\n", m(0,0), m(0,1), m(0,2));
127 printf("%6.4f %6.4f %6.4f\n", m(1,0), m(1,1), m(1,2));
128 printf("%6.4f %6.4f %6.4f\n", m(2,0), m(2,1), m(2,2));
132 string output_glsl_mat3(const string &name, const Eigen::Matrix3d &m)
136 "const mat3 %s = mat3(\n"
137 " %.8f, %.8f, %.8f,\n"
138 " %.8f, %.8f, %.8f,\n"
139 " %.8f, %.8f, %.8f);\n\n",
141 m(0,0), m(1,0), m(2,0),
142 m(0,1), m(1,1), m(2,1),
143 m(0,2), m(1,2), m(2,2));
147 void combine_two_samples(float w1, float w2, float *offset, float *total_weight, float *sum_sq_error)
149 assert(movit_initialized);
150 assert(w1 * w2 >= 0.0f); // Should not have differing signs.
151 float z; // Just a shorter name for offset.
152 if (fabs(w1 + w2) < 1e-6) {
158 // Round to the minimum number of bits we have measured earlier.
159 // The card will do this for us anyway, but if we know what the real z
160 // is, we can pick a better total_weight below.
161 z = lrintf(z / movit_texel_subpixel_precision) * movit_texel_subpixel_precision;
163 // Choose total weight w so that we minimize total squared error
164 // for the effective weights:
166 // e = (w(1-z) - a)² + (wz - b)²
168 // Differentiating by w and setting equal to zero:
170 // 2(w(1-z) - a)(1-z) + 2(wz - b)z = 0
171 // w(1-z)² - a(1-z) + wz² - bz = 0
172 // w((1-z)² + z²) = a(1-z) + bz
173 // w = (a(1-z) + bz) / ((1-z)² + z²)
175 // If z had infinite precision, this would simply reduce to w = w1 + w2.
176 *total_weight = (w1 * (1 - z) + w2 * z) / (z * z + (1 - z) * (1 - z));
179 if (sum_sq_error != NULL) {
180 float err1 = *total_weight * (1 - z) - w1;
181 float err2 = *total_weight * z - w2;
182 *sum_sq_error = err1 * err1 + err2 * err2;
185 assert(*offset >= 0.0f);
186 assert(*offset <= 1.0f);
189 GLuint fill_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data)
191 int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
197 glGenBuffers(1, &vbo);
199 glBindBuffer(GL_ARRAY_BUFFER, vbo);
201 glBufferData(GL_ARRAY_BUFFER, data_size, data, GL_STATIC_DRAW);
203 glEnableVertexAttribArray(attrib);
205 glVertexAttribPointer(attrib, size, type, GL_FALSE, 0, BUFFER_OFFSET(0));
207 glBindBuffer(GL_ARRAY_BUFFER, 0);
213 void cleanup_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLuint vbo)
215 int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
220 glDisableVertexAttribArray(attrib);
222 glDeleteBuffers(1, &vbo);
226 unsigned div_round_up(unsigned a, unsigned b)
228 return (a + b - 1) / b;
231 // Algorithm from http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2.
232 unsigned next_power_of_two(unsigned v)
244 void *get_gl_context_identifier()
246 #if defined(__DARWIN__)
247 return (void *)CGLGetCurrentContext();
249 return (void *)wglGetCurrentContext();
251 return (void *)glXGetCurrentContext();