-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphicEngine.cpp
813 lines (621 loc) · 22.3 KB
/
graphicEngine.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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
#include "graphicEngine.hpp"
#include <sys/types.h>
#include <sys/stat.h>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <math.h>
#include <IL/il.h>
//#include <glm/gtc/matrix_transform.hpp>
//#include <glm/gtc/type_ptr.hpp>
#include <glm/ext.hpp>
using namespace std;
using namespace DSGE;
GE::GE() : camera(this), shaderParam1(9.9), shaderParam2(0), shaderParam3(0), lastID(0)
{}
void GE::init(uint width, uint height)
{
glewExperimental=GL_TRUE;
ilInit();
if(GLIR(glewInit()) != GLEW_OK)
exit(17);
/*
glEnable(GL_DEBUG_OUTPUT_ARB);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE);
glDebugMessageCallbackARB(&debugOutput, NULL);
*/
GLC(glEnable(GL_CULL_FACE));
GLC(glCullFace(GL_BACK));
GLC(glEnable(GL_DEPTH_TEST));
GLC(glDisable(GL_BLEND));
GLC(glDisable(GL_MULTISAMPLE));
GLC(glDisable(GL_STENCIL_TEST));
GLC(glHint(GL_LINE_SMOOTH_HINT, GL_NICEST));
GLC(glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST));
GLC(glHint(GL_TEXTURE_COMPRESSION_HINT, GL_NICEST));
// create lights UBO
GLC(glGenBuffers(1, &lightsUBO));
GLC(glBindBuffer(GL_UNIFORM_BUFFER, lightsUBO));
GLC(glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::vec4)*2*NBLIGHTS, nullptr, GL_STREAM_DRAW));
GLC(glBindBuffer(GL_UNIFORM_BUFFER, 0));
GLC(glBindBufferRange(GL_UNIFORM_BUFFER, LIGHTSUBBP, lightsUBO, 0, sizeof(glm::vec4) *2* NBLIGHTS));
// create offline rectangle
GLfloat offscreenRectVertex[4*(2+2)] =
{
-1.0f,-1.0f, 0.0f, 0.0f,
1.0f,-1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 1.0f
};
GLuint offscreenRectIndex[2 * 3] =
{
0, 1, 2,
2, 3, 0
};
GLC(glGenVertexArrays(1, &offscreenVAO));
GLC(glBindVertexArray(offscreenVAO));
GLC(glGenBuffers(1, &offscreenVBO));
GLC(glBindBuffer(GL_ARRAY_BUFFER, offscreenVBO));
GLC(glBufferData(GL_ARRAY_BUFFER, 4 * (2 + 2) * sizeof(GLfloat), offscreenRectVertex, GL_STATIC_DRAW));
GLC(glEnableVertexAttribArray(VERTEX_POSITION_ATTRIB));
GLC(glGenBuffers(1, &offscreenIBO));
GLC(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,offscreenIBO));
GLC(glBufferData(GL_ELEMENT_ARRAY_BUFFER,2 * 3 * sizeof(GLuint), offscreenRectIndex, GL_STATIC_DRAW));
GLC(glEnableVertexAttribArray(VERTEX_TEXTCOORD_ATTRIB));
GLC(glVertexAttribPointer(VERTEX_POSITION_ATTRIB, 2, GL_FLOAT,GL_FALSE, 16, ( char * ) NULL + 0 ));
GLC(glVertexAttribPointer(VERTEX_TEXTCOORD_ATTRIB, 2, GL_FLOAT, GL_FALSE, 16, ( char * ) NULL + 8 ));
GLC(glBindVertexArray(0));
GLC(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
GLC(glBindBuffer(GL_ARRAY_BUFFER, 0));
// create FBO
GLC(glGenFramebuffers(1, &offscreenFBO));
resize(width, height);
}
void GE::resize(uint width, uint height)
{
this->width = width;
this->height = height;
GLC(glViewport (0, 0, width, height));
projectionMatrix = glm::perspective(75.0f, this->width / (float) this->height, 0.1f, 500.f);
GLC(glClearColor(0, 0, 0, 1));
GLC(glDeleteTextures(1, &offscreenColorTex));
GLC(glGenTextures(1, &offscreenColorTex));
GLC(glBindTexture(GL_TEXTURE_2D, offscreenColorTex));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0));
GLC(glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, this->width, this->height));
GLC(glDeleteTextures(1, &offscreenNormalTex));
GLC(glGenTextures(1, &offscreenNormalTex));
GLC(glBindTexture(GL_TEXTURE_2D, offscreenNormalTex));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0));
GLC(glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, this->width, this->height));
GLC(glDeleteTextures(1, &offscreenDepthTex));
GLC(glGenTextures(1, &offscreenDepthTex));
GLC(glBindTexture(GL_TEXTURE_2D, offscreenDepthTex));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
GLC(glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0));
GLC(glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32, this->width, this->height));
GLC(glBindFramebuffer(GL_FRAMEBUFFER, offscreenFBO));
GLC(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, offscreenColorTex, 0));
GLC(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, offscreenNormalTex, 0));
GLC(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, offscreenDepthTex, 0));
GLenum status;
status = GLCR(glCheckFramebufferStatus(GL_FRAMEBUFFER));
if(status != GL_FRAMEBUFFER_COMPLETE)
exit(13);
GLenum buffers[2]={GL_COLOR_ATTACHMENT0,GL_COLOR_ATTACHMENT1};
GLC(glDrawBuffers(2, buffers));
GLC(glBindFramebuffer(GL_FRAMEBUFFER, 0));
}
void GE::clearDepth()
{
glBindFramebuffer(GL_FRAMEBUFFER, offscreenFBO);
GLC(glClear(GL_DEPTH_BUFFER_BIT));
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void GE::render(SceneObject* o, float time)
{
this->time=time;
nbLights = 0;
o->computeLightsPositions(glm::mat4(1.f));
for(int i=nbLights; i<NBLIGHTS; ++i)
{
lights[i*4+3]=0;
}
GLC(glEnable(GL_CULL_FACE));
GLC(glCullFace(GL_BACK));
GLC(glEnable(GL_DEPTH_TEST));
GLC(glBindBuffer(GL_UNIFORM_BUFFER, lightsUBO));
GLC(glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::vec4)*2*NBLIGHTS, lights));
GLC(glBindBuffer(GL_UNIFORM_BUFFER, 0));
GLC(glBindFramebuffer(GL_FRAMEBUFFER, offscreenFBO));
o->draw(glm::mat4(1.f));
GLC(glBindFramebuffer(GL_FRAMEBUFFER, 0));
GLC(glBindVertexArray(0));
GLC(glUseProgram(0));
GLC(glBindTexture(GL_TEXTURE_2D, 0));
return;
}
void GE::renderPostFX(uint program, float time)
{
GLC(glDisable(GL_CULL_FACE));
GLC(glDisable(GL_DEPTH_TEST));
GLC(glBindVertexArray(offscreenVAO));
GLC(glActiveTexture(GL_TEXTURE0)); // TODO Use custom enum
GLC(glBindTexture(GL_TEXTURE_2D, offscreenColorTex));
GLC(glActiveTexture(GL_TEXTURE0+1)); // TODO Use custom enum
GLC(glBindTexture(GL_TEXTURE_2D, offscreenNormalTex));
GLC(glActiveTexture(GL_TEXTURE0+2)); // TODO Use custom enum
GLC(glBindTexture(GL_TEXTURE_2D, offscreenDepthTex));
GLC(glUseProgram(programs[program].id));
GLI(glUniform1f(TIME_UNIFORM, time));
GLI(glUniform1f(HEIGHT_UNIFORM, height));
GLI(glUniform1f(WIDTH_UNIFORM, width));
GLI(glUniform1f(PARAM1_UNIFORM, shaderParam1));
GLI(glUniform1f(PARAM2_UNIFORM, shaderParam2));
GLI(glUniform1f(PARAM3_UNIFORM, shaderParam3));
GLC(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0));
GLC(glActiveTexture(GL_TEXTURE0));
}
void GE::drawSolid(Solid* of, glm::mat4 mat)
{
mesh m = meshs[of->mesh];
texture t = textures[of->texture];
program p = programs[of->program];
GLC(glUseProgram(p.id));
GLI(glUniform1f(TIME_UNIFORM, time));
cout << time <<endl;
GLI(glUniform1f(PARAM1_UNIFORM, shaderParam1));
GLI(glUniform1f(PARAM2_UNIFORM, shaderParam2));
GLI(glUniform1f(PARAM3_UNIFORM, shaderParam3));
glm::vec4 camPos = glm::inverse(camera.transform) * glm::vec4(0, 0, 0, 1);
glm::mat4 modelViewProjectionMatrix = projectionMatrix * camera.transform * mat;
glm::mat3 normalMatrix = glm::mat3(mat);
GLI(glUniform3f(CAMERA_UNIFORM, camPos.x, camPos.y, camPos.z));
GLC(glUniformMatrix4fv(MODELVIEWPROJECTIONMAT_UNIFORM, 1, 0, (GLfloat*) glm::value_ptr(modelViewProjectionMatrix)));
GLI(glUniformMatrix4fv(MODELMAT_UNIFORM, 1, 0, (GLfloat*) glm::value_ptr(mat)));
GLI(glUniformMatrix3fv(NORMALMAT_UNIFORM, 1, 0, (GLfloat*) glm::value_ptr(normalMatrix)));
GLC(glBindVertexArray(m.VAO));
GLC(glActiveTexture(GL_TEXTURE0));
GLC(glBindTexture(GL_TEXTURE_2D, t.id));
GLC(glDrawElements(GL_TRIANGLES, m.sizeOfIBO, GL_UNSIGNED_INT, 0));
}
void GE::positionLight(Light* l, glm::mat4 mat)
{
if(nbLights == NBLIGHTS)
return;
glm::vec4 lightPos = mat * glm::vec4(0, 0, 0, 1);
lights[nbLights*4+0]=lightPos.x;
lights[nbLights*4+1]=lightPos.y;
lights[nbLights*4+2]=lightPos.z;
lights[nbLights*4+3]=l->radius;
lights[NBLIGHTS*4+nbLights*4+0]=l->color.r;
lights[NBLIGHTS*4+nbLights*4+1]=l->color.g;
lights[NBLIGHTS*4+nbLights*4+2]=l->color.b;
lights[NBLIGHTS*4+nbLights*4+3]=l->color.a;
nbLights++;
}
uint GE::loadMesh(string name, string filePath)
{
struct point
{
float x,y,z;
};
struct normal
{
float x,y,z;
};
struct coord
{
float x,y;
};
struct vertex
{
uint pointId;
uint normalId;
uint coordId;
bool operator<(const vertex& v) const
{
if( this->pointId < v.pointId
|| ( this->pointId == v.pointId &&
( this->normalId < v.normalId || (this->normalId == v.normalId && this->coordId < v.coordId))))
return true;
else
return false;
}
};
struct triangle
{
uint ids[3];
};
vector<point> points;
vector<normal> normals;
vector<coord> coords;
map<vertex,uint> vertices;
vector<triangle> triangles;
uint vertCounter=0;
ifstream file(filePath.c_str(),ios::in);
if (!file)
return 0;
string buffer, key;
int progress=0;
struct stat filestatus;
stat( filePath.c_str(), &filestatus );
int fileSize = filestatus.st_size;
cout << "Loading mesh : " << filePath << endl;
while(getline(file, buffer))
{
istringstream line(buffer);
key = "";
line >> key;
if(key == "v")
{
point tmp;
line >> tmp.x >> tmp.y >> tmp.z;
points.push_back ( tmp );
}
else if(key == "vn")
{
normal tmp;
line >> tmp.x >> tmp.y >> tmp.z;
normals.push_back ( tmp );
}
else if(key == "vt")
{
coord tmp;
line >> tmp.x >> tmp.y;
coords.push_back ( tmp );
}
else if(key == "g")
{
//TODO implement
}
else if(key == "f")
{
triangle t;
vertex v;
string sv;
int c;
for(int i = 0; i < 3; i++)
{
line >> sv;
c = sv.find_first_of('/');
v.pointId = atoi(sv.substr(0, c).c_str());
sv=sv.substr(c + 1, sv.length() - c - 1);
c=sv.find_first_of('/');
if (c == 0)
v.coordId = 0;
v.coordId = atoi(sv.substr(0, c).c_str());
sv=sv.substr ( c+1,sv.length()-c-1 );
c=sv.find_first_of('/');
if (c == 0)
v.normalId = 0;
v.normalId = atoi(sv.substr(0, c).c_str());
if(vertices.find(v) != vertices.end())
t.ids[i]=vertices[v];
else
{
vertices[v]=vertCounter;
t.ids[i]=vertCounter;
vertCounter++;
}
}
triangles.push_back(t);
}
if(progress%100 == 0)
{
printf("%3.2f%%\t\r",file.tellg()/(float)fileSize*100);
}
progress++;
}
cout << "100.00% "<<endl;
file.close();
// TODO calculate bounding sphere
GLfloat* vertexArray = new GLfloat[vertices.size() * (3 + 3 + 2)];
/* TODO Optimize the order of vertices */
for(auto it=vertices.begin(); it!=vertices.end(); it++)
{
vertexArray[it->second*8] = points[it->first.pointId-1].x;
vertexArray[it->second*8+1] = points[it->first.pointId-1].y;
vertexArray[it->second*8+2] = points[it->first.pointId-1].z;
if(it->first.normalId)
{
vertexArray[it->second*8+3] = normals[it->first.normalId-1].x;
vertexArray[it->second*8+4] = normals[it->first.normalId-1].y;
vertexArray[it->second*8+5] = normals[it->first.normalId-1].z;
}
else
{
vertexArray[it->second*8+3] = 0;
vertexArray[it->second*8+4] = 0;
vertexArray[it->second*8+5] = 0;
}
if(it->first.coordId)
{
vertexArray[it->second*8+6] = coords[it->first.coordId-1].x;
vertexArray[it->second*8+7] = coords[it->first.coordId-1].y;
}
else
{
vertexArray[it->second*8+6] = 0;
vertexArray[it->second*8+7] = 0;
}
/*
cout << "px " << points[it->first.pointId].x << endl;
cout << "py " << points[it->first.pointId].y << endl;
cout << "pz " << points[it->first.pointId].z << endl;
cout << "nx " << normals[it->first.pointId].x << endl;
cout << "ny " << normals[it->first.pointId].y << endl;
cout << "nz " << normals[it->first.pointId].z << endl;
cout << "cx " << coords[it->first.pointId].x << endl;
cout << "cy " << coords[it->first.pointId].y << endl;
cout << endl;
*/
}
GLuint* indexArray = new GLuint[triangles.size() * 3];
for(uint i=0; i<triangles.size();++i)
{
indexArray[i*3] = triangles[i].ids[0];
indexArray[i*3+1] = triangles[i].ids[1];
indexArray[i*3+2] = triangles[i].ids[2];
}
GLuint VBO;
GLC(glGenBuffers(1, &VBO));
GLC(glBindBuffer(GL_ARRAY_BUFFER, VBO));
GLC(glBufferData(GL_ARRAY_BUFFER, vertices.size() * (3 + 3 + 2) * sizeof(GLfloat), vertexArray, GL_STATIC_DRAW));
GLC(glBindBuffer(GL_ARRAY_BUFFER, 0));
GLuint IBO;
GLC(glGenBuffers(1, &IBO));
GLC(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,IBO));
GLC(glBufferData(GL_ELEMENT_ARRAY_BUFFER,triangles.size() * 3 * sizeof(GLuint), indexArray, GL_STATIC_DRAW));
GLC(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
free(vertexArray);
free(indexArray);
cout << "vertex count: " << vertices.size() << endl;
cout << "triangle count: " << triangles.size() << endl<<endl;
GLuint VAO;
GLC(glGenVertexArrays(1, &VAO));
GLC(glBindVertexArray(VAO));
GLC(glEnableVertexAttribArray(VERTEX_POSITION_ATTRIB));
GLC(glEnableVertexAttribArray(VERTEX_NORMAL_ATTRIB));
GLC(glEnableVertexAttribArray(VERTEX_TEXTCOORD_ATTRIB));
GLC(glBindBuffer(GL_ARRAY_BUFFER, VBO));
GLC(glVertexAttribPointer (VERTEX_POSITION_ATTRIB, 3, GL_FLOAT,GL_FALSE, 32, ( char * ) NULL + 0 ));
GLC(glVertexAttribPointer(VERTEX_NORMAL_ATTRIB, 3, GL_FLOAT,GL_TRUE, 32, ( char * ) NULL + 12 ));
GLC(glVertexAttribPointer (VERTEX_TEXTCOORD_ATTRIB, 2, GL_FLOAT, GL_FALSE, 32, ( char * ) NULL + 24 ));
GLC(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO));
GLC(glBindVertexArray(0));
// Get file inode number
struct stat sb;
stat(filePath.c_str(), &sb);
//cout << sb.st_ino << endl;
// TODO save inode
mesh m;
m.IBO=IBO;
m.VBO=VBO;
m.VAO=VAO;
m.size=vertices.size() * (3 + 3 + 2) + triangles.size() * 3;
m.sizeOfIBO = triangles.size() * 3;
m.backrefs=0;
m.name=name;
uint ID=getNextID();
this->meshs.insert(pair<uint,mesh>(ID,m));
return ID;
}
uint GE::loadShader(string name, string filePath, GLenum type)
{
GLuint shader;
GLsizei logsize;
GLint compile_status = GL_TRUE;
char *log;
shader = GLCR(glCreateShader(type));
ifstream file(filePath,ios::in);
string src,line;
while(getline(file,line))
src+="\n"+line;
file.close();
const char* strchar=src.c_str();
GLC(glShaderSource(shader, 1, (const GLchar**) &(strchar), NULL));
GLC(glCompileShader(shader));
GLC(glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status));
if (compile_status != GL_TRUE)
{
GLC(glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logsize));
log = (char*)malloc(logsize + 1);
GLC(glGetShaderInfoLog(shader, logsize, &logsize, log));
cout << endl << "Error compiling " << filePath << " :" <<endl << log << endl;
free (log);
GLC(glDeleteShader(shader));
return 0;
}
GE::shader s;
uint ID=getNextID();
s.id = shader;
s.type = type;
s.size=0; //TODO complete
s.backrefs=0;
s.name=name;
this->shaders.insert(pair<uint,GE::shader>(ID,s));
return ID;
}
uint GE::createProgram(string name)
{
uint ID=getNextID();
program p;
p.id=GLCR(glCreateProgram());
p.backrefs=0;
p.name=name;
this->programs.insert(pair<uint,program>(ID,p));
return ID;
}
void GE::addShaderToProgram(uint shader, uint program)
{
GLuint p, s;
p = this->programs[program].id;
s = this->shaders[shader].id;
if(!GLCR(glIsShader(s)))
exit (12);
GLC(glAttachShader(p, s));
//TODO garbage collection ?
}
void GE::linkProgram(uint program)
{
GE::program* p = &(this->programs[program]);
/*
GLC(glBindAttribLocation(p, VERTEX_POSITION_ATTRIB, "in_position"));
GLC(glBindAttribLocation(p, VERTEX_NORMAL_ATTRIB, "in_normal"));
GLC(glBindAttribLocation(p, VERTEX_TEXTCOORD_ATTRIB, "in_textCoord"));
*/
GLC(glLinkProgram(p->id));
//this->programs[program].modelViewProjectionMatrixID =
// GLCR(glGetUniformLocation(p, "modelViewProjectionMatrix" ));
p->lightsBlockIndex =
GLCR(glGetUniformBlockIndex(p->id, "lights"));
if(p->lightsBlockIndex >= 0)
GLC(glUniformBlockBinding(p->id, p->lightsBlockIndex, LIGHTSUBBP));
GLint link_status = GL_TRUE;
GLint logsize=200;
char* log;
GLC(glGetProgramiv(p->id, GL_LINK_STATUS, &link_status));
if(link_status != GL_TRUE)
{
GLC(glGetProgramiv(p->id, GL_INFO_LOG_LENGTH, &logsize));
log = (char*)malloc(logsize + 1);
GLC(glGetProgramInfoLog(p->id, logsize, &logsize, log));
cout << endl<< "Error while linking:"<< endl << log << endl;
free ( log );
GLC(glDeleteProgram(p->id));
exit(-1);
}
}
uint GE::loadTexture(string name, string filePath, bool withMimaps)
{
ILuint ilID;
GLuint glID;
ilGenImages(1, &ilID);
ilBindImage(ilID);
if (!ilLoadImage(filePath.c_str()))
exit (11);
if (!ilConvertImage(IL_RGBA,IL_UNSIGNED_BYTE))
exit (11);
GLC(glGenTextures(1,&glID));
GLC(glBindTexture(GL_TEXTURE_2D,glID));
int mipmapLevels = floor(log2(glm::max(ilGetInteger(IL_IMAGE_WIDTH),ilGetInteger(IL_IMAGE_HEIGHT)))+1);
GLC(glTexStorage2D(GL_TEXTURE_2D, withMimaps?mipmapLevels:1, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT)));
GLC(glTexSubImage2D(
GL_TEXTURE_2D,
0,
0,
0,
ilGetInteger(IL_IMAGE_WIDTH),
ilGetInteger(IL_IMAGE_HEIGHT),
ilGetInteger(IL_IMAGE_FORMAT),
GL_UNSIGNED_BYTE,
ilGetData()
));
if(withMimaps)
{
// Enable trilinear filtering
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, mipmapLevels));
GLC(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16));
GLC(glGenerateMipmap(GL_TEXTURE_2D));
}
else
{
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
}
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT));
GLC(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT));
GE::texture t;
t.id = glID;
t.name = name;
t.height = ilGetInteger(IL_IMAGE_HEIGHT);
t.width = ilGetInteger(IL_IMAGE_WIDTH);
t.size = ilGetInteger(IL_IMAGE_WIDTH) * ilGetInteger(IL_IMAGE_HEIGHT) * ilGetInteger(IL_IMAGE_BPP);
t.backrefs = 0;
ilDeleteImages(1, &ilID);
uint ID = getNextID();
this->textures.insert(pair<uint,texture>(ID,t));
return ID;
}
// -----------------------
Object3D::Object3D(GE* ge): ge(ge) {}
SceneObject::SceneObject(GE* ge): Object3D(ge) {}
SceneComposite::SceneComposite(GE* ge): SceneObject(ge) {}
Solid::Solid(GE* ge): SceneObject(ge) {}
Light::Light(GE* ge): SceneObject(ge) {}
// ----------------
void Object3D::rotate(float angle, float x, float y, float z)
{
transform = glm::rotate(transform,angle,glm::vec3(x, y, z));
}
void Object3D::translate(float x, float y, float z)
{
transform = glm::translate(transform,glm::vec3(x, y, z));
}
void Object3D::scale(float x, float y, float z)
{
transform = glm::scale(transform,glm::vec3(x, y, z));
}
void Object3D::identity()
{
transform= glm::mat4(1.0f);
}
// -------------------------
void SceneComposite::add(SceneObject* o)
{
children.push_front(o);
}
void SceneComposite::remove(SceneObject* o)
{
children.remove(o);
}
//--------------------
void SceneComposite::draw(glm::mat4 mat)
{
mat *= transform;
for(auto it = children.begin(); it != children.end(); ++it)
{
(*it)->draw(mat);
}
}
void Solid::draw(glm::mat4 mat)
{
mat *= transform;
ge->drawSolid(this, mat);
}
// -----------------
void SceneComposite::computeLightsPositions(glm::mat4 mat)
{
mat *= transform;
for(auto it = children.begin(); it != children.end(); ++it)
{
(*it)->computeLightsPositions(mat);
}
}
void Light::computeLightsPositions(glm::mat4 mat)
{
mat *= transform;
ge->positionLight(this, mat);
}