-
Notifications
You must be signed in to change notification settings - Fork 32
/
renderer.cpp
346 lines (278 loc) · 8.12 KB
/
renderer.cpp
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "core.h"
#include <GL/glew.h>
#include <stack>
#include "renderer.h"
namespace P3D::Graphics {
namespace Renderer {
GLFLAG WIREFRAME = GL_LINE;
GLFLAG FILL = GL_FILL;
GLFLAG POINT = GL_POINT;
GLFLAG FRONT_AND_BACK = GL_FRONT_AND_BACK;
GLFLAG STATIC_DRAW = GL_STATIC_DRAW;
GLFLAG STREAM_DRAW = GL_STREAM_DRAW;
GLFLAG DYNAMIC_DRAW = GL_DYNAMIC_DRAW;
GLFLAG ARRAY_BUFFER = GL_ARRAY_BUFFER;
GLFLAG PATCHES = GL_PATCHES;
GLFLAG QUADS = GL_QUADS;
GLFLAG LINE_STRIP = GL_LINE_STRIP;
GLFLAG TRIANGLES = GL_TRIANGLES;
GLFLAG LINES = GL_LINES;
GLFLAG POINTS = GL_POINTS;
GLFLAG FLOAT = GL_FLOAT;
GLFLAG UINT = GL_UNSIGNED_INT;
std::stack<RenderState> states;
RenderState current;
RenderState saveState() {
RenderState state;
int intbuffer;
unsigned char boolbuffer;
/*glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &intbuffer);
state.dfbo = intbuffer;
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &intbuffer);
state.rfbo = intbuffer;
glGetIntegerv(GL_RENDERBUFFER_BINDING, &intbuffer);
state.rbo = intbuffer;*/
// glGetIntegerv(GL_TEXTURE_BINDING_2D, &intbuffer);
// state.texture = intbuffer;
// glGetIntegerv(GL_CURRENT_PROGRAM, &intbuffer);
// state.program = intbuffer;
// glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &intbuffer);
// state.vao = intbuffer;
// glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &intbuffer);
// state.vbo = intbuffer;
// glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &intbuffer);
// state.vbo = intbuffer;
glGetBooleanv(GL_DEPTH_WRITEMASK, &boolbuffer);
state.depthmask = boolbuffer;
glGetIntegerv(GL_ACTIVE_TEXTURE, &state.activeTexture);
glGetIntegerv(GL_VIEWPORT, state.viewport);
glGetIntegerv(GL_POLYGON_MODE, state.mode);
state.blend = glIsEnabled(GL_BLEND);
state.cull = glIsEnabled(GL_CULL_FACE);
state.depth = glIsEnabled(GL_DEPTH_TEST);
//state.scissor = glIsEnabled(GL_SCISSOR_TEST);
return state;
}
void loadState(const RenderState& state) {
current = state;
/*bindReadbuffer(state.rfbo);
bindDrawbuffer(state.dfbo);
bindRenderbuffer(state.rbo);*/
// glUseProgram(state.program);
// glBindVertexArray(state.vao);
// glBindBuffer(GL_ARRAY_BUFFER, state.vbo);
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state.ibo);
// glActiveTexture(state.activeTexture);
// glBindTexture(GL_TEXTURE_2D, state.texture);
glDepthMask(state.depthmask);
glViewport(state.viewport[0], state.viewport[1], state.viewport[2], state.viewport[3]);
glPolygonMode(state.mode[0], state.mode[1]);
if (state.blend) glEnable(GL_BLEND); else glDisable(GL_BLEND);
if (state.cull) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE);
if (state.depth) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
//if (state.scissor) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST);
}
RenderState getState() {
RenderState state;
int intbuffer;
unsigned char boolbuffer;
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &intbuffer);
state.dfbo = intbuffer;
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &intbuffer);
state.rfbo = intbuffer;
glGetIntegerv(GL_RENDERBUFFER_BINDING, &intbuffer);
state.rbo = intbuffer;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &intbuffer);
state.texture = intbuffer;
glGetIntegerv(GL_CURRENT_PROGRAM, &intbuffer);
state.program = intbuffer;
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &intbuffer);
state.vao = intbuffer;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &intbuffer);
state.vbo = intbuffer;
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &intbuffer);
state.vbo = intbuffer;
glGetBooleanv(GL_DEPTH_WRITEMASK, &boolbuffer);
state.depthmask = boolbuffer;
glGetIntegerv(GL_ACTIVE_TEXTURE, &state.activeTexture);
glGetIntegerv(GL_VIEWPORT, state.viewport);
glGetIntegerv(GL_POLYGON_MODE, state.mode);
state.blend = glIsEnabled(GL_BLEND);
state.cull = glIsEnabled(GL_CULL_FACE);
state.depth = glIsEnabled(GL_DEPTH_TEST);
state.scissor = glIsEnabled(GL_SCISSOR_TEST);
return state;
}
void beginScene() {
RenderState backup = saveState();
states.push(backup);
}
void endScene() {
RenderState backup = states.top();
loadState(backup);
states.pop();
}
bool initGLEW() {
return glewInit() == GLEW_OK;
}
void clearDepth() {
glClear(GL_DEPTH_BUFFER_BIT);
}
void clearColor() {
glClear(GL_COLOR_BUFFER_BIT);
}
void clearStencil() {
glClear(GL_STENCIL_BUFFER_BIT);
}
void lineStipple(int factor, short pattern) {
glLineStipple(factor, pattern);
}
void lineWidth(float size) {
glLineWidth(size);
}
void viewport(const Vec2i& origin, const Vec2i& dimension) {
current.viewport[0] = origin.x;
current.viewport[1] = origin.y;
current.viewport[2] = dimension.x;
current.viewport[3] = dimension.y;
glViewport(origin.x, origin.y, dimension.x, dimension.y);
}
void enableDepthTest() {
glEnable(GL_DEPTH_TEST);
}
void disableDepthTest() {
glDisable(GL_DEPTH_TEST);
}
void enableDepthMask() {
glDepthMask(GL_TRUE);
}
void disableDepthMask() {
glDepthMask(GL_FALSE);
}
void enableCulling() {
glEnable(GL_CULL_FACE);
}
void disableCulling() {
glDisable(GL_CULL_FACE);
}
void enableBlending() {
glEnable(GL_BLEND);
}
void disableBlending() {
glDisable(GL_BLEND);
}
void standardBlendFunction() {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void enableMultisampling() {
glEnable(GL_MULTISAMPLE);
}
void polygonMode(int face, int mode) {
glPolygonMode(face, mode);
}
const char* getVendor() {
return reinterpret_cast<const char*>(glGetString(GL_VENDOR));
}
const char* getVersion() {
return reinterpret_cast<const char*>(glGetString(GL_VERSION));
}
const char* getRenderer() {
return reinterpret_cast<const char*>(glGetString(GL_RENDERER));
}
const char* getShaderVersion() {
return reinterpret_cast<const char*>(glGetString(GL_SHADING_LANGUAGE_VERSION));
}
int parseShaderVersion(const char* version) {
return 100 * (version[0] - '0') + 10 * (version[1] - '0');
}
int getMaxTextureUnits() {
int result;
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS_ARB, &result);
return result;
}
void genBuffers(int count, GLID* id) {
glGenBuffers(count, id);
}
void delBuffers(int count, GLID* id) {
glDeleteBuffers(count, id);
}
void genVertexArrays(int count, GLID* id) {
glGenVertexArrays(count, id);
}
void delVertexArrays(int count, GLID* id) {
glDeleteVertexArrays(count, id);
}
void bindBuffer(GLFLAG target, GLID id) {
glBindBuffer(target, id);
}
void bufferData(GLFLAG target, int size, int offset, GLFLAG type) {
glBufferData(target, size, reinterpret_cast<const void*>(offset), type);
}
void bufferSubData(GLFLAG target, int offset, int size, const void* pointer) {
glBufferSubData(target, offset, size, pointer);
}
void enableVertexAttribArray(GLID id) {
glEnableVertexAttribArray(id);
}
void bindTexture2D(GLID id) {
glBindTexture(GL_TEXTURE_2D, id);
}
void activeTexture(GLID unit) {
glActiveTexture(GL_TEXTURE0 + unit);
}
void vertexAttribPointer(GLID id, int size, GLFLAG type, bool normalized, int stride, const void* pointer) {
glVertexAttribPointer(id, size, type, normalized, stride, pointer);
}
void bindShader(GLID id) {
//if (current.program != id) {
// current.program = id;
glUseProgram(id);
//}
}
void bindFramebuffer(GLID id) {
glBindFramebuffer(GL_FRAMEBUFFER, id);
}
void bindDrawbuffer(GLID id) {
//if (current.dfbo != id) {
// current.dfbo = id;
glBindFramebuffer(GL_DRAW_BUFFER, id);
//}
}
void bindReadbuffer(GLID id) {
//if (current.rfbo != id) {
// current.rfbo = id;
glBindFramebuffer(GL_READ_BUFFER, id);
//}
}
void bindRenderbuffer(GLID id) {
//if (current.rbo != id) {
// current.rbo = id;
glBindRenderbuffer(GL_RENDERBUFFER, id);
//}
}
void bindVertexArray(GLID id) {
glBindVertexArray(id);
}
void scissor(int x, int y, int width, int height) {
glScissor(x, y, width, height);
}
void drawElements(GLFLAG mode, std::size_t count, GLFLAG type, const void* offset) {
glDrawElements(mode, count, type, offset);
}
void drawElementsInstanced(GLFLAG mode, std::size_t count, GLFLAG type, const void* offset, std::size_t primitives) {
glDrawElementsInstanced(mode, count, type, offset, primitives);
}
void drawArrays(GLFLAG mode, int first, std::size_t count) {
glDrawArrays(mode, first, count);
}
void defaultSettings(GLID defaultFrameBuffer) {
bindFramebuffer(defaultFrameBuffer);
standardBlendFunction();
enableDepthTest();
glLineWidth(1.5);
disableCulling();
clearColor();
clearDepth();
}
}
};