-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmp.cpp
More file actions
420 lines (369 loc) · 17.2 KB
/
mp.cpp
File metadata and controls
420 lines (369 loc) · 17.2 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
// mp.cpp : Defines the entry point for the application.
/*
Assets Credits:
Tank Model: https://sketchfab.com/3d-models/t-90am-0fabde7367a64de297f93da096cd126c
Ground: https://www.freepik.com/free-photo/close-up-ground-texture-detail_23670142.htm
Skybox: https://www.pngegg.com/en/png-hzcii/download
Objects:
Fuel Canister: https://sketchfab.com/3d-models/fuel-can-low-poly-1ba93895512e47be945484b2fbdbc0a3
Compass: https://sketchfab.com/3d-models/compass-obj-3d-model-low-poly-obj-fb9c8e7a1c5f4e0a9b2c8e7b1a1f0c6
Computer: https://sketchfab.com/3d-models/retro-computer-f844c0357d284fd8baa1435e9ff31bb2
Stone: https://sketchfab.com/3d-models/monumental-runic-stone-optimised-20k-d95a850cd9114828a18b5dd72878d9ec#download
Map: https://sketchfab.com/3d-models/map-obj-3d-model-low-poly-obj-9c8e7a1c5f4e0a9b2c8e7b1a1f0c6
Signal Jammer: https://sketchfab.com/3d-models/signal-jammer-obj-3d-model-low-poly-obj-9c8e7a1c5f4e0a9b2c8e7b1a1f0c6
*/
#include <glad/gl.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include "stb_image.h"
#include "Shader.h"
#include "Model.h"
#include "Player.h"
#include "Camera.h"
#include "Light.h"
#include "Skybox.h"
using namespace std;
using namespace glm;
const int WINDOW_W = 800;
const int WINDOW_H = 800;
const float TANK_SCALE = 5.0f;
const float GROUND_SIZE = 500.f;
struct ClueObject {
int index;
string name;
string clue;
bool found;
float triggerDist;
};
vector<ClueObject> clues = {
{ 0, "Strange Compass",
"[CLUE FOUND!] The compass spins wildly... but one direction glows faintly.\n"
" >> Head toward the brightest star on the horizon.",
false, 8.f },
{ 1, "Torn Map Fragment",
"[CLUE FOUND!] A fragment of a map -- it shows a circular portal marked 'Origin Gate'.\n"
" >> The gate only opens when all 6 relics are found.",
false, 8.f },
{ 2, "Ancient Rune Stone",
"[CLUE FOUND!] Engraved: 'The sky you came from is the sky you must return to.'\n"
" >> The night sky above holds the answer.",
false, 8.f },
{ 3, "Cracked Signal Beacon",
"[CLUE FOUND!] A damaged radio beacon. Static, then a voice: '...coordinates locked...'\n"
" >> Frequency: 77.7 MHz. The portal activates at midnight.",
false, 8.f },
{ 4, "Glowing Fuel Canister",
"[CLUE FOUND!] Your tank's fuel reads EMPTY -- but this canister refills it with\n"
" a strange luminescent energy. The engine hums differently now.",
false, 8.f },
{ 5, "Final Transmission Log",
"[CLUE FOUND!] A holographic log plays: 'Return to the origin point -- where you\n"
" first appeared. All 6 relics unlocked. The gate will open. Go home.'\n"
" >> ALL CLUES FOUND. Return to the center of the map (0, 0)!",
false, 8.f },
};
int cluesFound = 0;
void printControls() {
cout << "\n===================================================\n";
cout << " Home is where... -- CONTROLS\n";
cout << "===================================================\n";
cout << " [1] Binoculars (Night Vision) [2] Orthographic\n";
cout << " 3rd Person: W/S move, A/D turn, SPACE jump, MOUSE orbit, SCROLL zoom\n";
cout << " Binoculars: W/S look up/down, A/D left/right, Q/E zoom\n";
cout << " Ortho: W/S/A/D pan, LMB drag to orbit\n";
cout << " [F] Cycle point light [ESC] Quit\n";
cout << "===================================================\n\n";
}
int camMode = 0;
bool nightVision = false;
PerspectiveCamera* gPerspCam = nullptr;
BinocularsCamera* gBinoCam = nullptr;
OrthoCamera* gOrthoCam = nullptr;
Player* gPlayer = nullptr;
PointLight* gPointLight = nullptr;
float lastX = WINDOW_W / 2.f, lastY = WINDOW_H / 2.f;
bool firstMouse = true;
float deltaTime = 0.f, lastFrame = 0.f;
bool orthoDragging = false;
float orthoDragLastX = 0.f, orthoDragLastY = 0.f;
float orthoYaw = 0.f, orthoPitch = 90.f;
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
bool down = (action == GLFW_PRESS) || (action == GLFW_REPEAT);
if (!down) return;
if (key == GLFW_KEY_1) {
camMode = (camMode == 1) ? 0 : 1;
nightVision = (camMode == 1);
cout << (nightVision ? "[Camera] Binoculars ON\n" : "[Camera] 3rd Person\n");
}
if (key == GLFW_KEY_2) {
camMode = (camMode == 2) ? 0 : 2;
cout << (camMode == 2 ? "[Camera] Orthographic\n" : "[Camera] 3rd Person\n");
}
if (key == GLFW_KEY_F && gPointLight) gPointLight->cycleIntensity();
if (key == GLFW_KEY_ESCAPE) glfwSetWindowShouldClose(window, true);
if (camMode == 0 && gPlayer) {
if (key == GLFW_KEY_W) gPlayer->moveForward(deltaTime);
if (key == GLFW_KEY_S) gPlayer->moveBackward(deltaTime);
if (key == GLFW_KEY_A) gPlayer->turnLeft(deltaTime);
if (key == GLFW_KEY_D) gPlayer->turnRight(deltaTime);
if (key == GLFW_KEY_SPACE) gPlayer->jump();
}
if (camMode == 2 && gOrthoCam) {
const float p = 0.5f;
if (key == GLFW_KEY_W) gOrthoCam->pan(0.f, -p);
if (key == GLFW_KEY_S) gOrthoCam->pan(0.f, p);
if (key == GLFW_KEY_A) gOrthoCam->pan(-p, 0.f);
if (key == GLFW_KEY_D) gOrthoCam->pan(p, 0.f);
}
if (camMode == 1 && gBinoCam) {
const float l = 2.f;
if (key == GLFW_KEY_W) gBinoCam->rotatePitch(l);
if (key == GLFW_KEY_S) gBinoCam->rotatePitch(-l);
if (key == GLFW_KEY_A) gBinoCam->rotateYaw(-l);
if (key == GLFW_KEY_D) gBinoCam->rotateYaw(l);
if (key == GLFW_KEY_Q) gBinoCam->zoom(2.f);
if (key == GLFW_KEY_E) gBinoCam->zoom(-2.f);
}
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
if (camMode != 2) return;
if (button == GLFW_MOUSE_BUTTON_LEFT) {
orthoDragging = (action == GLFW_PRESS);
if (orthoDragging) {
double x, y; glfwGetCursorPos(window, &x, &y);
orthoDragLastX = (float)x; orthoDragLastY = (float)y;
}
}
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
if (firstMouse) { lastX = (float)xpos; lastY = (float)ypos; firstMouse = false; }
float dx = (float)(xpos - lastX), dy = (float)(lastY - ypos);
lastX = (float)xpos; lastY = (float)ypos;
if (camMode == 2 && orthoDragging && gOrthoCam) {
float ddx = (float)(xpos - orthoDragLastX), ddy = (float)(ypos - orthoDragLastY);
orthoDragLastX = (float)xpos; orthoDragLastY = (float)ypos;
orthoYaw += ddx * 0.4f;
orthoPitch -= ddy * 0.4f;
orthoPitch = glm::clamp(orthoPitch, 10.f, 90.f);
float r = 50.f, yR = radians(orthoYaw), pR = radians(orthoPitch);
gOrthoCam->setPosition(vec3(r * cos(pR) * sin(yR), r * sin(pR), r * cos(pR) * cos(yR)));
}
if (camMode == 0 && gPerspCam) {
gPerspCam->rotateYaw(dx * 0.3f);
gPerspCam->rotatePitch(dy * 0.3f);
}
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
if (camMode == 0 && gPerspCam) gPerspCam->zoom((float)yoffset * 0.5f);
}
static void buildGround(GLuint& VAO, GLuint& VBO, float size, float uvTile) {
float s = size, u = uvTile;
float verts[] = {
-s,0,-s, 0,1,0, 0,u, s,0,-s, 0,1,0, u,u, s,0, s, 0,1,0, u,0,
-s,0,-s, 0,1,0, 0,u, s,0, s, 0,1,0, u,0, -s,0, s, 0,1,0, 0,0,
};
glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); glEnableVertexAttribArray(2);
glBindVertexArray(0);
}
void checkClues(const vec3& playerPos, Model* objects[6]) {
if (cluesFound >= 6) {
float d = sqrt(playerPos.x * playerPos.x + playerPos.z * playerPos.z);
static bool winPrinted = false;
if (!winPrinted && d < 5.f) {
winPrinted = true;
cout << "\n===================================================\n";
cout << " YOU MADE IT HOME! Mission complete.\n";
cout << "===================================================\n\n";
}
return;
}
for (auto& c : clues) {
if (c.found || !objects[c.index]) continue;
vec3 diff = playerPos - objects[c.index]->position;
if (sqrt(diff.x * diff.x + diff.y * diff.y + diff.z * diff.z) <= c.triggerDist) {
c.found = true; cluesFound++;
cout << "\n---------------------------------------------------\n";
cout << " * " << c.name << " *\n";
cout << c.clue << "\n [" << cluesFound << "/6]\n";
cout << "---------------------------------------------------\n\n";
if (cluesFound == 6) cout << "*** ALL CLUES FOUND! Return to (0,0)! ***\n\n";
}
}
}
int main() {
if (!glfwInit()) { cerr << "GLFW init failed\n"; return -1; }
GLFWwindow* window = glfwCreateWindow(WINDOW_W, WINDOW_H, "Home is where...", NULL, NULL);
if (!window) { glfwTerminate(); return -1; }
glfwMakeContextCurrent(window);
gladLoadGL(glfwGetProcAddress);
glfwSetKeyCallback(window, key_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glEnable(GL_DEPTH_TEST);
printControls();
Shader litShader("Shaders/sample.vert", "Shaders/sample.frag");
Shader skyboxShader("Shaders/skybox.vert", "Shaders/skybox.frag");
string faces[6] = {
"Skybox/night_rt.png","Skybox/night_lf.png",
"Skybox/night_up.png","Skybox/night_dn.png",
"Skybox/night_ft.png","Skybox/night_bk.png"
};
Skybox skybox(faces);
Player player("3D/main_tank.obj", "3D/T90_Rust.png", TANK_SCALE, 0.5f);
gPlayer = &player;
Model* objects[6] = {
new Model("3D/obj/compass.obj", "3D/obj/compass_tex.png"),
new Model("3D/obj/map.obj", "3D/obj/map_tex.png"),
new Model("3D/obj/stone.obj", "3D/obj/stone_tex.jpg"),
new Model("3D/obj/jammer.obj", "3D/obj/jammer_tex.png"),
new Model("3D/obj/fuel.obj", "3D/obj/fuel_tex.png"),
new Model("3D/obj/computer.obj", "3D/obj/computer_tex.jpeg"),
};
objects[0]->position = vec3(-12.f, 0.f, -10.f);
objects[1]->position = vec3(16.f, 0.f, 8.f);
objects[2]->position = vec3(-9.f, 11.0f, 18.f);
objects[3]->position = vec3(18.f, 0.f, 14.f);
objects[4]->position = vec3(6.f, 0.f, -28.f);
objects[5]->position = vec3(-30.f, 1.5f, 30.f);
for (int i = 0; i < 6; i++) objects[i]->scale = vec3(2.5f);
// ---- Ground texture ----
GLuint groundVAO, groundVBO;
buildGround(groundVAO, groundVBO, GROUND_SIZE, 50.f);
stbi_set_flip_vertically_on_load(true);
int gw, gh, gch;
unsigned char* gdata = stbi_load("3D/ground_texture.jpg", &gw, &gh, &gch, 0);
GLuint groundTex = 0;
if (gdata) {
glGenTextures(1, &groundTex);
glBindTexture(GL_TEXTURE_2D, groundTex);
GLenum fmt = (gch == 4) ? GL_RGBA : GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, fmt, gw, gh, 0, fmt, GL_UNSIGNED_BYTE, gdata);
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(gdata);
}
else cerr << "[Ground] Failed to load texture\n";
// ---- Normal map (loaded ONCE here, before the render loop) ----
int nw, nh, nch;
unsigned char* ndata = stbi_load("3D/T90normal.jpg", &nw, &nh, &nch, 0);
GLuint normalMapTex = 0;
if (ndata) {
glGenTextures(1, &normalMapTex);
glBindTexture(GL_TEXTURE_2D, normalMapTex);
GLenum fmt = (nch == 4) ? GL_RGBA : GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, fmt, nw, nh, 0, fmt, GL_UNSIGNED_BYTE, ndata);
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(ndata);
cout << "[NormalMap] Loaded T90normal.jpg\n";
}
else cerr << "[NormalMap] Failed to load T90normal.jpg\n";
// ---- Cameras ----
PerspectiveCamera perspCam(vec3(0.f), 10.f, 60.f, WINDOW_W, WINDOW_H);
BinocularsCamera binoCam(vec3(0.f, 1.5f, 0.f), 60.f, WINDOW_W, WINDOW_H);
OrthoCamera orthoCam(vec3(0.f, 50.f, 0.f), -30.f, 30.f, -30.f, 30.f, 0.1f, 200.f);
gPerspCam = &perspCam; gBinoCam = &binoCam; gOrthoCam = &orthoCam;
// ---- Lights ----
PointLight pointLight(vec3(0.f, 2.f, 2.f), vec3(1.f, 0.9f, 0.7f), 8.f);
DirectionalLight dirLight(vec3(-0.3f, -1.f, -0.3f), vec3(0.6f, 0.7f, 1.0f), 0.35f);
SpotLight spotLight(vec3(0.f), vec3(0.f, 0.f, -1.f), 10.f, 20.f, vec3(1.f, 0.97f, 0.85f), 80.f);
gPointLight = &pointLight;
GLint screenSizeLoc = glGetUniformLocation(litShader.getID(), "screenSize");
// ===================== RENDER LOOP =====================
while (!glfwWindowShouldClose(window)) {
float currentFrame = (float)glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
glClearColor(0.005f, 0.005f, 0.01f, 1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
player.update(deltaTime);
checkClues(player.position, objects);
perspCam.setTarget(player.position);
binoCam.attachTo(player.position, player.yaw);
pointLight.setPosition(player.getFrontPosition());
spotLight.setPosition(player.getFrontPosition());
spotLight.setDirection(player.getForwardVector());
Camera* activeCam = (camMode == 0) ? (Camera*)&perspCam
: (camMode == 1) ? (Camera*)&binoCam
: (Camera*)&orthoCam;
mat4 view = activeCam->getViewMatrix();
mat4 projection = activeCam->getProjectionMatrix();
vec3 camPos = activeCam->getPosition();
skybox.draw(skyboxShader, view, projection);
litShader.use();
glUniform2f(screenSizeLoc, (float)WINDOW_W, (float)WINDOW_H);
litShader.setMat4("view", value_ptr(view));
litShader.setMat4("projection", value_ptr(projection));
litShader.setVec3("cameraPos", camPos.x, camPos.y, camPos.z);
litShader.setFloat("specStrength", 0.5f);
litShader.setFloat("specPhong", 32.f);
litShader.setBool("isUnlit", false);
litShader.setBool("isNightVision", nightVision);
litShader.setFloat("ambientStrength", nightVision ? 0.25f : 0.06f);
vec3 aColor = nightVision ? vec3(0.1f, 0.5f, 0.1f) : vec3(0.2f, 0.22f, 0.4f);
litShader.setVec3("ambientColor", aColor.x, aColor.y, aColor.z);
litShader.setVec3("fogColor", 0.005f, 0.005f, 0.01f); // match glClearColor
litShader.setFloat("fogStart", 60.f);
litShader.setFloat("fogEnd", 250.f);
pointLight.apply(litShader);
dirLight.apply(litShader);
spotLight.apply(litShader);
// ---- Tank (diffuse on unit 0, normal map on unit 1) ----
mat4 tankModel = player.getModelMatrix();
litShader.setMat4("transform", value_ptr(tankModel));
glActiveTexture(GL_TEXTURE0);
litShader.setInt("tex0", 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, normalMapTex);
litShader.setInt("normalMap", 1);
litShader.setBool("hasNormalMap", normalMapTex != 0);
player.draw();
// Disable normal map for everything else
litShader.setBool("hasNormalMap", false);
// ---- Ground ----
litShader.setMat4("transform", value_ptr(mat4(1.f)));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, groundTex);
litShader.setInt("tex0", 0);
glBindVertexArray(groundVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
// ---- Clue objects ----
for (int i = 0; i < 6; i++) {
if (!objects[i]) continue;
litShader.setMat4("transform", value_ptr(objects[i]->getModelMatrix()));
litShader.setInt("tex0", 0);
objects[i]->draw();
}
glfwSwapBuffers(window);
glfwPollEvents();
}
for (int i = 0; i < 6; i++) if (objects[i]) delete objects[i];
glDeleteVertexArrays(1, &groundVAO);
glDeleteBuffers(1, &groundVBO);
glDeleteTextures(1, &groundTex);
glDeleteTextures(1, &normalMapTex);
glfwTerminate();
return 0;
}