11 GLint get_uniform_location(GLuint glsl_program_num, const std::string &prefix, const std::string &key)
13 std::string name = prefix + "_" + key;
14 return glGetUniformLocation(glsl_program_num, name.c_str());
17 void set_uniform_int(GLuint glsl_program_num, const std::string &prefix, const std::string &key, int value)
19 GLint location = get_uniform_location(glsl_program_num, prefix, key);
24 glUniform1i(location, value);
28 void set_uniform_float(GLuint glsl_program_num, const std::string &prefix, const std::string &key, float value)
30 GLint location = get_uniform_location(glsl_program_num, prefix, key);
35 glUniform1f(location, value);
39 void set_uniform_vec2(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
41 GLint location = get_uniform_location(glsl_program_num, prefix, key);
46 glUniform2fv(location, 1, values);
50 void set_uniform_vec3(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
52 GLint location = get_uniform_location(glsl_program_num, prefix, key);
57 glUniform3fv(location, 1, values);
61 void set_uniform_vec4(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
63 GLint location = get_uniform_location(glsl_program_num, prefix, key);
68 glUniform4fv(location, 1, values);
72 void set_uniform_vec4_array(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values, size_t num_values)
74 GLint location = get_uniform_location(glsl_program_num, prefix, key);
79 glUniform4fv(location, num_values, values);
83 void set_uniform_mat3(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const Eigen::Matrix3d& matrix)
85 GLint location = get_uniform_location(glsl_program_num, prefix, key);
91 // Convert to float (GLSL has no double matrices).
93 for (unsigned y = 0; y < 3; ++y) {
94 for (unsigned x = 0; x < 3; ++x) {
95 matrixf[y + x * 3] = matrix(y, x);
99 glUniformMatrix3fv(location, 1, GL_FALSE, matrixf);
103 bool Effect::set_int(const std::string &key, int value)
105 if (params_int.count(key) == 0) {
108 *params_int[key] = value;
112 bool Effect::set_float(const std::string &key, float value)
114 if (params_float.count(key) == 0) {
117 *params_float[key] = value;
121 bool Effect::set_vec2(const std::string &key, const float *values)
123 if (params_vec2.count(key) == 0) {
126 memcpy(params_vec2[key], values, sizeof(float) * 2);
130 bool Effect::set_vec3(const std::string &key, const float *values)
132 if (params_vec3.count(key) == 0) {
135 memcpy(params_vec3[key], values, sizeof(float) * 3);
139 bool Effect::set_vec4(const std::string &key, const float *values)
141 if (params_vec4.count(key) == 0) {
144 memcpy(params_vec4[key], values, sizeof(float) * 4);
148 void Effect::register_int(const std::string &key, int *value)
150 assert(params_int.count(key) == 0);
151 params_int[key] = value;
154 void Effect::register_float(const std::string &key, float *value)
156 assert(params_float.count(key) == 0);
157 params_float[key] = value;
160 void Effect::register_vec2(const std::string &key, float *values)
162 assert(params_vec2.count(key) == 0);
163 params_vec2[key] = values;
166 void Effect::register_vec3(const std::string &key, float *values)
168 assert(params_vec3.count(key) == 0);
169 params_vec3[key] = values;
172 void Effect::register_vec4(const std::string &key, float *values)
174 assert(params_vec4.count(key) == 0);
175 params_vec4[key] = values;
178 void Effect::register_1d_texture(const std::string &key, float *values, size_t size)
180 assert(params_tex_1d.count(key) == 0);
185 tex.needs_update = false;
186 glGenTextures(1, &tex.texture_num);
188 glBindTexture(GL_TEXTURE_1D, tex.texture_num);
190 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
192 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
194 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, size, 0, GL_LUMINANCE, GL_FLOAT, values);
197 params_tex_1d[key] = tex;
200 void Effect::invalidate_1d_texture(const std::string &key)
202 assert(params_tex_1d.count(key) != 0);
203 params_tex_1d[key].needs_update = true;
206 // Output convenience uniforms for each parameter.
207 // These will be filled in per-frame.
208 std::string Effect::output_convenience_uniforms() const
210 std::string output = "";
211 for (std::map<std::string, float*>::const_iterator it = params_float.begin();
212 it != params_float.end();
215 sprintf(buf, "uniform float PREFIX(%s);\n", it->first.c_str());
218 for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
219 it != params_vec2.end();
222 sprintf(buf, "uniform vec2 PREFIX(%s);\n", it->first.c_str());
225 for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
226 it != params_vec3.end();
229 sprintf(buf, "uniform vec3 PREFIX(%s);\n", it->first.c_str());
232 for (std::map<std::string, float*>::const_iterator it = params_vec4.begin();
233 it != params_vec4.end();
236 sprintf(buf, "uniform vec4 PREFIX(%s);\n", it->first.c_str());
239 for (std::map<std::string, Texture1D>::const_iterator it = params_tex_1d.begin();
240 it != params_tex_1d.end();
243 sprintf(buf, "uniform sampler1D PREFIX(%s);\n", it->first.c_str());
249 void Effect::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
251 for (std::map<std::string, float*>::const_iterator it = params_float.begin();
252 it != params_float.end();
254 set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
256 for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
257 it != params_vec2.end();
259 set_uniform_vec2(glsl_program_num, prefix, it->first, it->second);
261 for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
262 it != params_vec3.end();
264 set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
266 for (std::map<std::string, float*>::const_iterator it = params_vec4.begin();
267 it != params_vec4.end();
269 set_uniform_vec4(glsl_program_num, prefix, it->first, it->second);
272 for (std::map<std::string, Texture1D>::iterator it = params_tex_1d.begin();
273 it != params_tex_1d.end();
275 glActiveTexture(GL_TEXTURE0 + *sampler_num);
277 glBindTexture(GL_TEXTURE_1D, it->second.texture_num);
280 if (it->second.needs_update) {
281 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, it->second.size, 0, GL_LUMINANCE, GL_FLOAT, it->second.values);
283 it->second.needs_update = false;
286 set_uniform_int(glsl_program_num, prefix, it->first, *sampler_num);
291 void Effect::clear_gl_state() {}