]> git.sesse.net Git - movit/blob - effect_util.cpp
Send shader compile log to stderr instead of stdout.
[movit] / effect_util.cpp
1 #include <GL/glew.h>
2 #include <Eigen/Core>
3 #include <stddef.h>
4 #include <string>
5 #include "util.h"
6
7 using namespace std;
8
9 GLint get_uniform_location(GLuint glsl_program_num, const string &prefix, const string &key)
10 {
11         string name = prefix + "_" + key;
12         return glGetUniformLocation(glsl_program_num, name.c_str());
13 }
14
15 void set_uniform_int(GLuint glsl_program_num, const string &prefix, const string &key, int value)
16 {
17         GLint location = get_uniform_location(glsl_program_num, prefix, key);
18         if (location == -1) {
19                 return;
20         }
21         check_error();
22         glUniform1i(location, value);
23         check_error();
24 }
25
26 void set_uniform_float(GLuint glsl_program_num, const string &prefix, const string &key, float value)
27 {
28         GLint location = get_uniform_location(glsl_program_num, prefix, key);
29         if (location == -1) {
30                 return;
31         }
32         check_error();
33         glUniform1f(location, value);
34         check_error();
35 }
36
37 void set_uniform_vec2(GLuint glsl_program_num, const string &prefix, const string &key, const float *values)
38 {
39         GLint location = get_uniform_location(glsl_program_num, prefix, key);
40         if (location == -1) {
41                 return;
42         }
43         check_error();
44         glUniform2fv(location, 1, values);
45         check_error();
46 }
47
48 void set_uniform_vec3(GLuint glsl_program_num, const string &prefix, const string &key, const float *values)
49 {
50         GLint location = get_uniform_location(glsl_program_num, prefix, key);
51         if (location == -1) {
52                 return;
53         }
54         check_error();
55         glUniform3fv(location, 1, values);
56         check_error();
57 }
58
59 void set_uniform_vec4(GLuint glsl_program_num, const string &prefix, const string &key, const float *values)
60 {
61         GLint location = get_uniform_location(glsl_program_num, prefix, key);
62         if (location == -1) {
63                 return;
64         }
65         check_error();
66         glUniform4fv(location, 1, values);
67         check_error();
68 }
69
70 void set_uniform_vec4_array(GLuint glsl_program_num, const string &prefix, const string &key, const float *values, size_t num_values)
71 {
72         GLint location = get_uniform_location(glsl_program_num, prefix, key);
73         if (location == -1) {
74                 return;
75         }
76         check_error();
77         glUniform4fv(location, num_values, values);
78         check_error();
79 }
80
81 void set_uniform_mat3(GLuint glsl_program_num, const string &prefix, const string &key, const Eigen::Matrix3d& matrix)
82 {
83         GLint location = get_uniform_location(glsl_program_num, prefix, key);
84         if (location == -1) {
85                 return;
86         }
87         check_error();
88
89         // Convert to float (GLSL has no double matrices).
90         float matrixf[9];
91         for (unsigned y = 0; y < 3; ++y) {
92                 for (unsigned x = 0; x < 3; ++x) {
93                         matrixf[y + x * 3] = matrix(y, x);
94                 }
95         }
96
97         glUniformMatrix3fv(location, 1, GL_FALSE, matrixf);
98         check_error();
99 }