-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticleSystem.cpp
456 lines (386 loc) · 12 KB
/
particleSystem.cpp
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
/*
* Copyright 1993-2010 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*
*/
#include "particleSystem.h"
#include "particleSystem.cuh"
#include "particles_kernel.cuh"
#include <cutil_inline.h> // includes cuda.h and cuda_runtime_api.h
#include <assert.h>
#include <math.h>
#include <memory.h>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <GL/glew.h>
#ifndef CUDART_PI_F
#define CUDART_PI_F 3.141592654f
#endif
ParticleSystem::ParticleSystem(uint numParticles, uint3 gridSize, bool bUseOpenGL) :
m_bInitialized(false),
m_bUseOpenGL(bUseOpenGL),
m_numParticles(numParticles),
m_hPos(0),
m_hVel(0),
m_dPos(0),
m_dVel(0),
m_gridSize(gridSize),
m_timer(0),
m_solverIterations(1)
{
m_numGridCells = m_gridSize.x*m_gridSize.y*m_gridSize.z;
float3 worldSize = make_float3(2.0f, 2.0f, 2.0f);
m_gridSortBits = 18; // increase this for larger grids
// set simulation parameters
m_params.gridSize = m_gridSize;
m_params.numCells = m_numGridCells;
m_params.numBodies = m_numParticles;
m_params.particleRadius = 1.0f / 80.0f;
m_params.colliderPos = make_float3(-1.2f, -0.8f, 0.8f);
m_params.colliderRadius = 0.2f;
m_params.worldOrigin = make_float3(-1.0f, -1.0f, -1.0f);
// m_params.cellSize = make_float3(worldSize.x / m_gridSize.x, worldSize.y / m_gridSize.y, worldSize.z / m_gridSize.z);
float cellSize = m_params.particleRadius * 2.0f; // cell size equal to particle diameter
m_params.cellSize = make_float3(cellSize, cellSize, cellSize);
m_params.spring = 0.01f;
m_params.damping = 0.02f;
m_params.shear = 0.1f;
m_params.attraction = 0.0f;
//m_params.boundaryDamping = -0.5f;
m_params.boundaryDamping = 0.1f;
m_params.gravity = make_float3(0.0f, -0.0003f, 0.0f);
m_params.globalDamping = 1.0f;
_initialize(numParticles);
}
ParticleSystem::~ParticleSystem()
{
_finalize();
m_numParticles = 0;
}
uint
ParticleSystem::createVBO(uint size)
{
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
return vbo;
}
inline float lerp(float a, float b, float t)
{
return a + t*(b-a);
}
// create a color ramp
void colorRamp(float t, float *r)
{
const int ncolors = 7;
float c[ncolors][3] = {
{ 1.0, 0.0, 0.0, },
{ 1.0, 0.5, 0.0, },
{ 1.0, 1.0, 0.0, },
{ 0.0, 1.0, 0.0, },
{ 0.0, 1.0, 1.0, },
{ 0.0, 0.0, 1.0, },
{ 1.0, 0.0, 1.0, },
};
t = t * (ncolors-1);
int i = (int) t;
float u = t - floor(t);
r[0] = lerp(c[i][0], c[i+1][0], u);
r[1] = lerp(c[i][1], c[i+1][1], u);
r[2] = lerp(c[i][2], c[i+1][2], u);
}
void
ParticleSystem::_initialize(int numParticles)
{
assert(!m_bInitialized);
m_numParticles = numParticles;
// allocate host storage
m_hPos = new float[m_numParticles*4];
m_hVel = new float[m_numParticles*4];
memset(m_hPos, 0, m_numParticles*4*sizeof(float));
memset(m_hVel, 0, m_numParticles*4*sizeof(float));
m_hCellStart = new uint[m_numGridCells];
memset(m_hCellStart, 0, m_numGridCells*sizeof(uint));
m_hCellEnd = new uint[m_numGridCells];
memset(m_hCellEnd, 0, m_numGridCells*sizeof(uint));
// allocate GPU data
unsigned int memSize = sizeof(float) * 4 * m_numParticles;
if (m_bUseOpenGL) {
m_posVbo = createVBO(memSize);
registerGLBufferObject(m_posVbo, &m_cuda_posvbo_resource);
} else {
cutilSafeCall( cudaMalloc( (void **)&m_cudaPosVBO, memSize )) ;
}
allocateArray((void**)&m_dVel, memSize);
allocateArray((void**)&m_dSortedPos, memSize);
allocateArray((void**)&m_dSortedVel, memSize);
allocateArray((void**)&m_dGridParticleHash, m_numParticles*sizeof(uint));
allocateArray((void**)&m_dGridParticleIndex, m_numParticles*sizeof(uint));
allocateArray((void**)&m_dCellStart, m_numGridCells*sizeof(uint));
allocateArray((void**)&m_dCellEnd, m_numGridCells*sizeof(uint));
if (m_bUseOpenGL) {
m_colorVBO = createVBO(m_numParticles*4*sizeof(float));
registerGLBufferObject(m_colorVBO, &m_cuda_colorvbo_resource);
// fill color buffer
glBindBufferARB(GL_ARRAY_BUFFER, m_colorVBO);
float *data = (float *) glMapBufferARB(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
float *ptr = data;
for(uint i=0; i<m_numParticles; i++) {
float t = i / (float) m_numParticles;
#if 0
*ptr++ = rand() / (float) RAND_MAX;
*ptr++ = rand() / (float) RAND_MAX;
*ptr++ = rand() / (float) RAND_MAX;
#else
colorRamp(t, ptr);
ptr+=3;
#endif
*ptr++ = 1.0f;
}
glUnmapBufferARB(GL_ARRAY_BUFFER);
} else {
cutilSafeCall( cudaMalloc( (void **)&m_cudaColorVBO, sizeof(float)*numParticles*4) );
}
cutilCheckError(cutCreateTimer(&m_timer));
setParameters(&m_params);
m_bInitialized = true;
}
void
ParticleSystem::_finalize()
{
assert(m_bInitialized);
delete [] m_hPos;
delete [] m_hVel;
delete [] m_hCellStart;
delete [] m_hCellEnd;
freeArray(m_dVel);
freeArray(m_dSortedPos);
freeArray(m_dSortedVel);
freeArray(m_dGridParticleHash);
freeArray(m_dGridParticleIndex);
freeArray(m_dCellStart);
freeArray(m_dCellEnd);
if (m_bUseOpenGL) {
unregisterGLBufferObject(m_cuda_posvbo_resource);
glDeleteBuffers(1, (const GLuint*)&m_posVbo);
glDeleteBuffers(1, (const GLuint*)&m_colorVBO);
} else {
cutilSafeCall( cudaFree(m_cudaPosVBO) );
cutilSafeCall( cudaFree(m_cudaColorVBO) );
}
}
// step the simulation
void
ParticleSystem::update(float deltaTime)
{
assert(m_bInitialized);
float *dPos;
if (m_bUseOpenGL) {
dPos = (float *) mapGLBufferObject(&m_cuda_posvbo_resource);
} else {
dPos = (float *) m_cudaPosVBO;
}
// update constants
setParameters(&m_params);
// integrate
integrateSystem(
dPos,
m_dVel,
deltaTime,
m_numParticles);
// calculate grid hash
calcHash(
m_dGridParticleHash,
m_dGridParticleIndex,
dPos,
m_numParticles);
// sort particles based on hash
sortParticles(m_dGridParticleHash, m_dGridParticleIndex, m_numParticles);
// reorder particle arrays into sorted order and
// find start and end of each cell
reorderDataAndFindCellStart(
m_dCellStart,
m_dCellEnd,
m_dSortedPos,
m_dSortedVel,
m_dGridParticleHash,
m_dGridParticleIndex,
dPos,
m_dVel,
m_numParticles,
m_numGridCells);
// process eject particles
/*eject(
m_dVel,
m_dSortedPos,
m_dSortedVel,
m_dGridParticleIndex,
m_dCellStart,
m_dCellEnd,
m_numParticles,
m_numGridCells);*/
// process collisions
collide(
m_dVel,
m_dSortedPos,
m_dSortedVel,
m_dGridParticleIndex,
m_dCellStart,
m_dCellEnd,
m_numParticles,
m_numGridCells);
// note: do unmap at end here to avoid unnecessary graphics/CUDA context switch
if (m_bUseOpenGL) {
unmapGLBufferObject(m_cuda_posvbo_resource);
}
}
void
ParticleSystem::dumpGrid()
{
// dump grid information
copyArrayFromDevice(m_hCellStart, m_dCellStart, 0, sizeof(uint)*m_numGridCells);
copyArrayFromDevice(m_hCellEnd, m_dCellEnd, 0, sizeof(uint)*m_numGridCells);
uint maxCellSize = 0;
for(uint i=0; i<m_numGridCells; i++) {
if (m_hCellStart[i] != 0xffffffff) {
uint cellSize = m_hCellEnd[i] - m_hCellStart[i];
// printf("cell: %d, %d particles\n", i, cellSize);
if (cellSize > maxCellSize) maxCellSize = cellSize;
}
}
printf("maximum particles per cell = %d\n", maxCellSize);
}
void
ParticleSystem::dumpParticles(uint start, uint count)
{
// debug
copyArrayFromDevice(m_hPos, 0, &m_cuda_posvbo_resource, sizeof(float)*4*count);
copyArrayFromDevice(m_hVel, m_dVel, 0, sizeof(float)*4*count);
for(uint i=start; i<start+count; i++) {
// printf("%d: ", i);
printf("pos: (%.4f, %.4f, %.4f, %.4f)\n", m_hPos[i*4+0], m_hPos[i*4+1], m_hPos[i*4+2], m_hPos[i*4+3]);
printf("vel: (%.4f, %.4f, %.4f, %.4f)\n", m_hVel[i*4+0], m_hVel[i*4+1], m_hVel[i*4+2], m_hVel[i*4+3]);
}
}
float*
ParticleSystem::getArray(ParticleArray array)
{
assert(m_bInitialized);
float* hdata = 0;
float* ddata = 0;
struct cudaGraphicsResource *cuda_vbo_resource = 0;
switch (array)
{
default:
case POSITION:
hdata = m_hPos;
ddata = m_dPos;
cuda_vbo_resource = m_cuda_posvbo_resource;
break;
case VELOCITY:
hdata = m_hVel;
ddata = m_dVel;
break;
}
copyArrayFromDevice(hdata, ddata, &cuda_vbo_resource, m_numParticles*4*sizeof(float));
return hdata;
}
void
ParticleSystem::setArray(ParticleArray array, const float* data, int start, int count)
{
assert(m_bInitialized);
switch (array)
{
default:
case POSITION:
{
if (m_bUseOpenGL) {
unregisterGLBufferObject(m_cuda_posvbo_resource);
glBindBuffer(GL_ARRAY_BUFFER, m_posVbo);
glBufferSubData(GL_ARRAY_BUFFER, start*4*sizeof(float), count*4*sizeof(float), data);
glBindBuffer(GL_ARRAY_BUFFER, 0);
registerGLBufferObject(m_posVbo, &m_cuda_posvbo_resource);
}
}
break;
case VELOCITY:
copyArrayToDevice(m_dVel, data, start*4*sizeof(float), count*4*sizeof(float));
break;
}
}
inline float frand()
{
return rand() / (float) RAND_MAX;
}
void
ParticleSystem::initGrid(uint *size, float spacing, float jitter, uint numParticles)
{
srand(1973);
for(uint z=0; z<size[2]; z++) {
for(uint y=0; y<size[1]; y++) {
for(uint x=0; x<size[0]; x++) {
uint i = (z*size[1]*size[0]) + (y*size[0]) + x;
if (i < numParticles) {
m_hPos[i*4] = (spacing * x) + m_params.particleRadius - 1.0f ; //+ (frand()*2.0f-1.0f); *jitter;
m_hPos[i*4+1] = (spacing * y) + m_params.particleRadius - 1.0f ; //+ (frand()*2.0f-1.0f); *jitter;
m_hPos[i*4+2] = (spacing * z+1) + m_params.particleRadius - 1.0f ; //+ (frand()*2.0f-1.0f); *jitter;
m_hPos[i*4+3] = 1.0f;
m_hVel[i*4] = 0.0f;
m_hVel[i*4+1] = 0.0f;
m_hVel[i*4+2] = 0.0f;
m_hVel[i*4+3] = 0.0f;
}
}
}
}
}
void
ParticleSystem::reset(ParticleConfig config)
{
switch(config)
{
default:
case CONFIG_RANDOM:
{
int p = 0, v = 0;
for(uint i=0; i < m_numParticles; i++)
{
float point[3];
point[0] = frand();
point[1] = frand();
point[2] = frand();
m_hPos[p++] = 2 * (point[0] - 0.5f);
m_hPos[p++] = 2 * (point[1] - 0.5f);
m_hPos[p++] = 2 * (point[2] - 0.5f);
m_hPos[p++] = 1.0f; // radius
m_hVel[v++] = 0.0f;
m_hVel[v++] = 0.0f;
m_hVel[v++] = 0.0f;
m_hVel[v++] = 0.0f;
}
}
break;
case CONFIG_GRID:
{
float jitter = m_params.particleRadius*0.01f;
//uint s = (int) ceilf(powf((float) m_numParticles, 1.0f / 3.0f));
uint gridSize[3];
//gridSize[0] = gridSize[1] = gridSize[2] = s;
gridSize[0] = m_gridSize.x;
gridSize[1] = m_gridSize.y;
gridSize[2] = m_gridSize.z;
initGrid(gridSize, m_params.particleRadius*2.0f, jitter, m_numParticles);
}
break;
}
setArray(POSITION, m_hPos, 0, m_numParticles);
setArray(VELOCITY, m_hVel, 0, m_numParticles);
}