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