-
Notifications
You must be signed in to change notification settings - Fork 0
/
particleSystem.h
135 lines (99 loc) · 3.97 KB
/
particleSystem.h
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
/*
* 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.
*
*/
#ifndef __PARTICLESYSTEM_H__
#define __PARTICLESYSTEM_H__
#define DEBUG_GRID 0
#define DO_TIMING 0
#include "particles_kernel.cuh"
#include "vector_functions.h"
// Particle system class
class ParticleSystem
{
public:
ParticleSystem(uint numParticles, uint3 gridSize, bool bUseOpenGL);
~ParticleSystem();
enum ParticleConfig
{
CONFIG_RANDOM,
CONFIG_GRID,
_NUM_CONFIGS
};
enum ParticleArray
{
POSITION,
VELOCITY,
};
void update(float deltaTime);
void reset(ParticleConfig config);
float* getArray(ParticleArray array);
void setArray(ParticleArray array, const float* data, int start, int count);
int getNumParticles() const { return m_numParticles; }
unsigned int getCurrentReadBuffer() const { return m_posVbo; }
unsigned int getColorBuffer() const { return m_colorVBO; }
void * getCudaPosVBO() const { return (void *)m_cudaPosVBO; }
void * getCudaColorVBO() const { return (void *)m_cudaColorVBO; }
void dumpGrid();
void dumpParticles(uint start, uint count);
void setIterations(int i) { m_solverIterations = i; }
void setDamping(float x) { m_params.globalDamping = x; }
void setGravity(float x) { m_params.gravity = make_float3(0.0f, x, 0.0f); }
void setCollideSpring(float x) { m_params.spring = x; }
void setCollideDamping(float x) { m_params.damping = x; }
void setCollideShear(float x) { m_params.shear = x; }
void setCollideAttraction(float x) { m_params.attraction = x; }
void setColliderPos(float3 x) { m_params.colliderPos = x; }
float getParticleRadius() { return m_params.particleRadius; }
float3 getColliderPos() { return m_params.colliderPos; }
float getColliderRadius() { return m_params.colliderRadius; }
uint3 getGridSize() { return m_params.gridSize; }
float3 getWorldOrigin() { return m_params.worldOrigin; }
float3 getCellSize() { return m_params.cellSize; }
void addSphere(int index, float *pos, float *vel, int r, float spacing);
protected: // methods
ParticleSystem() {}
uint createVBO(uint size);
void _initialize(int numParticles);
void _finalize();
void initGrid(uint *size, float spacing, float jitter, uint numParticles);
protected: // data
bool m_bInitialized, m_bUseOpenGL;
uint m_numParticles;
// CPU data
float* m_hPos; // particle positions
float* m_hVel; // particle velocities
uint* m_hParticleHash;
uint* m_hCellStart;
uint* m_hCellEnd;
// GPU data
float* m_dPos;
float* m_dVel;
float* m_dSortedPos;
float* m_dSortedVel;
// grid data for sorting method
uint* m_dGridParticleHash; // grid hash value for each particle
uint* m_dGridParticleIndex;// particle index for each particle
uint* m_dCellStart; // index of start of each cell in sorted list
uint* m_dCellEnd; // index of end of cell
uint m_gridSortBits;
uint m_posVbo; // vertex buffer object for particle positions
uint m_colorVBO; // vertex buffer object for colors
float *m_cudaPosVBO; // these are the CUDA deviceMem Pos
float *m_cudaColorVBO; // these are the CUDA deviceMem Color
struct cudaGraphicsResource *m_cuda_posvbo_resource; // handles OpenGL-CUDA exchange
struct cudaGraphicsResource *m_cuda_colorvbo_resource; // handles OpenGL-CUDA exchange
// params
SimParams m_params;
uint3 m_gridSize;
uint m_numGridCells;
uint m_timer;
uint m_solverIterations;
};
#endif // __PARTICLESYSTEM_H__