-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
365 lines (306 loc) · 12.1 KB
/
Copy pathmain.cpp
File metadata and controls
365 lines (306 loc) · 12.1 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
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <imgui/imgui.h>
#include <imgui/backends/imgui_impl_glfw.h>
#include <imgui/backends/imgui_impl_opengl3.h>
#include <stb_image/stb_image.h>
#include <iostream>
#include "Source/Model.h"
#include "Source/Camera.h"
#include "Source/Shader.h"
#include "Source/GameObject.h"
#include "Source/Scene.h"
#include "Source/Transform.h"
void framebufferSizeCallback(GLFWwindow* window, int width, int height);
void mouseCallback(GLFWwindow* window, double xpos, double ypos);
void scrollCallback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow* window);
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
void updateWireFrame();
void drawImGui();
void setupScene(Scene& scene);
float deltaTime = 0.0f;
double lastFrame = 0.0;
bool wireFrame = false;
bool captureMouse = false;
Scene scene;
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(scene.screenWidth, scene.screenHeight, "3DRenderer", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
glfwSetCursorPos(window, (float)scene.screenWidth / 2, (float)scene.screenHeight / 2);
glfwSetCursorPosCallback(window, mouseCallback);
glfwSetScrollCallback(window, scrollCallback);
glfwSetKeyCallback(window, keyCallback);
glfwSwapInterval(1);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glEnable(GL_DEPTH_TEST);
// Initialize ImGui
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330");
stbi_set_flip_vertically_on_load(true);
Shader shader("Assets/Shaders/vertex.vs", "Assets/Shaders/fragment.fs");
Shader lightShader("Assets/Shaders/vertex.vs", "Assets/Shaders/lightFragment.fs");
Shader tessShader("Assets/Shaders/vertex.vs", "Assets/Shaders/fragment.fs", "Assets/Shaders/tessControl.tcs", "Assets/Shaders/tessEval.tes");
setupScene(scene);
while (!glfwWindowShouldClose(window))
{
double currentFrame = glfwGetTime();
deltaTime = (float)(currentFrame - lastFrame);
lastFrame = currentFrame;
processInput(window);
scene.update(deltaTime);
scene.draw(shader, lightShader, tessShader);
drawImGui();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void processInput(GLFWwindow* window)
{
//if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
// glfwSetWindowShouldClose(window, true);
// Camera controls
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
scene.camera.processKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
scene.camera.processKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
scene.camera.processKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
scene.camera.processKeyboard(RIGHT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
scene.camera.processKeyboard(UP, deltaTime);
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
scene.camera.processKeyboard(DOWN, deltaTime);
}
void mouseCallback(GLFWwindow* window, double xpos, double ypos)
{
static double lastX = (float)scene.screenWidth / 2;
static double lastY = (float)scene.screenHeight / 2;
static bool firstMouse = true;
if (!captureMouse)
{
firstMouse = true;
return;
}
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
double xOffset = xpos - lastX;
double yOffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
scene.camera.processMouseMovement((float)xOffset, (float)yOffset);
}
void scrollCallback(GLFWwindow* window, double xoffset, double yoffset)
{
if (!captureMouse)
return;
scene.camera.processMouseScroll((float)yoffset);
}
void framebufferSizeCallback(GLFWwindow* window, int width, int height)
{
scene.screenWidth = width;
scene.screenHeight = height;
glViewport(0, 0, width, height);
}
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_F && action == GLFW_PRESS)
{
wireFrame = !wireFrame;
updateWireFrame();
}
if (key == GLFW_KEY_B && action == GLFW_PRESS)
{
scene.useBlinn = !scene.useBlinn;
}
if (key == GLFW_KEY_N && action == GLFW_PRESS)
{
scene.isDayLight = !scene.isDayLight;
scene.updateNight();
}
// Capture mouse on left control, cannot do this in processInput because this will be called only once
if (key == GLFW_KEY_LEFT_CONTROL && action == GLFW_PRESS)
{
captureMouse = !captureMouse;
if (captureMouse)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
else
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
// Change camera mode
if (key == GLFW_KEY_1 && action == GLFW_PRESS)
scene.camera.setMode(STATIC_SCENE);
if (key == GLFW_KEY_2 && action == GLFW_PRESS)
scene.camera.setMode(STATIC_TRACKING);
if (key == GLFW_KEY_3 && action == GLFW_PRESS)
scene.camera.setMode(ATTACHED);
if (key == GLFW_KEY_4 && action == GLFW_PRESS)
scene.camera.setMode(FREE);
}
void updateWireFrame()
{
if (wireFrame)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
void setupScene(Scene& scene)
{
Model* trainModel = new Model("Assets/Objects/GEVO/Gevo.obj", false);
Model* sphereModel = new Model("Assets/Objects/basics/sphere.obj");
Model* floorModel = new Model("Assets/Objects/basics/floor.obj");
Model* trexModel = new Model("Assets/Objects/trex/trex.obj");
scene.sphereModel = sphereModel;
Transform trainTransform;
trainTransform.scale = glm::vec3(1.0f);
auto train = std::make_unique<GameObject>(trainModel, trainTransform, "Train");
train->isMoving = true;
scene.gameObjects.push_back(std::move(train));
scene.camera.TargetTransform = &scene.gameObjects[0]->transform;
Transform floorTransform;
floorTransform.scale = glm::vec3(100, 0.0001f, 100);
scene.gameObjects.push_back(std::make_unique<GameObject>(floorModel, floorTransform, "Floor"));
Transform sphereTransform;
sphereTransform.position = glm::vec3(0.0f, 1.0f, 0.0f);
scene.gameObjects.push_back(std::make_unique<GameObject>(sphereModel, sphereTransform, "Sphere"));
Transform trexTransform;
trexTransform.position = glm::vec3(0.0f, -0.05f, 7.0f);
scene.gameObjects.push_back(std::make_unique<GameObject>(trexModel, trexTransform, "T-rex"));
Transform trex2Transform;
trex2Transform.position = glm::vec3(20.0f, -0.05f, -7.0f);
scene.gameObjects.push_back(std::make_unique<GameObject>(trexModel, trex2Transform, "T-rex2"));
Transform trex3Transform;
trex3Transform.position = glm::vec3(-40.0f, -0.05f, 7.0f);
scene.gameObjects.push_back(std::make_unique<GameObject>(trexModel, trex3Transform, "T-rex3"));
}
void drawImGui()
{
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::Begin("Settings");
if (ImGui::Checkbox("Wireframe (F)", &wireFrame))
{
updateWireFrame();
}
ImGui::Checkbox("Blinn (B)", &scene.useBlinn);
if (ImGui::Checkbox("Day/Night (N)", &scene.isDayLight))
{
scene.updateNight();
}
ImGui::SliderFloat("Fog Distance", &scene.fogDistance, 0.0f, 100.0f);
ImGui::ColorEdit3("Sky Color", &scene.skyColor.x);
ImGui::Text("FPS: %.1f", ImGui::GetIO().Framerate);
// Point Lights
if (ImGui::CollapsingHeader("Point Lights"))
{
for (size_t i = 0; i < scene.pointLights.size(); i++)
{
if (ImGui::TreeNode(("Point Light " + std::to_string(i + 1)).c_str()))
{
ImGui::SliderFloat3(("Position##" + std::to_string(i)).c_str(), &scene.pointLights[i].position.x, -10.0f, 10.0f);
ImGui::SliderFloat(("Constant##" + std::to_string(i)).c_str(), &scene.pointLights[i].constant, 0.0f, 1.0f);
ImGui::SliderFloat(("Linear##" + std::to_string(i)).c_str(), &scene.pointLights[i].linear, 0.0f, 0.1f);
ImGui::SliderFloat(("Quadratic##" + std::to_string(i)).c_str(), &scene.pointLights[i].quadratic, 0.0f, 0.1f);
ImGui::ColorEdit3(("Ambient##" + std::to_string(i)).c_str(), &scene.pointLights[i].ambient.x);
ImGui::ColorEdit3(("Diffuse##" + std::to_string(i)).c_str(), &scene.pointLights[i].diffuse.x);
ImGui::ColorEdit3(("Specular##" + std::to_string(i)).c_str(), &scene.pointLights[i].specular.x);
ImGui::TreePop();
}
}
}
if (ImGui::CollapsingHeader("Directional Light"))
{
ImGui::SliderFloat3("Direction##", &scene.dirLight.direction.x, -10.0f, 10.0f);
ImGui::ColorEdit3("Ambient##", &scene.dirLight.ambient.x);
ImGui::ColorEdit3("Diffuse##", &scene.dirLight.diffuse.x);
ImGui::ColorEdit3("Specular##", &scene.dirLight.specular.x);
}
if (ImGui::CollapsingHeader("Train Spot Light"))
{
ImGui::SliderFloat3("Train light direction", &scene.trainLightDirection.x, -1.0f, 1.0f);
ImGui::SliderFloat("Cut Off", &scene.spotLight.cutOff, 0.0f, 90.0f);
ImGui::SliderFloat("Outer Cut Off", &scene.spotLight.outerCutOff, 0.0f, 90.0f);
ImGui::SliderFloat("Constant", &scene.spotLight.constant, 0.0f, 1.0f);
ImGui::SliderFloat("Linear", &scene.spotLight.linear, 0.0f, 0.1f);
ImGui::SliderFloat("Quadratic", &scene.spotLight.quadratic, 0.0f, 0.1f);
ImGui::ColorEdit3("Ambient", &scene.spotLight.ambient.x);
ImGui::ColorEdit3("Diffuse", &scene.spotLight.diffuse.x);
ImGui::ColorEdit3("Specular", &scene.spotLight.specular.x);
}
if (ImGui::CollapsingHeader("Objects"))
{
for (auto& obj : scene.gameObjects)
{
if (ImGui::TreeNode(obj->name.c_str()))
{
ImGui::SliderFloat3("Position", &obj->transform.position.x, -20.0f, 20.0f);
ImGui::SliderFloat3("Rotation", &obj->transform.rotation.x, 0.0f, 360.0f);
ImGui::SliderFloat3("Scale", &obj->transform.scale.x, 0.1f, 20.0f);
ImGui::TreePop();
}
}
}
// Train movement
if (!scene.gameObjects.empty())
{
ImGui::Text("Train Movement");
ImGui::Checkbox("Enable Movement", &scene.gameObjects[0]->isMoving);
ImGui::SliderFloat("Movement Radius", &scene.gameObjects[0]->radius, 1.0f, 50.0f);
ImGui::SliderFloat("Movement Speed", &scene.gameObjects[0]->speed, 0.1f, 2.0f);
}
if (ImGui::CollapsingHeader("Bezier Patch"))
{
ImGui::SliderFloat3("Position##Bezier", &scene.bezierTransform.position.x, -20.0f, 20.0f);
ImGui::SliderFloat3("Rotation##Bezier", &scene.bezierTransform.rotation.x, 0.0f, 360.0f);
ImGui::SliderFloat3("Scale##Bezier", &scene.bezierTransform.scale.x, 0.1f, 20.0f);
}
ImGui::SliderFloat("Tessellation Level", &scene.tessLevel, 1.0f, 64.0f);
ImGui::End();
ImGui::Begin("Controls");
ImGui::Text("[1] - Static camera");
ImGui::Text("[2] - Static tracking camera");
ImGui::Text("[3] - Train camera");
ImGui::Text("[4] - Free camera");
ImGui::Text("Camera Controls:");
ImGui::Text("WASD - Move Camera");
ImGui::Text("Space - Move Up");
ImGui::Text("Left Shift - Move Down");
ImGui::Text("Left Control - Capture Mouse");
ImGui::Text("Scroll - Zoom");
ImGui::Text("F - Toggle Wireframe");
ImGui::Text("B - Toggle Blinn-Phong");
ImGui::Text("N - Toggle Day/Night");
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}