8 #include "effect_util.h"
11 bool Effect::set_int(const std::string &key, int value)
13 if (params_int.count(key) == 0) {
16 *params_int[key] = value;
20 bool Effect::set_float(const std::string &key, float value)
22 if (params_float.count(key) == 0) {
25 *params_float[key] = value;
29 bool Effect::set_vec2(const std::string &key, const float *values)
31 if (params_vec2.count(key) == 0) {
34 memcpy(params_vec2[key], values, sizeof(float) * 2);
38 bool Effect::set_vec3(const std::string &key, const float *values)
40 if (params_vec3.count(key) == 0) {
43 memcpy(params_vec3[key], values, sizeof(float) * 3);
47 bool Effect::set_vec4(const std::string &key, const float *values)
49 if (params_vec4.count(key) == 0) {
52 memcpy(params_vec4[key], values, sizeof(float) * 4);
56 void Effect::register_int(const std::string &key, int *value)
58 assert(params_int.count(key) == 0);
59 params_int[key] = value;
62 void Effect::register_float(const std::string &key, float *value)
64 assert(params_float.count(key) == 0);
65 params_float[key] = value;
68 void Effect::register_vec2(const std::string &key, float *values)
70 assert(params_vec2.count(key) == 0);
71 params_vec2[key] = values;
74 void Effect::register_vec3(const std::string &key, float *values)
76 assert(params_vec3.count(key) == 0);
77 params_vec3[key] = values;
80 void Effect::register_vec4(const std::string &key, float *values)
82 assert(params_vec4.count(key) == 0);
83 params_vec4[key] = values;
86 void Effect::register_1d_texture(const std::string &key, float *values, size_t size)
88 assert(params_tex_1d.count(key) == 0);
93 tex.needs_update = false;
94 glGenTextures(1, &tex.texture_num);
96 glBindTexture(GL_TEXTURE_1D, tex.texture_num);
98 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
100 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
102 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, size, 0, GL_LUMINANCE, GL_FLOAT, values);
105 params_tex_1d[key] = tex;
108 void Effect::invalidate_1d_texture(const std::string &key)
110 assert(params_tex_1d.count(key) != 0);
111 params_tex_1d[key].needs_update = true;
114 // Output convenience uniforms for each parameter.
115 // These will be filled in per-frame.
116 std::string Effect::output_convenience_uniforms() const
118 std::string output = "";
119 for (std::map<std::string, float*>::const_iterator it = params_float.begin();
120 it != params_float.end();
123 sprintf(buf, "uniform float PREFIX(%s);\n", it->first.c_str());
126 for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
127 it != params_vec2.end();
130 sprintf(buf, "uniform vec2 PREFIX(%s);\n", it->first.c_str());
133 for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
134 it != params_vec3.end();
137 sprintf(buf, "uniform vec3 PREFIX(%s);\n", it->first.c_str());
140 for (std::map<std::string, float*>::const_iterator it = params_vec4.begin();
141 it != params_vec4.end();
144 sprintf(buf, "uniform vec4 PREFIX(%s);\n", it->first.c_str());
147 for (std::map<std::string, Texture1D>::const_iterator it = params_tex_1d.begin();
148 it != params_tex_1d.end();
151 sprintf(buf, "uniform sampler1D PREFIX(%s);\n", it->first.c_str());
157 void Effect::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
159 for (std::map<std::string, float*>::const_iterator it = params_float.begin();
160 it != params_float.end();
162 set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
164 for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
165 it != params_vec2.end();
167 set_uniform_vec2(glsl_program_num, prefix, it->first, it->second);
169 for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
170 it != params_vec3.end();
172 set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
174 for (std::map<std::string, float*>::const_iterator it = params_vec4.begin();
175 it != params_vec4.end();
177 set_uniform_vec4(glsl_program_num, prefix, it->first, it->second);
180 for (std::map<std::string, Texture1D>::iterator it = params_tex_1d.begin();
181 it != params_tex_1d.end();
183 glActiveTexture(GL_TEXTURE0 + *sampler_num);
185 glBindTexture(GL_TEXTURE_1D, it->second.texture_num);
188 if (it->second.needs_update) {
189 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, it->second.size, 0, GL_LUMINANCE, GL_FLOAT, it->second.values);
191 it->second.needs_update = false;
194 set_uniform_int(glsl_program_num, prefix, it->first, *sampler_num);
199 void Effect::clear_gl_state() {}