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_vec2(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
38 std::string name = prefix + "_" + key;
39 GLint l = glGetUniformLocation(glsl_program_num, name.c_str());
44 glUniform2fv(l, 1, values);
48 void set_uniform_vec3(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 glUniform3fv(l, 1, values);
60 bool Effect::set_int(const std::string &key, int value)
62 if (params_int.count(key) == 0) {
65 *params_int[key] = value;
69 bool Effect::set_float(const std::string &key, float value)
71 if (params_float.count(key) == 0) {
74 *params_float[key] = value;
78 bool Effect::set_vec2(const std::string &key, const float *values)
80 if (params_vec2.count(key) == 0) {
83 memcpy(params_vec2[key], values, sizeof(float) * 2);
87 bool Effect::set_vec3(const std::string &key, const float *values)
89 if (params_vec3.count(key) == 0) {
92 memcpy(params_vec3[key], values, sizeof(float) * 3);
96 void Effect::register_int(const std::string &key, int *value)
98 assert(params_int.count(key) == 0);
99 params_int[key] = value;
102 void Effect::register_float(const std::string &key, float *value)
104 assert(params_float.count(key) == 0);
105 params_float[key] = value;
108 void Effect::register_vec2(const std::string &key, float *values)
110 assert(params_vec2.count(key) == 0);
111 params_vec2[key] = values;
114 void Effect::register_vec3(const std::string &key, float *values)
116 assert(params_vec3.count(key) == 0);
117 params_vec3[key] = values;
120 void Effect::register_1d_texture(const std::string &key, float *values, size_t size)
122 assert(params_tex_1d.count(key) == 0);
127 tex.needs_update = false;
128 glGenTextures(1, &tex.texture_num);
130 glBindTexture(GL_TEXTURE_1D, tex.texture_num);
132 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
134 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
136 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, size, 0, GL_LUMINANCE, GL_FLOAT, values);
139 params_tex_1d[key] = tex;
142 void Effect::invalidate_1d_texture(const std::string &key)
144 assert(params_tex_1d.count(key) != 0);
145 params_tex_1d[key].needs_update = true;
148 // Output convenience uniforms for each parameter.
149 // These will be filled in per-frame.
150 std::string Effect::output_convenience_uniforms()
152 std::string output = "";
153 for (std::map<std::string, float*>::const_iterator it = params_float.begin();
154 it != params_float.end();
157 sprintf(buf, "uniform float PREFIX(%s);\n", it->first.c_str());
160 for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
161 it != params_vec2.end();
164 sprintf(buf, "uniform vec2 PREFIX(%s);\n", it->first.c_str());
167 for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
168 it != params_vec3.end();
171 sprintf(buf, "uniform vec3 PREFIX(%s);\n", it->first.c_str());
174 for (std::map<std::string, Texture1D>::const_iterator it = params_tex_1d.begin();
175 it != params_tex_1d.end();
178 sprintf(buf, "uniform sampler1D PREFIX(%s);\n", it->first.c_str());
184 void Effect::set_uniforms(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
186 for (std::map<std::string, float*>::const_iterator it = params_float.begin();
187 it != params_float.end();
189 set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
191 for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
192 it != params_vec2.end();
194 set_uniform_vec2(glsl_program_num, prefix, it->first, it->second);
196 for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
197 it != params_vec3.end();
199 set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
202 for (std::map<std::string, Texture1D>::const_iterator it = params_tex_1d.begin();
203 it != params_tex_1d.end();
205 glActiveTexture(GL_TEXTURE0 + *sampler_num);
207 glBindTexture(GL_TEXTURE_1D, it->second.texture_num);
210 if (it->second.needs_update) {
211 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, it->second.size, 0, GL_LUMINANCE, GL_FLOAT, it->second.values);
215 set_uniform_int(glsl_program_num, prefix, it->first, *sampler_num);