]> git.sesse.net Git - movit/blob - effect.cpp
Remove obsolete file todo.glsl.
[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(GLhandleARB 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_vec3(GLhandleARB 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         glUniform3fv(l, 1, values);
33         check_error();
34 }
35
36 bool Effect::set_int(const std::string &key, int value)
37 {
38         if (params_int.count(key) == 0) {
39                 return false;
40         }
41         *params_int[key] = value;
42         return true;
43 }
44
45 bool Effect::set_float(const std::string &key, float value)
46 {
47         if (params_float.count(key) == 0) {
48                 return false;
49         }
50         *params_float[key] = value;
51         return true;
52 }
53
54 bool Effect::set_vec3(const std::string &key, const float *values)
55 {
56         if (params_vec3.count(key) == 0) {
57                 return false;
58         }
59         memcpy(params_vec3[key], values, sizeof(float) * 3);
60         return true;
61 }
62
63 void Effect::register_int(const std::string &key, int *value)
64 {
65         assert(params_int.count(key) == 0);
66         params_int[key] = value;
67 }
68
69 void Effect::register_float(const std::string &key, float *value)
70 {
71         assert(params_float.count(key) == 0);
72         params_float[key] = value;
73 }
74
75 void Effect::register_vec3(const std::string &key, float *values)
76 {
77         assert(params_vec3.count(key) == 0);
78         params_vec3[key] = values;
79 }
80
81 // Output convenience uniforms for each parameter.
82 // These will be filled in per-frame.
83 std::string Effect::output_convenience_uniforms()
84 {
85         std::string output = "";
86         for (std::map<std::string, float*>::const_iterator it = params_float.begin();
87              it != params_float.end();
88              ++it) {
89                 char buf[256];
90                 sprintf(buf, "uniform float PREFIX(%s);\n", it->first.c_str());
91                 output.append(buf);
92         }
93         for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
94              it != params_vec3.end();
95              ++it) {
96                 char buf[256];
97                 sprintf(buf, "uniform vec3 PREFIX(%s);\n", it->first.c_str());
98                 output.append(buf);
99         }
100         return output;
101 }
102
103 void Effect::set_uniforms(GLhandleARB glsl_program_num, const std::string& prefix)
104 {
105         for (std::map<std::string, float*>::const_iterator it = params_float.begin();
106              it != params_float.end();
107              ++it) {
108                 set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
109         }
110         for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
111              it != params_vec3.end();
112              ++it) {
113                 set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
114         }
115 }