]> git.sesse.net Git - movit/blob - effect.cpp
Use GLSL 1.30-style attributes for the main shader.
[movit] / effect.cpp
1 #include <GL/glew.h>
2 #include <assert.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <utility>
6
7 #include "effect.h"
8 #include "effect_util.h"
9
10 using namespace std;
11
12 namespace movit {
13
14 bool Effect::set_int(const string &key, int value)
15 {
16         if (params_int.count(key) == 0) {
17                 return false;
18         }
19         *params_int[key] = value;
20         return true;
21 }
22
23 bool Effect::set_float(const string &key, float value)
24 {
25         if (params_float.count(key) == 0) {
26                 return false;
27         }
28         *params_float[key] = value;
29         return true;
30 }
31
32 bool Effect::set_vec2(const string &key, const float *values)
33 {
34         if (params_vec2.count(key) == 0) {
35                 return false;
36         }
37         memcpy(params_vec2[key], values, sizeof(float) * 2);
38         return true;
39 }
40
41 bool Effect::set_vec3(const string &key, const float *values)
42 {
43         if (params_vec3.count(key) == 0) {
44                 return false;
45         }
46         memcpy(params_vec3[key], values, sizeof(float) * 3);
47         return true;
48 }
49
50 bool Effect::set_vec4(const string &key, const float *values)
51 {
52         if (params_vec4.count(key) == 0) {
53                 return false;
54         }
55         memcpy(params_vec4[key], values, sizeof(float) * 4);
56         return true;
57 }
58
59 void Effect::register_int(const string &key, int *value)
60 {
61         assert(params_int.count(key) == 0);
62         params_int[key] = value;
63 }
64
65 void Effect::register_float(const string &key, float *value)
66 {
67         assert(params_float.count(key) == 0);
68         params_float[key] = value;
69 }
70
71 void Effect::register_vec2(const string &key, float *values)
72 {
73         assert(params_vec2.count(key) == 0);
74         params_vec2[key] = values;
75 }
76
77 void Effect::register_vec3(const string &key, float *values)
78 {
79         assert(params_vec3.count(key) == 0);
80         params_vec3[key] = values;
81 }
82
83 void Effect::register_vec4(const string &key, float *values)
84 {
85         assert(params_vec4.count(key) == 0);
86         params_vec4[key] = values;
87 }
88
89 // Output convenience uniforms for each parameter.
90 // These will be filled in per-frame.
91 string Effect::output_convenience_uniforms() const
92 {
93         string output = "";
94         for (map<string, float*>::const_iterator it = params_float.begin();
95              it != params_float.end();
96              ++it) {
97                 char buf[256];
98                 sprintf(buf, "uniform float PREFIX(%s);\n", it->first.c_str());
99                 output.append(buf);
100         }
101         for (map<string, float*>::const_iterator it = params_vec2.begin();
102              it != params_vec2.end();
103              ++it) {
104                 char buf[256];
105                 sprintf(buf, "uniform vec2 PREFIX(%s);\n", it->first.c_str());
106                 output.append(buf);
107         }
108         for (map<string, float*>::const_iterator it = params_vec3.begin();
109              it != params_vec3.end();
110              ++it) {
111                 char buf[256];
112                 sprintf(buf, "uniform vec3 PREFIX(%s);\n", it->first.c_str());
113                 output.append(buf);
114         }
115         for (map<string, float*>::const_iterator it = params_vec4.begin();
116              it != params_vec4.end();
117              ++it) {
118                 char buf[256];
119                 sprintf(buf, "uniform vec4 PREFIX(%s);\n", it->first.c_str());
120                 output.append(buf);
121         }
122         return output;
123 }
124
125 void Effect::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
126 {
127         for (map<string, float*>::const_iterator it = params_float.begin();
128              it != params_float.end();
129              ++it) {
130                 set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
131         }
132         for (map<string, float*>::const_iterator it = params_vec2.begin();
133              it != params_vec2.end();
134              ++it) {
135                 set_uniform_vec2(glsl_program_num, prefix, it->first, it->second);
136         }
137         for (map<string, float*>::const_iterator it = params_vec3.begin();
138              it != params_vec3.end();
139              ++it) {
140                 set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
141         }
142         for (map<string, float*>::const_iterator it = params_vec4.begin();
143              it != params_vec4.end();
144              ++it) {
145                 set_uniform_vec4(glsl_program_num, prefix, it->first, it->second);
146         }
147 }
148
149 void Effect::clear_gl_state() {}
150
151 }  // namespace movit