-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuniforms.h
81 lines (68 loc) · 2.21 KB
/
uniforms.h
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
#ifndef _F_UNIFORMS_H_
#define _F_UNIFORMS_H_
#include <string>
#if defined(__APPLE__) || defined(__GNUC__)
#include <unordered_map>
#define hash_map unordered_map
#else // WIN32
#include <hash_map>
#endif
class KeyFrame;
// Generic uniform interface.
class iUniform {
public:
virtual ~iUniform(){}
virtual const std::string& name() = 0;
virtual std::string toString() = 0;
virtual bool parse(const std::string& line) = 0;
virtual void send(int prog) = 0; // send to shader
virtual void bindToUI(void* bar) = 0; // register as tw var
virtual bool link(KeyFrame* kf) = 0; // link to storage within keyframe.
virtual iUniform* Clone() = 0;
virtual bool ok() = 0;
};
// Uniform interface instance smart ptr.
// Can be used safely in hash_map<>
class iUniformPtr {
public:
iUniformPtr() : ptr_(NULL) {}
explicit iUniformPtr(iUniform* ptr) : ptr_(ptr) {}
// Copy constructor needs to Clone() ptr_.
iUniformPtr(const iUniformPtr& other) : ptr_(NULL) {
if (other.ptr_) ptr_ = other.ptr_->Clone();
}
// Assignment operator also needs to Clone() ptr_.
iUniformPtr& operator=(const iUniformPtr& other) {
if (ptr_) { delete ptr_; ptr_ = NULL; }
if (other.ptr_) ptr_ = other.ptr_->Clone();
return *this;
}
iUniform* operator->(void) { return ptr_; }
~iUniformPtr() { delete ptr_; }
bool ok() { return ptr_ != NULL && ptr_->ok(); }
private:
bool operator==(const iUniformPtr& other);
bool operator<(const iUniformPtr& other);
iUniform* ptr_;
};
// Our active uniforms.
class Uniforms {
public:
// Try parse declared uniforms from GLSL source,
// including UI attributes from comment.
bool parseFromGlsl(const std::string& glsl);
// Map a uniform var to local storage backed by kf.
void link(KeyFrame* kf);
// Register linked vars with UI.
void bindToUI(void* bar);
// Send to shader.
void send(int program);
private:
bool parseLine(const std::string& line, iUniformPtr* uni);
#if defined(__GNUC__) || defined(__APPLE__)
std::hash_map<std::string, iUniformPtr, std::hash<std::string> > uniforms;
#else
std::hash_map<std::string, iUniformPtr> uniforms;
#endif
};
#endif // F_UNIFORMS_H_