This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.cpp
684 lines (600 loc) · 29.7 KB
/
Source.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
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
//#include <assimp/Importer.hpp>
//#include <assimp/scene.h>
//#include <assimp/postprocess.h>
#include <iostream>
#include <vector>
#include "shader.h"
//#include "model.h"
#include "stb_image.h"
#include "chanCamera.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow* window);
unsigned int loadTexture(const char* path);
unsigned int loadCubemap(std::vector<std::string> faces);
void glfwSetup();
void glfwConfigureWindow(GLFWwindow* window);
bool gladSetup();
bool CheckCollision(chanCamera cam, glm::vec3 cubePos);
void renderQuad();
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
// camera
//Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));//FlyCamera
chanCamera camera(glm::vec3(0.0f, 0.0f, 3.0f));//chanCamera
//Camera camera(1.0f, SCR_WIDTH, SCR_HEIGHT, 0.1f, 100.0f, glm::vec3(0.0f, 0.0f, 3.0f));//FreeCamera
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
//DeltaTime
float deltaTime = 0.0f; // Time between current frame and last frame
float lastFrame = 0.0f; // Time of last frame
// meshes
unsigned int planeVAO;
//light source
glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
glm::vec3 texturedPos(-1.2f, 1.0f, 2.0f);
int main(){
glfwSetup();
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if(window == NULL){
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwConfigureWindow(window);
if(!gladSetup()) return -1;
stbi_set_flip_vertically_on_load(true);//flip loaded texture's on the y-axis (before loading model).
// build and compile our shader program
//Shader ourShader("nReflectiveShader.vs", "nReflectiveShader.fs");
Shader reflectShader("reflectiveShader.vs", "reflectiveShader.fs");
//Shader flatShader("reflective2.vs", "reflective2.fs");
Shader lightCubeShader("lightCube.vs", "lightCube.fs");
Shader skyboxShader("skybox.vs", "skybox.fs");
Shader simpleDepthShader("shadow_mapping_depth.vs", "shadow_mapping_depth.fs");
Shader debugDepthQuad("debug_quad.vs", "debug_quad_depth.fs");
glEnable(GL_DEPTH_TEST);
//Model ourModel("textures/models/backpack/backpack.obj"); //TODO: THIS PART CAUSES A CRASH - WHY? ASK LA! MODEL LOADING IS EXTREMELY USEFUL AND SHOULD BE IMPLEMENTED!
// set up vertex data (and buffer(s)) and configure vertex attributes
float planeVertices[] = {
// positions // normals // texcoords
25.0f, -0.5f, 25.0f, 0.0f, 1.0f, 0.0f, 25.0f, 0.0f,
-25.0f, -0.5f, 25.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-25.0f, -0.5f, -25.0f, 0.0f, 1.0f, 0.0f, 0.0f, 25.0f,
25.0f, -0.5f, 25.0f, 0.0f, 1.0f, 0.0f, 25.0f, 0.0f,
-25.0f,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1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f
};
glm::vec3 cubePositions[] = {// world space positions of our cubes
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3(2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3(1.3f, -2.0f, -2.5f),
glm::vec3(1.5f, 2.0f, -2.5f),
glm::vec3(1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};
unsigned int cubeVBO, VAO;
glGenVertexArrays(1, &VAO);//vertex array object
glGenBuffers(1, &cubeVBO);//vertex buffer object
//glGenBuffers(1, &EBO);//element buffer object - allows vertex overlap (without it you have to define overlapping vertices seperately)
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);//copy user defined data to bound buffer
glBindVertexArray(VAO);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 11 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);// position attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 11 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);// color attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 11 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);// texture attribute
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 11 * sizeof(float), (void*)(8 * sizeof(float)));
glEnableVertexAttribArray(3);// normal attribute
// skybox VAO
unsigned int skyboxVAO, skyboxVBO;
glGenVertexArrays(1, &skyboxVAO);
glGenBuffers(1, &skyboxVBO);
glBindVertexArray(skyboxVAO);
glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);//textures
std::vector<std::string> faces{
"textures/skybox/right.jpg",
"textures/skybox/left.jpg",
"textures/skybox/top.jpg",
"textures/skybox/bottom.jpg",
"textures/skybox/front.jpg",
"textures/skybox/back.jpg"
};
unsigned int cubemapTexture = loadCubemap(faces);
skyboxShader.use();
skyboxShader.setInt("skybox", 0);
// configure light's VAO (VBO stays same, vertices are the same for the light object)
unsigned int lightVAO;
glGenVertexArrays(1, &lightVAO);
glBindVertexArray(lightVAO);
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
//belongs to the white cube (lamp)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 11 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
unsigned int texturedVAO;
glGenVertexArrays(1, &texturedVAO);
glBindVertexArray(texturedVAO);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 11 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 11 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// texture attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 11 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
// normal attribute
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 11 * sizeof(float), (void*)(8 * sizeof(float)));
glEnableVertexAttribArray(3);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// remember: do NOT unbind the EBO while a VAO is active as the bound element buffer object IS stored in the VAO; keep the EBO bound.
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);// uncomment this to draw in wireframe polygons
//load textures
unsigned int texture1 = loadTexture("textures/container.jpg");
unsigned int texture2 = loadTexture("textures/awesomeface.png");
unsigned int texture3 = loadTexture("textures/matrix.jpg");
// configure depth map FBO (for shadow mapping)
const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
unsigned int depthMapFBO;
glGenFramebuffers(1, &depthMapFBO);
// create depth texture
unsigned int depthMap;
glGenTextures(1, &depthMap);
glBindTexture(GL_TEXTURE_2D, depthMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// attach depth texture as FBO's depth buffer
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);
// shader configuration
// ------------------------------------------------------------
//ourShader.use(); // don't forget to activate the shader before setting uniforms!
////glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 0); // set it manually
//ourShader.setInt("texture1", 0);// or with shader class
//ourShader.setInt("texture2", 1);
reflectShader.use();
reflectShader.setInt("material.diffuse", 0);
reflectShader.setInt("material.specular", 1);
reflectShader.setInt("material.emission", 2);
reflectShader.setInt("shadowMap", 3);
//render the triangle - can be outside as long as it doesn't exceed texture limit
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);// bind diffuse map
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);// bind specular map
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, texture3);// bind emission map
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, depthMap);
debugDepthQuad.use();
debugDepthQuad.setInt("depthMap", 0);
// world, camera/view, screen projection transformations
glm::mat4 model, view, projection, transform;
glm::vec3 lightColor, diffuseColor, ambientColor;
float currentFrame;
glfwSetCursorPos(window, lastX, lastY);//avoids cursor jump at program start
// render loop
while(!glfwWindowShouldClose(window)){
processInput(window);// input
for (unsigned int i = 0; i < 10; i++) {
if (CheckCollision(camera, cubePositions[i])) {//Do collision logic here, since the only objects in our scene is these cubes
camera.RevertPos(true);
}
}
//per-frame time calculation
currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
//glClearColor(0.2f, 0.3f, 0.3f, 1.0f);// background color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//clear color and depth buffer
// 1. render depth of scene to texture (from light's perspective)
// --------------------------------------------------------------
glm::mat4 lightProjection, lightView;
glm::mat4 lightSpaceMatrix;
float near_plane = 1.0f, far_plane = 7.5f;
lightProjection = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, near_plane, far_plane);
lightView = glm::lookAt(lightPos, glm::vec3(0.0f), glm::vec3(0.0, 1.0, 0.0));
lightSpaceMatrix = lightProjection * lightView;
// render scene from light's point of view
simpleDepthShader.use();
simpleDepthShader.setMat4("lightSpaceMatrix", lightSpaceMatrix);
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
glClear(GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glBindVertexArray(VAO);//render scene (since we only have cubes, render those)
for (unsigned int i = 0; i < 10; i++) {// calculate model matrix for each object, pass it to shader
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, cubePositions[i]);
float angle = 20.0f * i;
if (i % 3 == 0) //every 3rd cube, we set the angle using time function
angle = glfwGetTime() * 25.0f;
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
simpleDepthShader.setMat4("model", model);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// reset viewport
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 2. render scene as normal using the generated depth/shadow map
// --------------------------------------------------------------
//lightColor.x = sin(glfwGetTime() * 2.0f);
//lightColor.y = sin(glfwGetTime() * 0.7f);
//lightColor.z = sin(glfwGetTime() * 1.3f);
diffuseColor = lightColor * glm::vec3(0.5f);
ambientColor = diffuseColor * glm::vec3(0.2f);
reflectShader.use();
// projection transformation
projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 500.0f);
reflectShader.setMat4("projection", projection);
// camera/view transformation
view = camera.GetViewMatrix();
reflectShader.setMat4("view", view);
reflectShader.setVec3("viewPos", camera.Position);
reflectShader.setFloat("material.shininess", 32.0f);
// directional light
reflectShader.setVec3("dirLight.direction", -0.2f, -1.0f, -0.3f);
reflectShader.setVec3("dirLight.ambient", 0.05f, 0.05f, 0.05f);
reflectShader.setVec3("dirLight.diffuse", 0.4f, 0.4f, 0.4f);
reflectShader.setVec3("dirLight.specular", 0.5f, 0.5f, 0.5f);
// point light 1
reflectShader.setVec3("pointLights[0].position", lightPos);
reflectShader.setMat4("lightSpaceMatrix", lightSpaceMatrix);
reflectShader.setVec3("pointLights[0].ambient", 0.05f, 0.05f, 0.05f);
reflectShader.setVec3("pointLights[0].diffuse", 0.8f, 0.8f, 0.8f);
reflectShader.setVec3("pointLights[0].specular", 1.0f, 1.0f, 1.0f);
reflectShader.setFloat("pointLights[0].constant", 1.0f);
reflectShader.setFloat("pointLights[0].linear", 0.09f);
reflectShader.setFloat("pointLights[0].quadratic", 0.032f);
// spotLight
reflectShader.setVec3("spotLight.position", camera.Position);
reflectShader.setVec3("spotLight.direction", camera.Front);
reflectShader.setVec3("spotLight.ambient", 0.0f, 0.0f, 0.0f);
reflectShader.setVec3("spotLight.diffuse", 1.0f, 1.0f, 1.0f);
reflectShader.setVec3("spotLight.specular", 1.0f, 1.0f, 1.0f);
reflectShader.setFloat("spotLight.constant", 1.0f);
reflectShader.setFloat("spotLight.linear", 0.09f);
reflectShader.setFloat("spotLight.quadratic", 0.032f);
reflectShader.setFloat("spotLight.cutOff", glm::cos(glm::radians(12.5f)));
reflectShader.setFloat("spotLight.outerCutOff", glm::cos(glm::radians(15.0f)));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);// bind diffuse map
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);// bind specular map
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, texture3);// bind emission map
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, depthMap);
// create transformations - make sure to initialize matrix to identity matrix first
transform = glm::translate(glm::mat4(1.0f), glm::vec3(0.5f, -0.5f, 0.0f));
transform = glm::rotate(transform, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
glBindVertexArray(VAO);//render cubes
for (unsigned int i = 0; i < 10; i++){// calculate model matrix for each object, pass it to shader
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, cubePositions[i]);
float angle = 20.0f * i;
if (i % 3 == 0) //every 3rd cube, we set the angle using time function
angle = glfwGetTime() * 25.0f;
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
reflectShader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
//glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);//when doing a cube, only rendered a triangle
// also draw the lamp object
lightCubeShader.use();
lightCubeShader.setMat4("projection", projection);
lightCubeShader.setMat4("view", view);
model = glm::mat4(1.0f);
model = glm::translate(model, lightPos);
model = glm::scale(model, glm::vec3(0.2f)); // a smaller cube
lightCubeShader.setMat4("model", model);
glBindVertexArray(lightVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
////textured cube ------ not used in final product, just demonstrates the object only having textures
//ourShader.use();//activate shader
//ourShader.setMat4("projection", projection);
//ourShader.setMat4("view", view);
//model = glm::mat4(1.0f);
//model = glm::translate(model, texturedPos);
//model = glm::scale(model, glm::vec3(0.2f)); // a smaller cube
//ourShader.setMat4("model", model);
//glBindVertexArray(texturedVAO);
//glDrawArrays(GL_TRIANGLES, 0, 36);
// draw skybox as last
glDepthFunc(GL_LEQUAL); // change depth function so depth test passes when values are equal to depth buffer's content
skyboxShader.use();
view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // remove translation from the view matrix
skyboxShader.setMat4("view", view);
skyboxShader.setMat4("projection", projection);
// skybox cube
glBindVertexArray(skyboxVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glDepthFunc(GL_LESS); // set depth function back to default
// render Depth map to quad for visual debugging
// ---------------------------------------------
debugDepthQuad.use();
debugDepthQuad.setFloat("near_plane", near_plane);
debugDepthQuad.setFloat("far_plane", far_plane);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, depthMap);
//renderQuad();
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
glfwSwapBuffers(window);
glfwPollEvents();
}
//de-allocate resources:
glDeleteVertexArrays(1, &VAO);
glDeleteVertexArrays(1, &lightVAO);
glDeleteVertexArrays(1, &skyboxVAO);
glDeleteBuffers(1, &cubeVBO);
glDeleteBuffers(1, &skyboxVBO);
//glDeleteBuffers(1, &EBO);
// glfw: terminate, clearing all previously allocated GLFW resources.
glfwTerminate();
return 0;
}
// renderQuad() renders a 1x1 XY quad in NDC
// -----------------------------------------
unsigned int quadVAO = 0;
unsigned int quadVBO;
void renderQuad()
{
if (quadVAO == 0)
{
float quadVertices[] = {
// positions // texture Coords
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
};
// setup plane VAO
//glEnable(GL_CULL_FACE);
glGenVertexArrays(1, &quadVAO);
std::cout << "QUAD_VAO is as following:\n " << quadVAO;
glGenBuffers(1, &quadVBO);
glBindVertexArray(quadVAO);
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
}
glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
void processInput(GLFWwindow* window){
float cameraSpeed = static_cast<float>(3.5 * deltaTime);//changes dynamically according to fps
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(Camera_Movement::FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(Camera_Movement::BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(Camera_Movement::LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(Camera_Movement::RIGHT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height){//whenever the window size changed (by OS or user resize)
// make sure viewport matches new window dimensions; note that width-height
// will be significantly larger than specified on retina displays
glViewport(0, 0, width, height);
}
void mouse_callback(GLFWwindow* window, double xposIn, double yposIn){//whenever the mouse moves
float xpos = static_cast<float>(xposIn);
float ypos = static_cast<float>(yposIn);
if(firstMouse){
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;// reversed since y-coordinates go from bottom to top
lastX = xpos;
lastY = ypos;
camera.ProcessMouseMovement(xoffset, yoffset);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset){//whenever the mouse scroll wheel scrolls
camera.ProcessMouseScroll(static_cast<float>(yoffset));
}
unsigned int loadTexture(char const* path){
unsigned int textureID;
int width, height, nrComponents;
unsigned char* data = stbi_load(path, &width, &height, &nrComponents, 0);
glGenTextures(1, &textureID);
if(data){
GLenum format;
if (nrComponents == 1) format = GL_RED;
else if (nrComponents == 3) format = GL_RGB;
else format = GL_RGBA;//else if (nrComponents == 4) format = GL_RGBA;
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
stbi_image_free(data);
}
else{
std::cout << "Texture failed to load. Texture path: " << path << std::endl;
stbi_image_free(data);
}
return textureID;
}
unsigned int loadCubemap(std::vector<std::string> faces){
unsigned int textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
int width, height, nrChannels;
for(unsigned int i = 0; i < faces.size(); i++){
unsigned char* data = stbi_load(faces[i].c_str(), &width, &height, &nrChannels, 0);
if(data){
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
else{
std::cout << "Cubemap texture failed to load at path: " << faces[i] << std::endl;
stbi_image_free(data);
}
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
return textureID;
}
void glfwSetup(){//initialize glfw and configure
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}
void glfwConfigureWindow(GLFWwindow* window){// glfw window creation
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);// tell GLFW to capture our mouse
}
bool gladSetup(){// glad: load OpenGL function pointers
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
std::cout << "Failed to initialize GLAD" << std::endl;
return false;
}
glEnable(GL_DEPTH_TEST);//enable depth buffer
//glEnable(GL_CULL_FACE);
return true;
}
bool CheckCollision(chanCamera cam, glm::vec3 cubePos){ // AABB - AABB collision
// collision x-axis?
bool collisionX = cam.Position.x + 0.5 >= cubePos.x &&
cubePos.x + 1 >= cam.Position.x;
// collision y-axis?
bool collisionY = cam.Position.y + 0.5 >= cubePos.y &&
cubePos.y + 1 >= cam.Position.y;
// collision z-axis?
bool collisionZ = cam.Position.z + 0.5 >= cubePos.z &&
cubePos.z + 1 >= cam.Position.z;
// collision only if on all axes
return collisionX && collisionY && collisionZ;
}