1 #define GL_GLEXT_PROTOTYPES 1
12 void set_uniform_int(GLuint glsl_program_num, const std::string &prefix, const std::string &key, int value)
14 std::string name = prefix + "_" + key;
15 GLint l = glGetUniformLocation(glsl_program_num, name.c_str());
20 glUniform1i(l, value);
24 void set_uniform_float(GLuint glsl_program_num, const std::string &prefix, const std::string &key, float value)
26 std::string name = prefix + "_" + key;
27 GLint l = glGetUniformLocation(glsl_program_num, name.c_str());
32 glUniform1f(l, value);
36 void set_uniform_float_array(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values, size_t num_values)
38 std::string name = prefix + "_" + key;
39 GLint l = glGetUniformLocation(glsl_program_num, name.c_str());
44 glUniform1fv(l, num_values, values);
48 void set_uniform_vec2(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
50 std::string name = prefix + "_" + key;
51 GLint l = glGetUniformLocation(glsl_program_num, name.c_str());
56 glUniform2fv(l, 1, values);
60 void set_uniform_vec3(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
62 std::string name = prefix + "_" + key;
63 GLint l = glGetUniformLocation(glsl_program_num, name.c_str());
68 glUniform3fv(l, 1, values);
72 bool Effect::set_int(const std::string &key, int value)
74 if (params_int.count(key) == 0) {
77 *params_int[key] = value;
81 bool Effect::set_float(const std::string &key, float value)
83 if (params_float.count(key) == 0) {
86 *params_float[key] = value;
90 bool Effect::set_vec2(const std::string &key, const float *values)
92 if (params_vec2.count(key) == 0) {
95 memcpy(params_vec2[key], values, sizeof(float) * 2);
99 bool Effect::set_vec3(const std::string &key, const float *values)
101 if (params_vec3.count(key) == 0) {
104 memcpy(params_vec3[key], values, sizeof(float) * 3);
108 void Effect::register_int(const std::string &key, int *value)
110 assert(params_int.count(key) == 0);
111 params_int[key] = value;
114 void Effect::register_float(const std::string &key, float *value)
116 assert(params_float.count(key) == 0);
117 params_float[key] = value;
120 void Effect::register_vec2(const std::string &key, float *values)
122 assert(params_vec2.count(key) == 0);
123 params_vec2[key] = values;
126 void Effect::register_vec3(const std::string &key, float *values)
128 assert(params_vec3.count(key) == 0);
129 params_vec3[key] = values;
132 void Effect::register_1d_texture(const std::string &key, float *values, size_t size)
134 assert(params_tex_1d.count(key) == 0);
139 tex.needs_update = false;
140 glGenTextures(1, &tex.texture_num);
142 glBindTexture(GL_TEXTURE_1D, tex.texture_num);
144 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
146 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
148 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, size, 0, GL_LUMINANCE, GL_FLOAT, values);
151 params_tex_1d[key] = tex;
154 void Effect::invalidate_1d_texture(const std::string &key)
156 assert(params_tex_1d.count(key) != 0);
157 params_tex_1d[key].needs_update = true;
160 // Output convenience uniforms for each parameter.
161 // These will be filled in per-frame.
162 std::string Effect::output_convenience_uniforms() const
164 std::string output = "";
165 for (std::map<std::string, float*>::const_iterator it = params_float.begin();
166 it != params_float.end();
169 sprintf(buf, "uniform float PREFIX(%s);\n", it->first.c_str());
172 for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
173 it != params_vec2.end();
176 sprintf(buf, "uniform vec2 PREFIX(%s);\n", it->first.c_str());
179 for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
180 it != params_vec3.end();
183 sprintf(buf, "uniform vec3 PREFIX(%s);\n", it->first.c_str());
186 for (std::map<std::string, Texture1D>::const_iterator it = params_tex_1d.begin();
187 it != params_tex_1d.end();
190 sprintf(buf, "uniform sampler1D PREFIX(%s);\n", it->first.c_str());
196 void Effect::set_uniforms(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
198 for (std::map<std::string, float*>::const_iterator it = params_float.begin();
199 it != params_float.end();
201 set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
203 for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
204 it != params_vec2.end();
206 set_uniform_vec2(glsl_program_num, prefix, it->first, it->second);
208 for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
209 it != params_vec3.end();
211 set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
214 for (std::map<std::string, Texture1D>::const_iterator it = params_tex_1d.begin();
215 it != params_tex_1d.end();
217 glActiveTexture(GL_TEXTURE0 + *sampler_num);
219 glBindTexture(GL_TEXTURE_1D, it->second.texture_num);
222 if (it->second.needs_update) {
223 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, it->second.size, 0, GL_LUMINANCE, GL_FLOAT, it->second.values);
227 set_uniform_int(glsl_program_num, prefix, it->first, *sampler_num);