PDA

View Full Version : Optimizing Particles



rilestyle
12-07-2003, 12:11 AM
Hi I have a particle system, and the biggest hit to the software is obviously rendering the particles. I was wondering if the code I have here can be optimized at all:


glPushMatrix();
glTranslatef(m_Xpos,m_Ypos,0);
glScalef(Globals::g_P_Size,Globals::g_P_Size,1);
glColor4ub(m_R,m_G,m_B,Globals::g_P_Alpha);
glVertexPointer(3, GL_FLOAT, 0, Globals::g_P_Vert);
glTexCoordPointer(2, GL_FLOAT, 0, Globals::g_P_TexCoord);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, Globals::g_P_Indices);
glPopMatrix();

Basically its a bunch of texture mapped triangles. I loaded the basic texture into a vertex array and call it for each particle. Don't know if this is the fastest way , or if push/pop matrix is causing any hits to my render. Any help would be great, thanks.

Dan

IronRaph
12-07-2003, 05:24 AM
Hum I am not sure but you call this fonction for each particule?? or you build arrays and call this fnc just one time?

the second solution is in my opinion the faster way to render particules but you can use EXT like point sprite ...

jwatte
12-07-2003, 09:44 AM
That's a very slow way of doing it. Each call to DrawElements() has a fixed overhead; also, each call to change the modelview matrix or re-bind textures have a fixed overhead.

The way to do it, for particles, is to sort particles by which texture they use. The use the CPU to add position relative the center of the particle cloud/emitter, and to billboard the particles (easy; just add right/up vectors) and generate them ALL into a single vertex array. Then bind the texture and draw the array in one swell foop, while the modelview is set up for looking at the center/emitter of the particle cloud.

Repeat for each texture used by your system.

rilestyle
12-07-2003, 07:00 PM
This sounds good, however, the number of particles may vary every frame from 1000 to 15000 particles, should I delete my vertex arrays, etc. and re-allocate them each time based on how many particles I want to draw, or should the array size always be the maximum, and only the index array is reallocated? How do you handle this?

Bob
12-08-2003, 12:45 AM
Have a fixed size array that you fill as much as needed. If you don't know the maximum number of particles at compile time, allocate a base size and use it, and reallocate the buffer if you want to store more particles that the current buffer can handle, but don't do anything if it can store everything.

I suggest use std::vector for this, it will handle all this reallucations automatically.