-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuniforms.cc
331 lines (300 loc) · 9.04 KB
/
uniforms.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "uniforms.h"
#include <iostream>
#include <string>
#include <sstream>
#include <AntTweakBar.h>
#include "camera.h"
#define GL_DECLARE_ONLY
#include "shader_procs.h"
using namespace std;
namespace {
// Parse a line into type,name,attr.
// TODO: handle arrays?
bool parseLine(const string& line,
string* type,
string* name,
string* attr) {
istringstream is(line);
string uniform;
is >> uniform; // drop "uniform"
is >> *type;
is >> *name;
size_t semi = name->find(';');
if (semi == string::npos) return false;
name->erase(semi);
size_t at = line.find('{');
if (at != string::npos) {
size_t end_at = line.find('}', at);
if (end_at != string::npos) {
attr->assign(line, at + 1, end_at - at - 1);
}
}
return true;
}
}
class IntUniform : public iUniform {
public:
IntUniform() : adr_(NULL) {}
virtual ~IntUniform() {}
iUniform* Clone() { return new IntUniform(*this); }
bool parse(const string& line) {
return parseLine(line, &type_, &name_, &attr_);
}
const string& name() { return name_; }
string toString() {
ostringstream o;
o << "int " << name_ << " ";
if (adr_ != NULL) o << *adr_ << " @" << adr_;
else o << "(nil)";
return o.str();
}
void bindToUI(void* vbar) {
TwAddVarRW((TwBar*)vbar, name_.c_str(), TW_TYPE_INT32, adr_,
attr_.c_str());
}
void send(int program) {
glUniform1i(glGetUniformLocation(program, name_.c_str()), *adr_);
}
bool link(KeyFrame* kf) {
adr_ = (int*)kf->map_address(type_, name_, 1);
return adr_ != NULL;
}
bool ok() { return adr_ != NULL; }
private:
IntUniform(const IntUniform& other) :
adr_(other.adr_), name_(other.name_),
type_(other.type_), attr_(other.attr_) {}
IntUniform& operator=(const IntUniform& other);
int* adr_;
string name_;
string type_;
string attr_;
};
class BoolUniform : public iUniform {
public:
BoolUniform() : adr_(NULL) {}
virtual ~BoolUniform() {}
iUniform* Clone() { return new BoolUniform(*this); }
bool parse(const string& line) {
return parseLine(line, &type_, &name_, &attr_);
}
const string& name() { return name_; }
string toString() {
ostringstream o;
o << "bool " << name_ << " ";
if (adr_ != NULL) o << *adr_ << " @" << adr_;
else o << "(nil)";
return o.str();
}
void bindToUI(void* vbar) {
TwAddVarRW((TwBar*)vbar, name_.c_str(), TW_TYPE_BOOL32, adr_,
attr_.c_str());
}
void send(int program) {
glUniform1i(glGetUniformLocation(program, name_.c_str()), *adr_);
}
bool link(KeyFrame* kf) {
adr_ = (int*)kf->map_address("int", name_, 1);
return adr_ != NULL;
}
bool ok() { return adr_ != NULL; }
private:
BoolUniform(const BoolUniform& other) :
adr_(other.adr_), name_(other.name_),
type_(other.type_), attr_(other.attr_) {}
BoolUniform& operator=(const BoolUniform& other);
int* adr_;
string name_;
string type_;
string attr_;
};
class FloatUniform : public iUniform {
public:
FloatUniform() : adr_(NULL) {}
virtual ~FloatUniform() {}
iUniform* Clone() { return new FloatUniform(*this); }
bool parse(const string& line) {
return parseLine(line, &type_, &name_, &attr_);
}
const string& name() { return name_; }
string toString() {
ostringstream o;
o << "float " << name_ << " ";
if (adr_ != NULL) o << *adr_ << " @" << adr_;
else o << "(nil)";
return o.str();
}
void bindToUI(void* bar) {
TwAddVarRW((TwBar*)bar, name_.c_str(), TW_TYPE_FLOAT, adr_, attr_.c_str());
}
void send(int program) {
glUniform1f(glGetUniformLocation(program, name_.c_str()), *adr_);
}
bool link(KeyFrame* kf) {
adr_ = (float*)kf->map_address(type_, name_, 1);
return adr_ != NULL;
}
bool ok() { return adr_ != NULL; }
private:
FloatUniform(const FloatUniform& other) :
adr_(other.adr_), name_(other.name_),
type_(other.type_), attr_(other.attr_) {}
FloatUniform& operator=(const FloatUniform& other);
float* adr_;
string name_;
string type_;
string attr_;
};
#if defined(GL_ARB_gpu_shader_fp64)
class DoubleUniform : public iUniform {
public:
DoubleUniform() : adr_(NULL) {}
virtual ~DoubleUniform() {}
iUniform* Clone() { return new DoubleUniform(*this); }
bool parse(const string& line) {
return parseLine(line, &type_, &name_, &attr_);
}
const string& name() { return name_; }
string toString() {
ostringstream o;
o << "double " << name_ << " ";
if (adr_ != NULL) o << *adr_ << " @" << adr_;
else o << "(nil)";
return o.str();
}
void bindToUI(void* bar) {
TwAddVarRW((TwBar*)bar, name_.c_str(), TW_TYPE_DOUBLE, adr_,
attr_.c_str());
}
void send(int program) {
glUniform1d(glGetUniformLocation(program, name_.c_str()), *adr_);
}
bool link(KeyFrame* kf) {
adr_ = (double*)kf->map_address(type_, name_, 1);
return adr_ != NULL;
}
bool ok() { return adr_ != NULL; }
private:
DoubleUniform(const DoubleUniform& other) :
adr_(other.adr_), name_(other.name_),
type_(other.type_), attr_(other.attr_) {}
DoubleUniform& operator=(const DoubleUniform& other);
double* adr_;
string name_;
string type_;
string attr_;
};
#endif
class Vec3Uniform : public iUniform {
public:
Vec3Uniform() : adr_(NULL) {}
virtual ~Vec3Uniform() {}
iUniform* Clone() { return new Vec3Uniform(*this); }
bool parse(const string& line) {
return parseLine(line, &type_, &name_, &attr_);
}
const string& name() { return name_; }
string toString() {
ostringstream o;
o << "vec3 " << name_ << " ";
if (adr_ != NULL) o << adr_[0] << " "
<< adr_[1] << " " << adr_[2] << " @" << adr_;
else o << "(nil)";
return o.str();
}
void bindToUI(void* bar) {
if (name_.find("Color") != string::npos) {
TwAddVarRW((TwBar*)bar, name_.c_str(), TW_TYPE_COLOR3F, adr_,
attr_.c_str());
} else if (name_.find("Vector") != string::npos) {
TwAddVarRW((TwBar*)bar, name_.c_str(), TW_TYPE_DIR3F, adr_,
attr_.c_str());
}
}
void send(int program) {
glUniform3fv(glGetUniformLocation(program, name_.c_str()), 1, adr_);
}
bool link(KeyFrame* kf) {
adr_ = (float*)kf->map_address(type_, name_, 1);
return adr_ != NULL;
}
bool ok() { return adr_ != NULL; }
private:
Vec3Uniform(const Vec3Uniform& other) :
adr_(other.adr_), name_(other.name_),
type_(other.type_), attr_(other.attr_) {}
Vec3Uniform& operator=(const Vec3Uniform& other);
float* adr_;
string name_;
string type_;
string attr_;
};
bool Uniforms::parseLine(const string& line, iUniformPtr* uni) {
if (line.empty() || line[0] != 'u') return false;
istringstream is(line);
string uniform;
is >> uniform;
if (uniform.compare("uniform")) return false;
string type;
is >> type;
if (type.compare("int") == 0) {
iUniformPtr tmp(new IntUniform);
if (!tmp->parse(line)) return false;
*uni = tmp;
} else if (type.compare("float") == 0) {
iUniformPtr tmp(new FloatUniform);
if (!tmp->parse(line)) return false;
*uni = tmp;
#if defined(GL_ARB_gpu_shader_fp64)
} else if (type.compare("double") == 0) {
iUniformPtr tmp(new DoubleUniform);
if (!tmp->parse(line)) return false;
*uni = tmp;
#endif
} else if (type.compare("vec3") == 0) {
iUniformPtr tmp(new Vec3Uniform);
if (!tmp->parse(line)) return false;
*uni = tmp;
} else if (type.compare("bool") == 0) {
iUniformPtr tmp(new BoolUniform);
if (!tmp->parse(line)) return false;
*uni = tmp;
} else {
return false;
}
return true;
}
bool Uniforms::parseFromGlsl(const string& glsl) {
istringstream in(glsl);
string line;
while (getline(in, line)) {
iUniformPtr uni;
if (parseLine(line, &uni)) {
cout << "glsl UNI: " << uni->toString() << endl;
uniforms.insert(make_pair(uni->name(), uni));
}
}
return true;
}
void Uniforms::link(KeyFrame* kf) {
for (hash_map<string, iUniformPtr>::iterator it =
uniforms.begin(); it != uniforms.end(); ++it) {
if (it->second->link(kf)) {
cout << "link UNI: " << it->second->toString() << endl;
}
}
}
void Uniforms::bindToUI(void* bar) {
for (hash_map<string, iUniformPtr>::iterator it =
uniforms.begin(); it != uniforms.end(); ++it) {
if (it->second->ok())
it->second->bindToUI(bar);
}
}
void Uniforms::send(int program) {
for (hash_map<string, iUniformPtr>::iterator it =
uniforms.begin(); it != uniforms.end(); ++it) {
if (it->second->ok())
it->second->send(program);
}
}