How to make a customized mesh use openFramework's renderer


I want to make a dynamic particle system similar to this blog post:
Flexible particle system container

I know that the typical way of sending particle data to the renderer in OF is to have an instance of ofMesh or ofVboMesh and then call its member function draw(). However, I still want to be able to customize the data structure of my particle system. Would it be wise to just use my own container and then call mesh.addVertices() and mesh.addColors() and then draw those? I’m not good at optimizing code yet, but I have a feeling this would give a lot of overhead. Is there perhaps a way to have my custom container call ofGlProgrammableRenderer by itself? I don’t want to have to code my own renderer…

Can anyone tell me what the best implementation is?

Many thanks!

Hello @macusual, if for “flexible” you mean that you can resize the number of particles in the particle system, try to have a look at this repository by @hahakid
Anyway, i would suggest to to start with a Particle class and a vector<Particle> container, and start to think about performance only when you reach a limit.

As davide says try to make it work in the simplest manner and if you need more performance then try to optimize it.

In any case the fastest way to do this would be to send the whole particles vector to a vbo as vertex, color and any other info using strides. for example if you have

class Particle{
    ofVec3f pos;
    ofFloatColor color;
    ofVec3f vel;
    ofVec3f acc;

vector<Particle> particles;

you can pass the whole vector to a buffer object and then use that for the different attributes of a vbo like:

ofBufferObject buffer;
ofVbo vbo;

buffer.allocate(particles, GL_STREAM_DRAW);
vbo.setVertexBuffer(buffer, 3, sizeof(Particle), 0);
vbo.setColorBuffer(buffer, sizeof(Particle), sizeof(ofVec3f));

the last 2 calls tell the ofVbo to use the same buffer for the vertices and colors. For both the stride is sizeof(Particle) so it jumps the size of the Particle in memory to find the next vertex or color and for the colors the offset is sizeof(ofVec3f) so it jumps the ofVec3f pos in the particle class to find the color.

The order of the declarations of in the particle class are important and if you change them or add some other variable in between pos and color it’ll stop working.