-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader_helpers.h
168 lines (135 loc) · 4.57 KB
/
shader_helpers.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
166
167
168
#ifndef SHADER_HELPERS_H
#define SHADER_HELPERS_H
#include <QObject>
#include "shader_program_set.h"
struct sVertexAttributesHelper
{
// enum eType{
// VERTEX,
// COLOR,
// TEXTURE_COORDINATE,
// };
// eType type;
QString name;
GLuint location;
GLuint elementCount;
GLuint stride;
GLuint offset;
sVertexAttributesHelper(/*eType type,*/
QString name,
GLuint location,
GLuint elementCount,
GLuint stride,
GLuint offset):
// type(type),
name(name),
location(location),
elementCount(elementCount),
stride(stride),
offset(offset)
{
}
QString location_str()
{
return QString::number(location);
}
void applyAttrib(QGLFunctions *f)
{
f->glVertexAttribPointer(location,
elementCount,
GL_FLOAT, GL_FALSE,
stride,
(void*)offset);
f->glEnableVertexAttribArray(location);
}
};
#undef VERTEX_ATTRIBUTES_HELPER
#define VERTEX_ATTRIBUTES_HELPER(name, location, elementCount,\
stride, offset) \
sVertexAttributesHelper name(""#name"", location, elementCount, \
stride, offset)
#undef VA_HELPER_VERTEX
#define VA_HELPER_VERTEX(name, location) \
VERTEX_ATTRIBUTES_HELPER(name, location, \
3, 3 * sizeof(GL_FLOAT), 0)
#undef VA_HELPER_VERTEX_COLOR
#define VA_HELPER_VERTEX_COLOR(nameVertex, locationVertex, \
nameColor, locationColor) \
VERTEX_ATTRIBUTES_HELPER(nameVertex, locationVertex, \
3, 6 * sizeof(GL_FLOAT), 0);\
VERTEX_ATTRIBUTES_HELPER(nameColor, locationColor, \
3, 6 * sizeof(GL_FLOAT), \
3 * sizeof(GL_FLOAT))
#undef VA_HELPER_VERTEX_COLOR_TEXTURE
#define VA_HELPER_VERTEX_COLOR_TEXTURE(nameVertex, locationVertex, \
nameColor, locationColor,\
nameTexture, locationTexture) \
VERTEX_ATTRIBUTES_HELPER(nameVertex, locationVertex, \
3, 8 * sizeof(GL_FLOAT), 0);\
VERTEX_ATTRIBUTES_HELPER(nameColor, locationColor, \
3, 8 * sizeof(GL_FLOAT), \
3 * sizeof(GL_FLOAT));\
VERTEX_ATTRIBUTES_HELPER(nameTexture, locationTexture,\
2, 8 * sizeof(GL_FLOAT), \
6 * sizeof(GL_FLOAT))
#undef VA_HELPER_VERTEX_NORMAL
#define VA_HELPER_VERTEX_NORMAL(nameVertex, locationVertex, \
nameNormal, locationNormal) \
VA_HELPER_VERTEX_COLOR(nameVertex, locationVertex, \
nameNormal, locationNormal)
#undef VA_HELPER_VERTEX_NORMAL_TEXTURE
#define VA_HELPER_VERTEX_NORMAL_TEXTURE(nameVertex, locationVertex, \
nameNormal, locationNormal,\
nameTexture, locationTexture) \
VA_HELPER_VERTEX_COLOR_TEXTURE(nameVertex, locationVertex, \
nameNormal, locationNormal,\
nameTexture, locationTexture)
template <class MatrixType>
struct sMatrixHelper{
static quint32 counter()
{
static quint32 counter = 0;
return counter++;
}
QString model;
QString view ;
QString projection ;
MatrixType modelMat;
MatrixType viewMat;
MatrixType projMat;
sMatrixHelper()
{
model = "modelMatrix" + QString::number(counter());
view = "viewMatrix" + QString::number(counter());
projection = "projectionMatrix" + QString::number(counter());
}
const char* model_cstr()
{
return model.toUtf8().constData();
}
const char* view_cstr()
{
return view.toUtf8().constData();
}
const char* projection_cstr()
{
return projection.toUtf8().constData();
}
void loadMatrixToShader(ShaderProgramSet *prog)
{
prog->use();
prog->setUniformValue(model_cstr(),
modelMat);
prog->setUniformValue(view_cstr(),
viewMat);
prog->setUniformValue(projection_cstr(),
projMat);
}
};
struct sShaderHelper{
QString text;
sShaderHelper(QString text) : text(text)
{
}
};
#endif // SHADER_HELPERS_H