-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
432 lines (347 loc) · 13.9 KB
/
main.cpp
File metadata and controls
432 lines (347 loc) · 13.9 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
421
422
423
424
425
426
427
428
429
430
431
432
#include "VerletCircle.hpp"
#include "libs/Shapes/Shapes.hpp"
#include <utility>
#define STB_IMAGE_IMPLEMENTATION
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "Constants.hpp"
#include "Types.hpp"
#include "VerletSolver.hpp"
#include "Window.hpp"
#include "libs/Shader/Shader.hpp"
#include "libs/stb_image/stb_image.hpp"
#include <OpenGL/OpenGL.h>
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <stdio.h>
#include <string>
#include <sys/types.h>
#include <vector>
using namespace verletSolver;
Window g_window;
uint VAO_tex;
uint circle_VAO;
uint g_texture_id;
uint g_texture_id_2;
int g_circle_num_points = 30;
bool g_sim_running = false;
Solver g_physics_simulation;
shader::Shader triangle_shader_tex;
shader::Shader circle_shader;
bool init();
void run();
void quit();
void spawn_circle(double pos_x, double pos_y);
void create_triangle_tex();
void create_circle();
void process_input(GLFWwindow *window);
void create_texture();
void mouse_button_callback(
GLFWwindow *window, int button, int action, int mods);
float normalize_value(
float value, float range_min_1, float range_max_1, float range_min_2,
float range_max_2);
int main(int argc, char *args[]) {
if (!init()) {
return 1;
}
triangle_shader_tex = {
"shaders/base_triangle_tex/shader.vs",
"shaders/base_triangle_tex/shader.fs"};
circle_shader = {"shaders/circle/shader.vs", "shaders/circle/shader.fs"};
create_triangle_tex();
create_circle();
run();
quit();
return 0;
}
bool init() {
if (!g_window.init()) {
printf("Error initializing window\n");
return false;
}
glfwSetMouseButtonCallback(g_window.get_window(), mouse_button_callback);
return true;
}
void quit() {
g_window.free();
glfwTerminate();
}
void run() {
bool quit = false;
bool simulation_running = false;
// while (SDL_PollEvent(&event) != 0) {
// if (event.type == SDL_QUIT) {
// quit = true;
// } else if (
// event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_s)
// { simulation_running = !simulation_running;
// } else if (
// event.type == SDL_MOUSEBUTTONDOWN &&
// event.button.button == SDL_BUTTON_LEFT) {
// spawn_rect(physics_simulation, event.button.x,
// event.button.y);
// }
//
// g_window.handle_event(event, g_renderer);
// }
g_physics_simulation.add_circle({0.f, 0.7f}, 0.03f);
g_physics_simulation.add_circle({0.f, 0.5f}, 0.03f);
glEnable(GL_DEPTH_TEST);
double old_time = 0.f;
while (!glfwWindowShouldClose(g_window.get_window())) {
double time = glfwGetTime();
double delta_time = time - old_time;
old_time = time;
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
process_input(g_window.get_window());
// set color to use when clear function is called
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
circle_shader.use();
glBindVertexArray(circle_VAO);
std::pair<int, int> window_size = g_window.get_window_size();
float aspect = (float)window_size.first / window_size.second;
// for (int i = 0; i < circles.size(); ++i) {
// float circle_radius = circles[i].get_radius();
// // set matrix to scale object in world coordinates
// glm::mat4 model = glm::mat4(1.0f);
// model = glm::translate(model, *circles[i].get_position());
// model = glm::scale(
// model,
// glm::vec3(circle_radius / 10.f, circle_radius / 10.f, 1.0f));
// // set matrix for object translation
// glm::mat4 view = glm::mat4(1.0f);
// view = glm::translate(view, glm::vec3(0.f, 0.f, 0.f));
// // set projection matrix to preserve aspect ratio after window
// size
// // changes
// glm::mat4 projection = glm::mat4(1.0f);
// projection = glm::ortho(-aspect, aspect, -1.0f, 1.0f);
// circle_shader.set_mat4f("model", model);
// circle_shader.set_mat4f("view", view);
// circle_shader.set_mat4f("projection", projection);
// glDrawArrays(GL_TRIANGLE_FAN, 0, circle_num_points);
// }
std::vector<verletCircle::VerletCircle *> verlet_circles =
g_physics_simulation.get_circles();
for (int i = 0; i < verlet_circles.size(); ++i) {
// set matrix to scale object in world coordinates
glm::mat4 model = glm::mat4(1.0f);
glm::vec3 position = glm::vec3(
verlet_circles[i]->m_current_position.x,
verlet_circles[i]->m_current_position.y, 0.0f);
// position.x = -1.0f + 2.0f * (position.x /
// constants::WINDOW_WIDTH); position.y = 1.0f - 2.0f * (position.y
// / constants::WINDOW_HEIGHT);
model = glm::translate(model, position);
model = glm::scale(
model, glm::vec3(
verlet_circles[i]->get_radius(),
verlet_circles[i]->get_radius(), 1.0f));
// set matrix for object translation
glm::mat4 view = glm::mat4(1.0f);
view = glm::translate(view, glm::vec3(0.f, 0.f, 0.f));
// set projection matrix to preserve aspect ratio after window size
// changes
glm::mat4 projection = glm::mat4(1.0f);
projection = glm::ortho(-aspect, aspect, -1.0f, 1.0f);
circle_shader.set_mat4f("model", model);
circle_shader.set_mat4f("view", view);
circle_shader.set_mat4f("projection", projection);
glDrawArrays(GL_TRIANGLE_FAN, 0, g_circle_num_points);
}
glUseProgram(0);
glBindVertexArray(0);
glfwSwapBuffers(g_window.get_window());
glfwPollEvents();
if (g_sim_running) {
g_physics_simulation.update(delta_time);
}
}
}
void create_circle() {
uint VBO;
int num_points = g_circle_num_points;
GLfloat vertex_data[6 * num_points];
uint current_index = 0;
int theta = 0;
float radius = 1.f;
while (theta < 360) {
GLfloat x = (GLfloat)radius * cosf(theta * M_PI / 180.0f);
GLfloat y = (GLfloat)radius * sinf(theta * M_PI / 180.0f);
vertex_data[current_index++] = x;
vertex_data[current_index++] = y;
vertex_data[current_index++] = 0.5f;
vertex_data[current_index++] = 1.0f;
vertex_data[current_index++] = 1.0f;
vertex_data[current_index++] = 1.0f;
theta += 360 / num_points;
}
glGenVertexArrays(1, &circle_VAO);
glBindVertexArray(circle_VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(
GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data, GL_STATIC_DRAW);
glVertexAttribPointer(
0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (void *)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(
1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat),
(void *)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void process_input(GLFWwindow *window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
g_sim_running = true;
} else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
g_sim_running = false;
} else if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
g_physics_simulation.reset_gravity();
}
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
g_physics_simulation.set_gravity({-2.0f, 0.f});
} else if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
g_physics_simulation.set_gravity({2.0f, 0.f});
} else if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
g_physics_simulation.set_gravity({0.0f, 2.f});
} else if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
g_physics_simulation.reset_gravity();
}
}
void create_texture() {
int width, height, channel_count;
unsigned char *data =
stbi_load("textures/container.jpg", &width, &height, &channel_count, 0);
if (!data) {
printf("Error while loading texture!\n");
return;
}
// gen texture buffers on the GPU
glGenTextures(1, &g_texture_id);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, g_texture_id);
// set texture filtering on x and y axis
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering for mipmaps
glTexParameteri(
GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE,
data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
glBindTexture(GL_TEXTURE_2D, 0);
/*------------------------------------------*/
// stbi_set_flip_vertically_on_load(true);
data = stbi_load(
"textures/awesomeface.png", &width, &height, &channel_count, 0);
if (!data) {
printf("Error while loading awesomeface image\n");
return;
}
glGenTextures(1, &g_texture_id_2);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, g_texture_id_2);
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);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
glBindTexture(GL_TEXTURE_2D, 0);
}
void create_triangle_tex() {
uint VBO;
GLfloat vertex_data[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f};
// The VAO saves all the information about the things we want to draw
// so we declare it before all the VBO, etc creation and setting.
// Then we unbind it and select it if we want to use it.
// (Say we want to have multiple triangles and rectangles, we first make the
// VAOs and VBOs, etc and then we select it for drawing).
glGenVertexArrays(1, &VAO_tex);
glBindVertexArray(VAO_tex);
// set the Vertex Buffer Objects
// (buffer in GPU memory with vertex positions)
glGenBuffers(1, &VBO);
// OpenGL allows different buffers open at the
// same time as long taht they are of different types
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// load the data in the current buffer
glBufferData(
GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data, GL_STATIC_DRAW);
// index of attrib, type, normalized, offset between values, starting index
glVertexAttribPointer(
0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(
1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
(void *)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
create_texture();
triangle_shader_tex.use();
triangle_shader_tex.set_int("texture1", 0);
triangle_shader_tex.set_int("texture2", 1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void spawn_circle(double pos_x, double pos_y) {
g_physics_simulation.add_circle({pos_x, pos_y}, 0.03f);
}
void mouse_button_callback(
GLFWwindow *window, int button, int action, int mods) {
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
double pos_x, pos_y;
glfwGetCursorPos(g_window.get_window(), &pos_x, &pos_y);
spawn_circle(
normalize_value(
pos_x, 0, (float)constants::WINDOW_WIDTH, -1.0f, 1.0f),
normalize_value(
pos_y, (float)constants::WINDOW_HEIGHT, 0, -1.0f, 1.0f));
}
}
float normalize_value(
float value, float range_min_1, float range_max_1, float range_min_2,
float range_max_2) {
return ((range_max_2 - range_min_2) * (value - range_min_1) /
(range_max_1 - range_min_1)) +
range_min_2;
}