-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshadow3d.c
742 lines (477 loc) · 23.1 KB
/
shadow3d.c
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
/*NOMES - NÚMEROS USP*/
/*João Victor Dell Agli Floriano - 10799783*/
/*Karoliny Oliveira - 10368020*/
/*Alex Soares Prestes - 10407962*/
//PROJETO DESENVOLVIDO NO WINDOWS
//COMANDO DE COMPILAÇÃO: gcc scene3d.c ..\GLib.c -lglfw3 -lglew32 -lgdi32 -lopengl32 -lSDL2_image -lSDL2main -lSDL2 -o scene3d
#include <SDL2\SDL_surface.h>
#include <SDL2\SDL.h> //VERSÃO 2.0.14
#include <SDL2\SDL_main.h>
#include <SDL2\SDL_image.h> //VERSÃO 2.0.1
#include <cglm/cam.h>
#include <cglm/mat4.h>
#include <cglm/project.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL\glew.h> //VERSÃO 2.10
#define GLFW_INCLUDE_NONE
#include <GLFW\glfw3.h> //VERSÃO 3.3.4
#include <cglm\cglm.h>
#include <math.h>
#include "..\GLib.h"
/*INICIALIZANDO FUNÇÕES PARA EVENTOS DE TECLADO E TEMAIS VARIÁVEIS*/
GLfloat largura = 1920;
GLfloat altura = 1080;
GLfloat angle = 0.0;
float tx, ty, tz;
float sx, sy, sz;
Vec3 transl;
Vec3 scale;
Vec3 angles;
//Já tinha uma estrutura Vec3 bem antes de instalar o glm, e não tive tempo de mudar o nome
vec3 eye0 = {-80.0, 60.0, 10.0};
vec3 center0 = {0.0, 0.0, 0.0};
vec3 up0 = {0.0, 1.0, 0.0};
vec3 boundaries = {100, 100, 100}; //Fronteiras a não ser passadas
vec3 galpao0 = {2.055846, -0.007984, -18.088926}; // Fronteiras para verificar se a camera está dentro de um objeto
vec3 galpao1 = {17.985043, 10.072246, -8.044674};
float speed = 0.5; //Velocidade de movimento pelo cenário
int P = 0;
GLfloat near = 0.1;
int N = 0;
GLfloat far = 500;
int F = 0;
GLfloat fov = 45.0;
int Fo = 0;
static void eye_check(){
if(eye0[0] > boundaries[0]) eye0[0] = boundaries[0];
else if(eye0[0] < -boundaries[0]) eye0[0] = -boundaries[0];
if(eye0[1] > boundaries[1]) eye0[1] = boundaries[1];
else if(eye0[1] < -boundaries[1]) eye0[1] = -boundaries[1];
if(eye0[2] > boundaries[2]) eye0[2] = boundaries[2];
else if(eye0[2] < -boundaries[2]) eye0[2] = -boundaries[2];
}
static void key_event(GLFWwindow* window, int key, int scancode, int action, int mods){
// printf("Pressionando tecla %d\n", key);
vec3 aux;
if(key == 68 && (action == 1|| action == 2)) {
glm_vec3_cross(center0, up0, aux);
glm_vec3_normalize(aux);
eye0[0] += speed*aux[0];
eye0[1] += speed*aux[1];
eye0[2] += speed*aux[2];
} // d
else if(key == 65 && (action == 1|| action == 2)){
glm_vec3_cross(center0, up0, aux);
glm_vec3_normalize(aux);
eye0[0] -= speed*aux[0];
eye0[1] -= speed*aux[1];
eye0[2] -= speed*aux[2];
} // a
else if(key == 87 && (action == 1|| action == 2)){
// glm_vec3_cross(, float *b, aux)
eye0[0] += speed*center0[0];
eye0[1] += speed*center0[1];
eye0[2] += speed*center0[2];
} // w
else if(key == 83 && (action == 1|| action == 2)){
eye0[0] -= speed*center0[0];
eye0[1] -= speed*center0[1];
eye0[2] -= speed*center0[2];
} // s
if(key == GLFW_KEY_P && action == GLFW_PRESS && P == 0) P = 1;
else if(key == GLFW_KEY_P && action == GLFW_PRESS && P == 1) P = 0;
if(key == GLFW_KEY_Q && action == GLFW_PRESS) F = 1;
else if(key == GLFW_KEY_E && action == GLFW_PRESS) F = -1;
if(key == GLFW_KEY_Q && action == GLFW_RELEASE) F = 0; //esquerdo
else if(key == GLFW_KEY_E && action == GLFW_RELEASE) F = 0; // direito
}
int FirstMouse = 1;
int LastX = 960; //Metade da largura
int LastY = 540; //Metade da altura
float yaw = -90.0;
float pitch = 0.0;
static void mouse_event(GLFWwindow *window, double xpos, double ypos){
if(FirstMouse){
LastX = xpos;
LastY = ypos;
FirstMouse = 0;
}
float xoffset = xpos - LastX;
float yoffset = LastY - ypos;
LastX = xpos;
LastY = ypos;
float sensitivity = 0.3;
xoffset *= sensitivity;
yoffset *= sensitivity;
yaw += xoffset;
pitch += yoffset;
if(pitch > 89.9) pitch = 89.9;
else if(pitch < -89.9) pitch = -89.9;
vec3 front;
front[0] = cos(glm_rad(yaw))*cos(glm_rad(pitch));
front[1] = sin(glm_rad(pitch));
front[2] = sin(glm_rad(yaw))*cos(glm_rad(pitch));
glm_normalize(front);
center0[0] = front[0];
center0[1] = front[1];
center0[2] = front[2];
// glfwSetCursorPos(window, largura/2, altura/2);
}
static void mouse_click(GLFWwindow* window, int button, int action, int mods){
if(button == 0 && action == GLFW_PRESS) N = 1; // esquerdo
else if(button == 1 && action == GLFW_PRESS) N = 2; // direito
if(button == 0 && action == GLFW_RELEASE) N = 0; //esquerdo
else if(button == 1 && action == GLFW_RELEASE) N = 0; // direito
}
static void scroll_callback(GLFWwindow* window, double xoffset, double yoffset){
if(yoffset > 0) Fo = 1;
else if(yoffset < 0) Fo = -1;
yoffset = 0.0l;
}
int WinMain(){ //A função main teve de ser alterada pra WinMain pois conflitava com a main do SDL
/*INICIALIZACAO DO GLFW*/
glfwInit();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_SAMPLES, 4); //HABILITANDO O MULTISAMPLING NO GLFW
GLFWwindow *janela = glfwCreateWindow(largura, altura, "Janela do Lamp", NULL, NULL);
glfwMakeContextCurrent(janela);
/*FIM DA INICIALIZACAO DO GLFW*/
/*INICIALIZACAO DO GLEW*/
GLint init = glewInit();
printf("Status da inicializacao do GLEW: %s\n", glewGetErrorString(init));
/*FIM INICIALIZACAO DO GLEW*/
/*INICIALIZACAO E COMPILACAO DO GLSL*/
GLuint program = GLSLCompile3DLightShadow();
// GLuint program2 = GLSLCompileQuad();
/*FIM DA INICIALIZACAO E COMPILACAO DO GLSL*/
//SETUP DO QUAD PARA DEBUG
// Vec3 quad[4];
// quad[0].x = -1.0; quad[1].y = -1.0; quad[1].z = 0.0;
// quad[1].x = 1.0; quad[1].y = -1.0; quad[1].z = 0.0;
// quad[2].x = -1.0; quad[1].y = 1.0; quad[1].z = 0.0;
// quad[3].x = 1.0; quad[1].y = 1.0; quad[1].z = 0.0;
// Vec2 quadtex[4];
// quad[0].x = -1.0; quad[1].y = -1.0; quad[1].z = 0.0;
// quad[1].x = 1.0; quad[1].y = -1.0; quad[1].z = 0.0;
// quad[2].x = -1.0; quad[1].y = 1.0; quad[1].z = 0.0;
// quad[3].x = 1.0; quad[1].y = 1.0; quad[1].z = 0.0;
// GLuint Bufferquad[2];
// glGenBuffers(1, &Bufferquad[0]);
// GLint loc_a = glGetAttribLocation(program2, "position");
// GLint loc_t = glGetAttribLocation(program2, "tex_coord");
// glBindBuffer(GL_ARRAY_BUFFER, Bufferquad[0]);
// glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_DYNAMIC_DRAW);
// glEnableVertexAttribArray(loc_a);
// glVertexAttribPointer(loc_a, 3, GL_FLOAT, GL_FALSE, sizeof(quad[0]), (void*) 0); //Para 3D, substituir a segunda entrada por "3"
// glBindBuffer(GL_ARRAY_BUFFER, Bufferquad[1]);
// glBufferData(GL_ARRAY_BUFFER, sizeof(quadtex), quadtex, GL_DYNAMIC_DRAW);
// glEnableVertexAttribArray(loc_a);
// glVertexAttribPointer(loc_a, 2, GL_FLOAT, GL_FALSE, sizeof(quadtex[0]), (void*) 0); //Para 3D, substituir a segunda entrada por "3"
//FIM DO SETUP DO QUAD PARA DEBUG
/*INICIALIZACAO DOS VERTICES*/
//Abaixo é a função de coletar os vários objetos em um .obj
Scene *scene = SceneFromFile("quadra/", "quadra.obj");
/*COPIANDO OS VERTICES PARA MANDAR PARA GPU*/
Vec3 vert_vert[scene->total_vert];
for(int j = 0; j < scene->total_vert; j++){
vert_vert[j] = scene->general_array[j];
}
Vec2 text_vert[scene->total_vert];
for(int j = 0; j < scene->total_vert; j++){
text_vert[j] = scene->general_text[j];
}
Vec3 norm_vert[scene->total_vert];
for(int j = 0; j < scene->total_vert; j++){
// printf("%f %f %f\n", scene->general_norm[j].x, scene->general_norm[j].y, scene->general_norm[j].z);
norm_vert[j] = scene->general_norm[j];
// printf("%f %f %f\n", norm_vert[j].x, norm_vert[j].y, norm_vert[j].z);
}
GLfloat angle1 = -2*M_PI/36000;
GLfloat angle3 = 0.0;
transl.x = 0.0;
transl.y = 0.0;
transl.z = 0.0;
angles.x = 0.0;
angles.y = 0.0;
angles.z = 0.0;
scale.x = 1.0;
scale.y = 1.0;
scale.z = 1.0;
//Variáveis a não serem alteradas
Vec3 transl0, angles0, scale0;
transl0.x = 0.0;
transl0.y = 0.0;
transl0.z = 0.0;
angles0.x = 0.0;
angles0.y = 0.0;
angles0.z = 0.0;
scale0.x = 1.0;
scale0.y = 1.0;
scale0.z = 1.0;
vec4 a[4];
vec4 perspec[4];
glm_mat4_identity(a);
glm_mat4_identity(perspec);
GLfloat angle_anim = 0.0;
GLfloat angle_anim2 = 0.0;
glm_lookat(eye0, center0, up0, a);
glm_perspective(glm_rad(fov), largura/altura, near, far, perspec);
glm_mat4_transpose(a);
glm_mat4_transpose(perspec);
for(int i = 0; i < scene->numb_objs; i++){
scene->array_objs[i].model_matrix = IdentityMatrix();
scene->array_objs[i].view_matrix = mat_from_glm(a);
scene->array_objs[i].projection_matrix = mat_from_glm(perspec);
scene->array_objs[i].Reference_Matrix = CoordMatrix(scene->array_objs[i].center_of_mass);
//Inicializando as texturas de cada objeto
scene->array_objs[i].texture_info.format = GL_RGBA;
}
/*ORDEM DOS OBJETOS
1 - banco_Cube.001
2 - cesta1_Cylinder
3 - cesta2_Cylinder.002
4 - galpao_Cube.002
5 - telhado_Cube.005
6 - chão_Plane
7 - bolafora_Sphere.002
8 - bola2_Sphere.003
9 - bola3_Sphere.004
10 - estante1_Cube
11 - estante2_Cube.004
12 - bola5_Sphere.006
13 - bola4_Sphere.005
14 - caixa_cima1_Cube.006
15 - caixa_baixo_Cube.008
16 - caixa_cima2_Cube.009
17 - skybox_Cube.007
18 - caixa_energia_Cube.010
19 - cabos_Cylinder.001
20 - grades_Cube.011
*/
/*INICIALIZAÇÃO DAS TEXTURAS*/
/*ATENÇÃO, IMPORTANTE*/
//A versão do SDL_image funcionando era a 2.0.1, anteriores a esta estavam bugando.
//Este bug inclusive já foi identificado por outros pela internet, e submetido aos desenvolvedores.
// glActiveTexture(GL_TEXTURE0);
TextureFromFile("quadra/textures/banco.png", &scene->array_objs[0]);
TextureFromFile("quadra/textures/cesta1.png", &scene->array_objs[1]);
TextureFromFile("quadra/textures/cesta2.png", &scene->array_objs[2]);
TextureFromFile("quadra/textures/text1.png", &scene->array_objs[3]);
TextureFromFile("quadra/textures/telhado.png", &scene->array_objs[4]);
TextureFromFile("quadra/textures/chão.png", &scene->array_objs[5]);
TextureFromFile("quadra/textures/bola1.png", &scene->array_objs[6]); //bola central
TextureFromFile("quadra/textures/bola1.png", &scene->array_objs[7]);
TextureFromFile("quadra/textures/bola1.png", &scene->array_objs[8]);
TextureFromFile("quadra/textures/estante.png", &scene->array_objs[9]);
TextureFromFile("quadra/textures/estante.png", &scene->array_objs[10]);
TextureFromFile("quadra/textures/bola1.png", &scene->array_objs[11]);
TextureFromFile("quadra/textures/bola1.png", &scene->array_objs[12]);
TextureFromFile("quadra/textures/caixa_cima2.png", &scene->array_objs[13]);
TextureFromFile("quadra/textures/caixa_baixo.png", &scene->array_objs[14]);
TextureFromFile("quadra/textures/caixa_cima2.png", &scene->array_objs[15]);
scene->array_objs[16].texture_info.format = GL_RGB;
TextureFromFile("quadra/textures/skybox2.png", &scene->array_objs[16]);
RelativeRotateXaxis(M_PI/2, scene->array_objs[16].model_matrix, scene->array_objs[16].center_of_mass);//Rotação para ajustar a skybox
TextureFromFile("quadra/textures/caixa_energia.png", &scene->array_objs[17]);
TextureFromFile("quadra/textures/cabos.png", &scene->array_objs[18]);
TextureFromFile("quadra/textures/grades.png", &scene->array_objs[19]); //A grade deve vir por ultimo para que a transparência dela valha
scene->array_objs[3].texture_info.texture_inside_id = LoneTextureFromFile("quadra/textures/text_in.png", GL_RGBA);
scene->array_objs[3].texture_info.has_inside = 1;
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[0]);
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[1]);
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[2]);
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[3]);
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[4]);
DefineObj3DIlum(0.9, 1.0, 3.0, 40.0, &scene->array_objs[5]);
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[6]);
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[7]);
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[8]);
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[9]);
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[10]);
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[11]);
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[12]);
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[13]);
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[14]);
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[15]);
DefineObj3DIlum(1.0, 0.0, 0.0, 1.0, &scene->array_objs[16]);
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[17]);
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[18]);
DefineObj3DIlum(0.9, 1.0, 5, 50, &scene->array_objs[19]);
for(int i = 0; i < scene->numb_objs; i++){
scene->array_objs[i].in_material.Ka = 1.0;
scene->array_objs[i].in_material.Kd = 0.0;
scene->array_objs[i].in_material.Ks = 0.0;
scene->array_objs[i].in_material.ns = 1.0;
}
Vec3 lightPos;
lightPos.x = -80.0;
lightPos.y = 60.0;
lightPos.z = 0.0;
Color lightColor = DefineColor(217.0, 93.0, 13.0, 1.0);
Color ambientColor = DefineColor(255.0, 255.0, 255.0, 1.0);
/*FIM DA INICIALIZAÇÃO DAS TEXTURAS*/
/*FIM DA INICIALIZACAO DOS VERTICES*/
/*SETUP DOS BUFFERS*/
GLuint Buffers[3];
glGenBuffers(3, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vert_vert), vert_vert, GL_DYNAMIC_DRAW);
GLint loc = glGetAttribLocation(program, "position");
GLint color = glGetUniformLocation(program, "color");
GLint model = glGetUniformLocation(program, "model");
GLint view = glGetUniformLocation(program, "view");
GLint proj = glGetUniformLocation(program, "projection");
glEnableVertexAttribArray(loc);
glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, sizeof(vert_vert[0]), (void*) 0); //Para 3D, substituir a segunda entrada por "3"
glBindBuffer(GL_ARRAY_BUFFER, Buffers[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(text_vert), text_vert, GL_STATIC_DRAW);
GLint loc_texture = glGetAttribLocation(program, "texture_coord");
glEnableVertexAttribArray(loc_texture);
glVertexAttribPointer(loc_texture, 2, GL_FLOAT, GL_FALSE, sizeof(text_vert[0]), (void*) 0);
//Enviando vertices das normais
glBindBuffer(GL_ARRAY_BUFFER, Buffers[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(norm_vert), norm_vert, GL_STATIC_DRAW);
GLint loc_norm = glGetAttribLocation(program, "normals");
GLint loc_lightPos = glGetUniformLocation(program, "lightPos");
GLint loc_lightColor = glGetUniformLocation(program, "lightColor");
GLint loc_ambientColor = glGetUniformLocation(program, "ambientColor");
GLint loc_viewPos = glGetUniformLocation(program, "viewPos");
GLint Ka = glGetUniformLocation(program, "Ka");
GLint Kd = glGetUniformLocation(program, "Kd");
GLint Ks = glGetUniformLocation(program, "Ks");
GLint ns = glGetUniformLocation(program, "ns");
glUniform3f(loc_lightPos, lightPos.x, lightPos.y, lightPos.z);
glUniform3f(loc_lightColor, lightColor.r, lightColor.g, lightColor.b);
glUniform3f(loc_ambientColor, ambientColor.r, ambientColor.g, ambientColor.b);
glEnableVertexAttribArray(loc_norm);
glVertexAttribPointer(loc_norm, 3, GL_FLOAT, GL_FALSE, sizeof(norm_vert[0]), (void*) 0);
//TENTATIVA DE DEPTH BUFFER
const GLuint SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
// glActiveTexture(GL_TEXTURE1);
GLuint DepthMap;
glGenTextures(1, &DepthMap);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, DepthMap);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
for(int i = 0; i < scene->numb_objs; i++){
scene->array_objs[i].depth_texture_id = DepthMap;
}
// mat4 lightProjection, lightView, lightSpaceMatrix;
vec3 lightPosv = {lightPos.x, lightPos.y, lightPos.z};
vec3 lightCenter = {0.0, 0.0, 0.0};
vec3 lightUp = {0.0, 1.0, 0.0};
float nearlight = 0.1;
float farlight = 400;
GLint loc_lightSpaceMatrix = glGetUniformLocation(program, "lightSpaceMatrix");
mat4 lightProjection, lightView, lightSpaceMatrix;
// glm_perspective(glm_rad(45.0), 1.0, nearlight, farlight, lightProjection);
glm_ortho(-7, 7, -7, 7, nearlight, farlight, lightProjection);
glm_lookat(lightPosv, lightCenter, lightUp, lightView);
glm_mat4_mul(lightProjection, lightView, lightSpaceMatrix);
glm_mat4_transpose(lightSpaceMatrix);
GLfloat *lsm = mat_from_glm(lightSpaceMatrix);
glUniformMatrix4fv(loc_lightSpaceMatrix, 1, GL_TRUE, lsm);
//MAPEAMENTO DAS DUAS VARIÁVEIS DE TEXTURA NO PROGRAMA
//Essa parte é importante pois diferencia dois "canais" de textura, os permitindo serem usados em um mesmo shader
GLint loc_tex = glGetUniformLocation(program, "samplerTexture");
glUniform1i(loc_tex, 0); //Atribuindo valor "0" à textura "samplerTexture"
loc_tex = glGetUniformLocation(program, "shadowMap");
glUniform1i(loc_tex, 1); //Atribuindo valor "1" à textura "shadowMap"
GLuint DepthMapFBO;
glGenFramebuffers(1, &DepthMapFBO);
glClear(GL_DEPTH_BUFFER_BIT);
glBindFramebuffer(GL_FRAMEBUFFER, DepthMapFBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, DepthMap, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
//FIM DA TENTATIVA DE DEPTH BUFFER
/*FIM DO SETUP DOS BUFFERS*/
/* REGISTRANDO EVENTOS DE ENTRADA*/
glfwSetKeyCallback(janela, key_event);
glfwSetMouseButtonCallback(janela, mouse_click);
glfwSetCursorPosCallback(janela, mouse_event);
glfwSetScrollCallback(janela, scroll_callback);
glfwSetCursorPos(janela, largura/2, altura/2);
double mousex, mousey;
glfwGetCursorPos(janela, &mousex, &mousey);
/*FIM DOS REGISTROS DE EVENTOS DE ENTRADA*/
glfwShowWindow(janela);
glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE); //HABILITANDO O MULTISAMPLING NO OPENGL
/*LACO PRINCIPAL*/
// glfwSetCursorPos(janela, largura/2, altura/2);
while(!glfwWindowShouldClose(janela)){
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0);
/*PARAMETROS PROJECTION E RENDERIZAÇÃO*/
if(P == 1)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else if(P == 0)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
if(F == 1) far += 1.0;
else if(F == -1) far -= 1.0;
if(N == 1) near += 0.1;
else if(N == 2) near -= 0.1;
if(Fo == -1){fov += 1.0; Fo = 0;}
else if(Fo == 1){fov -= 1.0; Fo = 0;}
if(fov > 90.0) fov = 90.0;
else if (fov < 0.0) fov = 0.0;
if(far <= near) far = near;
else if(far >= 500.0) far = 500.0;
if(near <= 0.01) near = 0.01;
else if(near >= far) near = far;
/*FIM DOS PARAMETROS PROJECTION E RENDERIZAÇÃO*/
/*ANIMAÇÃO*/
// IlumObj3D(scene->array_objs[6], Ka, Kd, Ks, ns, eye0, loc_viewPos);
angle_anim += M_PI/3600000;
angle_anim2 += M_PI/270;
Translate(0, 0.005*cos(angle_anim2), 0, scene->array_objs[6].model_matrix);
//Rotação ta bugando a renderização de luz, necessário ajustar.
// RelativeRotateYaxis(angle_anim, scene->array_objs[6].model_matrix, scene->array_objs[6].center_of_mass);
// RotateYaxis(angle_anim, scene->array_objs[6].model_matrix);
// IlumObj3D(scene->array_objs[6], Ka, Kd, Ks, ns);
/*FIM DA ANIMAÇÃO*/
center0[0] += transl.x; center0[1] += transl.y; center0[2] += transl.z;
eye_check();
//Renderizando a cena para o depthmap
SendLightSpaceMatrix(lightPosv, lightCenter, up0, nearlight, farlight, loc_lightSpaceMatrix);
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
glBindFramebuffer(GL_FRAMEBUFFER, DepthMapFBO);
glClear(GL_DEPTH_BUFFER_BIT);
for(int i = 0; i < scene->numb_objs; i++){
LightViewUpdate(lightPosv, center0, up0, nearlight, farlight, fov, SHADOW_WIDTH, SHADOW_HEIGHT, &scene->array_objs[i]); //Passando a camera da luz como view e projection
UpdateObj3D(&scene->array_objs[i], model, view, proj, transl0, angles, scale, GLOBAL);
glActiveTexture(GL_TEXTURE0);
RenderObj3D(scene->array_objs[i], color, Inside_Check(galpao0, galpao1, eye0));
// glActiveTexture(GL_TEXTURE1);
// glBindTexture(GL_TEXTURE_2D, DepthMap);
}
//Renderizando a cena
glViewport(0, 0, largura, altura);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0);
for(int i = 0; i < scene->numb_objs; i++){
ViewUpdate(eye0, center0, up0, near, far, fov, largura, altura, &scene->array_objs[i]);
UpdateObj3D(&scene->array_objs[i], model, view, proj, transl0, angles, scale, GLOBAL);
IlumObj3D(scene->array_objs[i], Ka, Kd, Ks, ns, eye0, loc_viewPos, Inside_Check(galpao0, galpao1, eye0));
glActiveTexture(GL_TEXTURE0);
RenderObj3D(scene->array_objs[i], color, Inside_Check(galpao0, galpao1, eye0));
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, DepthMap);
}
glfwSwapBuffers(janela);
}
/*FIM LACO PRINCIPAL*/
glfwDestroyWindow(janela);
glfwTerminate();
glEnd();
return 0;
}