16 #if defined(__APPLE__)
17 #include <OpenGL/OpenGL.h>
19 #include <epoxy/wgl.h>
21 #include <epoxy/glx.h>
22 #include <epoxy/egl.h>
29 extern string *movit_data_directory;
31 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b)
37 float hp = (h * 180.0 / M_PI) / 60.0;
38 float x = c * (1 - fabs(fmod(hp, 2.0f) - 1.0f));
40 if (hp >= 0 && hp < 1) {
44 } else if (hp >= 1 && hp < 2) {
48 } else if (hp >= 2 && hp < 3) {
52 } else if (hp >= 3 && hp < 4) {
56 } else if (hp >= 4 && hp < 5) {
72 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b)
74 float ref_r, ref_g, ref_b;
75 hsv2rgb(h, s, v, r, g, b);
76 hsv2rgb(h, 0.0f, v, &ref_r, &ref_g, &ref_b);
77 float lum = 0.2126 * *r + 0.7152 * *g + 0.0722 * *b;
78 float ref_lum = 0.2126 * ref_r + 0.7152 * ref_g + 0.0722 * ref_b;
80 float fac = ref_lum / lum;
87 string read_file(const string &filename)
89 const string full_pathname = *movit_data_directory + "/" + filename;
91 FILE *fp = fopen(full_pathname.c_str(), "r");
93 perror(full_pathname.c_str());
97 int ret = fseek(fp, 0, SEEK_END);
99 perror("fseek(SEEK_END)");
103 int size = ftell(fp);
105 ret = fseek(fp, 0, SEEK_SET);
107 perror("fseek(SEEK_SET)");
113 ret = fread(&str[0], size, 1, fp);
119 fprintf(stderr, "Short read when trying to read %d bytes from %s\n",
120 size, full_pathname.c_str());
128 string read_version_dependent_file(const string &base, const string &extension)
130 if (movit_shader_model == MOVIT_GLSL_130) {
131 return read_file(base + ".130." + extension);
132 } else if (movit_shader_model == MOVIT_GLSL_150) {
133 return read_file(base + ".150." + extension);
134 } else if (movit_shader_model == MOVIT_ESSL_300) {
135 return read_file(base + ".300es." + extension);
141 GLuint compile_shader(const string &shader_src, GLenum type)
143 GLuint obj = glCreateShader(type);
144 const GLchar* source[] = { shader_src.data() };
145 const GLint length[] = { (GLint)shader_src.size() };
146 glShaderSource(obj, 1, source, length);
147 glCompileShader(obj);
149 GLchar info_log[4096];
150 GLsizei log_length = sizeof(info_log) - 1;
151 glGetShaderInfoLog(obj, log_length, &log_length, info_log);
152 info_log[log_length] = 0;
153 if (strlen(info_log) > 0) {
154 fprintf(stderr, "Shader compile log: %s\n", info_log);
158 glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
159 if (status == GL_FALSE) {
160 fprintf(stderr, "Failed to compile shader: %s\n", shader_src.c_str());
167 void print_3x3_matrix(const Eigen::Matrix3d& m)
169 printf("%6.4f %6.4f %6.4f\n", m(0,0), m(0,1), m(0,2));
170 printf("%6.4f %6.4f %6.4f\n", m(1,0), m(1,1), m(1,2));
171 printf("%6.4f %6.4f %6.4f\n", m(2,0), m(2,1), m(2,2));
175 string output_glsl_mat3(const string &name, const Eigen::Matrix3d &m)
177 // Use stringstream to be independent of the current locale in a thread-safe manner.
179 ss.imbue(locale("C"));
182 ss << "const mat3 " << name << " = mat3(\n";
183 ss << " " << m(0,0) << ", " << m(1,0) << ", " << m(2,0) << ",\n";
184 ss << " " << m(0,1) << ", " << m(1,1) << ", " << m(2,1) << ",\n";
185 ss << " " << m(0,2) << ", " << m(1,2) << ", " << m(2,2) << ");\n\n";
189 string output_glsl_float(const string &name, float x)
191 // Use stringstream to be independent of the current locale in a thread-safe manner.
193 ss.imbue(locale("C"));
196 ss << "const float " << name << " = " << x << ";\n";
200 string output_glsl_vec2(const string &name, float x, float y)
202 // Use stringstream to be independent of the current locale in a thread-safe manner.
204 ss.imbue(locale("C"));
207 ss << "const vec2 " << name << " = vec2(" << x << ", " << y << ");\n";
211 string output_glsl_vec3(const string &name, float x, float y, float z)
213 // Use stringstream to be independent of the current locale in a thread-safe manner.
215 ss.imbue(locale("C"));
218 ss << "const vec3 " << name << " = vec3(" << x << ", " << y << ", " << z << ");\n";
222 GLuint generate_vbo(GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data)
225 glGenBuffers(1, &vbo);
227 glBindBuffer(GL_ARRAY_BUFFER, vbo);
229 glBufferData(GL_ARRAY_BUFFER, data_size, data, GL_STATIC_DRAW);
231 glBindBuffer(GL_ARRAY_BUFFER, 0);
237 GLuint fill_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data)
239 int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
244 GLuint vbo = generate_vbo(size, type, data_size, data);
246 glBindBuffer(GL_ARRAY_BUFFER, vbo);
248 glEnableVertexAttribArray(attrib);
250 glVertexAttribPointer(attrib, size, type, GL_FALSE, 0, BUFFER_OFFSET(0));
252 glBindBuffer(GL_ARRAY_BUFFER, 0);
258 void cleanup_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLuint vbo)
260 int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
265 glDisableVertexAttribArray(attrib);
267 glDeleteBuffers(1, &vbo);
271 unsigned div_round_up(unsigned a, unsigned b)
273 return (a + b - 1) / b;
276 // Algorithm from http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2.
277 unsigned next_power_of_two(unsigned v)
289 void *get_gl_context_identifier()
291 #if defined(__APPLE__)
292 return (void *)CGLGetCurrentContext();
294 return (void *)wglGetCurrentContext();
296 void *ret = (void *)eglGetCurrentContext();
297 if (ret != nullptr) {
300 return (void *)glXGetCurrentContext();
304 void abort_gl_error(GLenum err, const char *filename, int line)
306 const char *err_text = "unknown";
308 // All errors listed in the glGetError(3G) man page.
311 err_text = "GL_NO_ERROR"; // Should not happen.
313 case GL_INVALID_ENUM:
314 err_text = "GL_INVALID_ENUM";
316 case GL_INVALID_VALUE:
317 err_text = "GL_INVALID_VALUE";
319 case GL_INVALID_OPERATION:
320 err_text = "GL_INVALID_OPERATION";
322 case GL_INVALID_FRAMEBUFFER_OPERATION:
323 err_text = "GL_INVALID_FRAMEBUFFER_OPERATION";
325 case GL_OUT_OF_MEMORY:
326 err_text = "GL_OUT_OF_MEMORY";
328 case GL_STACK_UNDERFLOW:
329 err_text = "GL_STACK_UNDERFLOW";
331 case GL_STACK_OVERFLOW:
332 err_text = "GL_STACK_OVERFLOW";
335 fprintf(stderr, "GL error 0x%x (%s) at %s:%d\n", err, err_text, filename, line);