-
Notifications
You must be signed in to change notification settings - Fork 1
/
newtons_cradle.cpp
308 lines (259 loc) · 12.2 KB
/
newtons_cradle.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
#include "vendor/imgui/imgui.h"
#include "vendor/imgui/imgui_impl_glfw.h"
#include "vendor/imgui/imgui_impl_opengl3.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "src/Renderer.hpp"
void logString(const std::string& s);
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window, Camera& camera);
const unsigned int SCR_WIDTH = 1280;
const unsigned int SCR_HEIGHT = 720;
bool firstMouse = true;
float lastX = (float)SCR_WIDTH / 2.0;
float lastY = (float)SCR_HEIGHT / 2.0;
float fov = 45.0f;
float deltaTime = 0.0f; // time between current frame and last frame
float lastFrame = 0.0f;
static void glfw_error_callback(int error, const char* description) {
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}
const float PLANE_SCALE = 10.0f;
const int NUM_SPHERES = 5;
const float SPHERE_RADIUS = 2.0f;
const float BOUNDING_BOX_DIST = 50.0f;
int main() {
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit())
return 1;
const char* glsl_version = "#version 130";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "MoonShot", NULL, NULL);
if (window == NULL) {
std::cout << "Window creation failed" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync
GLenum err = glewInit();
if (err != GLEW_OK) {
cout << "glewInit failed: " << glewGetErrorString(err) << endl;
exit(1);
}
glEnable(GL_DEPTH_TEST);
Renderer renderer = Renderer(PerpectiveProperties(SCR_WIDTH, SCR_HEIGHT), 0.01, glm::vec3(60.0f, 60.0f, 0.0f));
Scene* scene = renderer.getScene();
CollisionPhysx physx = CollisionPhysx();
scene->attachPhysics(&physx);
Plane groundPlane = Plane("/home/karthikrangasai/Documents/Acads/4th Year/4 - 2/IS F311 Comp Graphics/assignment/assignment_2/problem_statement/plane.obj");
groundPlane._translation[1] = -1.0f * SPHERE_RADIUS;
groundPlane._scale[0] = groundPlane._scale[1] = groundPlane._scale[2] = PLANE_SCALE;
groundPlane.updateTransforms();
groundPlane.meshes[0].material.setDiffuseColor(glm::vec3(1.0f, 1.0f, 1.0f));
vector<Sphere> spheres;
for (int i = 0; i < NUM_SPHERES; ++i) {
spheres.push_back(Sphere(SPHERE_RADIUS, 30));
spheres[i]._translation[0] = 0.0f;
spheres[i]._translation[1] = 0.0f;
spheres[i]._translation[2] = i * 2 * SPHERE_RADIUS;
spheres[i].updateTransforms();
spheres[i].meshes[0].material.setDiffuseColor(glm::vec3((1.0f * rand()) / RAND_MAX, (1.0f * rand()) / RAND_MAX, (1.0f * rand()) / RAND_MAX));
}
Sphere mainSphere = Sphere(SPHERE_RADIUS, 30);
mainSphere._translation[0] = 0.0f;
mainSphere._translation[1] = 0.0f;
mainSphere._translation[2] = -10.0f * SPHERE_RADIUS;
mainSphere.updateTransforms();
mainSphere.meshes[0].material.setDiffuseColor(glm::vec3((1.0f * rand()) / RAND_MAX, (1.0f * rand()) / RAND_MAX, (1.0f * rand()) / RAND_MAX));
Plane wallOne = Plane("/home/karthikrangasai/Documents/Acads/4th Year/4 - 2/IS F311 Comp Graphics/assignment/assignment_2/problem_statement/plane.obj");
wallOne._translation[2] = -BOUNDING_BOX_DIST;
wallOne._scale[0] = wallOne._scale[1] = wallOne._scale[2] = PLANE_SCALE;
wallOne._rotation[0] = 90.0f;
wallOne.updateTransforms();
wallOne.meshes[0].material.setDiffuseColor(glm::vec3((1.0f * rand()) / RAND_MAX, (1.0f * rand()) / RAND_MAX, (1.0f * rand()) / RAND_MAX));
Plane wallTwo = Plane("/home/karthikrangasai/Documents/Acads/4th Year/4 - 2/IS F311 Comp Graphics/assignment/assignment_2/problem_statement/plane.obj");
wallTwo._translation[0] = -BOUNDING_BOX_DIST;
wallTwo._scale[0] = wallTwo._scale[1] = wallTwo._scale[2] = PLANE_SCALE;
wallTwo._rotation[2] = 90.0f;
wallTwo.updateTransforms();
wallTwo.meshes[0].material.setDiffuseColor(glm::vec3((1.0f * rand()) / RAND_MAX, (1.0f * rand()) / RAND_MAX, (1.0f * rand()) / RAND_MAX));
Plane wallThree = Plane("/home/karthikrangasai/Documents/Acads/4th Year/4 - 2/IS F311 Comp Graphics/assignment/assignment_2/problem_statement/plane.obj");
wallThree._translation[0] = BOUNDING_BOX_DIST;
wallThree._scale[0] = wallThree._scale[1] = wallThree._scale[2] = PLANE_SCALE;
wallThree._rotation[2] = -90.0f;
wallThree.updateTransforms();
wallThree.meshes[0].material.setDiffuseColor(glm::vec3((1.0f * rand()) / RAND_MAX, (1.0f * rand()) / RAND_MAX, (1.0f * rand()) / RAND_MAX));
Plane wallFour = Plane("/home/karthikrangasai/Documents/Acads/4th Year/4 - 2/IS F311 Comp Graphics/assignment/assignment_2/problem_statement/plane.obj");
wallFour._translation[2] = BOUNDING_BOX_DIST;
wallFour._scale[0] = wallFour._scale[1] = wallFour._scale[2] = PLANE_SCALE;
wallFour._rotation[0] = -90.0f;
wallFour.updateTransforms();
wallFour.meshes[0].material.setDiffuseColor(glm::vec3((1.0f * rand()) / RAND_MAX, (1.0f * rand()) / RAND_MAX, (1.0f * rand()) / RAND_MAX));
scene->addModel(&groundPlane);
for (int i = 0; i < NUM_SPHERES; ++i) {
scene->addModel(&spheres[i]);
}
scene->addModel(&mainSphere);
PhysxObject gPlane = PhysxObject(PhysxShape::PLANE, &groundPlane, 2.0f, glm::vec3(0, 0, 0));
PhysxObject gWallOne = PhysxObject(PhysxShape::PLANE, &wallOne, 2.0f, glm::vec3(0, 0, 0));
PhysxObject gwallTwo = PhysxObject(PhysxShape::PLANE, &wallTwo, 2.0f, glm::vec3(0, 0, 0));
PhysxObject gwallThree = PhysxObject(PhysxShape::PLANE, &wallThree, 2.0f, glm::vec3(0, 0, 0));
PhysxObject gwallFour = PhysxObject(PhysxShape::PLANE, &wallFour, 2.0f, glm::vec3(0, 0, 0));
vector<PhysxObject> spherePhysx;
for (int i = 0; i < NUM_SPHERES; ++i) {
spherePhysx.push_back(PhysxObject(PhysxShape::SPHERE, &spheres[i], 5.0f, glm::vec3(0.0f, 0.0f, 0.0f)));
}
PhysxObject mainSpherePhysx = PhysxObject(PhysxShape::SPHERE, &mainSphere, 5.0f, glm::vec3(0, 0, 15.0f));
physx.addObject(&gPlane);
physx.addObject(&gWallOne);
physx.addObject(&gwallTwo);
physx.addObject(&gwallThree);
physx.addObject(&gwallFour);
for (int i = 0; i < NUM_SPHERES; ++i) {
physx.addObject(&spherePhysx[i]);
}
physx.addObject(&mainSpherePhysx);
scene->isPhysicsOn = false;
// ImGui Setup
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
(void)io;
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f);
glUseProgram(renderer.shader.ID);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window, renderer.camera);
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
if (ImGui::Begin("Settings")) {
{
ImGui::SliderFloat3("Light Position", renderer.scene.light._position, -60.0f, 60.0f);
renderer.scene.light.updateLighting();
}
ImGui::Separator();
for (unsigned int i = 0; i < renderer.scene.models.size(); ++i) {
char v[] = "Model 10";
snprintf(v, (5 + 1 + 2), "Model %d", (i + 1));
ImGui::Checkbox(v, &(renderer.scene.models[i]->visibility));
if ((i + 1) != renderer.scene.models.size()) {
ImGui::SameLine();
}
}
ImGui::Separator();
static int modelNumber = 0;
for (unsigned int i = 0; i < renderer.scene.models.size(); ++i) {
char v[] = "Model 10";
snprintf(v, (5 + 1 + 2), "Model %d", (i + 1));
ImGui::RadioButton(v, &modelNumber, i);
if ((i + 1) != renderer.scene.models.size()) {
ImGui::SameLine();
}
}
ImGui::Separator();
{
ImGui::BeginChild("Camera Properties Child", ImVec2(0, 100), true);
ImGui::Text("Camera Properties");
static float speed = 2.5f;
static float sensitivity = 0.05f;
ImGui::SliderFloat("Camera Speed", &speed, 1.0f, 5.0f);
ImGui::SliderFloat("Camera Sensitivity", &sensitivity, 0.01f, 5.0f);
renderer.camera.updateCameraSpeed(speed);
renderer.camera.updateCameraSensitivity(sensitivity);
ImGui::EndChild();
}
ImGui::Separator();
if (ImGui::Button("Toggle Physics")) {
renderer.scene.isPhysicsOn = !renderer.scene.isPhysicsOn;
}
ImGui::Separator();
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
}
renderer.renderAll();
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
void processInput(GLFWwindow* window, Camera& camera) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
camera.processKeyboard(Camera_Movement::PINNED_LEFT, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
camera.processKeyboard(Camera_Movement::PINNED_RIGHT, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
camera.processKeyboard(Camera_Movement::PINNED_UP, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
camera.processKeyboard(Camera_Movement::PINNED_DOWN, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
camera.processKeyboard(Camera_Movement::FORWARD, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
camera.processKeyboard(Camera_Movement::BACKWARD, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
camera.processKeyboard(Camera_Movement::LEFT, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
camera.processKeyboard(Camera_Movement::RIGHT, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
camera.processKeyboard(Camera_Movement::UP, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS) {
camera.processKeyboard(Camera_Movement::DOWN, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_I) == GLFW_PRESS) {
camera.processKeyboard(Camera_Movement::PITCH_UP, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_K) == GLFW_PRESS) {
camera.processKeyboard(Camera_Movement::PITCH_DOWN, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_J) == GLFW_PRESS) {
camera.processKeyboard(Camera_Movement::YAW_RIGHT, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) {
camera.processKeyboard(Camera_Movement::YAW_LEFT, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_O) == GLFW_PRESS) {
camera.processKeyboard(Camera_Movement::ROLL_RIGHT, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_U) == GLFW_PRESS) {
camera.processKeyboard(Camera_Movement::ROLL_LEFT, deltaTime);
}
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}
void logString(const std::string& s) {
std::cout << s << std::endl;
}