-
Notifications
You must be signed in to change notification settings - Fork 8
/
r_method.h
165 lines (129 loc) · 3.32 KB
/
r_method.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
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
#ifndef R_METHOD_HDR
#define R_METHOD_HDR
#include "r_common.h"
#include "u_string.h"
#include "u_vector.h"
#include "u_optional.h"
#include "u_map.h"
#include "m_mat.h"
namespace m {
struct perspective;
}
namespace r {
struct method;
struct uniform {
enum type {
kInt,
kSampler = kInt,
kInt2,
kFloat,
kVec2,
kVec3,
kVec4,
kMat3x4Array,
kMat4
};
uniform();
uniform(type t);
void set(int value); // kInt
void set(int x, int y); // kInt2
void set(float value); // kFloat
void set(const m::vec2 &value); // kVec2
void set(const m::vec3 &value); // kVec3
void set(const m::vec4 &value); // kVec4
void set(size_t count, const float *mats); // kMat3x4Array
void set(const m::mat4 &value); // kMat4
void post();
private:
friend struct method;
type m_type;
const method *m_method;
union {
int asInt;
int asInt2[2];
float asFloat;
m::vec2 asVec2;
m::vec3 asVec3;
m::vec4 asVec4;
m::mat4 asMat4;
struct {
m::mat3x4 *data;
size_t count;
} asMat3x4Array;
};
GLint m_handle;
};
inline uniform::uniform(type t)
: uniform()
{
m_type = t;
m_handle = -1;
}
struct method {
static constexpr size_t kMat3x4Space = 80;
method();
~method();
void enable();
void destroy();
bool init(const char *description = "unknown");
bool reload();
void define(const char *string);
void define(const char *string, size_t value);
void define(const char *string, float value);
uniform *getUniform(const u::string &name, uniform::type type);
protected:
bool addShader(GLenum shaderType, const char *shaderText);
bool finalize(const u::initializer_list<const char *> &attributes = {},
const u::initializer_list<const char *> &fragData = {},
bool initial = true);
u::optional<u::string> preprocess(const u::string &file, u::set<u::string> &uniforms, bool initial = true);
void post();
private:
friend struct uniform;
static m::mat3x4 m_mat3x4Scratch[kMat3x4Space];
static const method *m_currentMethod;
struct shader {
shader();
const char *shaderFile;
u::string shaderText;
GLuint object;
};
u::string m_prelude;
u::map<GLenum, shader> m_shaders;
u::map<u::string, uniform> m_uniforms;
u::initializer_list<const char *> m_attributes;
u::initializer_list<const char *> m_fragData;
GLuint m_program;
const char *m_description;
u::vector<u::string> m_defines;
};
inline method::shader::shader()
: shaderFile(nullptr)
, object(0)
{
}
struct defaultMethod : method {
defaultMethod();
bool init();
void setColorTextureUnit(int unit);
void setPerspective(const m::perspective &p);
private:
uniform *m_screenSize;
uniform *m_colorTextureUnit;
};
struct bboxMethod : method {
bboxMethod();
bool init();
void setWVP(const m::mat4 &wvp);
void setColor(const m::vec3 &color);
private:
uniform *m_WVP;
uniform *m_color;
};
inline bboxMethod::bboxMethod()
: m_WVP(nullptr)
, m_color(nullptr)
{
}
}
#endif