1 #define GL_GLEXT_PROTOTYPES 1
12 GLint get_uniform_location(GLuint glsl_program_num, const std::string &prefix, const std::string &key)
14 std::string name = prefix + "_" + key;
15 return glGetUniformLocation(glsl_program_num, name.c_str());
18 void set_uniform_int(GLuint glsl_program_num, const std::string &prefix, const std::string &key, int value)
20 GLint location = get_uniform_location(glsl_program_num, prefix, key);
25 glUniform1i(location, value);
29 void set_uniform_float(GLuint glsl_program_num, const std::string &prefix, const std::string &key, float value)
31 GLint location = get_uniform_location(glsl_program_num, prefix, key);
36 glUniform1f(location, value);
40 void set_uniform_float_array(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values, size_t num_values)
42 GLint location = get_uniform_location(glsl_program_num, prefix, key);
47 glUniform1fv(location, num_values, values);
51 void set_uniform_vec2(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
53 GLint location = get_uniform_location(glsl_program_num, prefix, key);
58 glUniform2fv(location, 1, values);
62 void set_uniform_vec3(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
64 GLint location = get_uniform_location(glsl_program_num, prefix, key);
69 glUniform3fv(location, 1, values);
73 void set_uniform_vec4_array(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values, size_t num_values)
75 GLint location = get_uniform_location(glsl_program_num, prefix, key);
80 glUniform4fv(location, num_values, values);
84 bool Effect::set_int(const std::string &key, int value)
86 if (params_int.count(key) == 0) {
89 *params_int[key] = value;
93 bool Effect::set_float(const std::string &key, float value)
95 if (params_float.count(key) == 0) {
98 *params_float[key] = value;
102 bool Effect::set_vec2(const std::string &key, const float *values)
104 if (params_vec2.count(key) == 0) {
107 memcpy(params_vec2[key], values, sizeof(float) * 2);
111 bool Effect::set_vec3(const std::string &key, const float *values)
113 if (params_vec3.count(key) == 0) {
116 memcpy(params_vec3[key], values, sizeof(float) * 3);
120 void Effect::register_int(const std::string &key, int *value)
122 assert(params_int.count(key) == 0);
123 params_int[key] = value;
126 void Effect::register_float(const std::string &key, float *value)
128 assert(params_float.count(key) == 0);
129 params_float[key] = value;
132 void Effect::register_vec2(const std::string &key, float *values)
134 assert(params_vec2.count(key) == 0);
135 params_vec2[key] = values;
138 void Effect::register_vec3(const std::string &key, float *values)
140 assert(params_vec3.count(key) == 0);
141 params_vec3[key] = values;
144 void Effect::register_1d_texture(const std::string &key, float *values, size_t size)
146 assert(params_tex_1d.count(key) == 0);
151 tex.needs_update = false;
152 glGenTextures(1, &tex.texture_num);
154 glBindTexture(GL_TEXTURE_1D, tex.texture_num);
156 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
158 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
160 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, size, 0, GL_LUMINANCE, GL_FLOAT, values);
163 params_tex_1d[key] = tex;
166 void Effect::invalidate_1d_texture(const std::string &key)
168 assert(params_tex_1d.count(key) != 0);
169 params_tex_1d[key].needs_update = true;
172 // Output convenience uniforms for each parameter.
173 // These will be filled in per-frame.
174 std::string Effect::output_convenience_uniforms() const
176 std::string output = "";
177 for (std::map<std::string, float*>::const_iterator it = params_float.begin();
178 it != params_float.end();
181 sprintf(buf, "uniform float PREFIX(%s);\n", it->first.c_str());
184 for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
185 it != params_vec2.end();
188 sprintf(buf, "uniform vec2 PREFIX(%s);\n", it->first.c_str());
191 for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
192 it != params_vec3.end();
195 sprintf(buf, "uniform vec3 PREFIX(%s);\n", it->first.c_str());
198 for (std::map<std::string, Texture1D>::const_iterator it = params_tex_1d.begin();
199 it != params_tex_1d.end();
202 sprintf(buf, "uniform sampler1D PREFIX(%s);\n", it->first.c_str());
208 void Effect::set_uniforms(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
210 for (std::map<std::string, float*>::const_iterator it = params_float.begin();
211 it != params_float.end();
213 set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
215 for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
216 it != params_vec2.end();
218 set_uniform_vec2(glsl_program_num, prefix, it->first, it->second);
220 for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
221 it != params_vec3.end();
223 set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
226 for (std::map<std::string, Texture1D>::const_iterator it = params_tex_1d.begin();
227 it != params_tex_1d.end();
229 glActiveTexture(GL_TEXTURE0 + *sampler_num);
231 glBindTexture(GL_TEXTURE_1D, it->second.texture_num);
234 if (it->second.needs_update) {
235 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, it->second.size, 0, GL_LUMINANCE, GL_FLOAT, it->second.values);
239 set_uniform_int(glsl_program_num, prefix, it->first, *sampler_num);