-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphicEngine.hpp
414 lines (335 loc) · 8.07 KB
/
graphicEngine.hpp
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include <sys/types.h> // uint, ino_t
#include <iostream>
#include <map>
#include <vector>
#include <list>
#include <string>
#include <GL/glew.h>
//#define GLM_FORCE_INLINE
#include <glm/glm.hpp>
#include <GL/glext.h>
/**
* This macros displays OpenGL errors on stderr if in DEBUG mode.
* GLC means openGL Check error
* GLCR means openGL Check error with Return values
*/
#ifdef DEBUG
#define GLC(stmt) {stmt;GE::GLCheckError(#stmt, __FILE__,__LINE__);}
#define GLCR(stmt) [=]()->decltype(stmt){auto tmp=stmt;GE::GLCheckError(#stmt,__FILE__,__LINE__);return tmp;}()
#define GLI(stmt) {stmt;glGetError();}
#define GLIR(stmt) [=]()->decltype(stmt){auto tmp=stmt;glGetError();return tmp;}()
#else
#define GLC(stmt) stmt
#define GLCR(stmt) stmt
#define GLI(stmt) stmt
#define GLIR(stmt) stmt
#endif
using namespace std;
/** TODO
* Fonctionnalities:
* Phong lights
* Sample Distribution Shadow Maps
* Real-time cube map reflexions implemented with geometry shaders
* Key-Frame based animations
* Tesselation
* HDR pipeline and fragment shader tonemapping and blooming
* Normals maps / height maps / paralax mapping / subsurface scattering maps
* transparents faces
* fake refractions
* DOF
* screen space directionnal occlusion
* instanciated rendering
*
* Optimisations:
* texture compression
* indexed triangles stripping with primitive restart
* Frustrum culling with octree and mesh bunding and occlusion queries
*
*
* VA //no
* VBO //yes
* IBO //yes
* VAO //yes
* FBO //TODO
* TBO //no
* UBO //yes
* PBO // ???
* RBO // ???
*
*
* change GLEW
* choose devil
*
* Others:
* GL_NV_texture_barrier
* GL_EXT_direct_state_access
* GL_NVX_gpu_memory_info
* GL_ATI_meminfo
*
* OpenGL 3.0
* ARB_uniform_buffer_object
* ARB_texture_buffer_object
*
* OpenGL 3.1
* ARB_bindable_uniform_buffer_object
*
* OpenGL 3.2
* ARB_sync
* ARB_texture_query_lod
* ARB_texture_gather
* ARB_texture_cube_map_array
* ARB_texture_seamless_cube_map
*
* OpenGL 3.3:
* ARB_explicit_attrib_location YES
* ARB_sampler_objects
* ARB_texture_swizzle
* ARB_timer_query
* ARB_vertex_type_2_10_10_10_rev
*
*
* OpenGL 4.0
* ARB_shader_subroutine
* ARB_texture_buffer_object_rgb32
* ARB_transform_feedback2
* ARB_draw_indirect
* ARB_tessellation_shader
*
* OpenGL 4.1
* ARB_separate_shader_objects
* ARB_debug_output
*
*
* OpenGL 4.2
* ARB_shading_language_420pack
* ARB_map_buffer_alignment
*
* OpenGL 4.3
* ARB_vertex_attrib_binding
* KHR_debug
* ARB_program_interface_query
* ARB_explicit_uniform_location YES
*
*/
namespace DSGE
{
class GE;
class Object3D
{
public:
Object3D(GE* ge);
void rotate(float angle, float x, float y, float z);
void translate(float x, float y, float z);
void scale(float x, float y, float z);
void identity();
protected:
GE* ge;
glm::mat4 transform;
friend class GE;
};
class SceneObject : public Object3D
{
public:
SceneObject(GE* ge);
virtual void draw(glm::mat4 mat) = 0;
virtual void computeLightsPositions(glm::mat4 mat) = 0;
};
class SceneComposite : public SceneObject
{
public:
SceneComposite(GE* ge);
void add(SceneObject* o);
void remove(SceneObject* o);
virtual void draw(glm::mat4 mat);
virtual void computeLightsPositions(glm::mat4 mat);
private:
list<SceneObject*> children;
};
class Solid : public SceneObject
{
public:
uint mesh;
uint program;
uint texture;
Solid(GE* ge);
virtual void draw(glm::mat4 mat);
virtual void computeLightsPositions(glm::mat4) {}
};
#define NBLIGHTS 2
class Light : public SceneObject
{
public:
// TODO add properties
Light(GE* ge);
virtual void draw(glm::mat4) {}
virtual void computeLightsPositions(glm::mat4 mat);
glm::vec4 color;
float radius;
};
class GE
{
public:
GE();
void init(uint width,uint height);
void resize(uint width,uint height);
void render(DSGE::SceneObject* o, float time); // TODO let choose which framebuffer
void renderPostFX(uint program, float time); // TODO allow to render on an offscreen framebuffer
void clearDepth();
// FS Resource loaders
uint loadShader(string name, string filePath, GLenum type);
void unloadShader(uint id);
uint loadMesh(string name, string filePath);
void unloadMesh(uint id);
uint loadTexture(string name, string filePath, bool withMimaps);
uint unloadTexture(uint id);
// Shader program methods
uint createProgram(string name);
void addShaderToProgram(uint shader, uint program);
void linkProgram(uint program);
void deleteProgram(uint id);
// Memory management
void collectGarbadges();
/**
* TODO
* add properties
* add lights
*/
private: // Methods
uint getNextID()
{
return ++lastID;
}
private: // Class methods
void static inline GLCheckError(const char* stmt, const char* file, int line)
{
GLenum err = glGetError();
if (err == GL_NO_ERROR)
return;
std::cerr << "OpenGL error ";
switch(err)
{
case GL_INVALID_ENUM:
std::cerr << "GL_INVALID_ENUM";
break;
case GL_INVALID_VALUE:
std::cerr << "GL_INVALID_VALUE";
break;
case GL_INVALID_OPERATION:
std::cerr << "GL_INVALID_OPERATION";
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
break;
case GL_OUT_OF_MEMORY:
std::cerr << "GL_OUT_OF_MEMORY";
break;
case GL_STACK_UNDERFLOW:
std::cerr << "GL_STACK_UNDERFLOW";
break;
case GL_STACK_OVERFLOW:
std::cerr << "GL_STACK_OVERFLOW";
break;
default:
std::cerr << "UNKNOWN_ERROR";
}
std::cerr << ", at " << file << ":" << line << ", for " << stmt << endl;
}
public:
Object3D camera;
float shaderParam1;
float shaderParam2;
float shaderParam3;
float time;
private:
uint lastID;
enum
{
VERTEX_POSITION_ATTRIB = 0,
VERTEX_NORMAL_ATTRIB = 1,
VERTEX_TEXTCOORD_ATTRIB = 2,
};
enum
{
MODELMAT_UNIFORM = 0,
MODELVIEWMAT_UNIFORM = 1,
MODELVIEWPROJECTIONMAT_UNIFORM = 2,
NORMALMAT_UNIFORM = 3,
CAMERA_UNIFORM = 4,
TIME_UNIFORM = 5,
HEIGHT_UNIFORM = 6,
WIDTH_UNIFORM = 7,
PARAM1_UNIFORM = 8,
PARAM2_UNIFORM = 9,
PARAM3_UNIFORM = 10
};
enum{LIGHTSUBBP = 1}; // Lights Uniform Buffer Binding Pointer
struct Resource
{
string name;
uint backrefs;
};
/** Filesystem level resources
*
* Corresponds to one file on the file system.
*
* NOTE: We keep back reference count to desallocate
* resources not needed anymore.
*/
struct FSResource : Resource
{
uint size;
};
struct mesh : FSResource
{
GLuint VBO;
GLuint IBO;
GLuint VAO;
uint sizeOfIBO;
// TODO bounding sphere
};
struct texture : FSResource
{
GLuint id;
uint height, width;
// TODO compression type
};
map<ino_t, uint> inodeToResource;
map<uint, mesh> meshs;
map<uint, texture> textures;
/** Shaders
*/
struct shader : FSResource
{
GLuint id;
GLenum type;
};
struct program : Resource
{
GLuint id;
GLint lightsBlockIndex;
};
map<uint, shader> shaders;
map<uint, program> programs;
// --------------
uint width, height;
glm::mat4 projectionMatrix;
GLuint lightsUBO;
float lights[4*2*NBLIGHTS];
uint nbLights;
// Post FX
GLuint offscreenFBO;
GLuint offscreenVAO;
GLuint offscreenVBO;
GLuint offscreenIBO;
GLuint offscreenColorTex;
GLuint offscreenNormalTex;
GLuint offscreenDepthTex;
private:
friend class SceneObject;
friend class SceneComposite;
friend class Solid;
friend class Light;
void drawSolid(Solid* of, glm::mat4 mat);
void positionLight(Light* l, glm::mat4 mat);
};
}