-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
285 lines (229 loc) · 10.3 KB
/
main.cpp
File metadata and controls
285 lines (229 loc) · 10.3 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
#include <iostream>
#include <algorithm>
#include "pgr-portable.h"
#include "meshes/mesh.h"
#include "meshes/model/objMesh.h"
#include "meshes/label/textLabel.h"
#include "meshes/label/imageSequenceLabel.h"
#include "geometry/camera.h"
#include "light/light.h"
#include "parser/input.h"
#include "shaders/shader.h"
#include "animation/spline.h"
#include "animation/catmullroll.h"
#include "animation/bezier.h"
#include "meshes/collision/collisionBox.h"
#include "meshes/collision/collisionSphere.h"
#include "meshes/collision/rigidBody.h"
#include "meshes/collision/rigidSphere.h"
#include "meshes/label/imageLabel.h"
#include "meshes/label/imageMoving.h"
#include "meshes/model/skybox.h"
#include "scenes/sample/sampleScene.h"
#include "scenes/template/templateScene.h"
#include "scenes/bowling/bowlingScene.h"
#include "scenes/scene.h"
/** @brief PGR Semestral work with sample scenes and bowling.
* doxygen was generated with a help of LLM */
namespace copakond {
void menuCallback(int option);
float SIMULATION_SPEED = 1.0f;
int winWidth = 1280;
int winHeight = 720;
Shader* shader;
Input* input;
Scene* currentScene;
/** @brief Initializes OpenGL context, scene data, and sets up GLUT callbacks */
void init() {
glutCreateMenu(menuCallback);
glutAddMenuEntry("Player Cam", 1);
glutAddMenuEntry("Free Cam", 2);
glutAddMenuEntry("Catmull Roll Cam", 3);
glutAddMenuEntry("Static Cam 1", 4);
glutAddMenuEntry("Static Cam 2", 5);
glutAddMenuEntry("Full Screen", 6);
glutAddMenuEntry("Exit Game", 7);
glutAttachMenu(GLUT_RIGHT_BUTTON);
Camera *camera = new Camera(
glm::vec3(0.0f, 0.0f, 5.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
1000.0f
);
input = new Input(*camera, winWidth, winHeight);
shader = new Shader();
//currentScene = new TemplateScene("Template Scene", input, shader, winWidth, winHeight);
//currentScene = new SampleScene("Sample Scene", input, shader, winWidth, winHeight);
currentScene = new BowlingScene("Bowling Scene", input, shader, winWidth, winHeight);
GLuint shaderUID = shader->init(
"shaders/shaders/shader.vert",
"shaders/shaders/shader.frag"
);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glViewport(0, 0, glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
currentScene->init();
for (Mesh *mesh: currentScene->getMeshes()) {
mesh->init(shaderUID);
}
std::vector<Light*> lights = currentScene->getLights();
for (size_t i = 0; i < lights.size(); i++) {
shader->setLight(lights[i], i);
}
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glCullFace( GL_BACK);
glEnable(GL_CULL_FACE);
// enable stencil test for mouse clicking
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
}
/** @brief Main render loop. Handles updates, physics, and rendering of all scene elements */
void draw() {
Camera camera = currentScene->getCamera();
float deltaTime = currentScene->updateTime() * SIMULATION_SPEED; // calculate delta time
// update spline animations
for (Spline *spline: currentScene->getSplines()) {
spline->update(deltaTime);
}
// update lights
for (Light *light: currentScene->getLights()) {
shader->updateLight(light);
}
// process physics
std::vector<CollisionShape*> colliders = currentScene->getColliders();
for (CollisionShape *collider: colliders) {
auto *rigBody = dynamic_cast<RigidBody*>(collider);
auto *rigSphere = dynamic_cast<RigidSphere*>(collider);
if (rigBody != nullptr) { rigBody->physics_process(deltaTime, colliders); }
if (rigSphere != nullptr) { rigSphere->physics_process(deltaTime, colliders); }
}
currentScene->physics_update(deltaTime);
currentScene->update(deltaTime);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// sort all meshes, from the furthest to the nearest (for transparent meshes), all meshes, could be optimized more...
std::vector<Mesh*> meshes = currentScene->getMeshes();
glm::vec3 camPos = camera.getPosition();
std::sort(meshes.begin(), meshes.end(), [&camPos](Mesh* a, Mesh* b) {
glm::vec3 pos1 = glm::vec3(a->getModelMatrix()[3]); // position
glm::vec3 pos2 = glm::vec3(b->getModelMatrix()[3]); // position
return glm::distance(camPos, pos1) > glm::distance(camPos, pos2);
});
// draw Non-transparent Meshes
shader->update(camera, winWidth, winHeight, deltaTime); // use main shader
for (Mesh *mesh: meshes) {
if (mesh->getMaterial()->getAlpha() <= 0.999f) { continue; }
if (dynamic_cast<const TextLabel*>(mesh) != nullptr) { continue; }
if (stencilMode == StencilSelect::ALL) {
glStencilFunc(GL_ALWAYS, mesh->getId(), 0);
} else if (stencilMode == StencilSelect::MESHES) {
if (dynamic_cast<const CollisionShape*>(mesh) != nullptr) { continue; }
glStencilFunc(GL_ALWAYS, mesh->getId(), 0);
} else if (stencilMode == StencilSelect::COLLISION) {
if (dynamic_cast<const CollisionShape*>(mesh) == nullptr) { continue; }
glStencilFunc(GL_ALWAYS, mesh->getId(), 0);
}
shader->draw(*mesh, false, deltaTime); // drawing non-transparent objects
}
// draw skybox
Skybox *skybox = currentScene->getSkybox();
float skyboxBlending = currentScene->getSkyboxBlendingCoeff();
if (skybox) {
glStencilFunc(GL_ALWAYS, 1, 0); // id = 1 is for skybox
skybox->update(camera, winWidth, winHeight, deltaTime, skyboxBlending); // use skybox shader
skybox->draw(deltaTime);
}
// draw Transparent Meshes
glEnable(GL_BLEND);
glDepthMask(GL_FALSE); // if the front triangle would render before the back it would fail
shader->update(camera, winWidth, winHeight, 0.0f); // use main shader
for (Mesh *mesh: meshes) {
if (mesh->getMaterial()->getAlpha() > 0.9999f) { continue; }
if (stencilMode == StencilSelect::ALL) {
glStencilFunc(GL_ALWAYS, mesh->getId(), 0);
} else if (stencilMode == StencilSelect::MESHES) {
if (dynamic_cast<const CollisionShape*>(mesh) != nullptr) { continue; }
glStencilFunc(GL_ALWAYS, mesh->getId(), 0);
} else if (stencilMode == StencilSelect::COLLISION) {
if (dynamic_cast<const CollisionShape*>(mesh) == nullptr) { continue; }
glStencilFunc(GL_ALWAYS, mesh->getId(), 0);
}
shader->draw(*mesh, true, deltaTime); // drawing transparent objects
}
glDepthMask(GL_TRUE); // restore
glDisable(GL_BLEND);
glutSwapBuffers(); // swap front and back screen buffer
glutPostRedisplay(); // !!!!!!!!! schedules display, doesnt stack!!!
}
void menuCallback(int option) {
if (option == 7) {
exit(0);
}
if (currentScene) { currentScene->onMenuEvent(option); }
}
void keyboardInputEvent(unsigned char key, int x, int y) {
input->keyboardInputEvent(key, x, y);
if (currentScene) { currentScene->onKeyboardEvent(key, x, y, true); }
}
void specKeyboardInputEvent(int key, int x, int y) {
input->specKeyboardInputEvent(key, x, y);
if (currentScene) { currentScene->onKeyboardEvent(key, x, y, true); }
}
void keyboardUpInputEvent(unsigned char key, int x, int y) {
input->keyboardUpInputEvent(key, x, y);
}
void specKeyboardUpInputEvent(int key, int x, int y) {
input->specKeyboardUpInputEvent(key, x, y);
}
void mouseButtonEvent(int button, int state, int x, int y) {
input->mouseButtonEvent(button, state, x, y);
if (currentScene) { currentScene->onMouseButtonEvent(button, state, x, y); }
}
void mouseMoveEvent(int x, int y) {
input->mouseMoveEvent(x, y);
if (currentScene) { currentScene->onMouseMoveEvent(x, y); }
}
void mouseWheelEvent(int wheel, int direction, int x, int y) {
input->mouseWheelEvent(wheel, direction, x, y);
if (currentScene) { currentScene->onMouseWheelEvent(wheel, direction, x, y); }
}
/** @brief Handles window resize events to maintain correct projection aspect ratio */
void screenResizeEvent(int width, int height) {
if (width == 0) width = 1; // sadly it can be 0 for some reason.
if (height == 0) height = 1;
winWidth = width;
winHeight = height;
glViewport(0, 0, width, height);
input->screenResize(winWidth, winHeight);
if (currentScene) { currentScene->onScreenResizeEvent(width, height); }
}
}
using namespace copakond;
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitContextVersion(pgr::OGL_VER_MAJOR, pgr::OGL_VER_MINOR);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("PGR Semestral Work Copakond");
// INPUT - keyboard and mouse event callbacks
glutKeyboardFunc(keyboardInputEvent);
glutSpecialFunc(specKeyboardInputEvent);
glutKeyboardUpFunc(keyboardUpInputEvent);
glutSpecialUpFunc(specKeyboardUpInputEvent);
glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF); // turn of the spammign of character when holding it down (w ______ wwwwww)
glutMouseFunc(mouseButtonEvent);
glutPassiveMotionFunc(mouseMoveEvent);
glutMotionFunc(mouseMoveEvent);
glutMouseWheelFunc(mouseWheelEvent);
glutReshapeFunc(screenResizeEvent);
//glutSetCursor(GLUT_CURSOR_NONE); // hide cursor
// SET DRAW CALLBACK
glutDisplayFunc(draw);
if (!pgr::initialize(pgr::OGL_VER_MAJOR, pgr::OGL_VER_MINOR)) {
pgr::dieWithError("pgr init failed, required OpenGL not supported?");
}
init();
glutMainLoop();
return 0;
}