Revision 4:ab6db404403a CollidoscopeApp
| CollidoscopeApp/include/ParticleController.h | ||
|---|---|---|
| 10 | 10 |
|
| 11 | 11 |
struct Particle {
|
| 12 | 12 |
|
| 13 |
ci::vec2 mCloudCenter; |
|
| 14 |
ci::vec2 mVel; |
|
| 15 |
float mCloudSize; |
|
| 13 |
ci::vec2 mCloudCenter; // initial positin of the particle |
|
| 14 |
ci::vec2 mVel; // velocity |
|
| 15 |
float mCloudSize; // how big is the area where particle float around. When a particle hits the |
|
| 16 |
// border of the area it gets deflected |
|
| 16 | 17 |
|
| 17 |
int mAge; |
|
| 18 |
int mLifespan; |
|
| 19 |
bool mFlyOver; |
|
| 18 |
int mAge; // when mAge == mLifeSpan the particle is disposed
|
|
| 19 |
int mLifespan; // how long a particle lives
|
|
| 20 |
bool mFlyOver; // some particles last longer and fly over the screen and reach the other user
|
|
| 20 | 21 |
|
| 21 | 22 |
}; |
| 22 | 23 |
|
| ... | ... | |
| 28 | 29 |
// current number of active particles |
| 29 | 30 |
size_t mNumParticles; |
| 30 | 31 |
|
| 31 |
ci::gl::VboRef mParticleVbo; |
|
| 32 |
ci::gl::VboRef mParticleVbo; // virtual buffer object
|
|
| 32 | 33 |
ci::gl::BatchRef mParticleBatch; |
| 33 | 34 |
|
| 34 | 35 |
public: |
| CollidoscopeApp/src/ParticleController.cpp | ||
|---|---|---|
| 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 |
|
| CollidoscopeApp/src/Wave.cpp | ||
|---|---|---|
| 16 | 16 |
mChunks.emplace_back( i ); |
| 17 | 17 |
} |
| 18 | 18 |
|
| 19 |
// init cinder batch drawing |
|
| 19 | 20 |
auto lambert = gl::ShaderDef().color(); |
| 20 | 21 |
gl::GlslProgRef shader = gl::getStockShader( lambert ); |
| 21 | 22 |
mChunkBatch = gl::Batch::create( geom::Rect( ci::Rectf( 0, 0, Chunk::kWidth, 1 ) ), shader ); |
| ... | ... | |
| 63 | 64 |
|
| 64 | 65 |
double elapsed = now - itr->second.lastUpdate; |
| 65 | 66 |
|
| 67 |
// A chunk of audio corresponds to a certain time according to sample rate. |
|
| 68 |
// Use elapsed time to advance through chunks so that the cursor is animated |
|
| 69 |
// and goes from start to end of the seleciton in the time span of the grain |
|
| 66 | 70 |
itr->second.pos = mSelection.getStart() + int( elapsed / secondsPerChunk ); |
| 67 | 71 |
|
| 68 |
/* check we don't go too far off */
|
|
| 72 |
// check we don't go too far off
|
|
| 69 | 73 |
if (itr->second.pos > mSelection.getEnd()){
|
| 70 | 74 |
itr->second.pos = Cursor::kNoPosition; |
| 71 | 75 |
} |
Also available in: Unified diff