Particle System question

I have a particle system and one of the behaviors is Smoke. So each particle starts small and grows big until it fades away. A problem with this is the fact that since the texture is being enlarged, it appears as if it is moving to the right. This can be resolved by rendering the particle´s x position minus half its width. However, this small division when coupled with thousands of particles can cause performance drops. My question is, is there another way to remove the “drifting to the right” bug? Perhaps an optimization? My code with the division is…

P.S. The bug also affects the image to look like its drifting down. Thus the minus half the height in the code:


glBegin(GL_QUADS);
   glTexCoord2f(ci.getXD(),ci.getYD());// Upper-left
	glVertex2i((int)p.getX()-p.getWidth()/2, (int)p.getY()-p.getHeight()/2); 
	glTexCoord2f(ci.getXWD(), ci.getYD());// Upper-right
	glVertex2i((int)p.getX()+p.getWidth()/2, (int)p.getY()-p.getHeight()/2); 
	glTexCoord2f(ci.getXWD(), ci.getYHD());// Bottom-right
	glVertex2i((int)p.getX()+p.getWidth()/2, (int)p.getY()+p.getHeight()/2); 
	glTexCoord2f(ci.getXD(), ci.getYHD()); // Bottom-left
	glVertex2i((int)p.getX()-p.getWidth()/2, (int)p.getY()+p.getHeight()/2);
glEnd();

I managed to remove 6 of the divisions by precalculating the width and height offsets. Still i hate using divisions in loops. Heres the code:


int hw = p.getWidth()/2;
int hh = p.getHeight()/2;
glBegin(GL_QUADS);
	glTexCoord2f(ci.getXD(),ci.getYD());// Upper-left
	glVertex2i((int)p.getX()-hw, (int)p.getY()-hh); 
	glTexCoord2f(ci.getXWD(), ci.getYD());// Upper-right
	glVertex2i((int)p.getX()+hw, (int)p.getY()-hh); 
	glTexCoord2f(ci.getXWD(), ci.getYHD());// Bottom-right
	glVertex2i((int)p.getX()+hw, (int)p.getY()+hh); 
	glTexCoord2f(ci.getXD(), ci.getYHD()); // Bottom-left
	glVertex2i((int)p.getX()-hw, (int)p.getY()+hh);
glEnd();

You’re going a bit crazy with premature optimization here. In a particle system your bottleneck is much more likely to lie in fill rate / GPU. A few extra divisions (or primitive type casts for that matter) are negligible, i.e. including them won’t affect your particle system’s performance in any detectable way.

A more sane optimization would be to only call glBegin/glEnd once per loop; then repeat the drawing code (glTexCoord/glVertex) as necessary for each particle.

Better still would be to use vertex arrays:
http://www.songho.ca/opengl/gl_vertexarray.html

Oh great. K thanks. By the way, do you know any tutorials for vertex array buffers for 2D?

Oh, god… You’re doing 10 OpenGL calls for each particle… Kill it with fire! Store the data in a ByteBuffer and upload it to a VBO each frame, and draw everything with glDrawArrays.

If you’re still having a problem with the texture drifting then I suspect the problem is your texture coordinates. If you can’t figure it out, post the code for the methods that generate the texture coordinates.

Yeah thats my next task for optimizing this code. But i dont know how to use VBO arrays. Im still looking for a tutorial specifically for 2D only…

I just got my particles system working using vertex arrays and the performance boost was insane. Almost double the particle count. It is 2D and I could send you the code or you can google opengl vertex arrays/VBO if your using lwjgl then

http://lwjgl.org/wiki/index.php?title=Using_Vertex_Buffer_Objects_(VBO)

hope this helps.

I didnt understand it very well… :clue: . Ill research more sources…

K i understand it now. However, when I bind a texture to use for VBO, I use a large sprite sheet. Obviously I dont want to render the entire sheet so how do i bind a specific texture coordinate of a large image?