-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
328 lines (250 loc) · 11.3 KB
/
main.cpp
File metadata and controls
328 lines (250 loc) · 11.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
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
#include <glad/glad.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
#include <glm/detail/type_vec.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include <vector>
#include <GL/glext.h>
#include "shader.h"
#include "compute_shader.h"
#include "cloth_renderer.h"
#include "cloth_handler.h"
#include "camera.h"
#include "sphere.h"
#include "collision_handler.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
// void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void processInput(GLFWwindow* window);
void printVec3(glm::vec3 vec);
void standardScene(GLFWwindow* window);
void computeTest(GLFWwindow* window);
const unsigned int SCR_WIDTH = 1600;
const unsigned int SCR_HEIGHT = 900;
float delta_time = 0.0f;
float last_frame = 0.0f;
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
int main(int, char**){
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Stevica :D", NULL, NULL);
if(window == NULL){
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// glfwSwapInterval(0);
if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
if(glfwGetWindowAttrib(window, GLFW_TRANSPARENT_FRAMEBUFFER)){
std::cout << "The framebuffer is transparrent!" << std::endl;
}
glEnable(GL_DEPTH_TEST);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(0xFFFF);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// glEnable(GL_MULTISAMPLE);
standardScene(window);
glfwTerminate();
return 0;
}
void standardScene(GLFWwindow* window){
Shader cloth_vertex_shader = Shader("/home/stevica/openGL_projects/cloth/shaders/v_cloth_vertex.glsl",
"/home/stevica/openGL_projects/cloth/shaders/f_cloth_vertex.glsl");
Shader spring_shader = Shader("/home/stevica/openGL_projects/cloth/shaders/v_spring.glsl",
"/home/stevica/openGL_projects/cloth/shaders/f_spring.glsl");
Shader cloth_shader = Shader("/home/stevica/openGL_projects/cloth/shaders/v_cloth.glsl",
"/home/stevica/openGL_projects/cloth/shaders/f_cloth.glsl");
Shader sphere_shader = Shader("/home/stevica/openGL_projects/cloth/shaders/v_sphere.glsl",
"/home/stevica/openGL_projects/cloth/shaders/f_sphere.glsl");
cloth_shader.use();
cloth_shader.setMat4("projection", Global::projection);
sphere_shader.use();
sphere_shader.setMat4("projection", Global::projection);
cloth_vertex_shader.use();
cloth_vertex_shader.setMat4("projection", Global::projection);
spring_shader.use();
spring_shader.setMat4("projection", Global::projection);
const unsigned int cols = Global::cloth_cols;
const unsigned int rows = Global::cloth_rows;
glm::vec3 cloth_vertex_positions[rows][cols];
float masses[rows][cols];
for(int i = 0; i < rows; i++){ // Generating position data for each cloth vertex
for(int j = cols-1; j >= 0; j--){
// cloth_vertex_positions[i][j] = glm::vec3(Global::subdivision_length * (j-5), Global::subdivision_length * (i-5), -1.0f);
cloth_vertex_positions[i][j] = glm::vec3(Global::subdivision_length * (j-5), 0.0f, Global::subdivision_length * (i-5)); // not good, use cloth position
masses[i][j] = 0.008f;
}
}
float spring_stiffness = 1000.0f;
ClothHandler cloth_handler = ClothHandler(cloth_vertex_positions, masses, spring_stiffness, Global::subdivision_length, glm::vec3(0.0, 1.5f, -4.5f));
CollisionHandler collision_handler;
ClothRenderer *renderer = new ClothRenderer();
cloth_handler.PinVertices(glm::vec2((float)(Global::cloth_rows-1), 0.0f), glm::vec2((float)(Global::cloth_rows-1), (float)(Global::cloth_cols-1)));
glm::vec3 lightPos = glm::vec3(1.0f, 4.0f, -2.0f);
glm::vec3 lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
glm::vec3 clothColor = glm::vec3(0.3f, 0.8f, 0.4f);
glm::vec3 sphereColor = glm::vec3(0.8f, 0.2f, 0.3f);
cloth_shader.use();
cloth_shader.setVec3("lightPos", lightPos);
cloth_shader.setVec3("lightColor", lightColor);
cloth_shader.setVec3("clothColor", clothColor);
sphere_shader.use();
sphere_shader.setVec3("lightPos", lightPos);
sphere_shader.setVec3("lightColor", lightColor);
sphere_shader.setVec3("sphereColor", sphereColor);
Sphere *s1 = new Sphere(glm::vec3(0.0, -1.0, -3.0), 0.5);
std::vector<Sphere*> spheres;
spheres.push_back(s1);
int counter = 0;
while(!glfwWindowShouldClose(window)){
float current_frame = glfwGetTime();
delta_time = current_frame - last_frame;
last_frame = current_frame;
if(counter++ > 100){
std::cout << "FPS: " << 1.0/delta_time << std::endl;
counter = 0;
}
processInput(window);
glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
cloth_shader.use();
cloth_shader.setMat4("view", camera.GetViewMatrix());
cloth_shader.setVec3("viewPos", camera.Position);
sphere_shader.use();
sphere_shader.setMat4("view", camera.GetViewMatrix());
sphere_shader.setVec3("viewPos", camera.Position);
cloth_vertex_shader.use();
cloth_vertex_shader.setMat4("view", camera.GetViewMatrix());
spring_shader.use();
spring_shader.setMat4("view", camera.GetViewMatrix());
cloth_handler.CalculateVertexNextPos(delta_time);
collision_handler.HandleCollision(cloth_handler, spheres, delta_time);
cloth_handler.UpdateVertexPositions();
cloth_handler.UpdateVertexNormals();
renderer->RenderCloth(cloth_handler, cloth_shader);
// renderer.RenderSprings(handler, spring_shader);
// renderer.RenderVertices(handler, cloth_vertex_shader);
for(Sphere* s : spheres){
s->RenderSphere(sphere_shader);
}
glfwSwapBuffers(window);
glfwPollEvents();
}
delete renderer;
for(Sphere* s : spheres){
delete s;
}
}
void computeTest(GLFWwindow* window){
Shader cloth_vertex_shader = Shader("/home/stevica/openGL_projects/cloth/shaders/v_cloth_vertex.glsl",
"/home/stevica/openGL_projects/cloth/shaders/f_cloth_vertex.glsl");
Shader spring_shader = Shader("/home/stevica/openGL_projects/cloth/shaders/v_spring.glsl",
"/home/stevica/openGL_projects/cloth/shaders/f_spring.glsl");
Shader cloth_shader = Shader("/home/stevica/openGL_projects/cloth/shaders/v_cloth.glsl",
"/home/stevica/openGL_projects/cloth/shaders/f_cloth.glsl");
Shader sphere_shader = Shader("/home/stevica/openGL_projects/cloth/shaders/v_sphere.glsl",
"/home/stevica/openGL_projects/cloth/shaders/f_sphere.glsl");
ComputeShader compute_test = ComputeShader("/home/stevica/openGL_projects/cloth/shaders/c_test.glsl");
cloth_shader.use();
cloth_shader.setMat4("projection", Global::projection);
sphere_shader.use();
sphere_shader.setMat4("projection", Global::projection);
cloth_vertex_shader.use();
cloth_vertex_shader.setMat4("projection", Global::projection);
spring_shader.use();
spring_shader.setMat4("projection", Global::projection);
glm::vec3 lightPos = glm::vec3(1.0f, 4.0f, -2.0f);
glm::vec3 lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
glm::vec3 clothColor = glm::vec3(0.3f, 0.8f, 0.4f);
glm::vec3 sphereColor = glm::vec3(0.8f, 0.2f, 0.3f);
cloth_shader.use();
cloth_shader.setVec3("lightPos", lightPos);
cloth_shader.setVec3("lightColor", lightColor);
cloth_shader.setVec3("clothColor", clothColor);
sphere_shader.use();
sphere_shader.setVec3("lightPos", lightPos);
sphere_shader.setVec3("lightColor", lightColor);
sphere_shader.setVec3("sphereColor", sphereColor);
float test_arr[SCR_HEIGHT][SCR_WIDTH];
unsigned int ssbo;
glGenBuffers(1, &ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(test_arr), NULL, GL_DYNAMIC_READ);
while(!glfwWindowShouldClose(window)){
float current_frame = glfwGetTime();
delta_time = current_frame - last_frame;
last_frame = current_frame;
processInput(window);
glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
cloth_shader.use();
cloth_shader.setMat4("view", camera.GetViewMatrix());
cloth_shader.setVec3("viewPos", camera.Position);
sphere_shader.use();
sphere_shader.setMat4("view", camera.GetViewMatrix());
sphere_shader.setVec3("viewPos", camera.Position);
cloth_vertex_shader.use();
cloth_vertex_shader.setMat4("view", camera.GetViewMatrix());
spring_shader.use();
spring_shader.setMat4("view", camera.GetViewMatrix());
glfwSwapBuffers(window);
glfwPollEvents();
}
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height){
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow* window){
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS){
glfwSetWindowShouldClose(window, true);
}
// if(glfwGetKey(window, GLFW_KEY_N) == GLFW_PRESS){
// n_pressed = true;
// }
// if(glfwGetKey(window, GLFW_KEY_N) == GLFW_RELEASE){
// n_released = true;
// }
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, delta_time);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, delta_time);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, delta_time);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT, delta_time);
}
void mouse_callback(GLFWwindow* window, double xposIn, double yposIn){
float xpos = static_cast<float>(xposIn);
float ypos = static_cast<float>(yposIn);
if (firstMouse) {
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
lastX = xpos;
lastY = ypos;
camera.ProcessMouseMovement(xoffset, yoffset);
}
void printVec3(glm::vec3 vec){
std::cout << "{" << vec.x << ", " << vec.y << ", " << vec.z << "}" << std::endl;
}