-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfluidsGL.cpp
More file actions
353 lines (273 loc) · 7.48 KB
/
Copy pathfluidsGL.cpp
File metadata and controls
353 lines (273 loc) · 7.48 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
// Includes
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <chrono>
#if defined(__APPLE__) || defined(MACOSX)
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#include <GLUT/glut.h>
//#include <freeglut.h>
//#include <glew.h>
#ifndef glutCloseFunc
#define glutCloseFunc glutWMCloseFunc
#endif
#else
#include <GL/freeglut.h>
#endif
#include "defines.h"
#include "fluidsGL_kernels.h"
#include <algorithm>
const char *sSDKname = "fluidsGL";
void cleanup(void);
void reshape(int x, int y);
static cData *vxfield = NULL;
static cData *vyfield = NULL;
cData *hvfield = NULL;
static int wWidth = std::max(4*512, DIM);
static int wHeight = std::max(2*512, DIM);
static int clicked = 0;
static int fpsCount = 0;
static int fpsLimit = 1;
static std::chrono::high_resolution_clock::time_point t1 ;
static std::chrono::high_resolution_clock::time_point t2;
static std::chrono::duration<double> time_span = std::chrono::milliseconds::zero();
GLuint vbo = 0;
// Particle data
cData* buffer = 0; // OpenGL vertex buffer object
static cData *particles = NULL; // particle positions in host memory
static int lastx = 0, lasty = 0;
// Texture pitch
size_t tPitch = 0; // Now this is compatible with gcc in 64-bit
char *ref_file = NULL;
bool g_bQAAddTestForce = true;
int g_iFrameToCompare = 100;
int g_TotalErrors = 0;
bool g_bExitESC = false;
void autoTest(char **);
extern "C" void addForces(cData *v, int dx, int dy, int spx, int spy, float fx, float fy, int r);
extern "C" void advectVelocity(cData *v, float *vx, float *vy, int dx, int pdx, int dy, float dt);
extern "C" void diffuseProject(cData *vx, cData *vy, int dx, int dy, float dt, float visc);
extern "C" void updateVelocity(cData *v, float *vx, float *vy, int dx, int pdx, int dy);
extern "C" void advectParticles(cData *partBuffer, cData *v, int dx, int dy, float dt);
void simulateFluids(void)
{
// simulate fluid
advectVelocity(hvfield, (float *)vxfield, (float *)vyfield, DIM, RPADW, DIM, DT);
diffuseProject(vxfield, vyfield, CPADW, DIM, DT, VIS);
updateVelocity(hvfield, (float *)vxfield, (float *)vyfield, DIM, RPADW, DIM);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
buffer = (cData*) glMapBuffer(GL_ARRAY_BUFFER,GL_READ_WRITE);
glBindBuffer(GL_ARRAY_BUFFER, 0);
advectParticles(buffer, hvfield, DIM, DIM, DT);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void display(void)
{
if (!ref_file)
{
t1 = std::chrono::high_resolution_clock::now();
simulateFluids();
}
// render points from vertex buffer
glClear(GL_COLOR_BUFFER_BIT);
// particle color:
//glColor4f(0,1,0,0.5f);
glClearColor(1.f/256*172, 1.f/256*101, 1.f/256*4, 0.f);
glColor4f(1.f, 1.f, 1.f, 0.5f);
glPointSize(1);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnableClientState(GL_VERTEX_ARRAY);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(2, GL_FLOAT, 0, NULL);
glDrawArrays(GL_POINTS, 0, DS);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
if (ref_file)
{
return;
}
// Finish timing before swap buffers to avoid refresh sync
t2 = std::chrono::high_resolution_clock::now();
time_span += std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1);
glutSwapBuffers();
fpsCount++;
if (fpsCount == fpsLimit)
{
char fps[256];
float ifps = 1.f / ((time_span).count()/ fpsCount);
sprintf(fps, "Cuda/GL Stable Fluids (%d x %d): %3.1f fps", DIM, DIM, ifps);
glutSetWindowTitle(fps);
fpsCount = 0;
time_span = time_span.zero();
fpsLimit = (int)std::max(ifps, 1.f);
//sdkResetTimer(&timer);
}
glutPostRedisplay();
}
float myrand(void)
{
static int seed = 72191;
char sq[22];
if (ref_file)
{
seed *= seed;
sprintf(sq, "%010d", seed);
// pull the middle 5 digits out of sq
sq[8] = 0;
seed = atoi(&sq[3]);
return seed/99999.f;
}
else
{
return rand()/(float)RAND_MAX;
}
}
void initParticles(cData *p, int dx, int dy)
{
int i, j;
for (i = 0; i < dy; i++)
{
for (j = 0; j < dx; j++)
{
p[i*dx+j].x = (j+0.5f+(myrand() - 0.5f))/dx;
p[i*dx+j].y = (i+0.5f+(myrand() - 0.5f))/dy;
}
}
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
g_bExitESC = true;
#if defined (__APPLE__) || defined(MACOSX)
exit(EXIT_SUCCESS);
#else
glutDestroyWindow(glutGetWindow());
return;
#endif
break;
case 'r':
memset(hvfield, 0, sizeof(cData) * DS);
initParticles(particles, DIM, DIM);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(cData) * DS,
particles, GL_DYNAMIC_DRAW_ARB);
glBindBuffer(GL_ARRAY_BUFFER, 0);
break;
default:
break;
}
}
void click(int button, int updown, int x, int y)
{
lastx = x;
lasty = y;
clicked = !clicked;
}
void motion(int x, int y)
{
// Convert motion coordinates to domain
float fx = (lastx / (float)wWidth);
float fy = (lasty / (float)wHeight);
int nx = (int)(fx * DIM);
int ny = (int)(fy * DIM);
if (clicked && nx < DIM-FR && nx > FR-1 && ny < DIM-FR && ny > FR-1)
{
int ddx = x - lastx;
int ddy = y - lasty;
fx = ddx / (float)wWidth;
fy = ddy / (float)wHeight;
int spy = ny-FR;
int spx = nx-FR;
addForces(hvfield, DIM, DIM, spx, spy, FORCE * DT * fx, FORCE * DT * fy, FR);
lastx = x;
lasty = y;
}
glutPostRedisplay();
}
void reshape(int x, int y)
{
wWidth = x;
wHeight = y;
glViewport(0, 0, x, y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1, 1, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutPostRedisplay();
}
void cleanup(void)
{
// Free all host and device resources
free(hvfield);
free(particles);
free(vxfield);
free(vyfield);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &vbo);
}
int initGL(int *argc, char **argv)
{
glutInit(argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(wWidth, wHeight);
glutCreateWindow("Compute Stable Fluids");
// background color
glClearColor(0, 0, 0, 1.f);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(click);
glutMotionFunc(motion);
glutReshapeFunc(reshape);
return true;
}
int main(int argc, char **argv)
{
int devID;
#if defined(__linux__)
setenv ("DISPLAY", ":0", 0);
#endif
printf("%s Starting...\n\n", sSDKname);
// First initialize OpenGL context, so we can properly set the GL for CUDA.
// This is necessary in order to achieve optimal performance with OpenGL/CUDA interop.
if (false == initGL(&argc, argv))
{
exit(EXIT_SUCCESS);
}
GLint bsize;
hvfield = (cData *)malloc(sizeof(cData) * DS);
memset(hvfield, 0, sizeof(cData) * DS);
vxfield = (cData *)malloc(sizeof(cData) * PDS);
vyfield = (cData *)malloc(sizeof(cData) * PDS);
particles = (cData *)malloc(sizeof(cData) * DS);
memset(particles, 0, sizeof(cData) * DS);
initParticles(particles, DIM, DIM);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(cData) * DS,
particles, GL_DYNAMIC_DRAW_ARB);
glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &bsize);
if (bsize != (sizeof(cData) * DS))
goto EXTERR;
glBindBuffer(GL_ARRAY_BUFFER, 0);
#if defined (__APPLE__) || defined(MACOSX)
atexit(cleanup);
#else
glutCloseFunc(cleanup);
#endif
glutMainLoop();
return 0;
EXTERR:
printf("Failed to initialize GL extensions.\n");
exit(EXIT_FAILURE);
}