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