-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
342 lines (303 loc) · 11.2 KB
/
main.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
//Includes--------------------------------------
#include <iostream>
#include <list>
#include <vector>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
//------ GLM - Open GL Mathematics: https://glm.g-truc.net/0.9.8/index.html
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
//------ stb_image - loads images as data to use for textures: https://github.com/nothings/stb/blob/master/stb_image.h
//------ | Credit goes to Sean Barett as well as the other contributors
#include <stb_image.h>
//------ shader_s header. Largely based on guide: https://learnopengl.com/#!Getting-started/Shaders
#include <Shader.h>
//------ Camera header. Largely based on guide: https://learnopengl.com/#!Getting-started/Camera
#include <Camera.h>
//------ Model header. Largely based on guide: https://learnopengl.com/#!Model-Loading/Model
#include <Model.h>
//------ Model header. Represents a snowman
#include <Snowman.h>
//------ Bullet header. Represents a bullet
#include <Bullet.h>
// functions
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
int main();
void processInput(GLFWwindow *window);
bool checkCollision(glm::vec3 aMax, glm::vec3 aMin, glm::vec3 bMax, glm::vec3 bMin);
// settings
const unsigned int SCR_WIDTH = 1000;
const unsigned int SCR_HEIGHT = 600;
//Setting up camera variables
Camera camera(glm::vec3(0.0f, 0.0f, 0.3f));
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
const glm::vec3 playerAABBmax(0.5, 2, 0.5);
const glm::vec3 playerAABBmin(-0.5, -2, -0.5);
bool playerDead = false;
int points = 0;
bool pointsWritten = false;
Bullet bullets[300];
int bulletCounter = 0;
Snowman snowmen[200];
int snowmanCounter = 0;
float lastSnowSpawn = 0;
float spawnRate = 6;
float deltaTime = 0.0f; //Time between current and last frame
float lastFrame = 0.0f; //Time of last frame
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);
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Snowman Shooter", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
//Capture the cursor
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
//sets mouse_callback to cursor position callback
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
//Enabling depth buffer
glEnable(GL_DEPTH_TEST);
//Initialize shader
Shader modelShader("Resources/Shaders/model_loading_vs.txt", "Resources/Shaders/model_loading_fs.txt");
//Initialize models
Model skyboxModel("Resources/Models/SkyBox/SkyBox.obj"); //Texture: https://gamebanana.com/textures/1875
Model gunModel("Resources/Models/Gun/gun.obj"); // Texture https://www.models-resource.com/pc_computer/counterstrikesource/model/15934/?source=genre
Model groundModel("Resources/Models/Ground/ground.obj"); //Texture: http://bgfons.com/download/1556
Model gameOver("Resources/Models/GameOver/gameOver.obj");
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
//Calculate deltatime for each frame
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// render
// ------
glClearColor(0.05f, 0.05f, 0.05f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//WireFrame mode:
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
modelShader.use();
//Projection matrix
//----------------------------------------------------------------------------------
glm::mat4 projection;
projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
modelShader.setMat4("projection", projection);
//View for camera
//----------------------------------------------------------------------------------
glm::mat4 view;
view = camera.GetViewMatrix();
modelShader.setMat4("view", view);
if (!playerDead)
{
//Drawing skybox
//----------------------------------------------------------------------------------
glm::mat4 skyMat;
skyMat = glm::translate(skyMat, camera.Position);
skyMat = glm::scale(skyMat, glm::vec3(50.0f, 50.0f, 50.0f));
modelShader.setMat4("model", skyMat);
skyboxModel.Draw(modelShader);
//Drawing snow ground
//----------------------------------------------------------------------------------
glm::mat4 mod;
mod = glm::translate(mod, glm::vec3(0.0f, -1.9f, 0.0f));
mod = glm::scale(mod, glm::vec3(5.0f, 5.0f, 5.0f));
modelShader.setMat4("model", mod);
groundModel.Draw(modelShader);
//Drawing gun
//----------------------------------------------------------------------------------
glm::mat4 model;
model = glm::translate(model, glm::vec3(0, -0.35, -1));
model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1, 0));
model = glm::scale(model, glm::vec3(0.1f, 0.1f, 0.1f));
glm::mat4 MVP = glm::inverse(camera.GetViewMatrix()) * model;
modelShader.setMat4("model", MVP);
gunModel.Draw(modelShader);
//Drawing bullets
//----------------------------------------------------------------------------------
for (int i = 0; i < bulletCounter; i++)
{
//Exclude all bullets that can't be seen
if (bullets[i].position.x > 60 + camera.Position.x || bullets[i].position.x < -60 + camera.Position.x ||
bullets[i].position.y > 60 + camera.Position.y || bullets[i].position.y < -10 + camera.Position.y ||
bullets[i].position.z > 60 + camera.Position.z || bullets[i].position.y < -60 + camera.Position.z)
{
continue;
}
modelShader.setMat4("model", bullets[i].getShootingMatrix(deltaTime));
bullets[i].model.Draw(modelShader);
}
//Drawing snowman + checking for collisions
//----------------------------------------------------------------------------------
if (glfwGetTime() > lastSnowSpawn + spawnRate)
{
lastSnowSpawn = glfwGetTime();
spawnRate -= spawnRate / glfwGetTime();
Snowman snowman(camera.Position);
snowmen[snowmanCounter] = snowman;
snowmanCounter++;
if (snowmanCounter == 200)
snowmanCounter = 0;
}
for (int i = 0; i < snowmanCounter; i++)
{
//Exclude all bullets that can't be seen
if (snowmen[i].dead)
{
continue;
}
modelShader.setMat4("model", snowmen[i].getSeekMatrix(camera.Position, deltaTime));
snowmen[i].model.Draw(modelShader);
//Update snowman and player AABB
snowmen[i].updateAABB();
glm::vec3 playerBoxMaxCoord(camera.Position.x + playerAABBmax.x, camera.Position.y + playerAABBmax.y, camera.Position.z + playerAABBmax.z);
glm::vec3 playerBoxMinCoord(camera.Position.x + playerAABBmin.x, camera.Position.y + playerAABBmin.y, camera.Position.z + playerAABBmin.z);
// player - snowman collision check
if (checkCollision(playerBoxMaxCoord, playerBoxMinCoord, snowmen[i].boxMaxCoord, snowmen[i].boxMinCoord))
{
playerDead = true;
}
//bullet - snowman collision check
for (int j = 0; j < bulletCounter; j++)
{
//Update bullet AABB
bullets[j].updateAABB();
if (checkCollision(bullets[j].boxMaxCoord, bullets[j].boxMinCoord, snowmen[i].boxMaxCoord, snowmen[i].boxMinCoord))
{
snowmen[i].dead = true;
points++;
}
}
}
}
else
{
//Drawing Game Over Plane
//----------------------------------------------------------------------------------
glm::mat4 gameOverMat;
gameOverMat = glm::translate(gameOverMat, glm::vec3(0, 0, -1));
gameOverMat = glm::rotate(gameOverMat, glm::radians(90.0f), glm::vec3(1, 0, 0));
gameOverMat = glm::scale(gameOverMat, glm::vec3(0.3f, 0.3f, 0.3f));
glm::mat4 MVP = glm::inverse(camera.GetViewMatrix()) * gameOverMat;
modelShader.setMat4("model", MVP);
gameOver.Draw(modelShader);
if (!pointsWritten)
{
std::cout << "You killed " << points << " snowmen!";
pointsWritten = true;
}
}
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
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);
}
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
{
camera.Zoom = 30.0f;
}
else
{
camera.Zoom = 45.0f;
}
}
// 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);
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
//Avoid jump in camera movement
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
camera.ProcessMouseMovement(xoffset, yoffset);
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
{
//Create new bullet
Bullet bullet(camera.Position, camera.Front);
bullets[bulletCounter] = bullet;
bulletCounter++;
if (bulletCounter == 300)
{
bulletCounter = 0;
}
}
}
bool checkCollision(glm::vec3 aMax, glm::vec3 aMin, glm::vec3 bMax, glm::vec3 bMin)
{
//Axis Alligned Bounding Boxes (AABB) - not suited for rotations
return (aMax.x > bMin.x && aMin.x < bMax.x &&
aMax.y > bMin.y && aMin.y < bMax.y &&
aMax.z > bMin.z && aMin.z < bMax.z);
}