]> git.sesse.net Git - movit/blob - effect.cpp
4cc1775d75587305e4504fdf0a534c78d73ec817
[movit] / effect.cpp
1 #define GL_GLEXT_PROTOTYPES 1
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <assert.h>
6 #include "effect.h"
7 #include "util.h"
8
9 #include <GL/gl.h>
10 #include <GL/glext.h>
11
12 void set_uniform_int(GLuint glsl_program_num, const std::string &prefix, const std::string &key, int value)
13 {
14         std::string name = prefix + "_" + key;
15         GLint l = glGetUniformLocation(glsl_program_num, name.c_str());
16         if (l == -1) {
17                 return;
18         }
19         check_error();
20         glUniform1i(l, value);
21         check_error();
22 }
23
24 void set_uniform_float(GLuint glsl_program_num, const std::string &prefix, const std::string &key, float value)
25 {
26         std::string name = prefix + "_" + key;
27         GLint l = glGetUniformLocation(glsl_program_num, name.c_str());
28         if (l == -1) {
29                 return;
30         }
31         check_error();
32         glUniform1f(l, value);
33         check_error();
34 }
35
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)
37 {
38         std::string name = prefix + "_" + key;
39         GLint l = glGetUniformLocation(glsl_program_num, name.c_str());
40         if (l == -1) {
41                 return;
42         }
43         check_error();
44         glUniform1fv(l, num_values, values);
45         check_error();
46 }
47
48 void set_uniform_vec2(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
49 {
50         std::string name = prefix + "_" + key;
51         GLint l = glGetUniformLocation(glsl_program_num, name.c_str());
52         if (l == -1) {
53                 return;
54         }
55         check_error();
56         glUniform2fv(l, 1, values);
57         check_error();
58 }
59
60 void set_uniform_vec3(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
61 {
62         std::string name = prefix + "_" + key;
63         GLint l = glGetUniformLocation(glsl_program_num, name.c_str());
64         if (l == -1) {
65                 return;
66         }
67         check_error();
68         glUniform3fv(l, 1, values);
69         check_error();
70 }
71
72 bool Effect::set_int(const std::string &key, int value)
73 {
74         if (params_int.count(key) == 0) {
75                 return false;
76         }
77         *params_int[key] = value;
78         return true;
79 }
80
81 bool Effect::set_float(const std::string &key, float value)
82 {
83         if (params_float.count(key) == 0) {
84                 return false;
85         }
86         *params_float[key] = value;
87         return true;
88 }
89
90 bool Effect::set_vec2(const std::string &key, const float *values)
91 {
92         if (params_vec2.count(key) == 0) {
93                 return false;
94         }
95         memcpy(params_vec2[key], values, sizeof(float) * 2);
96         return true;
97 }
98
99 bool Effect::set_vec3(const std::string &key, const float *values)
100 {
101         if (params_vec3.count(key) == 0) {
102                 return false;
103         }
104         memcpy(params_vec3[key], values, sizeof(float) * 3);
105         return true;
106 }
107
108 void Effect::register_int(const std::string &key, int *value)
109 {
110         assert(params_int.count(key) == 0);
111         params_int[key] = value;
112 }
113
114 void Effect::register_float(const std::string &key, float *value)
115 {
116         assert(params_float.count(key) == 0);
117         params_float[key] = value;
118 }
119
120 void Effect::register_vec2(const std::string &key, float *values)
121 {
122         assert(params_vec2.count(key) == 0);
123         params_vec2[key] = values;
124 }
125
126 void Effect::register_vec3(const std::string &key, float *values)
127 {
128         assert(params_vec3.count(key) == 0);
129         params_vec3[key] = values;
130 }
131
132 void Effect::register_1d_texture(const std::string &key, float *values, size_t size)
133 {
134         assert(params_tex_1d.count(key) == 0);
135
136         Texture1D tex;
137         tex.values = values;
138         tex.size = size;
139         tex.needs_update = false;
140         glGenTextures(1, &tex.texture_num);
141
142         glBindTexture(GL_TEXTURE_1D, tex.texture_num);
143         check_error();
144         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
145         check_error();
146         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
147         check_error();
148         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, size, 0, GL_LUMINANCE, GL_FLOAT, values);
149         check_error();
150
151         params_tex_1d[key] = tex;
152 }
153
154 void Effect::invalidate_1d_texture(const std::string &key)
155 {
156         assert(params_tex_1d.count(key) != 0);
157         params_tex_1d[key].needs_update = true;
158 }
159
160 // Output convenience uniforms for each parameter.
161 // These will be filled in per-frame.
162 std::string Effect::output_convenience_uniforms() const
163 {
164         std::string output = "";
165         for (std::map<std::string, float*>::const_iterator it = params_float.begin();
166              it != params_float.end();
167              ++it) {
168                 char buf[256];
169                 sprintf(buf, "uniform float PREFIX(%s);\n", it->first.c_str());
170                 output.append(buf);
171         }
172         for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
173              it != params_vec2.end();
174              ++it) {
175                 char buf[256];
176                 sprintf(buf, "uniform vec2 PREFIX(%s);\n", it->first.c_str());
177                 output.append(buf);
178         }
179         for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
180              it != params_vec3.end();
181              ++it) {
182                 char buf[256];
183                 sprintf(buf, "uniform vec3 PREFIX(%s);\n", it->first.c_str());
184                 output.append(buf);
185         }
186         for (std::map<std::string, Texture1D>::const_iterator it = params_tex_1d.begin();
187              it != params_tex_1d.end();
188              ++it) {
189                 char buf[256];
190                 sprintf(buf, "uniform sampler1D PREFIX(%s);\n", it->first.c_str());
191                 output.append(buf);
192         }
193         return output;
194 }
195
196 void Effect::set_uniforms(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
197 {
198         for (std::map<std::string, float*>::const_iterator it = params_float.begin();
199              it != params_float.end();
200              ++it) {
201                 set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
202         }
203         for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
204              it != params_vec2.end();
205              ++it) {
206                 set_uniform_vec2(glsl_program_num, prefix, it->first, it->second);
207         }
208         for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
209              it != params_vec3.end();
210              ++it) {
211                 set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
212         }
213
214         for (std::map<std::string, Texture1D>::const_iterator it = params_tex_1d.begin();
215              it != params_tex_1d.end();
216              ++it) {
217                 glActiveTexture(GL_TEXTURE0 + *sampler_num);
218                 check_error();
219                 glBindTexture(GL_TEXTURE_1D, it->second.texture_num);
220                 check_error();
221
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);
224                         check_error();
225                 }
226
227                 set_uniform_int(glsl_program_num, prefix, it->first, *sampler_num);
228                 ++*sampler_num;
229         }
230 }