comparison CollidoscopeApp/src/ParticleController.cpp @ 4:ab6db404403a

commented JackDevice
author Fiore Martin <f.martin@qmul.ac.uk>
date Wed, 13 Jul 2016 12:31:37 +0200
parents 02467299402e
children 75b744078d66
comparison
equal deleted inserted replaced
3:7fb593d53361 4:ab6db404403a
8 8
9 ParticleController::ParticleController() : 9 ParticleController::ParticleController() :
10 mNumParticles( 0 ) 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 mParticles.assign( kMaxParticles, Particle() ); 16 mParticles.assign( kMaxParticles, Particle() );
14 mParticlePositions.assign( kMaxParticles, vec2( -1, -1 ) ); 17 mParticlePositions.assign( kMaxParticles, vec2( -1, -1 ) );
15 18
16 mParticleVbo = gl::Vbo::create( GL_ARRAY_BUFFER, mParticlePositions, GL_DYNAMIC_DRAW ); 19 mParticleVbo = gl::Vbo::create( GL_ARRAY_BUFFER, mParticlePositions, GL_DYNAMIC_DRAW );
17 20
18 geom::BufferLayout particleLayout; 21 geom::BufferLayout particleLayout;
19 particleLayout.append( geom::Attrib::POSITION, 2, sizeof( vec2 ), 0 ); 22 particleLayout.append( geom::Attrib::POSITION, 2, sizeof( vec2 ), 0 );
20 23
21 auto mesh = gl::VboMesh::create( mParticlePositions.size(), GL_POINTS, { { particleLayout, mParticleVbo } } ); 24 auto mesh = gl::VboMesh::create( mParticlePositions.size(), GL_POINTS, { { particleLayout, mParticleVbo } } );
22 25
26 // creates glsl program to run the batch with
23 #if ! defined( CINDER_GL_ES ) 27 #if ! defined( CINDER_GL_ES )
24 auto glsl = gl::GlslProg::create( gl::GlslProg::Format() 28 auto glsl = gl::GlslProg::create( gl::GlslProg::Format()
25 .vertex( CI_GLSL( 150, 29 .vertex( CI_GLSL( 150,
26 uniform mat4 ciModelViewProjection; 30 uniform mat4 ciModelViewProjection;
27 in vec4 ciPosition; 31 in vec4 ciPosition;
68 72
69 } 73 }
70 74
71 void ParticleController::updateParticles() 75 void ParticleController::updateParticles()
72 { 76 {
77 // update the positions of the particles and dispose them if they're reached their timespan
73 for ( size_t i = 0; i < mNumParticles; i++ ){ 78 for ( size_t i = 0; i < mNumParticles; i++ ){
74 79
75 Particle &particle = mParticles[i]; 80 Particle &particle = mParticles[i];
76 vec2 &pos = mParticlePositions[i]; 81 vec2 &pos = mParticlePositions[i];
77 82
93 if ( ci::distance( pos, particle.mCloudCenter ) > particle.mCloudSize && !particle.mFlyOver ){ 98 if ( ci::distance( pos, particle.mCloudCenter ) > particle.mCloudSize && !particle.mFlyOver ){
94 particle.mVel = rotate<float>( particle.mVel, 5 ); 99 particle.mVel = rotate<float>( particle.mVel, 5 );
95 } 100 }
96 } 101 }
97 102
103 // Copy particle data onto the GPU.
104 // Map the GPU memory and write over it.
98 void *gpuMem = mParticleVbo->mapReplace(); 105 void *gpuMem = mParticleVbo->mapReplace();
99 memcpy( gpuMem, mParticlePositions.data(), mParticlePositions.size() * sizeof( vec2 ) ); 106 memcpy( gpuMem, mParticlePositions.data(), mParticlePositions.size() * sizeof( vec2 ) );
100 mParticleVbo->unmap(); 107 mParticleVbo->unmap();
101 } 108 }
102 109
103 void ParticleController::addParticles(int amount, const vec2 &initialLocation, const float cloudSize) 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 int reduction = ci::lmap<int>(mNumParticles, 0, kMaxParticles, 0, kMaxParticleAdd); 114 int reduction = ci::lmap<int>(mNumParticles, 0, kMaxParticles, 0, kMaxParticleAdd);
106 amount -= reduction; 115 amount -= reduction;
107 116
108 if ( mNumParticles + amount > kMaxParticles ){ 117 if ( mNumParticles + amount > kMaxParticles ){
109 //return; 118 //a.k.a. return if reached kMaxParticles
110 amount = kMaxParticles - mNumParticles; 119 amount = kMaxParticles - mNumParticles;
111 } 120 }
112 121
113 if( amount <= 0 ) 122 if( amount <= 0 )
114 return; 123 return;