-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBounce.cpp
More file actions
554 lines (434 loc) · 18.9 KB
/
Copy pathBounce.cpp
File metadata and controls
554 lines (434 loc) · 18.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/string_cast.hpp>
const char* vertexShaderSource = R"(
#version 430 core
layout (location = 0) in vec2 aPos;
uniform float time;
uniform mat4 proj_mat;
void main() {
vec4 pos = proj_mat * vec4(aPos.xy, 0.0, 1.0);
gl_Position = pos;
}
)";
const char* geometryShaderSource = R"(
#version 430 core
layout (points) in;
layout (triangle_strip, max_vertices = 168) out;
out float border;
void main() {
float del = 0.5f;
float rad = 0.05;
float borderThickness = 0.025;
for (float theta = 0.0; theta <= 2.0 * 3.14159265359; theta += del) {
// For the normal thang
vec3 position;
position.x = gl_in[0].gl_Position.x;
position.y = gl_in[0].gl_Position.y;
position.z = gl_in[0].gl_Position.z;
gl_Position = vec4(position, 1.0);
border = 1.0;
EmitVertex();
position.x = gl_in[0].gl_Position.x + rad * cos(theta);
position.y = gl_in[0].gl_Position.y + rad * sin(theta);
position.z = gl_in[0].gl_Position.z;
gl_Position = vec4(position, 1.0);
border = 1.0;
EmitVertex();
position.x = gl_in[0].gl_Position.x + rad * cos(theta + del);
position.y = gl_in[0].gl_Position.y + rad * sin(theta + del);
position.z = gl_in[0].gl_Position.z;
gl_Position = vec4(position, 1.0);
border = 1.0;
EmitVertex();
position.x = gl_in[0].gl_Position.x + (rad + borderThickness) * cos(theta);
position.y = gl_in[0].gl_Position.y + (rad + borderThickness) * sin(theta);
position.z = gl_in[0].gl_Position.z;
gl_Position = vec4(position, 1.0);
border = 0.0;
EmitVertex();
position.x = gl_in[0].gl_Position.x + (rad + borderThickness) * cos(theta + del);
position.y = gl_in[0].gl_Position.y + (rad + borderThickness) * sin(theta + del);
position.z = gl_in[0].gl_Position.z;
gl_Position = vec4(position, 1.0);
border = 0.0;
EmitVertex();
EndPrimitive();
}
}
)";
const char* fragmentShaderSource = R"(
#version 430 core
in float border;
out vec4 FragColor;
uniform float time;
vec3 hsv2rgb(vec3 c) {
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
void main() {
vec3 hsv = vec3(mod(time/10, 1.0), 1.0, 1.0);
vec3 borderColor = vec3(0.0, 0.0, 0.0);
vec3 rgb = mix(hsv2rgb(hsv), borderColor, 1-border);
// vec3 rgb = mix(vec3(1.0, 0.0, 0.0), borderColor, 1-border);
FragColor = vec4(rgb , 1.0);
}
)";
const char* computeShaderSource = R"(
#version 430
layout (local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
uniform float deltaTime;
uniform float numVerts;
// Buffer to read and write data
layout(std430, binding = 0) buffer InPosBuffer {
vec2 inPositions[];
};
layout(std430, binding = 1) buffer OutPosBuffer {
vec2 outPositions[];
};
layout(std430, binding = 2) buffer InVelBuffer {
vec2 inVelocities[];
};
layout(std430, binding = 3) buffer OutVelBuffer {
vec2 outVelocities[];
};
float maxSpeed = 2.0f;
// vec2 acc = vec2(0, -10);
vec2 acc = vec2(0, 0);
float dampY = 1.0f;
float dampX = 1.0f;
float gravityCoeff = 0.0001;
float rad = 0.05;
float borderThickness = 0.025;
void main() {
uint globalIndex = gl_GlobalInvocationID.x;
vec2 pos = inPositions[globalIndex];
vec2 vel = inVelocities[globalIndex];
float effectiveNegBorder = -1 + rad;
if(pos.y < effectiveNegBorder && vel.y < 0){
pos.y = 2*effectiveNegBorder - pos.y;
vel.y = -vel.y * dampY;
} else if(pos.y > -effectiveNegBorder && vel.y > 0){
pos.y = -2*effectiveNegBorder - pos.y;
vel.y = -vel.y * dampY;
}
if(pos.x < effectiveNegBorder && vel.x < 0){
pos.x = 2*effectiveNegBorder - pos.x;
vel.x = -vel.x * dampX;
} else if(pos.x > -effectiveNegBorder && vel.x > 0){
pos.x = -2*effectiveNegBorder - pos.x;
vel.x = -vel.x * dampX;
}
for(int i = 0; i < numVerts; i++){
if(i == globalIndex)
continue;
float effRad = rad + borderThickness;
float dis = distance(pos, inPositions[i]);
float delDis = abs(2*effRad - dis)/2.0;
vec2 outNormal = normalize(inPositions[i] - pos);
float divisor = pow(max(dis, effRad * 2), 2);
// acc += outNormal * (divisor > 0.0 ? gravityCoeff / divisor : 0.0);
if(dis > 2*effRad)
acc += outNormal * gravityCoeff / pow(dis, 2);
if(dis < 2*effRad){
pos = pos - outNormal * delDis;
float num = dot(vel - inVelocities[i], -outNormal);
// num /= pow(distance(pos, inPositions[i]), 2);
num /= pow(2*effRad, 1);
vel = vel - num * (pos - inPositions[i]);
}
}
vel = vel + acc * deltaTime;
// float speed = length(vel);
// vel = speed <= maxSpeed ? vel : vel * maxSpeed/speed;
outPositions[globalIndex] = pos + vel * deltaTime;
outVelocities[globalIndex] = vel;
}
)";
const int num_Vertices = 16;
float deltaTime = 0.0f;
float lastFrame = 0.0f;
void setInputAndOutputBuffersForCompute(
GLuint *inputPosBuffer, GLuint *outputPosBuffer,
GLuint *inputVelBuffer, GLuint *outputVelBuffer,
glm::vec2 *inputPos, glm::vec2 *inputVel
){
glBindBuffer(GL_SHADER_STORAGE_BUFFER, *inputPosBuffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, num_Vertices * sizeof(glm::vec2), inputPos, GL_DYNAMIC_COPY);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, *inputPosBuffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, *outputPosBuffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, num_Vertices * sizeof(glm::vec2), nullptr, GL_DYNAMIC_COPY);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, *outputPosBuffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, *inputVelBuffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, num_Vertices * sizeof(glm::vec2), inputVel, GL_DYNAMIC_COPY);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, *inputVelBuffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, *outputVelBuffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, num_Vertices * sizeof(glm::vec2), nullptr, GL_DYNAMIC_COPY);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, *outputVelBuffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
}
std::pair<glm::vec2*, glm::vec2*> useAndDispatchComputeShader(
GLuint *computeProgram,
GLuint *outPosBuffer, GLuint *outVelBuffer,
float time, GLint timeLoc, int divisor
){
// Use the compute shader program
glUseProgram(*computeProgram);
glUniform1f(timeLoc, time);
// Dispatch the compute shader
glDispatchCompute((num_Vertices + divisor - 1)/divisor, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
// Read the result from the output buffer
glm::vec2 *outputPos = new glm::vec2[num_Vertices];
glBindBuffer(GL_SHADER_STORAGE_BUFFER, *outPosBuffer);
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, num_Vertices * sizeof(glm::vec2), outputPos);
glm::vec2 *outputVel = new glm::vec2[num_Vertices];
glBindBuffer(GL_SHADER_STORAGE_BUFFER, *outVelBuffer);
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, num_Vertices * sizeof(glm::vec2), outputVel);
return std::pair<glm::vec2*, glm::vec2*>(outputPos, outputVel);
}
int createAndCompileComputeShaderandComputeProgram(GLuint *computeShader, GLuint *computeProgram){
// Load and compile the compute shader
*computeShader = glCreateShader(GL_COMPUTE_SHADER);
glShaderSource(*computeShader, 1, &computeShaderSource, nullptr);
glCompileShader(*computeShader);
// Check for shader compilation errors
GLint success;
glGetShaderiv(*computeShader, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar infoLog[512];
glGetShaderInfoLog(*computeShader, sizeof(infoLog), nullptr, infoLog);
std::cerr << "Compute shader compilation failed:\n" << infoLog << std::endl;
return -1;
}
// Create a compute shader program
*computeProgram = glCreateProgram();
glAttachShader(*computeProgram, *computeShader);
glLinkProgram(*computeProgram);
// Check for program linking errors
glGetProgramiv(*computeProgram, GL_LINK_STATUS, &success);
if (!success) {
GLchar infoLog[512];
glGetProgramInfoLog(*computeProgram, sizeof(infoLog), nullptr, infoLog);
std::cerr << "Program linking failed:\n" << infoLog << std::endl;
return -1;
}
return 0;
}
int createAndCompileVertexFragmentShaderAndProgram(GLuint *vertexShader, GLuint *geomteryShader, GLuint *fragmentShader, GLuint *shaderProgram){
*vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(*vertexShader, 1, &vertexShaderSource, nullptr);
glCompileShader(*vertexShader);
GLint success;
glGetShaderiv(*vertexShader, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar infoLog[512];
glGetShaderInfoLog(*vertexShader, sizeof(infoLog), nullptr, infoLog);
std::cerr << "Vertex shader compilation failed:\n" << infoLog << std::endl;
return -1;
}
*geomteryShader = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(*geomteryShader, 1, &geometryShaderSource, nullptr);
glCompileShader(*geomteryShader);
glGetShaderiv(*geomteryShader, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar infoLog[512];
glGetShaderInfoLog(*geomteryShader, sizeof(infoLog), nullptr, infoLog);
std::cerr << "Geometry shader compilation failed:\n" << infoLog << std::endl;
return -1;
}
*fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(*fragmentShader, 1, &fragmentShaderSource, nullptr);
glCompileShader(*fragmentShader);
glGetShaderiv(*fragmentShader, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar infoLog[512];
glGetShaderInfoLog(*fragmentShader, sizeof(infoLog), nullptr, infoLog);
std::cerr << "Frag shader compilation failed:\n" << infoLog << std::endl;
return -1;
}
*shaderProgram = glCreateProgram();
glAttachShader(*shaderProgram, *vertexShader);
glAttachShader(*shaderProgram, *geomteryShader);
glAttachShader(*shaderProgram, *fragmentShader);
glLinkProgram(*shaderProgram);
glGetProgramiv(*shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
GLchar infoLog[512];
glGetProgramInfoLog(*shaderProgram, sizeof(infoLog), nullptr, infoLog);
std::cerr << "Shader Program linking failed:\n" << infoLog << std::endl;
return -1;
}
return 0;
}
void setVAOandVBOforRender(GLuint *VAO, GLuint *VBO, glm::vec2 *VBOData){
glBindVertexArray(*VAO);
glBindBuffer(GL_ARRAY_BUFFER, *VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2) * (num_Vertices), VBOData, GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec2), (void*)0);
glEnableVertexAttribArray(0);
}
void updateDeltaTime() {
float currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}
float updateFPS(GLFWwindow* window){
float FPS = 1.0/deltaTime;
std::string windowTitle = "Bounce Bounce | FPS: " + std::to_string(FPS);
glfwSetWindowTitle(window, windowTitle.c_str());
// std::cout << windowTitle << std::endl;
return FPS;
}
int main() {
// Initialize GLFW
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
return -1;
}
// Set OpenGL version to 4.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
glfwWindowHint(GLFW_SAMPLES, 4); // anti-aliasing
// Create a windowed mode window and its OpenGL context
GLFWwindow* window = glfwCreateWindow(800, 600, "Bounce Bounce", NULL, NULL);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Make the window's context current
glfwMakeContextCurrent(window);
// Set clear color
glClearColor(0.529f, 0.808f, 0.922f, 1.0f);
// To make it RGB-A
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Anti-aliasing
glEnable(GL_MULTISAMPLE);
// Initialize GLEW
if (glewInit() != GLEW_OK) {
std::cerr << "Failed to initialize GLEW" << std::endl;
return -1;
}
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glfwMaximizeWindow(window);
// Check if the required OpenGL version is supported
if (!GLEW_VERSION_4_3) {
std::cerr << "OpenGL 4.3 is not supported" << std::endl;
return -1;
}
std::srand(static_cast<unsigned int>(std::time(nullptr)));
glm::vec2 *initialPositions = new glm::vec2[num_Vertices];
glm::vec2 *velocities = new glm::vec2[num_Vertices];
float heightWidthFactor = 0.9f;
float sqSide = 1.5f;
for (int i = 0; i < num_Vertices; ++i) {
float randomX = -sqSide/2 + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / sqSide));
float randomY = -sqSide/2 + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / sqSide));
float randomX1 = -1 + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / 2.0));
float randomY1 = -1 + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / 2.0));
// initialPositions[i] = glm::vec2((float)i*heightWidthFactor/(num_Vertices), (float)i*heightWidthFactor/(num_Vertices));
// initialPositions[i] = glm::vec2(randomX, randomY);
// velocities[i] = glm::vec2(randomX1/1, randomY1/1);
velocities[i] = glm::vec2(0.0f, 0.0f);
}
float num_on_side = sqrt(num_Vertices);
float spacing = sqSide/num_on_side;
for(int i = 0; i < num_on_side; i++){
for(int j = 0; j < num_on_side; j++){
float x = -sqSide/2 + i * spacing;
float y = -sqSide/2 + j * spacing;
initialPositions[(int)num_on_side * i + j] = glm::vec2(x, y);
}
}
// for(int i = 0; i < num_Vertices; i++)
// std::cout << glm::to_string(initialPositions[i]) << std::endl;
// ----------------------------------------------------- COLOR SHADER PART --------------------------------------------------
GLuint vertexShader, geometryShader, fragmentShader, shaderProgram;
GLuint VAO, VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
createAndCompileVertexFragmentShaderAndProgram(&vertexShader, &geometryShader, &fragmentShader, &shaderProgram);
setVAOandVBOforRender(&VAO, &VBO, initialPositions);
float projSize = 1.0f;
glm::mat4 projMat = glm::ortho(-projSize, projSize, -projSize, projSize);
glUseProgram(shaderProgram);
GLint timeShaderLoc = glGetUniformLocation(shaderProgram, "time");
GLint projMatShaderLoc = glGetUniformLocation(shaderProgram, "proj_mat");
glUniformMatrix4fv(projMatShaderLoc, 1, GL_FALSE, glm::value_ptr(projMat));
glUseProgram(0);
// ----------------------------------------------------- COMPUTE SHADER PART ------------------------------------------------
// Create buffers
GLuint inputPosBuffer, outputPosBuffer, inputVelBuffer, outputVelBuffer;
GLuint computeShader, computeProgram;
glGenBuffers(1, &inputPosBuffer);
glGenBuffers(1, &outputPosBuffer);
glGenBuffers(1, &inputVelBuffer);
glGenBuffers(1, &outputVelBuffer);
createAndCompileComputeShaderandComputeProgram(&computeShader, &computeProgram);
// For the first time, inputData is init value after that its always outputData
setInputAndOutputBuffersForCompute(&inputPosBuffer, &outputPosBuffer, &inputVelBuffer, &outputVelBuffer, initialPositions, velocities);
glUseProgram(computeProgram);
GLint timeComputeLoc = glGetUniformLocation(computeProgram, "deltaTime");
GLint numVertsComputeLoc = glGetUniformLocation(computeProgram, "numVerts");
glUniform1f(numVertsComputeLoc, num_Vertices);
glUseProgram(0);
glm::vec2 *outPositions, *outVelocities;
std::pair<glm::vec2*, glm::vec2*> outputs;
float sumFPS = 0;
float numFrames = 0;
while(!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
updateDeltaTime();
sumFPS += updateFPS(window);
numFrames++;
// ------------------------------------------------- Running Compute Shader ----------------------------------------
outputs = useAndDispatchComputeShader(&computeProgram, &outputPosBuffer, &outputVelBuffer, deltaTime, timeComputeLoc, 32);
outPositions = outputs.first;
outVelocities = outputs.second;
setInputAndOutputBuffersForCompute(&inputPosBuffer, &outputPosBuffer, &inputVelBuffer, &outputVelBuffer, outPositions, outVelocities);
// std::cout << deltaTime << ": " << glm::to_string(outPositions[0]) << " " << glm::to_string(outVelocities[0]) << std::endl;
// std::cout << "Velocity of 0: " << glm::to_string(outVelocities[0]) << std::endl;
glFinish();
// ------------------------------------------------ Running Color Shader ----------------------------------------
glUseProgram(shaderProgram);
glUniform1f(timeShaderLoc, static_cast<float>(glfwGetTime()));
setVAOandVBOforRender(&VAO, &VBO, outPositions);
glDrawArrays(GL_POINTS, 0, num_Vertices);
glfwSwapBuffers(window);
glfwPollEvents();
}
float avgFPS = sumFPS/numFrames;
std::cout << "Average FPS: " << avgFPS << std::endl;
// Clean up
delete[] outPositions;
glDeleteProgram(computeProgram);
glDeleteProgram(shaderProgram);
glDeleteShader(computeShader);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glDeleteBuffers(1, &inputPosBuffer);
glDeleteBuffers(1, &outputPosBuffer);
glDeleteBuffers(1, &inputVelBuffer);
glDeleteBuffers(1, &outputVelBuffer);
// Terminate GLFW
glfwTerminate();
return 0;
}