-
Notifications
You must be signed in to change notification settings - Fork 0
/
Window.cpp
599 lines (522 loc) · 16.2 KB
/
Window.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
#include "window.h"
#include <algorithm>
const char* window_title = "ERADICATION";
GLint shaderProgram;
GLint skyboxShader;
GLint depthShader;
GLint modelShader;
GLint shadowMapShader;
GLint particleShader;
int mouse = 0;
glm::vec3 lastPos, curPos;
Cube * cube;
Bomb * bomb;
Bomber* bomber;
Building * building;
Block* block;
Sphere* sphere;
vector<Block*> blocks;
Plane* mapPlane;
//Plane* crater;
Plane* ground;
bool showShadowMap = false;
bool canMove = false;
// On some systems you need to change this to the absolute path
#define VERTEX_SHADER_PATH "../shader.vert"
#define FRAGMENT_SHADER_PATH "../shader.frag"
// Default camera parameters
glm::vec3 cam_pos(40.0f, 40.0f, 0.0f); // e | Position of camera
glm::vec3 cam_look_at(0.0f, 0.0f, 0.0f); // d | This is where the camera looks at
glm::vec3 cam_up(0.0f, 1.0f, 0.0f); // up | What orientation "up" is
//glm::vec3 right = glm::vec3(1.0f, 0.0f, 0.0f);
int Window::width;
int Window::height;
glm::mat4 Window::P;
glm::mat4 Window::V;
const glm::mat4 I = glm::mat4(1.0f);
vector<std::string> faces
{
"right2.png",
"left2.png",
"up2.png",
"down2.png",
"front2.png",
"back2.png"
};
unsigned int skyboxTexture;
unsigned int SHADOW_WIDTH = 1280, SHADOW_HEIGHT = 720;
unsigned int depthMapFBO;
unsigned int Window::depthMap;
glm::mat4 Window::lightSpaceMatrix;
glm::mat4 lightProjection = glm::ortho(-35.0f, 35.0f, -30.0f, 30.0f, -20.0f, 40.0f);
glm::vec3 lightPos = glm::vec3(10, 15, -5);
glm::vec3 bomberStartPos = glm::vec3(0, 0, -70);
bool drawCity = true;
bool Window::hasBomb = true;
float Window::shadowOn = 1.0f;
float explosionRadius = 20.0f;
bool Window::planeView = true;
vector<Sphere*> craters;
vector<glm::vec3> blockPos;
ISoundEngine * Window::soundEngine = createIrrKlangDevice();
ISound * bgm;
ISound * engine;
ISound * bombSound;
particle_generator * smokeL;
particle_generator * smokeR;
int total_bds = 0;
int total_kills = 0;
void Window::initialize_objects()
{
srand(0);
shaderProgram = LoadShaders(VERTEX_SHADER_PATH, FRAGMENT_SHADER_PATH);
skyboxShader = LoadShaders("../skybox.vert", "../skybox.frag");
depthShader = LoadShaders("../depthShader.vert", "../depthShader.frag");
modelShader = LoadShaders("../model.vert", "../model.frag");
shadowMapShader = LoadShaders("../shadowMap.vert", "../shadowMap.frag");
particleShader = LoadShaders("../particle.vert", "../particle.frag");
smokeL = new particle_generator();
smokeR = new particle_generator();
cube = new Cube();
skyboxTexture = loadCubemap(faces);
bomber = new Bomber(bomberStartPos);
bomb = new Bomb(glm::vec3(0, 25, 0));
//building = new Building(4, 4, 1.0f, glm::vec3(0, 0, 0));
mapPlane = new Plane(glm::vec3(60, 0, 0), "../building.jpg");
mapPlane->isShadowMap = true;
mapPlane->toWorld = glm::scale(mapPlane->toWorld, glm::vec3(10, 0, 10));
ground = new Plane(glm::vec3(0,-0.1f,0), "ground3.jpg");
ground->scale(60, 60);
if (drawCity) {
for (int z = -1; z < 2; z++) {
for (int x = -1; x < 2; x++) {
glm::vec3 pos = glm::vec3(x*18.0f, 0, z*18.0f);
Block* blk = new Block(2.0f, pos);
blocks.push_back(blk);
blockPos.push_back(pos);
}
}
}
else {
block = new Block(2.0f, glm::vec3(0, 0, 0));
}
createDepthMap();
bgm = soundEngine->play2D("BGM.flac", true, false, true);
engine = soundEngine->play2D("engine.wav", true, false, true);
bgm->setVolume(0.5f);
engine->setVolume(0.4f);
}
// Treat this as a destructor function. Delete dynamically allocated memory here.
void Window::clean_up()
{
glDeleteProgram(shaderProgram);
glDeleteProgram(skyboxShader);
glDeleteProgram(modelShader);
glDeleteProgram(depthShader);
glDeleteProgram(shadowMapShader);
soundEngine->drop();
}
GLFWwindow* Window::create_window(int width, int height)
{
// Initialize GLFW
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return NULL;
}
// 4x antialiasing
glfwWindowHint(GLFW_SAMPLES, 4);
#ifdef __APPLE__ // Because Apple hates comforming to standards
// Ensure that minimum OpenGL version is 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Enable forward compatibility and allow a modern OpenGL context
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// Create the GLFW window
GLFWwindow* window = glfwCreateWindow(width, height, window_title, NULL, NULL);
// Check if the window could not be created
if (!window)
{
fprintf(stderr, "Failed to open GLFW window.\n");
fprintf(stderr, "Either GLFW is not installed or your graphics card does not support modern OpenGL.\n");
glfwTerminate();
return NULL;
}
// Make the context of the window
glfwMakeContextCurrent(window);
// Set swap interval to 1
glfwSwapInterval(1);
// Get the width and height of the framebuffer to properly resize the window
glfwGetFramebufferSize(window, &width, &height);
// Call the resize callback to make sure things get drawn immediately
Window::resize_callback(window, width, height);
glfwSetScrollCallback(window, scroll_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetCursorPosCallback(window, cursor_pos_callback);
glEnable(GL_CULL_FACE);
return window;
}
void Window::resize_callback(GLFWwindow* window, int width, int height)
{
#ifdef __APPLE__
glfwGetFramebufferSize(window, &width, &height); // In case your Mac has a retina display
#endif
Window::width = width;
Window::height = height;
// Set the viewport size. This is the only matrix that OpenGL maintains for us in modern OpenGL!
glViewport(0, 0, width, height);
SHADOW_HEIGHT = height;
SHADOW_WIDTH = width;
if (height > 0)
{
P = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.01f, 2000.0f);
V = glm::lookAt(cam_pos, cam_look_at, cam_up);
}
}
void Window::idle_callback()
{
if (canMove) {
bomber->update();
bomb->update();
if (planeView) {
updateCamera(bomber->transform);
}
if (!Bomber::turnBack) {
smokeL->reversed = false;
smokeR->reversed = false;
smokeL->Update(0.1f, bomber->transform + glm::vec3(2.5, 0.5f, -5.5), 2, glm::vec3(1.0f, 1.0f, 0.0f));
smokeR->Update(0.1f, bomber->transform + glm::vec3(-1.3, 0.5f, -5.5), 2, glm::vec3(1.0f, 1.0f, 0.0f));
}
else {
smokeL->reversed = true;
smokeR->reversed = true;
smokeL->Update(0.1f, bomber->transform + glm::vec3(1.3, 0.5f, 5.5), 2, glm::vec3(1.0f, 1.0f, 0.0f));
smokeR->Update(0.1f, bomber->transform + glm::vec3(-2.5, 0.5f, 5.5), 2, glm::vec3(1.0f, 1.0f, 0.0f));
}
}
}
void Window::display_callback(GLFWwindow* window)
{
// Clear the color and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderDepthMap();
glUseProgram(modelShader);
bomber->draw(modelShader);
bomb->draw(modelShader);
for (Sphere* c : craters) {
c->draw(modelShader);
}
glUseProgram(particleShader);
smokeL->Draw(particleShader);
smokeR->Draw(particleShader);
if (showShadowMap) {
glUseProgram(shadowMapShader);
mapPlane->draw(shadowMapShader);
}
glUseProgram(shaderProgram);
//crater->draw(shaderProgram);
ground->draw(shaderProgram);
if (drawCity) {
for (auto blk : blocks) {
blk->draw(shaderProgram);
}
}
else {
block->draw(shaderProgram);
}
drawSkybox();
glfwPollEvents();
// Swap buffers
glfwSwapBuffers(window);
}
void Window::key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
float speed = 0.1f;
if (action == GLFW_REPEAT) {
if (key == GLFW_KEY_W) {
glm::vec3 dir = cam_pos - cam_look_at;
cam_pos -= dir * speed;
cam_look_at -= dir * speed;
V = glm::lookAt(cam_pos, cam_look_at, cam_up);
}
if (key == GLFW_KEY_S) {
glm::vec3 dir = cam_pos - cam_look_at;
cam_pos += dir * speed;
cam_look_at += dir * speed;
V = glm::lookAt(cam_pos, cam_look_at, cam_up);
}
if (key == GLFW_KEY_UP) {
cam_look_at += glm::vec3(0, speed, 0);
V = glm::lookAt(cam_pos, cam_look_at, cam_up);
}
if (key == GLFW_KEY_DOWN) {
cam_look_at += glm::vec3(0, -speed, 0);
V = glm::lookAt(cam_pos, cam_look_at, cam_up);
}
if (key == GLFW_KEY_LEFT) {
bomber->moveLeft();
}
if (key == GLFW_KEY_RIGHT) {
bomber->moveRight();
}
}
// Check for a key press
if (action == GLFW_PRESS)
{
// Check if escape was pressed
if (key == GLFW_KEY_ESCAPE)
{
// Close the window. This causes the program to also terminate.
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (key == GLFW_KEY_R) {
random_shuffle(blocks.begin(), blocks.end());
for (int i = 0; i < blockPos.size(); i++) {
blocks[i]->moveTo(blockPos[i]);
blocks[i]->fastRebuild();
}
restart();
}
if (key == GLFW_KEY_T) {
cout << "Rebuilding city..." << endl;
if (drawCity) {
for (auto blk : blocks) {
blk->rebuild();
}
}
else {
block->rebuild();
}
renderDepthMap();
cout << "Rebuild completed!" << endl;
restart();
}
if (key == GLFW_KEY_Q) {
if (shadowOn == 1.0f)
shadowOn = 0.0f;
else
shadowOn = 1.0f;
}
if (key == GLFW_KEY_M) {
showShadowMap = !showShadowMap;
}
if (key == GLFW_KEY_SPACE) {
if (hasBomb) {
glm::vec3 bias = glm::vec3(0.6f, 0, -3);
if(Bomber::turnBack)
bias = glm::vec3(0.6f, 0, 3);
bomb->dropBomb(bomber->transform + bias);
hasBomb = false;
planeView = false;
//ground->toWorld = glm::rotate(ground->toWorld, glm::radians(90.0f), glm::vec3(0, 1, 0));
cam_pos = glm::vec3(-60, 25, 0);
cam_look_at = glm::vec3(0, 7, 0);
V = glm::lookAt(cam_pos, cam_look_at, cam_up);
engine->setVolume(0.25f);
bombSound = soundEngine->play2D("falling.wav", false, false, true);
bombSound->setVolume(0.2f);
}
}
if (key == GLFW_KEY_P) {
planeView = !planeView;
}
if (key == GLFW_KEY_O) {
canMove = !canMove;
}
}
}
void Window::mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
if (action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT) {
mouse = 1;
}
else {
mouse = 0;
}
}
void Window::cursor_pos_callback(GLFWwindow* window, double x, double y)
{
if (mouse == 1) {
lastPos = trackBallMapping(x, y);
mouse = 2;
}
else if (mouse == 2) {
curPos = trackBallMapping(x, y);
glm::vec3 rotAxis = glm::cross(lastPos, curPos);
float angle = asin(glm::length(rotAxis)) / 50.0f;
glm::mat4 R = glm::rotate(glm::mat4(1.0f), angle, rotAxis);
//glm::vec3 cam_dir = cam_pos - cam_look_at;
//cam_dir = glm::vec3(glm::vec4(cam_dir, 0.0f) * R);
glm::vec3 d = cam_pos - cam_look_at;
glm::vec4 temp = glm::vec4(d, 1.0f)*R;
cam_look_at = cam_pos - glm::vec3(temp);
V = glm::lookAt(cam_pos, cam_look_at, cam_up);
}
}
glm::vec3 Window::trackBallMapping(double x, double y) {
glm::vec3 v(0.0f);
v.x = (2 * x - width) / width;
v.y = (height - 2*y) / height;
float d = glm::length(v);
d = (d < 1.0) ? d : 1.0;
v.z = sqrtf(1.001 - d*d);
v = glm::normalize(v);
return v;
}
void Window::scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
if (yoffset > 0) {
glm::vec3 dir = cam_pos - cam_look_at;
cam_pos -= dir * 0.05f;
cam_look_at -= dir * 0.05f;
V = glm::lookAt(cam_pos, cam_look_at, cam_up);
}
else if (yoffset < 0) {
glm::vec3 dir = cam_pos - cam_look_at;
cam_pos += dir * 0.05f;
cam_look_at += dir * 0.05f;
V = glm::lookAt(cam_pos, cam_look_at, cam_up);
}
}
void Window::print(glm::vec3 &v) {
cout << v.x << " " << v.y << " " << v.z << endl;
}
glm::vec3 Window::getCamPos() {
return cam_pos;
}
void Window::drawSkybox() {
glUseProgram(skyboxShader);
glUniformMatrix4fv(glGetUniformLocation(skyboxShader, "projection"), 1, GL_FALSE, &Window::P[0][0]);
glUniformMatrix4fv(glGetUniformLocation(skyboxShader, "model"), 1, GL_FALSE, &cube->toWorld[0][0]);
glUniformMatrix4fv(glGetUniformLocation(skyboxShader, "view"), 1, GL_FALSE, &Window::V[0][0]);
glBindVertexArray(cube->VAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, skyboxTexture);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
unsigned int Window::loadCubemap(vector<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
{
cout << "Cubemap texture failed to load at path: " << 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 Window::createDepthMap() {
glGenFramebuffers(1, &depthMapFBO);
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_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
float borderColor[] = { 1.0, 1.0, 1.0, 1.0 };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
// 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);
}
void Window::renderDepthMap() {
// render depth of scene to texture (from light's perspective)
glm::mat4 lightView;
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
glUseProgram(depthShader);
glUniformMatrix4fv(glGetUniformLocation(depthShader, "lightSpaceMatrix"), 1, GL_FALSE, &lightSpaceMatrix[0][0]);
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
glClear(GL_DEPTH_BUFFER_BIT);
//building->draw(depthShader);
//plane->draw(depthShader);
if (drawCity) {
for (auto blk : blocks) {
blk->draw(depthShader);
}
}
else {
block->draw(depthShader);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void Window::updateCamera(glm::vec3 pos) {
cam_look_at = pos;
if(!Bomber::turnBack)
cam_pos = cam_look_at + glm::vec3(0, 12, -18);
else
cam_pos = cam_look_at + glm::vec3(0, 12, 18);
V = glm::lookAt(cam_pos, cam_look_at, cam_up);
}
void Window::explosion(glm::vec3 pos) {
createCrater(pos);
for (auto blk : blocks) {
vector<int> v = blk->detectExplosion(pos, explosionRadius);
total_bds += v[0];
total_kills += v[1];
}
if (Bomber::turnBack) {
cout << "--------------------------------------" << endl;
cout << "Mission Report:" << endl;
cout << "You destroyed " << total_bds << " buildings." << endl;
cout << "You eliminated " << total_kills << " infecteds." << endl;
cout << "Your rating for this mission: ";
if (total_kills > 25000)
cout << "A" << endl;
else if (total_kills > 23000)
cout << "B" << endl;
else if (total_kills > 20000)
cout << "C" << endl;
else
cout << "D" << endl;
cout << "--------------------------------------" << endl;
}
}
void Window::restart() {
bgm->stop();
bgm->drop();
engine->stop();
engine->drop();
craters.clear();
bomber->reset(bomberStartPos);
hasBomb = true;
planeView = true;
total_bds = 0;
total_kills = 0;
bgm = soundEngine->play2D("BGM.flac", true, false, true);
engine = soundEngine->play2D("engine.wav", true, false, true);
bgm->setVolume(0.5f);
engine->setVolume(0.4f);
}
void Window::createCrater(glm::vec3 pos) {
Sphere* sphere = new Sphere("sphere.obj", "crater.jpg");
sphere->toWorld = glm::translate(sphere->toWorld, glm::vec3(0, 0.1f, 0));
sphere->toWorld = glm::translate(sphere->toWorld, pos);
sphere->toWorld = glm::scale(sphere->toWorld, glm::vec3(25, 0.1f, 25));
craters.push_back(sphere);
}