-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hpp
470 lines (389 loc) · 13 KB
/
Main.hpp
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
/*/////////////////////////////////////////////////////////////////////////////
NOTE:
Add a copy of [irrKlang.dll] and [ikpMP3.dll] from libs/irrKlang to the build
folder for Visual Studio to run/debug with sound/music feature.
/////////////////////////////////////////////////////////////////////////////*/
/*C++*/
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <stdint.h>
#include <cstring>
#include <thread>
#include <algorithm>
#include <vector>
/*/////////////////////////////////////////////////////////////////////////////*/
#define _USE_MATH_DEFINES
#include <cmath>
#ifndef M_PI
#define M_PI (3.14159265358979323846)
#endif
#ifndef M_PIl
#define M_PIl (3.14159265358979323846264338327950288)
#endif
/*/////////////////////////////////////////////////////////////////////////////*/
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#define GLM_ENABLE_EXPERIMENTAL
#include "glm/gtx/string_cast.hpp"
/*/////////////////////////////////////////////////////////////////////////////*/
#include "core/ShaderGL.hpp"
#include "core/ModelLoader.hpp"
#include "core/MeshShaderGL.hpp"
#include "core/ModelGL.hpp"
#include "core/MeshGL.hpp"
#include "core/Camera.hpp"
#include "core/Light.hpp"
#include "core/MousePicker.hpp"
#include "physics/Collide.hpp"
#include "physics/Actor.hpp"
#include "sound/Sound.hpp"
/*Dear ImGui*/
#include "libs/dearimgui/imgui.h"
#include "libs/dearimgui/imgui_impl_glfw.h"
#include "libs/dearimgui/imgui_impl_opengl3.h"
#include "libs/dearimgui/imconfig.h"
#include "libs/dearimgui/imgui_internal.h"
#include "libs/dearimgui/imstb_rectpack.h"
#include "libs/dearimgui/imstb_textedit.h"
#include "libs/dearimgui/imstb_truetype.h"
/*/////////////////////////////////////////////////////////////////////////////*/
#define STB_IMAGE_IMPLEMENTATION
#include "libs/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "libs/stb_image_write.h"
/*imfilebrowser*/
#include "libs/imfilebrowser.h"
/*/////////////////////////////////////////////////////////////////////////////*/
using namespace std;
/*///////////////////////////////////////////////////////////////////////////////
GLOBALS
///////////////////////////////////////////////////////////////////////////////*/
GLFWwindow* window = NULL;
int windowWidth = 1920;
int windowHeight = 1080;
int framebufferWidth = windowWidth;
int framebufferHeight = windowHeight;
bool firstMouseMove = true;
double mouseX = -1;
double mouseY = -1;
bool leftMouseButtonDown = false;
static double cursor_pos_x = 0;
static double cursor_pos_y = 0;
static double delta_x = 0;
static double delta_y = 0;
int how_many_objs = 0;
int num_sound_objs = 0;
const float TRANSLATION_INC = 0.1;
const float CAMERA_WALK_SPEED = .1f;
const float CAMERA_ROTATE_SPEED = 30.0f;
glm::vec3 up1 = glm::vec3(0, 1, 0);
glm::vec3 eye1 = glm::vec3(-2, 2, 2);
glm::vec3 lookAt1 = glm::vec3(1, 0, 0);
float fov1 = 60.0f;
float nearPlane1 = 0.01f;
float farPlane1 = 30.0f;
int bufferWidth1 = framebufferWidth;
int bufferHeight1 = framebufferHeight;
Camera* camera = new Camera(eye1, lookAt1, up1, fov1, nearPlane1, farPlane1, bufferWidth1, bufferHeight1);
glm::vec3 pos1 = glm::vec3(0, 3, 2);
glm::vec3 color1 = glm::vec3(1, 1, 1);
bool isPtLt = true;
Light* light = new Light(pos1, color1, isPtLt);
float shiny = 10.0;
int materialCh = 0;
MeshShaderGL *shader;
MeshShaderGL* shaderBox;
ModelData* modelData;
ModelData* tempModelData;
ModelGL* modelGL;
Collide* collide;
Actor* anActor;
MousePicker* mousePick = new MousePicker(camera);
ModelGL *temp;
ModelGL *tempMove;
vector<Actor*> objFiles;
vector<Sound*> soundFiles;
bool rightMouseDown = false;
glm::mat4 tempModelMatrix;
glm::mat4 tempTransform;
string fileNameNew;
string fileNameNew1;
char aFileN[64] = "";
char aFileN1[64] = "";
string aFName;
int locationOfSpecificSound;
// State
bool show_file_load_window = false;
bool show_file_save = false;
bool load_an_obj_file = false;
bool show_controls = false;
bool add_sound = false;
bool stop_all_sounds = false;
bool volumeDown = false;
bool volumeUp = false;
bool pauseSound = false;
bool adjust_specific_sound = false;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
std::string ext;
// Finds file name from file path
string getFileName(const string& s) {
char sep = '/';
#ifdef _WIN32
sep = '\\';
#endif
size_t i = s.rfind(sep, s.length());
if (i != string::npos) {
return(s.substr(i + 1, s.length() - i));
}
return("");
}
static std::string GetFilePathExtension(const std::string& FileName) {
if (FileName.find_last_of(".") != std::string::npos)
return FileName.substr(FileName.find_last_of(".") + 1);
return "";
}
/*///////////////////////////////////////////////////////////////////////////////
FUNCTIONS - GLFW Callbacks
///////////////////////////////////////////////////////////////////////////////*/
// GLFW error callback
static void error_callback(int error, const char* description) {
cerr << "ERROR " << error << ": " << description << endl;
}
// GLFW key callback
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (action == GLFW_PRESS || action == GLFW_REPEAT) {
switch (key) {
case GLFW_KEY_SPACE:
if (how_many_objs >= 1) {
for (int i = 0; i < objFiles.size(); i++) {
tempMove = objFiles[i]->returnModelGL();
tempMove->reset();
}
}
break;
case GLFW_KEY_U:
if (how_many_objs >= 1) {
for (int i = 0; i < objFiles.size(); i++) {
tempMove = objFiles[i]->returnModelGL();
tempMove->rotate(5.0f, glm::vec3(0, 0, 1)); //rotate modelGL by positive 5 degrees, z axis
}
}
break;
case GLFW_KEY_O:
if (how_many_objs >= 1) {
for (int i = 0; i < objFiles.size(); i++) {
tempMove = objFiles[i]->returnModelGL();
tempMove->rotate(-5.0f, glm::vec3(0, 0, 1)); //rotate modelGL by negative 5 degrees, z axis
}
}
break;
case GLFW_KEY_E:
if (how_many_objs >= 1) {
for (int i = 0; i < objFiles.size(); i++) {
tempMove = objFiles[i]->returnModelGL();
tempMove->rotate(5.0f, glm::vec3(1, 0, 0)); //rotate modelGL by positive 5 degrees, x axis
}
}
break;
case GLFW_KEY_R:
if (how_many_objs >= 1) {
for (int i = 0; i < objFiles.size(); i++) {
tempMove = objFiles[i]->returnModelGL();
tempMove->rotate(-5.0f, glm::vec3(1, 0, 0)); //rotate modelGL by negative 5 degrees, x axis
}
}
break;
case GLFW_KEY_T:
if (how_many_objs >= 1) {
for (int i = 0; i < objFiles.size(); i++) {
tempMove = objFiles[i]->returnModelGL();
tempMove->rotate(5.0f, glm::vec3(0, 1, 0)); //rotate modelGL by positive 5 degrees, y axis
}
}
break;
case GLFW_KEY_Y:
if (how_many_objs >= 1) {
for (int i = 0; i < objFiles.size(); i++) {
tempMove = objFiles[i]->returnModelGL();
tempMove->rotate(-5.0f, glm::vec3(0, 1, 0)); //rotate modelGL by negative 5 degrees, y axis
}
}
break;
case GLFW_KEY_I:
if (how_many_objs >= 1) {
for (int i = 0; i < objFiles.size(); i++) {
tempMove = objFiles[i]->returnModelGL();
tempMove->translate(glm::vec3(0, TRANSLATION_INC, 0)); //translate modelGL by positive TRANSLATION_INC in Y
}
}
break;
case GLFW_KEY_K:
if (how_many_objs >= 1) {
for (int i = 0; i < objFiles.size(); i++) {
tempMove = objFiles[i]->returnModelGL();
tempMove->translate(glm::vec3(0, -TRANSLATION_INC, 0)); //translate modelGL by negative TRANSLATION_INC in Y
}
}
break;
case GLFW_KEY_L:
if (how_many_objs >= 1) {
for (int i = 0; i < objFiles.size(); i++) {
tempMove = objFiles[i]->returnModelGL();
tempMove->translate(glm::vec3(TRANSLATION_INC, 0, 0)); //translate modelGL by positive TRANSLATION_INC in X
}
}
break;
case GLFW_KEY_J:
if (how_many_objs >= 1) {
for (int i = 0; i < objFiles.size(); i++) {
tempMove = objFiles[i]->returnModelGL();
tempMove->translate(glm::vec3(-TRANSLATION_INC, 0, 0)); //translate modelGL by negative TRANSLATION_INC in X
}
}
break;
// Camera keys
case GLFW_KEY_W:
camera->forward(CAMERA_WALK_SPEED);
break;
case GLFW_KEY_S:
camera->forward(-CAMERA_WALK_SPEED);
break;
case GLFW_KEY_D:
camera->strafeRight(CAMERA_WALK_SPEED);
break;
case GLFW_KEY_A:
camera->strafeRight(-CAMERA_WALK_SPEED);
break;
// Extra
case GLFW_KEY_C:
if (light->getIsPointLight()) {
light->setIsPointLight(false);
}
else {
light->setIsPointLight(true);
}
break;
case GLFW_KEY_0:
materialCh = 0;
shader->setMaterialChoice(materialCh);
break;
//////////////////////////original look//////////////////////////
case GLFW_KEY_1:
materialCh = 1;
shader->setMaterialChoice(materialCh);
break;
/////////////////////////////////////////////////////////////////
case GLFW_KEY_V:
shiny *= 5.0;
shader->setShininess(shiny);
break;
case GLFW_KEY_B:
shiny /= 5.0;
if (shiny < 10.0) {
shiny = 10.0;
}
shader->setShininess(shiny);
break;
case GLFW_KEY_N:
glm::vec3 blue1 = glm::vec3(0, 0, 1);
light->setColor(blue1); //blue
break;
//////////////////////////original look//////////////////////////
case GLFW_KEY_M:
glm::vec3 white1 = glm::vec3(1, 1, 1);
light->setColor(white1);
break;
/////////////////////////////////////////////////////////////////
default:
break;
}
}
}
//Code modified from:////////////////////////////////////////////////////////////////////////////////////////////////////////
//https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-box-intersection
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool intersect(glm::vec3 rayOrg, glm::vec3 rayDir, glm::vec3 centerTemp, glm::vec3 sizeTemp)
{
glm::vec3 min;
glm::vec3 max;
min = centerTemp - sizeTemp/2.0f;
max = centerTemp + sizeTemp/2.0f;
cout << "min" << glm::to_string(min) << "max" << glm::to_string(max) << endl;
float tmin = (min.x - rayOrg.x) / rayDir.x;
float tmax = (max.x - rayOrg.x) / rayDir.x;
if (tmin > tmax) swap(tmin, tmax);
float tymin = (min.y - rayOrg.y) / rayDir.y;
float tymax = (max.y - rayOrg.y) / rayDir.y;
if (tymin > tymax) swap(tymin, tymax);
if ((tmin > tymax) || (tymin > tmax))
return false;
if (tymin > tmin)
tmin = tymin;
if (tymax < tmax)
tmax = tymax;
float tzmin = (min.z - rayOrg.z) / rayDir.z;
float tzmax = (max.z - rayOrg.z) / rayDir.z;
if (tzmin > tzmax) swap(tzmin, tzmax);
if ((tmin > tzmax) || (tzmin > tmax))
return false;
if (tzmin > tmin)
tmin = tzmin;
if (tzmax < tmax)
tmax = tzmax;
return true;
}
//------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------
// GLFW callback mouse position ------------> Last functionality still a work in progress. <------------
//------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------
static void mouse_position_callback(GLFWwindow* window, double xpos, double ypos) {
if (rightMouseDown == true) {
/*double v, w;
glfwGetCursorPos(window, &v, &w); //get screen coordinates from cursor
mousePick->saveCoordX(v);
mousePick->saveCoordY(w);
mousePick->updateBufferW(framebufferWidth);
mousePick->updateBufferH(framebufferHeight);
mousePick->update();
glm::vec3 currentRayAtTheMoment = mousePick->getCurrentRay(); //mouse ray
currentRayAtTheMoment = glm::normalize(currentRayAtTheMoment);
glm::vec3 currentEye = camera->getEye();
if (how_many_objs >= 1) {
for (int i = 0; i < objFiles.size(); i++) {
ModelData* tempModelDataM = objFiles[i]->returnModelData(); //get modeldata
glm::vec3 centerTemp = tempModelDataM->getCenterBB();
glm::vec3 sizeTemp = tempModelDataM->getSizeBB();
bool hitObject = intersect(currentEye, currentRayAtTheMoment, centerTemp, sizeTemp);
}
}*/
}
}
// GLFW callback mouse button
static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
rightMouseDown = true;
}
else if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_RELEASE) {
rightMouseDown = false;
}
}
// GLFW callback when the window changes size
void window_size_callback(GLFWwindow* window, int width, int height) {
windowWidth = width;
windowHeight = height;
}
// GLFW callback when the framebuffer changes size
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
framebufferWidth = width;
framebufferHeight = height;
}