forked from RupakO4/graphics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaincpp.cpp
More file actions
735 lines (587 loc) · 24.5 KB
/
maincpp.cpp
File metadata and controls
735 lines (587 loc) · 24.5 KB
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
#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 "glm/gtx/string_cast.hpp"
#include "Shader.h"
#include "Camera.h"
#include "Model.h"
#include <iostream>
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);
//cubeMap
unsigned int loadCubemap(vector<std::string> faces);
// settings
const unsigned int SCR_WIDTH = 3840;
const unsigned int SCR_HEIGHT = 2400;
// camera
Camera camera(glm::vec3(-30.0f, 0.0f, 30.0f));
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
// timing
float deltaTime = 0.0f;
float lastFrame = 0.0f;
// positions all containers
//glm::vec3 cubePositions[] = {
// 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),
//
//};
//position of dir light
glm::vec3 dirlightpos(-40.0f, 40.0f, 40.0f);
// positions of the point lights
glm::vec3 pointLightPositions[] = {
glm::vec3(3.6f, -2.0f, 31.6f),
glm::vec3(-7.3f, -2.0f, 28.5f),
glm::vec3(-25.7f, -2.0f, 10.2f),
glm::vec3(-28.1f, -2.0f, -0.6f),
glm::vec3(3.8f, -2.0f, -33.5f),
glm::vec3(36.2f,-2.0f, -1.0f),
glm::vec3(3.6f, 23.0f, -1.05f) //top[6]
};
//perspective
glm::mat4 myperspective(float fov, float aspectRatio, float zNear, float zFar)
{
glm::mat4 projection;
float scale = 1.0 / tan(fov / 2);
projection[0][0] = scale / aspectRatio;
projection[1][1] = scale; // scale the y coordinates
projection[2][3] = -1;
//projection[2][2] = -(zFar + zNear) / (zFar - zNear); // remap z to [0,1]
//projection[3][2] = (2*zFar*zNear) / (zNear - zFar);
if (GLM_DEPTH_CLIP_SPACE == GLM_DEPTH_ZERO_TO_ONE) {
projection[2][2] = zFar / (zNear - zFar);
projection[3][2] = -(zFar * zNear) / (zFar - zNear);
}
else {
projection[2][2] = -(zFar + zNear) / (zFar - zNear);
projection[3][2] = -(2 * zFar * zNear) / (zFar - zNear);
}
return projection;
}
//scale
glm::mat4 mat_scale(glm::mat4 mat1, glm::vec3 vec1)
{
glm::mat4 scaledMatrix;
scaledMatrix[0] = mat1[0] * vec1[0];
scaledMatrix[1] = mat1[1] * vec1[1];
scaledMatrix[2] = mat1[2] * vec1[2];
scaledMatrix[3] = mat1[3];
//std::cout << "Scaled Matrix: \n";
//std::cout << glm::to_string(scaledMatrix);
return scaledMatrix;
}
//rotate
glm::mat4 mat_rotate(glm::mat4 mat1, float angle, glm::vec3 vec1)
{
const float theta = angle; //glm::radians(angle) if input is in degree
const float cos_theta = cos(angle);
const float sin_theta = sin(theta);
glm::vec3 axis = normalize(vec1); // vec1/sqrt(vec1[0]*vec1[0] + vec1[1]*vec1[1] + vec1[2]*vec1[2])
glm::vec3 temp = (1 - cos_theta) * axis;
glm::mat4 Rotate; //Generating composite transformation matrix
Rotate[0][0] = cos_theta + temp[0] * axis[0];
Rotate[0][1] = temp[0] * axis[1] + sin_theta * axis[2];
Rotate[0][2] = temp[0] * axis[2] - sin_theta * axis[1];
Rotate[1][0] = temp[1] * axis[0] - sin_theta * axis[2];
Rotate[1][1] = cos_theta + temp[1] * axis[1];
Rotate[1][2] = temp[1] * axis[2] + sin_theta * axis[0];
Rotate[2][0] = temp[2] * axis[0] + sin_theta * axis[1];
Rotate[2][1] = temp[2] * axis[1] - sin_theta * axis[0];
Rotate[2][2] = cos_theta + temp[2] * axis[2];
glm::mat4 Result_Mat; //obtain rotated matrix by multiplying with CTM
Result_Mat[0] = mat1[0] * Rotate[0][0] + mat1[1] * Rotate[0][1] + mat1[2] * Rotate[0][2];
Result_Mat[1] = mat1[0] * Rotate[1][0] + mat1[1] * Rotate[1][1] + mat1[2] * Rotate[1][2];
Result_Mat[2] = mat1[0] * Rotate[2][0] + mat1[1] * Rotate[2][1] + mat1[2] * Rotate[2][2];
Result_Mat[3] = mat1[3];
/* std::cout << "Rotated Matrix: \n";
std::cout << glm::to_string(Result_Mat);*/
return Result_Mat;
}
//translate
glm::mat4 mat_Translate(glm::mat4 mat1, glm::vec3 vec1)
{
glm::mat4 Result_Mat; // cant do mat[0] + vec[0], mat[1] + vec[1], mat[2] + vec[2]
Result_Mat[0] = mat1[0];
Result_Mat[1] = mat1[1];
Result_Mat[2] = mat1[2];
Result_Mat[3] = mat1[0] * vec1[0] + mat1[1] * vec1[1] + mat1[2] * vec1[2] + mat1[3];
//std::cout << "Translated Matrix: \n";
//std::cout << glm::to_string(Result_Mat);
return Result_Mat;
}
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Boudhanath Stupa", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
// tell GLFW to capture our mouse
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// tell stb_image.h to flip loaded texture's on the y-axis (before loading model).
stbi_set_flip_vertically_on_load(true);
// configure global opengl state
// -----------------------------
glEnable(GL_DEPTH_TEST);
// build and compile shaders
// -------------------------
//Shader ourShader("modelVS.txt", "modelFS.txt");
Shader skyboxShader("skyboxVS.txt", "skyboxFS.txt");
//cubeMap
float skyboxVertices[] = {
// positions
-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, -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, 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,
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, 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
};
// 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);
vector<std::string> faces
{
"skybox/right.jpg",
"skybox/left.jpg",
"skybox/top.jpg",
"skybox/bottom.jpg",
"skybox/front.jpg",
"skybox/back.jpg"
};
//vector<std::string> faces
//{
// "skybox/negx.jpg",
// "skybox/posx.jpg",
// "skybox/posy.jpg",
// "skybox/negy.jpg",
// "skybox/negz.jpg",
// "skybox/posz.jpg"
//};
unsigned int cubemapTexture = loadCubemap(faces);
skyboxShader.use();
skyboxShader.setInt("skybox", 0);
//ground plane
const float groundPlaneVertices[] = {
// positions //texture coordinates
-1, 0, -1, 0, 1,
1, 0, -1, 1, 1,
1, 0, 1, 1, 0,
-1, 0, 1, 0, 0
};
const unsigned int groundPlaneIndices[] = {
0, 1, 2,
2, 3, 0
};
unsigned int groundPlaneVBO, groundPlaneVAO, groundPlaneEBO;
glGenVertexArrays(1, &groundPlaneVAO);
glGenBuffers(1, &groundPlaneVBO);
glGenBuffers(1, &groundPlaneEBO);
glBindVertexArray(groundPlaneVAO);
glBindBuffer(GL_ARRAY_BUFFER, groundPlaneVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(groundPlaneVertices), groundPlaneVertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, groundPlaneEBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(groundPlaneIndices), groundPlaneIndices, GL_STATIC_DRAW);
//position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
//texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// load and create ground texture
// -------------------------
unsigned int groundTexture;
glGenTextures(1, &groundTexture);
glBindTexture(GL_TEXTURE_2D, groundTexture); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
int width, height, nrChannels;
// The FileSystem::getPath(...) is part of the GitHub repository so we can find files on any IDE/platform; replace it with your own image path.
unsigned char* data = stbi_load("textures/brick.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
Shader groundShader("groundVS.txt", "groundFS.txt");
glm::mat4 groundModelMatrix = mat_Translate(glm::mat4(1.f), glm::vec3(1.f, -1.5f, 0.f));
groundModelMatrix = mat_scale(groundModelMatrix, glm::vec3(100.f));
groundShader.use();
groundShader.setMat4("model", groundModelMatrix);
groundShader.setInt("g_texture", 0);
float lightCubeVertices[] = {
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
};
unsigned int lightCubeVBO, lightCubeVAO;
glGenVertexArrays(1, &lightCubeVAO);
glGenBuffers(1, &lightCubeVBO);
glBindBuffer(GL_ARRAY_BUFFER, lightCubeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(lightCubeVertices), lightCubeVertices, GL_STATIC_DRAW);
glBindVertexArray(lightCubeVAO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
Shader lightCubeShader("lightCubeVS.txt", "lightCubeFS.txt");
// load models
// -----------
Model stupamodel("objects/stupa/stupa.obj");
// draw in wireframe
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//
//
// shader configuration
// --------------------
Shader lightingShader("light.VS", "light.FS");
lightingShader.use();
lightingShader.setInt("material.diffuse", 0);
lightingShader.setInt("material.specular", 1);
// LightFunction(lightingShader);
bool isNightMode = 0;
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// per-frame time logic
// --------------------
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.96f, 1.0f, 0.98f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// be sure to activate shader when setting uniforms/drawing objects
// material properties
lightingShader.use();
lightingShader.setVec3("viewPos", camera.Position);
lightingShader.setFloat("material.shininess", 30.0f);
lightingShader.setInt("dir", isNightMode);
// directional light
lightingShader.setVec3("dirLight.direction", 0.8f, 0.8f, 0.8f);
lightingShader.setVec3("dirLight.ambient", 0.8f, 0.8f, 0.8f);
lightingShader.setVec3("dirLight.diffuse", 0.6f, 0.6f, 0.6f);
lightingShader.setVec3("dirLight.specular", 0.5f, 0.5f, 0.5f);
if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS) {
isNightMode = 1;
lightingShader.setInt("dir", isNightMode);
}
if (glfwGetKey(window, GLFW_KEY_G) == GLFW_PRESS) {
isNightMode = 0;
lightingShader.setInt("dir", isNightMode);
}
// point light 1
lightingShader.setVec3("pointLights[0].position", pointLightPositions[0]);
lightingShader.setVec3("pointLights[0].ambient", 0.1f, 0.1f, 0.1f);
lightingShader.setVec3("pointLights[0].diffuse", 0.83f, 0.68f, 0.21f);//0.8f, 0.8f, 0.8f
lightingShader.setVec3("pointLights[0].specular", 1.0f, 1.0f, 1.0f);
lightingShader.setFloat("pointLights[0].constant", 1.0f);
lightingShader.setFloat("pointLights[0].linear", 0.09);
lightingShader.setFloat("pointLights[0].quadratic", 0.032);
// point light 2
lightingShader.setVec3("pointLights[1].position", pointLightPositions[1]);
lightingShader.setVec3("pointLights[1].ambient", 0.1f, 0.1f, 0.1f);
lightingShader.setVec3("pointLights[1].diffuse", 0.83f, 0.68f, 0.21f);
lightingShader.setVec3("pointLights[1].specular", 1.0f, 1.0f, 1.0f);
lightingShader.setFloat("pointLights[1].constant", 1.0f);
lightingShader.setFloat("pointLights[1].linear", 0.09);
lightingShader.setFloat("pointLights[1].quadratic", 0.032);
// point light 3
lightingShader.setVec3("pointLights[2].position", pointLightPositions[2]);
lightingShader.setVec3("pointLights[2].ambient", 0.1f, 0.1f, 0.1f);
lightingShader.setVec3("pointLights[2].diffuse", 0.83f, 0.68f, 0.21f);
lightingShader.setVec3("pointLights[2].specular", 1.0f, 1.0f, 1.0f);
lightingShader.setFloat("pointLights[2].constant", 1.0f);
lightingShader.setFloat("pointLights[2].linear", 0.09);
lightingShader.setFloat("pointLights[2].quadratic", 0.032);
// point light 4
lightingShader.setVec3("pointLights[3].position", pointLightPositions[3]);
lightingShader.setVec3("pointLights[3].ambient", 0.1f, 0.1f, 0.1f);
lightingShader.setVec3("pointLights[3].diffuse", 0.83f, 0.68f, 0.21f);
lightingShader.setVec3("pointLights[3].specular", 1.0f, 1.0f, 1.0f);
lightingShader.setFloat("pointLights[3].constant", 1.0f);
lightingShader.setFloat("pointLights[3].linear", 0.09);
lightingShader.setFloat("pointLights[3].quadratic", 0.032);
// point light 5
lightingShader.setVec3("pointLights[4].position", pointLightPositions[4]);
lightingShader.setVec3("pointLights[4].ambient", 0.1f, 0.1f, 0.1f);
lightingShader.setVec3("pointLights[4].diffuse", 0.83f, 0.68f, 0.21f);
lightingShader.setVec3("pointLights[4].specular", 1.0f, 1.0f, 1.0f);
lightingShader.setFloat("pointLights[4].constant", 1.0f);
lightingShader.setFloat("pointLights[4].linear", 0.09);
lightingShader.setFloat("pointLights[4].quadratic", 0.032);
// point light 6
lightingShader.setVec3("pointLights[5].position", pointLightPositions[5]);
lightingShader.setVec3("pointLights[5].ambient", 0.1f, 0.1f, 0.1f);
lightingShader.setVec3("pointLights[5].diffuse", 0.83f, 0.68f, 0.21f);
lightingShader.setVec3("pointLights[5].specular", 1.0f, 1.0f, 1.0f);
lightingShader.setFloat("pointLights[5].constant", 1.0f);
lightingShader.setFloat("pointLights[5].linear", 0.09);
lightingShader.setFloat("pointLights[5].quadratic", 0.032);
// point light 7
lightingShader.setVec3("pointLights[6].position", pointLightPositions[6]);
lightingShader.setVec3("pointLights[6].ambient", 0.8f, 0.8f, 0.8f);
lightingShader.setVec3("pointLights[6].diffuse", 0.83f, 0.68f, 0.21f);
lightingShader.setVec3("pointLights[6].specular", 1.0f, 1.0f, 1.0f);
lightingShader.setFloat("pointLights[6].constant", 1.0f);
lightingShader.setFloat("pointLights[6].linear", 0.045);
lightingShader.setFloat("pointLights[6].quadratic", 0.0075);
// view/projection transformations
glm::mat4 projection = myperspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
glm::mat4 view = camera.GetViewMatrix();
lightingShader.setMat4("projection", projection);
lightingShader.setMat4("view", view);
// render the loaded model
glm::mat4 model = glm::mat4(1.0f);
model = mat_Translate(model, glm::vec3(0.0f, -1.4f, 0.0f)); // translate it down so it's at the center of the scene
model = mat_scale(model, glm::vec3(0.1f, 0.1f, 0.1f));
model = mat_rotate(model, glm::radians(-135.0f), glm::vec3(0.0, 1.0, 0.0));
lightingShader.setMat4("model", model);
stupamodel.Draw(lightingShader);
//plane
glBindTexture(GL_TEXTURE_2D, groundTexture);
groundShader.use();
groundShader.setMat4("projection", projection);
groundShader.setMat4("view", view);
glBindVertexArray(groundPlaneVAO);
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
//lightCube drawing
lightCubeShader.use();
lightCubeShader.setMat4("projection", projection);
lightCubeShader.setMat4("view", view);
// bind diffuse map
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(lightCubeVAO);
model = glm::mat4(1.0f);
model = mat_Translate(model, dirlightpos);
model = mat_scale(model, glm::vec3(0.2f)); // Make it a smaller cube
lightCubeShader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
// we now draw as many light bulbs as we have point lights.
//for (unsigned int i = 0; i <= 6; i++)
//{
// model = glm::mat4(1.0f);
// model = mat_Translate(model, pointLightPositions[i]);
// model = mat_scale(model, glm::vec3(0.2f)); // Make it a smaller cube
// lightCubeShader.setMat4("model", model);
// glBindVertexArray(lightCubeVAO);
// 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
glfwSwapBuffers(window);
glfwPollEvents();
}
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT, deltaTime);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
// glfw: whenever the mouse moves, this callback is called
// -------------------------------------------------------
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
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);
}
// glfw: whenever the mouse scroll wheel scrolls, this callback is called
// ----------------------------------------------------------------------
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
camera.ProcessMouseScroll(yoffset);
}
// loads a cubemap texture from 6 individual texture faces
// order:
// +X (right)
// -X (left)
// +Y (top)
// -Y (bottom)
// +Z (front)
// -Z (back)
// -------------------------------------------------------
unsigned int loadCubemap(vector<std::string> faces)
{
stbi_set_flip_vertically_on_load(false);
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;
}