Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
337 lines (276 loc) · 9.13 KB
/
Copy pathmain.cpp
File metadata and controls
337 lines (276 loc) · 9.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
// Include CUDA headers
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include "CudaFunctions.cuh"
// Include standard headers
#include <stdio.h>
#include <stdlib.h>
// Include GLEW
#include <GL/glew.h>
// Include GLFW
#include <GLFW/glfw3.h>
GLFWwindow* window;
// Include GLM
#include <glm/glm.hpp>
using namespace glm;
// Include ImGUI
#include "GL/imgui/imgui.h"
#include "GL/imgui/backends/imgui_impl_glfw.h"
#include "GL/imgui/backends/imgui_impl_opengl3.h"
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <chrono>
#include <iostream>
#include <unordered_set>
#include "Constants.h"
#include "MyVec2.h"
#include "Particle.h"
#include "ParticleGrid.h"
#include "Simulation.h"
#define PIXEL_FORMAT GL_RGB
/// <summary> SPH simulation struct </summary>
Simulation simulation;
/// <summary> Current time in game loop </summary>
double t;
/// <summary> Time of last frame update </summary>
double t_old = 0.f;
/// <summary> Time elapsed from last frame update </summary>
double dt;
/// <summary> Color which is used as background </summary>
ImVec4 clearColor = ImVec4(0.45f, 0.55f, 0.60f, 1.0f);
/// <summary> Color of the particles </summary>
ImVec4 particlesColor = ImVec4(0.f, 0.f, 0.f, 1.0f);
/// <summary>
/// Renders simulation
/// </summary>
void Render() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glOrtho(0, simulation.VIEW_WIDTH, 0, simulation.VIEW_HEIGHT, 0, 1);
glColor4f(
particlesColor.x * particlesColor.w,
particlesColor.y * particlesColor.w,
particlesColor.z * particlesColor.w,
particlesColor.w
);
glBegin(GL_POINTS);
for (int i = 0; i < simulation.particleCount; i++) {
glVertex2f(simulation.particles[i].position.x, simulation.particles[i].position.y);
}
glEnd();
}
/// <summary>
/// Function responsible for kandling user key presses
/// </summary>
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) {
simulation.AddParticleRectangle();
}
else if (key == GLFW_KEY_R && action == GLFW_PRESS) {
simulation.Reset();
}
else if (key == GLFW_KEY_W && action == GLFW_PRESS) {
simulation.G.x = 0.0f;
simulation.G.y = GRAVITY_VAL;
}
else if (key == GLFW_KEY_S && action == GLFW_PRESS) {
simulation.G.x = 0.0f;
simulation.G.y = -GRAVITY_VAL;
}
else if (key == GLFW_KEY_A && action == GLFW_PRESS) {
simulation.G.x = -GRAVITY_VAL;
simulation.G.y = 0.0f;
}
else if (key == GLFW_KEY_D && action == GLFW_PRESS) {
simulation.G.x = GRAVITY_VAL;
simulation.G.y = 0.0f;
} else if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
}
/// <summary>
/// Callback after window resizing
/// </summary>
/// <param name="window">Window which was resized</param>
/// <param name="width">New window width</param>
/// <param name="height">New window height</param>
void window_size_callback(GLFWwindow* window, int width, int height)
{
// Update OpenGL viewport
int fbWidth, fbHeight;
glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
glViewport(0, 0, fbWidth, fbHeight);
// Udpate simulation
simulation.windowRescaleRoutine(width, height);
}
/// <summary>
/// Callback for GLFW errors
/// </summary>
static void ErrorCallback(int, const char* err_str)
{
std::cout << "GLFW Error: " << err_str << std::endl;
}
/// <summary>
/// Iniitalizes OpenGL stuff
/// </summary>
void InitGL() {
glClearColor(0.9f, 0.9f, 0.9f, 1);
glEnable(GL_POINT_SMOOTH);
glPointSize(H / 2.f);
glMatrixMode(GL_PROJECTION);
}
/// <summary>
/// Initializes ImGUI stuff
/// </summary>
/// <param name="glsl_version">Version of OpenGL being used</param>
void InitImGUI(const char* glsl_version) {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Setup Dear ImGui style
//ImGui::StyleColorsDark();
ImGui::StyleColorsClassic();
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
}
/// <summary>
/// Renders simulation controls using ImGUI
/// </summary>
void RenderGUIControlPanel() {
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::Begin("SPH settings", NULL, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::Checkbox("FixedTimestep", &simulation.fixedTimestep);
if (simulation.fixedTimestep) {
//ImGui::InputFloat("Delta Time", &DT);
}
ImGui::Spacing();
ImGui::SliderInt("Maximum particles", &simulation.MAX_PARTICLES, 0, 50000);
ImGui::SliderFloat("Bounds damping", &simulation.BOUND_DAMPING, -1.5f, 0);
ImGui::SliderFloat("Viscosity", &simulation.VISC, 10.0f, 500.0f);
ImGui::SliderFloat("Particle mass", &simulation.MASS, 0.5f, 20.0f);
ImGui::SliderFloat("Rest density", &simulation.REST_DENS, 10.0f, 500.0f);
ImGui::SliderFloat("Gas constant", &simulation.GAS_CONST, 500.0f, 6000.0f);
ImGui::Spacing();
ImGui::Text("Gravity: (%f, %f)", simulation.G.x, simulation.G.y);
if (ImGui::Button("Toggle Gravity")) {
if (simulation.G.x != 0.0f || simulation.G.y != 0.0f) {
simulation.G = MyVec2(0.0f, 0.0f);
} else {
simulation.G = MyVec2(0.0f, -GRAVITY_VAL);
}
}
if (ImGui::Button("Reset parameters to default")) {
simulation.SetDefaultParameters();
}
ImGui::Spacing();
ImGui::Checkbox("Accelerate CPU version with uniform grid", &simulation.useSpatialGrid);
ImGui::Checkbox("Use GPU for computations", &simulation.simulateOnGPU);
ImGui::Spacing();
ImGui::ColorEdit3("Background color", (float*)&clearColor);
ImGui::ColorEdit3("Particles color", (float*)&particlesColor);
ImGui::Spacing();
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::Text("Particle count: %d", simulation.particleCount);
ImGui::End();
// Rendering
ImGui::Render();
glClearColor(clearColor.x * clearColor.w, clearColor.y * clearColor.w, clearColor.z * clearColor.w, clearColor.w);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
int main(void)
{
glfwSetErrorCallback(ErrorCallback);
// Initialize the library
if (!glfwInit())
return -1;
// Decide GL+GLSL versions
#if defined(IMGUI_IMPL_OPENGL_ES2)
// GL ES 2.0 + GLSL 100
const char* glsl_version = "#version 100";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
#elif defined(__APPLE__)
// GL 3.2 + GLSL 150
const char* glsl_version = "#version 150";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
#else
// GL 3.0 + GLSL 130
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); // 3.2+ only
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
#endif
// Create a windowed mode window and its OpenGL context
GLFWwindow* window = glfwCreateWindow(simulation.WINDOW_WIDTH, simulation.WINDOW_HEIGHT, "Smoothed Particle Hydrodynamics", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
// Make the window's context current
glfwMakeContextCurrent(window);
// Set correct event calbacks
glfwSetKeyCallback(window, key_callback);
glfwSetWindowSizeCallback(window, window_size_callback);
// TODO: unlimited FPS / cap to screen refresh rate
glfwSwapInterval(0);
// Initialize open GL
InitGL();
// ImGui setup
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Setup Dear ImGui style
//ImGui::StyleColorsDark();
ImGui::StyleColorsClassic();
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
// Initialize SPH simulation
simulation.InitSPH();
// std::cout << "Particle count at start: " << simulation.particleCount << std::endl;
// Test ability to map host memory (from some page-lock tutorial)
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, 0);
if (!deviceProp.canMapHostMemory) {
fprintf(stderr, "Device %d cannot map host memory!\n", 0);
exit(EXIT_FAILURE);
}
// Loop until the user closes the window
while (!glfwWindowShouldClose(window))
{
// Process GLFW events eg. keypress, widnow resize
glfwPollEvents();
// Compute time between frames
t = glfwGetTime();
dt = t - t_old;
t_old = t;
// Update simulation
simulation.Update(dt);
// Render simulation
Render();
// Render ImGUI control panel
RenderGUIControlPanel();
glfwSwapBuffers(window);
}
// ImGUI Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwTerminate();
return 0;
}