Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Naive test for drawing trails
#include "cinder/app/App.h"
#include "cinder/app/RendererGl.h"
#include "cinder/gl/gl.h"
#include "cinder/Rand.h"
#include "cinder/Perlin.h"
using namespace ci;
using namespace ci::app;
using namespace std;
const static int max_trail_size = 10;
const static int number_particles = 2;
class Particle{
public:
Particle(){
}
vec2 mPos;
uint32_t indexInVbo = 0;
std::vector<vec2> previousPositions;
};
class TrailTestApp : public App {
public:
void setup() override;
void mouseMove( MouseEvent event ) override;
void update() override;
void draw() override;
std::deque<vec2> mPoints;
// std::vector< gl::VboMeshRef > mMeshes;
gl::VboRef mVbo;
gl::VboMeshRef mVboMesh;
gl::BatchRef mBatch;
std::vector<vec2> mVboPositions;
Perlin mPerlin;
std::vector<Particle>mParticles;
};
void TrailTestApp::setup()
{
// Vbo buffers
std::vector<ColorA> colors;
std::vector<uint32_t> indices;
uint32_t index = 0;
for(int i = 0; i < number_particles; i++){
Particle p;
p.mPos = vec2 { Rand::randFloat(30,200), Rand::randFloat(300, 400)};
p.indexInVbo = i;
auto& pv = p.previousPositions;
console() << "begin line ----" << std::endl;
for(int k = 0; k < max_trail_size - 1; k++){
auto pp =vec2( p.mPos.x - k*10, p.mPos.y);
// add the position to the particle trail and alsopositionsfor the VBO
pv.push_back(pp);
mVboPositions.push_back(pp);
auto c = k / float(max_trail_size);
colors.push_back( ColorA(1.0,0.0, c,1.0) );
console() << "index: " << index;
indices.push_back( index );
if( k <= max_trail_size - 2){
indices.push_back( index + 1 );
console() << " : " << index + 1;
}
console() << " < " << pp << std::endl;
index++;
}
index++;
mParticles.push_back(p);
}
vector<gl::VboMesh::Layout> layout = {
gl::VboMesh::Layout().usage( GL_STATIC_DRAW ).attrib( geom::Attrib::POSITION, 2 ),
gl::VboMesh::Layout().usage( GL_STATIC_DRAW ).attrib( geom::Attrib::COLOR, 4 )
};
auto ibo = gl::Vbo::create(GL_ELEMENT_ARRAY_BUFFER, indices);
mVboMesh = gl::VboMesh::create( mVboPositions.size(), GL_LINES, { layout }, indices.size() * sizeof(uint32_t), GL_UNSIGNED_INT, ibo);
mVboMesh->bufferAttrib<vec2>( geom::POSITION, mVboPositions );
mVboMesh->bufferAttrib<ColorA>( geom::COLOR, colors );
mBatch = gl::Batch::create( mVboMesh, gl::getStockShader( gl::ShaderDef().color() ) );
}
void TrailTestApp::mouseMove( MouseEvent event )
{
}
void TrailTestApp::update()
{
getWindow()->setTitle( to_string(getAverageFps()) );
auto mappedPosAttrib = mVboMesh->mapAttrib2f( geom::Attrib::POSITION, false );
for(int i = 0; i < mParticles.size(); i++){
mParticles[i].mPos += vec2(1, mPerlin.fBm( mParticles[i].mPos.y*0.001f, getElapsedFrames()*0.01f) * 3 );
auto& pv = mParticles[i].previousPositions;
pv[0] = mParticles[i].mPos;
// swap positions
for(int k = pv.size(); k >= 1; --k){
pv[k] = pv[k - 1];
}
// De
for(int k = 0; k < pv.size(); ++k){
mappedPosAttrib->x = pv[k].x;
mappedPosAttrib->y = pv[k].y;
++mappedPosAttrib;
}
}
mappedPosAttrib.unmap();
}
void TrailTestApp::draw()
{
gl::clear( Color( 0, 0, 0 ) );
// draw the VBO
mBatch->draw();
// Draw the trail using immediate mode for debug
gl::color(Color::white());
gl::begin(GL_POINTS);
gl::pointSize(5);
for(auto& p : mParticles){
gl::vertex( p.mPos );
auto& pv = p.previousPositions;
for(int k = pv.size() - 1; k > 0; k--){
gl::drawLine(pv[k], pv[k-1]);
}
}
gl::end();
}
CINDER_APP( TrailTestApp, RendererGl )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment