-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.cpp
More file actions
318 lines (241 loc) · 8.07 KB
/
Simulation.cpp
File metadata and controls
318 lines (241 loc) · 8.07 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
#define GLM_FORCE_RADIANS
#define BUFFER_OFFSET(i) (reinterpret_cast<void*>(i))
#include <string>
#ifdef TARGET_OS_MAC // MAC
std::string platform = "MAC";
// TODO: Include Mac Headers here
#elif defined __linux__ // LINUX
std::string platform = "LINUX";
#include "Aluminum/Includes.hpp"
#include "Aluminum/Program.hpp"
#include "Aluminum/MeshBuffer.hpp"
#include "Aluminum/MeshData.hpp"
#include "Aluminum/Shapes.hpp"
#include "Aluminum/Camera.hpp"
#include "Aluminum/Utils.hpp"
#include "Aluminum/MeshUtils.hpp"
#include "Aluminum/FBO.hpp"
#include "Aluminum/Behavior.hpp"
#include "Aluminum/ResourceHandler.hpp"
#include "Aluminum/Texture.hpp"
#include "Aluminum/RendererLinux.hpp"
#elif defined _WIN32 || defined _WIN64
std::string platform = "WINDOWS";
#else
#error "unknown platform"
#endif
#include "sphModel.hpp"
using glm::vec3;
using glm::mat4;
float pi = glm::pi<float>();
using namespace aluminum;
// TODO: pass in only points to shader and use geometry shader to create 3d particles
// TODO: see 3.5.1: flowing water and particle effects, stream output
class Simulation : public RendererLinux {
public:
static const unsigned N = 400;
unsigned M = 0;
ResourceHandler rh;
Camera camera;
Program program;
GLint posLoc = 0;
GLint normalLoc = 1;
GLint colLoc = 2;
MeshBuffer* mb;
mat4 view, proj;
Behavior rotateBehavior;
bool gravityOn;
bool paused = false;
SPH fluidsimulation = SPH(N); // Initialize Fluid simulation model with N particles
enum show { FLUID, FLUIDANDOBJECT, FLUIDANDOBJECTANDWALL };
unsigned showParticles = show::FLUIDANDOBJECTANDWALL;
enum ccodes { METABALLS, NONE, SPEED, VELOCITY, PRESSURE, DENSITY };
unsigned colorcoding = ccodes::NONE;
void onCreate() {
// Output Simulation state
std::cout << "\nModel Parameters after Initialization:\n" << fluidsimulation;
rh.loadProgram(program, "resources/simulation", posLoc, normalLoc, -1, colLoc);
M = fluidsimulation.getTotalParticles();
mb = new MeshBuffer[M];
for(unsigned i=0; i<M; ++i) {
MeshData md;
// make sure assertion for r>0 is passed
float r = std::max(fluidsimulation.getRadius(i),.01f);
// addCube(md,r);
addSphere(md,r,8,8);
mb[i].init(md,posLoc,normalLoc,-1,colLoc);
}
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, width, height);
rotateBehavior = Behavior(now()).delay(1000).length(5000).range(vec3(3.14, 3.14, 3.14)).reversing(true).repeats(-1).linear();
camera = Camera(glm::radians(60.0),1.,0.01,1000.0);
camera.translateZ(-400);
gravityOn = false;
}
void loadProgram(Program &p, const std::string& name) {
p.create();
p.attach(p.loadText(name + ".vsh"), GL_VERTEX_SHADER);
glBindAttribLocation(p.id(), posLoc, "vertexPosition");
// glBindAttribLocation(p.id(), colLoc, "vertexColor");
glBindAttribLocation(p.id(), normalLoc, "vertexNormal");
p.attach(p.loadText(name + ".fsh"), GL_FRAGMENT_SHADER);
p.link();
}
void onFrame(){
std::string displayStr;
std::string colorcodingStr;
switch(colorcoding) {
case ccodes::METABALLS:
colorcodingStr = "Metaballs";
break;
case ccodes::NONE:
colorcodingStr = "None";
break;
case ccodes::SPEED:
colorcodingStr = "Speed";
break;
case ccodes::VELOCITY:
colorcodingStr = "Velocity";
break;
case ccodes::PRESSURE:
colorcodingStr = "Pressure";
break;
case ccodes::DENSITY:
colorcodingStr = "Density";
break;
}
switch(showParticles) {
case show::FLUID: // Render fluid only
M = fluidsimulation.getFluidParticles();
displayStr = "Fluid";
break;
case show::FLUIDANDOBJECT: // Render fluid and immersed object
M = fluidsimulation.getObjectParticles();
displayStr = "Fluid + Object";
break;
case show::FLUIDANDOBJECTANDWALL: // Render all particles
M = fluidsimulation.getTotalParticles();
displayStr = "Fluid + Object + Walls";
break;
}
// PROPAGATE MODEL AND OUTPUT
if(!paused) {
// if(fluidsimulation.getTime() < 10.0 && !paused) {
// if(fluidsimulation.getTimeStepNumber() < 10 && !paused) {
fluidsimulation.timestep(.01); // Propagate fluidsimulation in time
std::cout << "\nDisplay: " << displayStr << ",\tColorcoding: " << colorcodingStr;
std::cout << fluidsimulation; // Output current status of Fluid particles
}
// Start rendering
glViewport(0, 0, width, height);
glClearColor(0,0,0,1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (camera.isTransformed) {
camera.transform();
}
float position[3];
float velocity[3];
vec3 normalizedVelocity;
float vmax = fluidsimulation.getVmax();
unsigned col;
unsigned Nobj = fluidsimulation.getObjectParticles();
for(unsigned i=0; i<M; ++i) {
program.bind(); {
col = colorcoding;
if(i>=N) col = 101;
if(i>=Nobj) col = 102;
mat4 model = mat4(1.0);
fluidsimulation.getPosition(i,position);
fluidsimulation.getVelocity(i,velocity);
normalizedVelocity = vec3(velocity[0],velocity[1],velocity[2]);
normalizedVelocity = .5f*(vec3(1.f)+normalizedVelocity/vmax);
model = glm::translate(model,vec3(position[0],position[1],position[2]));
glUniformMatrix4fv(program.uniform("model"), 1, 0, ptr(model));
glUniformMatrix4fv(program.uniform("view"), 1, 0, ptr(camera.view));
glUniformMatrix4fv(program.uniform("proj"), 1, 0, ptr(camera.projection));
glUniform1i(program.uniform("ccflag"),col);
glUniform3fv(program.uniform("velocity"), 1, ptr(normalizedVelocity));
glUniform1f(program.uniform("mass"), fluidsimulation.getMass(i));
mb[i].draw();
} program.unbind();
}
}
// Keyboard Interaction
void specialkeys(int key, int x, int y) {
// For interaction with MacOS Keycodes, replace '|| false' by '|| KEYCODE'
// Rotating the Camera
if(key == GLUT_KEY_UP || false) {
camera.rotateX(glm::radians(-2.));
} else if(key == GLUT_KEY_DOWN || false) {
camera.rotateX(glm::radians(2.));
} else if(key == GLUT_KEY_RIGHT || false) {
camera.rotateY(glm::radians(+2.));
} else if(key == GLUT_KEY_LEFT || false) {
camera.rotateY(glm::radians(-2.));
}
}
void keyboard(unsigned char key, int x, int y) {
float dxCamera = 5;
float dxBox = 1;
// For interaction with MacOS Keycodes, replace '|| false' by '|| KEYCODE'
if(key == ' ' || false) {
showParticles = (showParticles+1)%3;
// Moving the Cube
} else if(key == 'q' || false) {
fluidsimulation.moveBox(-dxBox,SPH::_axis::X1);
} else if(key == 'w' || false) {
fluidsimulation.moveBox(+dxBox,SPH::_axis::X1);
} else if(key == 'a' || false) {
fluidsimulation.moveBox(-dxBox,SPH::_axis::X2);
} else if(key == 's' || false) {
fluidsimulation.moveBox(+dxBox,SPH::_axis::X2);
} else if(key == 'z' || false) {
fluidsimulation.moveBox(-dxBox,SPH::_axis::X3);
} else if(key == 'x' || false) {
fluidsimulation.moveBox(+dxBox,SPH::_axis::X3);
// Moving the Camera
} else if(key == 'e' || false) {
camera.translateX(-dxCamera);
} else if(key == 'r' || false) {
camera.translateX(+dxCamera);
} else if(key == 'd' || false) {
camera.translateZ(+dxCamera);
} else if(key == 'f' || false) {
camera.translateZ(-dxCamera);
} else if(key == 'c' || false) {
camera.translateY(-dxCamera);
} else if(key == 'v' || false) {
camera.translateY(+dxCamera);
// Toggle Gravity
} else if(key == 'g' || false) {
if(gravityOn) {
fluidsimulation.setGravity(0);
gravityOn = false;
} else {
fluidsimulation.setGravity(-200);
gravityOn = true;
}
// Pause Simulation
} else if(key == 'p' || false) {
paused = !paused;
// Switch between colorcoding schemes
} else if(key == '0' || false) {
colorcoding = ccodes::METABALLS;
} else if(key == '9' || false) {
colorcoding = ccodes::NONE;
} else if(key == '8' || false) {
colorcoding = ccodes::SPEED;
} else if(key == '7' || false) {
colorcoding = ccodes::VELOCITY;
} else if(key == '6' || false) {
colorcoding = ccodes::PRESSURE;
} else if(key == '5' || false) {
colorcoding = ccodes::DENSITY;
}
}
};
int main(){
std::cout << "\n\nRunning on Platform: " << platform << "\n\n";
Simulation().start();
return 0;
}