Followers 0

# Advice for a more efficient particle system.

## 6 posts in this topic

I need some some advice on my particle system.

Right now I have a particle emitter which holds a list of particles and each particle has a VBO for its vertices and uvs.

Which is bad I know this, it with drawing only 150 particles, I go from just over 100 draw calls, to 3000 draw calls using gDEBugger.

Each particle keeps track of its own local and world matrix.

I know the general idea is that I need to create a single VBO for each piece of data in the particle emitter, but then how to I pass the mvp for each particle, my particles are not always going to start at the same size.

Do I do something like this:

int i = 0;

// Pass in new mvp here and any other data that may have changed like colour

glDrawArrays(GL_TRIANGLE_STRIP, i, 4);

i+= 4;

My code is below:

// Particle Initialization

uvs.push_back(Vector2(1,1));
vertices.push_back(Vector3(
0+(particleInfo.sizeX/2),
0+(particleInfo.sizeY/2),
0));

uvs.push_back(Vector2(0,1));
vertices.push_back(Vector3(
0-(particleInfo.sizeX/2),
0+(particleInfo.sizeY/2),
0));

uvs.push_back(Vector2(1,0));
vertices.push_back(Vector3(
0+(particleInfo.sizeX/2),
0-(particleInfo.sizeY/2),
0));

uvs.push_back(Vector2(0,0));
vertices.push_back(Vector3(
0-(particleInfo.sizeX/2),
0-(particleInfo.sizeY/2),
0));

// This is just a bind function.

// Binding the Data

void Particle::bind()
{
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vector3), &vertices[0], GL_STATIC_DRAW);

glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(Vector2), &uvs[0], GL_STATIC_DRAW);
}

Here is where I update the particle based on the properties.

// Updating the Matrix

/* removed for space */

localMatrix = Matrix4::toMatrix4(rotation,position,scale);

Drawing the Particles

void Particle::draw()
{
if(alive)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_ALWAYS);

Camera *cam = this->particleInfo.scene->getActivePerspectiveCamera();

worldMatrix = cam->getObjectToWorld().inverse() * localMatrix;

Matrix4 mvp = cam->getProjectionMatrix() * worldMatrix;

particleInfo.colour.R,
particleInfo.colour.G,
particleInfo.colour.B,
particleInfo.colour.A);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
3,							// size
GL_FLOAT,					// type
GL_FALSE,					// normalized?
0,							// stride
(void*)0					// array buffer offset
);

// 2nd attribute buffer : UVs
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glVertexAttribPointer(
2,							// size
GL_FLOAT,					// type
GL_FALSE,					// normalized?
0,							// stride
(void*)0					// array buffer offset
);

// Draw the triangle !
glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size());

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);

glDepthFunc(GL_LESS);

glDisable(GL_BLEND);

}
}

Particle Emitter Draw

void ParticleEmitter::draw()
{
Component::draw();

if(running)
{
iter = particles.begin();

while (iter != particles.end())
{
(*iter)->draw();
++iter;
}

glBindTexture(GL_TEXTURE_2D, 0);

}
}
Edited by harshman_chris
0

##### Share on other sites

You don’t pass a matrix for every particle.  You calculate the positions of the vertices manually on the CPU and you put the already-transformed coordinates into the buffer.

Your shader should account for this by excluding any “world” transforms from its calculations.

L. Spiro

1

##### Share on other sites
You don’t pass a matrix for every particle.  You calculate the positions of the vertices manually on the CPU and you put the already-transformed coordinates into the buffer.

Your shader should account for this by excluding any “world” transforms from its calculations.

L. Spiro

So what I should do then is hold the coordinates in the particle, then fetch them with the emitter every update and bind them to a buffer.

glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vector3), &vertices[0], GL_STATIC_DRAW);

Using this,  But isnt it bad to change the size of a buffer every time particles are added.

Should I delete and recreate the buffer or just reuse it?

0

##### Share on other sites

You are forgetting that you are not required to draw the whole buffer.  Nor are you required to draw it from its first index.

Also, the buffer should be dynamic, not static.

It should never need resizing.

Firstly, you should start adding vertices from the last place on the buffer where you have no added any.  Then draw that segment, and keep adding vertices after that point.

Don’t draw a part of the buffer and then start overwriting that part.  Walk the buffer sequentially like a ring buffer.

When you run out of room, just issue a draw call from there and then continue walking over the buffer back at its start.

You may (definitely) also want to double-buffer these and change buffers each frame.

L. Spiro

1

##### Share on other sites

to add to L. Spiro's answer, when updating the vbo(which should be dynamic), do not use glBufferData, instead map the buffer, and write the values like so:

//assumed that everything is setup for drawing.
MyVertexStruct *VBOMap = (MyVertexStruct*)
glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
int o=0;
for(int i=0;i<ParticlesCount;i++,o+=sizeof(MyVertexStruct)){
if(o>=BufferSize){
glUnMapBuffer(GL_ARRAY_BUFFER);
glDrawArrays(GL_TRIANGLES, 0, o);
VBOMap = (MyVertexStruct*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
o=0;
}
(VBOMap+o).xyz = Particle[i].xyz;
}
glUnMapBuffer(GL_ARRAY_BUFFER);
glDrawArrays(GL_TRIANGLES, 0, o)
//other stuff.
Edited by slicer4ever
1

##### Share on other sites

A couple of questions.

how do I create my buffers; like this?

glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vector3), &vertices[0], GL_DYNAMIC_DRAW);

That wont work because I if I create my buffer before any particle has been created and added to the list it wont work. Can I just Generate the buffer and bind it, without using glBufferData?

In the previous post you are using a array of whatever my struct is, which in this case is my Vector3 and Vector2 classes.

In addition, I am trying to figure out how to pass my colour property now for each particle, for that I guess I just need to create another VBO to how colour for each vertex, which seems like overkill since there is 1 colour per particle.

Going on to the glMapBuffer, and in general your code, you are using a single buffer to hold all the data write? where as I create a buffer for each type of data, I have never used the method you are using  before.

How exactly does MapBuffer work? how can I update my code as in seen in the first post? Why do you have two glDrawArrays, is it because you create a buffer of arbitrary size or do I never use glBufferData. Before your code I would need to bind the buffer as well.

0

## Create an account

Register a new account

Followers 0

• 10
• 11
• 20
• 11
• 28