| 10 |
10 |
mNumParticles( 0 )
|
| 11 |
11 |
|
| 12 |
12 |
{
|
|
13 |
// uses Cinder (and OpenGL) virtual buffer object based drawing
|
|
14 |
// see ParticleSphereCPU example in Cinder library
|
|
15 |
|
| 13 |
16 |
mParticles.assign( kMaxParticles, Particle() );
|
| 14 |
17 |
mParticlePositions.assign( kMaxParticles, vec2( -1, -1 ) );
|
| 15 |
18 |
|
| ... | ... | |
| 20 |
23 |
|
| 21 |
24 |
auto mesh = gl::VboMesh::create( mParticlePositions.size(), GL_POINTS, { { particleLayout, mParticleVbo } } );
|
| 22 |
25 |
|
|
26 |
// creates glsl program to run the batch with
|
| 23 |
27 |
#if ! defined( CINDER_GL_ES )
|
| 24 |
28 |
auto glsl = gl::GlslProg::create( gl::GlslProg::Format()
|
| 25 |
29 |
.vertex( CI_GLSL( 150,
|
| ... | ... | |
| 70 |
74 |
|
| 71 |
75 |
void ParticleController::updateParticles()
|
| 72 |
76 |
{
|
|
77 |
// update the positions of the particles and dispose them if they're reached their timespan
|
| 73 |
78 |
for ( size_t i = 0; i < mNumParticles; i++ ){
|
| 74 |
79 |
|
| 75 |
80 |
Particle &particle = mParticles[i];
|
| ... | ... | |
| 95 |
100 |
}
|
| 96 |
101 |
}
|
| 97 |
102 |
|
|
103 |
// Copy particle data onto the GPU.
|
|
104 |
// Map the GPU memory and write over it.
|
| 98 |
105 |
void *gpuMem = mParticleVbo->mapReplace();
|
| 99 |
106 |
memcpy( gpuMem, mParticlePositions.data(), mParticlePositions.size() * sizeof( vec2 ) );
|
| 100 |
107 |
mParticleVbo->unmap();
|
| ... | ... | |
| 102 |
109 |
|
| 103 |
110 |
void ParticleController::addParticles(int amount, const vec2 &initialLocation, const float cloudSize)
|
| 104 |
111 |
{
|
|
112 |
// reduce the particles liearly to the total number of particles already present
|
|
113 |
// the more particles aleary present the less particle are added
|
| 105 |
114 |
int reduction = ci::lmap<int>(mNumParticles, 0, kMaxParticles, 0, kMaxParticleAdd);
|
| 106 |
115 |
amount -= reduction;
|
| 107 |
116 |
|
| 108 |
117 |
if ( mNumParticles + amount > kMaxParticles ){
|
| 109 |
|
//return;
|
|
118 |
//a.k.a. return if reached kMaxParticles
|
| 110 |
119 |
amount = kMaxParticles - mNumParticles;
|
| 111 |
120 |
}
|
| 112 |
121 |
|