7 #include "effect_chain.h"
10 GLint get_uniform_location(GLuint glsl_program_num, const std::string &prefix, const std::string &key)
12 std::string name = prefix + "_" + key;
13 return glGetUniformLocation(glsl_program_num, name.c_str());
16 void set_uniform_int(GLuint glsl_program_num, const std::string &prefix, const std::string &key, int value)
18 GLint location = get_uniform_location(glsl_program_num, prefix, key);
23 glUniform1i(location, value);
27 void set_uniform_float(GLuint glsl_program_num, const std::string &prefix, const std::string &key, float value)
29 GLint location = get_uniform_location(glsl_program_num, prefix, key);
34 glUniform1f(location, value);
38 void set_uniform_vec2(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
40 GLint location = get_uniform_location(glsl_program_num, prefix, key);
45 glUniform2fv(location, 1, values);
49 void set_uniform_vec3(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
51 GLint location = get_uniform_location(glsl_program_num, prefix, key);
56 glUniform3fv(location, 1, values);
60 void set_uniform_vec4(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
62 GLint location = get_uniform_location(glsl_program_num, prefix, key);
67 glUniform4fv(location, 1, values);
71 void set_uniform_vec4_array(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values, size_t num_values)
73 GLint location = get_uniform_location(glsl_program_num, prefix, key);
78 glUniform4fv(location, num_values, values);
82 void set_uniform_mat3(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const Eigen::Matrix3d& matrix)
84 GLint location = get_uniform_location(glsl_program_num, prefix, key);
90 // Convert to float (GLSL has no double matrices).
92 for (unsigned y = 0; y < 3; ++y) {
93 for (unsigned x = 0; x < 3; ++x) {
94 matrixf[y + x * 3] = matrix(y, x);
98 glUniformMatrix3fv(location, 1, GL_FALSE, matrixf);
102 bool Effect::set_int(const std::string &key, int value)
104 if (params_int.count(key) == 0) {
107 *params_int[key] = value;
111 bool Effect::set_float(const std::string &key, float value)
113 if (params_float.count(key) == 0) {
116 *params_float[key] = value;
120 bool Effect::set_vec2(const std::string &key, const float *values)
122 if (params_vec2.count(key) == 0) {
125 memcpy(params_vec2[key], values, sizeof(float) * 2);
129 bool Effect::set_vec3(const std::string &key, const float *values)
131 if (params_vec3.count(key) == 0) {
134 memcpy(params_vec3[key], values, sizeof(float) * 3);
138 bool Effect::set_vec4(const std::string &key, const float *values)
140 if (params_vec4.count(key) == 0) {
143 memcpy(params_vec4[key], values, sizeof(float) * 4);
147 void Effect::register_int(const std::string &key, int *value)
149 assert(params_int.count(key) == 0);
150 params_int[key] = value;
153 void Effect::register_float(const std::string &key, float *value)
155 assert(params_float.count(key) == 0);
156 params_float[key] = value;
159 void Effect::register_vec2(const std::string &key, float *values)
161 assert(params_vec2.count(key) == 0);
162 params_vec2[key] = values;
165 void Effect::register_vec3(const std::string &key, float *values)
167 assert(params_vec3.count(key) == 0);
168 params_vec3[key] = values;
171 void Effect::register_vec4(const std::string &key, float *values)
173 assert(params_vec4.count(key) == 0);
174 params_vec4[key] = values;
177 void Effect::register_1d_texture(const std::string &key, float *values, size_t size)
179 assert(params_tex_1d.count(key) == 0);
184 tex.needs_update = false;
185 glGenTextures(1, &tex.texture_num);
187 glBindTexture(GL_TEXTURE_1D, tex.texture_num);
189 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
191 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
193 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, size, 0, GL_LUMINANCE, GL_FLOAT, values);
196 params_tex_1d[key] = tex;
199 void Effect::invalidate_1d_texture(const std::string &key)
201 assert(params_tex_1d.count(key) != 0);
202 params_tex_1d[key].needs_update = true;
205 // Output convenience uniforms for each parameter.
206 // These will be filled in per-frame.
207 std::string Effect::output_convenience_uniforms() const
209 std::string output = "";
210 for (std::map<std::string, float*>::const_iterator it = params_float.begin();
211 it != params_float.end();
214 sprintf(buf, "uniform float PREFIX(%s);\n", it->first.c_str());
217 for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
218 it != params_vec2.end();
221 sprintf(buf, "uniform vec2 PREFIX(%s);\n", it->first.c_str());
224 for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
225 it != params_vec3.end();
228 sprintf(buf, "uniform vec3 PREFIX(%s);\n", it->first.c_str());
231 for (std::map<std::string, float*>::const_iterator it = params_vec4.begin();
232 it != params_vec4.end();
235 sprintf(buf, "uniform vec4 PREFIX(%s);\n", it->first.c_str());
238 for (std::map<std::string, Texture1D>::const_iterator it = params_tex_1d.begin();
239 it != params_tex_1d.end();
242 sprintf(buf, "uniform sampler1D PREFIX(%s);\n", it->first.c_str());
248 void Effect::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
250 for (std::map<std::string, float*>::const_iterator it = params_float.begin();
251 it != params_float.end();
253 set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
255 for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
256 it != params_vec2.end();
258 set_uniform_vec2(glsl_program_num, prefix, it->first, it->second);
260 for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
261 it != params_vec3.end();
263 set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
265 for (std::map<std::string, float*>::const_iterator it = params_vec4.begin();
266 it != params_vec4.end();
268 set_uniform_vec4(glsl_program_num, prefix, it->first, it->second);
271 for (std::map<std::string, Texture1D>::iterator it = params_tex_1d.begin();
272 it != params_tex_1d.end();
274 glActiveTexture(GL_TEXTURE0 + *sampler_num);
276 glBindTexture(GL_TEXTURE_1D, it->second.texture_num);
279 if (it->second.needs_update) {
280 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, it->second.size, 0, GL_LUMINANCE, GL_FLOAT, it->second.values);
282 it->second.needs_update = false;
285 set_uniform_int(glsl_program_num, prefix, it->first, *sampler_num);
290 void Effect::clear_gl_state() {}