5 #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_float_array(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values, size_t num_values)
40 GLint location = get_uniform_location(glsl_program_num, prefix, key);
45 glUniform1fv(location, num_values, values);
49 void set_uniform_vec2(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 glUniform2fv(location, 1, values);
60 void set_uniform_vec3(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 glUniform3fv(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 bool Effect::set_int(const std::string &key, int value)
84 if (params_int.count(key) == 0) {
87 *params_int[key] = value;
91 bool Effect::set_float(const std::string &key, float value)
93 if (params_float.count(key) == 0) {
96 *params_float[key] = value;
100 bool Effect::set_vec2(const std::string &key, const float *values)
102 if (params_vec2.count(key) == 0) {
105 memcpy(params_vec2[key], values, sizeof(float) * 2);
109 bool Effect::set_vec3(const std::string &key, const float *values)
111 if (params_vec3.count(key) == 0) {
114 memcpy(params_vec3[key], values, sizeof(float) * 3);
118 void Effect::register_int(const std::string &key, int *value)
120 assert(params_int.count(key) == 0);
121 params_int[key] = value;
124 void Effect::register_float(const std::string &key, float *value)
126 assert(params_float.count(key) == 0);
127 params_float[key] = value;
130 void Effect::register_vec2(const std::string &key, float *values)
132 assert(params_vec2.count(key) == 0);
133 params_vec2[key] = values;
136 void Effect::register_vec3(const std::string &key, float *values)
138 assert(params_vec3.count(key) == 0);
139 params_vec3[key] = values;
142 void Effect::register_1d_texture(const std::string &key, float *values, size_t size)
144 assert(params_tex_1d.count(key) == 0);
149 tex.needs_update = false;
150 glGenTextures(1, &tex.texture_num);
152 glBindTexture(GL_TEXTURE_1D, tex.texture_num);
154 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
156 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
158 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, size, 0, GL_LUMINANCE, GL_FLOAT, values);
161 params_tex_1d[key] = tex;
164 void Effect::invalidate_1d_texture(const std::string &key)
166 assert(params_tex_1d.count(key) != 0);
167 params_tex_1d[key].needs_update = true;
170 void Effect::add_self_to_effect_chain(EffectChain *chain, const std::vector<Effect *> &inputs)
172 chain->add_effect_raw(this, inputs);
175 // Output convenience uniforms for each parameter.
176 // These will be filled in per-frame.
177 std::string Effect::output_convenience_uniforms() const
179 std::string output = "";
180 for (std::map<std::string, float*>::const_iterator it = params_float.begin();
181 it != params_float.end();
184 sprintf(buf, "uniform float PREFIX(%s);\n", it->first.c_str());
187 for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
188 it != params_vec2.end();
191 sprintf(buf, "uniform vec2 PREFIX(%s);\n", it->first.c_str());
194 for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
195 it != params_vec3.end();
198 sprintf(buf, "uniform vec3 PREFIX(%s);\n", it->first.c_str());
201 for (std::map<std::string, Texture1D>::const_iterator it = params_tex_1d.begin();
202 it != params_tex_1d.end();
205 sprintf(buf, "uniform sampler1D PREFIX(%s);\n", it->first.c_str());
211 void Effect::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
213 for (std::map<std::string, float*>::const_iterator it = params_float.begin();
214 it != params_float.end();
216 set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
218 for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
219 it != params_vec2.end();
221 set_uniform_vec2(glsl_program_num, prefix, it->first, it->second);
223 for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
224 it != params_vec3.end();
226 set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
229 for (std::map<std::string, Texture1D>::iterator it = params_tex_1d.begin();
230 it != params_tex_1d.end();
232 glActiveTexture(GL_TEXTURE0 + *sampler_num);
234 glBindTexture(GL_TEXTURE_1D, it->second.texture_num);
237 if (it->second.needs_update) {
238 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, it->second.size, 0, GL_LUMINANCE, GL_FLOAT, it->second.values);
240 it->second.needs_update = false;
243 set_uniform_int(glsl_program_num, prefix, it->first, *sampler_num);
248 void Effect::clear_gl_state() {}