]> git.sesse.net Git - movit/blob - effect.cpp
<sys/time.h> is needed for gettimeofday.
[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_float(GLuint glsl_program_num, const std::string &prefix, const std::string &key, float 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         glUniform1f(l, value);
21         check_error();
22 }
23
24 void set_uniform_vec2(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
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         glUniform2fv(l, 1, values);
33         check_error();
34 }
35
36 void set_uniform_vec3(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *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         glUniform3fv(l, 1, values);
45         check_error();
46 }
47
48 bool Effect::set_int(const std::string &key, int value)
49 {
50         if (params_int.count(key) == 0) {
51                 return false;
52         }
53         *params_int[key] = value;
54         return true;
55 }
56
57 bool Effect::set_float(const std::string &key, float value)
58 {
59         if (params_float.count(key) == 0) {
60                 return false;
61         }
62         *params_float[key] = value;
63         return true;
64 }
65
66 bool Effect::set_vec2(const std::string &key, const float *values)
67 {
68         if (params_vec2.count(key) == 0) {
69                 return false;
70         }
71         memcpy(params_vec2[key], values, sizeof(float) * 2);
72         return true;
73 }
74
75 bool Effect::set_vec3(const std::string &key, const float *values)
76 {
77         if (params_vec3.count(key) == 0) {
78                 return false;
79         }
80         memcpy(params_vec3[key], values, sizeof(float) * 3);
81         return true;
82 }
83
84 void Effect::register_int(const std::string &key, int *value)
85 {
86         assert(params_int.count(key) == 0);
87         params_int[key] = value;
88 }
89
90 void Effect::register_float(const std::string &key, float *value)
91 {
92         assert(params_float.count(key) == 0);
93         params_float[key] = value;
94 }
95
96 void Effect::register_vec2(const std::string &key, float *values)
97 {
98         assert(params_vec2.count(key) == 0);
99         params_vec2[key] = values;
100 }
101
102 void Effect::register_vec3(const std::string &key, float *values)
103 {
104         assert(params_vec3.count(key) == 0);
105         params_vec3[key] = values;
106 }
107
108 // Output convenience uniforms for each parameter.
109 // These will be filled in per-frame.
110 std::string Effect::output_convenience_uniforms()
111 {
112         std::string output = "";
113         for (std::map<std::string, float*>::const_iterator it = params_float.begin();
114              it != params_float.end();
115              ++it) {
116                 char buf[256];
117                 sprintf(buf, "uniform float PREFIX(%s);\n", it->first.c_str());
118                 output.append(buf);
119         }
120         for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
121              it != params_vec2.end();
122              ++it) {
123                 char buf[256];
124                 sprintf(buf, "uniform vec2 PREFIX(%s);\n", it->first.c_str());
125                 output.append(buf);
126         }
127         for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
128              it != params_vec3.end();
129              ++it) {
130                 char buf[256];
131                 sprintf(buf, "uniform vec3 PREFIX(%s);\n", it->first.c_str());
132                 output.append(buf);
133         }
134         return output;
135 }
136
137 void Effect::set_uniforms(GLuint glsl_program_num, const std::string& prefix)
138 {
139         for (std::map<std::string, float*>::const_iterator it = params_float.begin();
140              it != params_float.end();
141              ++it) {
142                 set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
143         }
144         for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
145              it != params_vec2.end();
146              ++it) {
147                 set_uniform_vec2(glsl_program_num, prefix, it->first, it->second);
148         }
149         for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
150              it != params_vec3.end();
151              ++it) {
152                 set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
153         }
154 }