1 #define GL_GLEXT_PROTOTYPES 1
7 #include "effect_chain.h"
13 GLint get_uniform_location(GLuint glsl_program_num, const std::string &prefix, const std::string &key)
15 std::string name = prefix + "_" + key;
16 return glGetUniformLocation(glsl_program_num, name.c_str());
19 void set_uniform_int(GLuint glsl_program_num, const std::string &prefix, const std::string &key, int value)
21 GLint location = get_uniform_location(glsl_program_num, prefix, key);
26 glUniform1i(location, value);
30 void set_uniform_float(GLuint glsl_program_num, const std::string &prefix, const std::string &key, float value)
32 GLint location = get_uniform_location(glsl_program_num, prefix, key);
37 glUniform1f(location, value);
41 void set_uniform_float_array(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values, size_t num_values)
43 GLint location = get_uniform_location(glsl_program_num, prefix, key);
48 glUniform1fv(location, num_values, values);
52 void set_uniform_vec2(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
54 GLint location = get_uniform_location(glsl_program_num, prefix, key);
59 glUniform2fv(location, 1, values);
63 void set_uniform_vec3(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
65 GLint location = get_uniform_location(glsl_program_num, prefix, key);
70 glUniform3fv(location, 1, values);
74 void set_uniform_vec4_array(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values, size_t num_values)
76 GLint location = get_uniform_location(glsl_program_num, prefix, key);
81 glUniform4fv(location, num_values, values);
85 bool Effect::set_int(const std::string &key, int value)
87 if (params_int.count(key) == 0) {
90 *params_int[key] = value;
94 bool Effect::set_float(const std::string &key, float value)
96 if (params_float.count(key) == 0) {
99 *params_float[key] = value;
103 bool Effect::set_vec2(const std::string &key, const float *values)
105 if (params_vec2.count(key) == 0) {
108 memcpy(params_vec2[key], values, sizeof(float) * 2);
112 bool Effect::set_vec3(const std::string &key, const float *values)
114 if (params_vec3.count(key) == 0) {
117 memcpy(params_vec3[key], values, sizeof(float) * 3);
121 void Effect::register_int(const std::string &key, int *value)
123 assert(params_int.count(key) == 0);
124 params_int[key] = value;
127 void Effect::register_float(const std::string &key, float *value)
129 assert(params_float.count(key) == 0);
130 params_float[key] = value;
133 void Effect::register_vec2(const std::string &key, float *values)
135 assert(params_vec2.count(key) == 0);
136 params_vec2[key] = values;
139 void Effect::register_vec3(const std::string &key, float *values)
141 assert(params_vec3.count(key) == 0);
142 params_vec3[key] = values;
145 void Effect::register_1d_texture(const std::string &key, float *values, size_t size)
147 assert(params_tex_1d.count(key) == 0);
152 tex.needs_update = false;
153 glGenTextures(1, &tex.texture_num);
155 glBindTexture(GL_TEXTURE_1D, tex.texture_num);
157 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
159 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
161 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, size, 0, GL_LUMINANCE, GL_FLOAT, values);
164 params_tex_1d[key] = tex;
167 void Effect::invalidate_1d_texture(const std::string &key)
169 assert(params_tex_1d.count(key) != 0);
170 params_tex_1d[key].needs_update = true;
173 void Effect::add_self_to_effect_chain(EffectChain *chain, const std::vector<Effect *> &inputs)
175 chain->add_effect_raw(this, inputs);
178 // Output convenience uniforms for each parameter.
179 // These will be filled in per-frame.
180 std::string Effect::output_convenience_uniforms() const
182 std::string output = "";
183 for (std::map<std::string, float*>::const_iterator it = params_float.begin();
184 it != params_float.end();
187 sprintf(buf, "uniform float PREFIX(%s);\n", it->first.c_str());
190 for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
191 it != params_vec2.end();
194 sprintf(buf, "uniform vec2 PREFIX(%s);\n", it->first.c_str());
197 for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
198 it != params_vec3.end();
201 sprintf(buf, "uniform vec3 PREFIX(%s);\n", it->first.c_str());
204 for (std::map<std::string, Texture1D>::const_iterator it = params_tex_1d.begin();
205 it != params_tex_1d.end();
208 sprintf(buf, "uniform sampler1D PREFIX(%s);\n", it->first.c_str());
214 void Effect::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
216 for (std::map<std::string, float*>::const_iterator it = params_float.begin();
217 it != params_float.end();
219 set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
221 for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
222 it != params_vec2.end();
224 set_uniform_vec2(glsl_program_num, prefix, it->first, it->second);
226 for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
227 it != params_vec3.end();
229 set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
232 for (std::map<std::string, Texture1D>::iterator it = params_tex_1d.begin();
233 it != params_tex_1d.end();
235 glActiveTexture(GL_TEXTURE0 + *sampler_num);
237 glBindTexture(GL_TEXTURE_1D, it->second.texture_num);
240 if (it->second.needs_update) {
241 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, it->second.size, 0, GL_LUMINANCE, GL_FLOAT, it->second.values);
243 it->second.needs_update = false;
246 set_uniform_int(glsl_program_num, prefix, it->first, *sampler_num);
251 void Effect::clear_gl_state() {}