-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
453 lines (375 loc) · 13 KB
/
Copy pathmain.cpp
File metadata and controls
453 lines (375 loc) · 13 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
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <vector>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
// Window
const int WIDTH = 800;
const int HEIGHT = 600;
static GLFWwindow* window;
// Simulation Parameters
float timeScale = 4.0f; // Determines simulation speed
float gravityAcc = 15.0f; // How fast particles acc. downward
int initParticleNumber = 800; // The number of particles to spawn
float pointSize = 15.0f; // The size of the rendered particle
float borderDampen = 0.5f; // Dampen border collisions
float interactionRadius = 0.2f; // Radius for neighbor interactions
float stiffness = 50.0f; // Pressure constant
float restDensity = 15.0f; // Rest density of the fluid
float viscosity = 25.0f; // Viscosity constant
float velocityCap = 10.0f; // Cap velocity to stop explosions!
float mousePower = 1.0f; // Mouse influence over particles
float mouseInteractionRadius = 0.2f; // Radius for neighbor interactions
float borderMoveSpeed = 0.0f; // How fast the user can move bonudaries
float borderWidth = 1.95f; // Starting border width
float borderHeight = 1.95f; // Starting border height
// Functions
int setupGui();
int cleanupGui();
void drawGui();
void setup3D();
void moveCamera(float deltaTime);
float randomFloat(float range);
// Camera
float cameraDistance = 5.0f; // Distance from the center
float cameraSpeed = 0.5f; // Speed of camera movement
float cameraHeight = 1.0f; // Height of the camera
struct Camera {
glm::vec3 position;
glm::vec3 rotation;
};
Camera camera = {
glm::vec3(0.0f, 2.0f, cameraDistance), // Initial position
glm::vec3(0.0f, 0.0f, 0.0f) // Rotation
};
// Particles
struct Particle {
glm::vec3 position;
glm::vec3 velocity;
float density = 0.0f;
float pressure = 0.0f;
};
std::vector<Particle> particles;
// Spawn in the particles
void initParticles() {
particles.clear();
int gridSize = static_cast<int>(cbrt(initParticleNumber));
float totalWidth = interactionRadius * (gridSize - 1);
float startX = -totalWidth / 2.0f;
float startY = -totalWidth / 2.0f;
float startZ = -totalWidth / 2.0f;
glm::vec3 velocity(0.0f);
for (int i = 0; i < gridSize; ++i) {
for (int j = 0; j < gridSize; ++j) {
for (int k = 0; k < gridSize; ++k) {
glm::vec3 position = glm::vec3(
startX + i * interactionRadius + randomFloat(0.01f),
startY + j * interactionRadius + randomFloat(0.01f),
startZ + k * interactionRadius + randomFloat(0.01f)
);
particles.push_back({position, velocity});
}
}
}
}
// Adjust particle characteristics
void computeDensityAndPressure() {
for (auto& p : particles) {
p.density = 0.0f;
for (const auto& neighbor : particles) {
glm::vec3 diff = p.position - neighbor.position;
float distance = glm::length(diff);
if (distance < interactionRadius) {
float q = distance / interactionRadius;
p.density += (1.0f - q) * (1.0f - q) * (1.0f - q); // Poly6 kernel
}
}
p.pressure = stiffness * (p.density - restDensity);
}
}
// Find the forces acting on a particle
void computeForces(float deltaTime) {
for (auto& p : particles) {
glm::vec3 pressureForce(0.0f);
glm::vec3 viscosityForce(0.0f);
for (const auto& neighbor : particles) {
glm::vec3 diff = p.position - neighbor.position;
float distance = glm::length(diff);
if (distance < interactionRadius && distance > 0.0f) {
glm::vec3 direction = glm::normalize(diff);
float q = distance / interactionRadius;
float pressureKernel = (1.0f - q);
pressureForce -= direction * (p.pressure + neighbor.pressure) / (2.0f * neighbor.density) * pressureKernel;
viscosityForce += viscosity * (neighbor.velocity - p.velocity) * pressureKernel;
}
}
glm::vec3 gravity(0.0f, -gravityAcc, 0.0f);
p.velocity += (pressureForce + viscosityForce + gravity) * deltaTime;
if (glm::length(p.velocity) > velocityCap) {
p.velocity = glm::normalize(p.velocity) * velocityCap;
}
}
}
void updateParticles(float deltaTime) {
computeDensityAndPressure();
computeForces(deltaTime);
float left = -borderWidth / 2.0f;
float right = borderWidth / 2.0f;
float top = borderHeight / 2.0f;
float bottom = -borderHeight / 2.0f;
float front = -borderWidth / 2.0f;
float back = borderWidth / 2.0f;
for (auto& p : particles) {
p.position += p.velocity * deltaTime;
// Boundary conditions (x-axis)
if (p.position.x < left) {
p.position.x = left;
p.velocity.x *= -borderDampen;
}
if (p.position.x > right) {
p.position.x = right;
p.velocity.x *= -borderDampen;
}
// Boundary conditions (y-axis)
if (p.position.y < bottom) {
p.position.y = bottom;
p.velocity.y *= -borderDampen;
}
if (p.position.y > top) {
p.position.y = top;
p.velocity.y *= -borderDampen;
}
// Boundary conditions (z-axis)
if (p.position.z < front) {
p.position.z = front;
p.velocity.z *= -borderDampen;
}
if (p.position.z > back) {
p.position.z = back;
p.velocity.z *= -borderDampen;
}
}
}
// Draw the particles to the screen as points
void renderParticles() {
glEnable(GL_POINT_SMOOTH); // Enable smooth points
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); // Use the nicest smoothing algorithm
glPointSize(pointSize);
glBegin(GL_POINTS);
for (const auto& p : particles) {
float normalizedDensity = glm::clamp((p.density - restDensity) / restDensity, 0.0f, 1.0f);
float r = normalizedDensity;
float g = 0.0f;
float b = 1.0f - normalizedDensity;
glColor3f(r, g, b);
glVertex3f(p.position.x, p.position.y, p.position.z);
}
glEnd();
}
float randomFloat(float range) {
return static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX) * 2.0f * range - range;
}
void drawBorder() {
// Define the borders of the cube
float left = -borderWidth / 2.0f;
float right = borderWidth / 2.0f;
float top = borderHeight / 2.0f;
float bottom = -borderHeight / 2.0f;
float front = -borderWidth / 2.0f;
float back = borderWidth / 2.0f;
// Set the color for the border
glColor3f(0.5f, 0.5f, 0.5f); // Gray color for the border
glLineWidth(2.0f); // Set line width
// Draw the edges of the cube
glBegin(GL_LINES);
// Bottom face
glVertex3f(left, bottom, front);
glVertex3f(right, bottom, front);
glVertex3f(right, bottom, front);
glVertex3f(right, bottom, back);
glVertex3f(right, bottom, back);
glVertex3f(left, bottom, back);
glVertex3f(left, bottom, back);
glVertex3f(left, bottom, front);
// Top face
glVertex3f(left, top, front);
glVertex3f(right, top, front);
glVertex3f(right, top, front);
glVertex3f(right, top, back);
glVertex3f(right, top, back);
glVertex3f(left, top, back);
glVertex3f(left, top, back);
glVertex3f(left, top, front);
// Vertical edges
glVertex3f(left, bottom, front);
glVertex3f(left, top, front);
glVertex3f(right, bottom, front);
glVertex3f(right, top, front);
glVertex3f(right, bottom, back);
glVertex3f(right, top, back);
glVertex3f(left, bottom, back);
glVertex3f(left, top, back);
glEnd();
}
void processInput() {
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
borderHeight += borderMoveSpeed; // Increase height
}
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
borderHeight -= borderMoveSpeed; // Decrease height
}
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
borderWidth += borderMoveSpeed; // Increase width
}
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
borderWidth -= borderMoveSpeed; // Decrease width
}
// Minimum size for the border
borderWidth = std::max(0.1f, borderWidth);
borderHeight = std::max(0.1f, borderHeight);
}
void framebufferSizeCallback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}
int main() {
std::srand(std::time(nullptr));
setupGui();
setup3D();
initParticles();
float lastTime = glfwGetTime();
while (!glfwWindowShouldClose(window)) {
float currentTime = glfwGetTime();
float deltaTime = (currentTime - lastTime) / timeScale;
lastTime = currentTime;
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
processInput();
glClear(GL_COLOR_BUFFER_BIT);
moveCamera(deltaTime);
drawBorder();
updateParticles(deltaTime);
renderParticles();
drawGui();
glfwSwapBuffers(window);
glfwPollEvents();
}
cleanupGui();
return 0;
}
int setupGui() {
glEnable(GL_PROGRAM_POINT_SIZE);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW!" << std::endl;
return -1;
}
// Init OpenGL + window
window = glfwCreateWindow(WIDTH, HEIGHT, "opengl", NULL, NULL);
if (!window) {
std::cerr << "Failed to create window!" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// glfwSwapInterval(1);
// Init ImGui
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
(void) io;
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330");
ImGui::StyleColorsDark();
return 0;
}
// Cleanup
int cleanupGui() {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
void drawGui() {
// Create Frame + Fullscreen
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// Make window
ImGui::Begin("Fluid Sim Parameters", NULL, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::Text("Framerate: %.1f FPS", ImGui::GetIO().Framerate);
ImGui::InputInt("Number", &initParticleNumber);
ImGui::InputFloat("Spacing", &interactionRadius); // Corrected missing semicolon
ImGui::SliderFloat("Size", &pointSize, 1.0f, 100.0f);
if (ImGui::Button("Start Simulation"))
initParticles();
if (ImGui::CollapsingHeader("Parameters")) {
ImGui::SliderFloat("Time Scale", &timeScale, 1.0f, 10.0f);
ImGui::InputFloat("Gravity", &gravityAcc);
ImGui::InputFloat("Border Dampening", &borderDampen);
ImGui::Separator();
ImGui::InputFloat("Interact Radius", &interactionRadius);
ImGui::InputFloat("Stiffness", &stiffness);
ImGui::InputFloat("Rest Density", &restDensity);
ImGui::InputFloat("Viscosity", &viscosity);
ImGui::Separator();
ImGui::InputFloat("Max Velocity", &velocityCap);
}
if (ImGui::CollapsingHeader("Camera")) {
ImGui::SliderFloat("Cam Speed", &cameraSpeed, 0.0f, 10.0f);
ImGui::SliderFloat("Cam Distance", &cameraDistance, 0.1f, 10.0f);
ImGui::SliderFloat("Cam Height", &cameraHeight, 0.0f, 5.0f);
}
if (ImGui::CollapsingHeader("User Interaction")) {
ImGui::SliderFloat("Border Move Speed", &borderMoveSpeed, 0.0f, 0.1f);
}
ImGui::End();
// Render GUI
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
void setup3D() {
// Projection matrix
glm::mat4 projection = glm::perspective(
glm::radians(45.0f),
(float)WIDTH / (float)HEIGHT,
0.1f, 100.0f
);
// View matrix (camera)
glm::mat4 view = glm::lookAt(
glm::vec3(0.0f, 0.0f, 5.0f), // Camera position
glm::vec3(0.0f, 0.0f, 0.0f), // Look-at point
glm::vec3(0.0f, 1.0f, 0.0f) // Up vector
);
// Set the matrices in OpenGL
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(glm::value_ptr(projection));
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(glm::value_ptr(view));
}
void moveCamera(float deltaTime) {
static float angle = 0.0f;
angle += cameraSpeed * deltaTime;
// Calculate the new position
camera.position.x = cameraDistance * cos(angle);
camera.position.z = cameraDistance * sin(angle);
camera.position.y = cameraHeight;
// The camera always points to the center of the simulation (0, 0, 0)
glm::vec3 target(0.0f, 0.0f, 0.0f);
glm::vec3 up(0.0f, 1.0f, 0.0f);
// Create the view matrix
glm::mat4 view = glm::lookAt(camera.position, target, up);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(glm::value_ptr(view));
}