12 #if defined(__APPLE__)
13 #include <OpenGL/OpenGL.h>
15 #include <epoxy/wgl.h>
17 #include <epoxy/glx.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 string read_version_dependent_file(const string &base, const string &extension)
101 if (movit_shader_model == MOVIT_GLSL_110) {
102 return read_file(base + "." + extension);
103 } else if (movit_shader_model == MOVIT_GLSL_130) {
104 return read_file(base + ".130." + extension);
105 } else if (movit_shader_model == MOVIT_ESSL_300) {
106 return read_file(base + ".300es." + extension);
112 GLuint compile_shader(const string &shader_src, GLenum type)
114 GLuint obj = glCreateShader(type);
115 const GLchar* source[] = { shader_src.data() };
116 const GLint length[] = { (GLint)shader_src.size() };
117 glShaderSource(obj, 1, source, length);
118 glCompileShader(obj);
120 GLchar info_log[4096];
121 GLsizei log_length = sizeof(info_log) - 1;
122 glGetShaderInfoLog(obj, log_length, &log_length, info_log);
123 info_log[log_length] = 0;
124 if (strlen(info_log) > 0) {
125 fprintf(stderr, "Shader compile log: %s\n", info_log);
129 glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
130 if (status == GL_FALSE) {
137 void print_3x3_matrix(const Eigen::Matrix3d& m)
139 printf("%6.4f %6.4f %6.4f\n", m(0,0), m(0,1), m(0,2));
140 printf("%6.4f %6.4f %6.4f\n", m(1,0), m(1,1), m(1,2));
141 printf("%6.4f %6.4f %6.4f\n", m(2,0), m(2,1), m(2,2));
145 string output_glsl_mat3(const string &name, const Eigen::Matrix3d &m)
149 "const mat3 %s = mat3(\n"
150 " %.8f, %.8f, %.8f,\n"
151 " %.8f, %.8f, %.8f,\n"
152 " %.8f, %.8f, %.8f);\n\n",
154 m(0,0), m(1,0), m(2,0),
155 m(0,1), m(1,1), m(2,1),
156 m(0,2), m(1,2), m(2,2));
160 void combine_two_samples(float w1, float w2, float *offset, float *total_weight, float *sum_sq_error)
162 assert(movit_initialized);
163 assert(w1 * w2 >= 0.0f); // Should not have differing signs.
164 float z; // Just a shorter name for offset.
165 if (fabs(w1 + w2) < 1e-6) {
171 // Round to the minimum number of bits we have measured earlier.
172 // The card will do this for us anyway, but if we know what the real z
173 // is, we can pick a better total_weight below.
174 z = lrintf(z / movit_texel_subpixel_precision) * movit_texel_subpixel_precision;
176 // Choose total weight w so that we minimize total squared error
177 // for the effective weights:
179 // e = (w(1-z) - a)² + (wz - b)²
181 // Differentiating by w and setting equal to zero:
183 // 2(w(1-z) - a)(1-z) + 2(wz - b)z = 0
184 // w(1-z)² - a(1-z) + wz² - bz = 0
185 // w((1-z)² + z²) = a(1-z) + bz
186 // w = (a(1-z) + bz) / ((1-z)² + z²)
188 // If z had infinite precision, this would simply reduce to w = w1 + w2.
189 *total_weight = (w1 * (1 - z) + w2 * z) / (z * z + (1 - z) * (1 - z));
192 if (sum_sq_error != NULL) {
193 float err1 = *total_weight * (1 - z) - w1;
194 float err2 = *total_weight * z - w2;
195 *sum_sq_error = err1 * err1 + err2 * err2;
198 assert(*offset >= 0.0f);
199 assert(*offset <= 1.0f);
202 GLuint fill_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data)
204 int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
210 glGenBuffers(1, &vbo);
212 glBindBuffer(GL_ARRAY_BUFFER, vbo);
214 glBufferData(GL_ARRAY_BUFFER, data_size, data, GL_STATIC_DRAW);
216 glEnableVertexAttribArray(attrib);
218 glVertexAttribPointer(attrib, size, type, GL_FALSE, 0, BUFFER_OFFSET(0));
220 glBindBuffer(GL_ARRAY_BUFFER, 0);
226 void cleanup_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLuint vbo)
228 int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
233 glDisableVertexAttribArray(attrib);
235 glDeleteBuffers(1, &vbo);
239 unsigned div_round_up(unsigned a, unsigned b)
241 return (a + b - 1) / b;
244 // Algorithm from http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2.
245 unsigned next_power_of_two(unsigned v)
257 void *get_gl_context_identifier()
259 #if defined(__APPLE__)
260 return (void *)CGLGetCurrentContext();
262 return (void *)wglGetCurrentContext();
264 return (void *)glXGetCurrentContext();