optimized rendering for particle system

Started by
10 comments, last by arthurprs 15 years, 8 months ago
I'm thinking in 3 options (my pseudocode) 1º glbegin(gl_quads) loop in particles: ..glvertex ..gltexcoord glend 2º loop in particles: ..glbegin(gl_trianglestrip) ..glvertex ..gltexcoord ..glend 3º glEnableClientState(GL_VERTEX_ARRAY) glVertexPointer(2, GL_FLOAT, SizeOf(TVector), dst) glEnableClientState(GL_TEXTURE_COORD_ARRAY) glTexCoordPointer(2, GL_FLOAT, SizeOf(TVector), tex) glDrawArrays(GL_QUADS, 0, 2 * 4) It's a 2d particlesystem, and another doubt, on the 3º method, how can i draw the particles rotated?
Advertisement
Quote:Original post by arthurprs
and another doubt, on the 3º method, how can i draw the particles rotated?


glRotatef
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Quote:Original post by V-man
Quote:Original post by arthurprs
and another doubt, on the 3º method, how can i draw the particles rotated?


glRotatef


i know, but particles have different angles

[Edited by - arthurprs on July 28, 2008 12:11:08 AM]
Then you either calculate the position/rotation on the CPU before sending the coordinates to the GPU or use a vertex shader/program to calculate position/rotation on the GPU (best option available nowadays).
glBegin is slow slow slow. Draw your particles with vertex arrays (the third method), and if needed manually rotate the particles on the cpu.
Quote:Original post by arthurprs
Quote:Original post by V-man
Quote:Original post by arthurprs
and another doubt, on the 3º method, how can i draw the particles rotated?


glRotatef


i know, but particles have different angles


I would use a vertex shader.
You can pass the angle in radians as a texture coordinate and in the shader, compute a rotation matrix and you can even translate if you submit translation factors as texcoordinates.
The other way is to submit rotation and translation as a uniform array. Submit an index using a texcoord and access the array.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
I'm not very experienced on opengl and i have to say that i don't know how to do much of the options you guys talked about :(
i know rotating the vextex on the cpu, but i think this method is not very fast,
can you guys post some snippets of the other ways?


thanks.

ps: sorry my english errors, i'm from brazil

I don't have any code snippets.
I recommend learning some GLSL and then you'll know what to, or ask GLSL related questions. A nice tut site is http://www.lighthouse3d.com/opengl/glsl/

If performance is not an issue, if you aren't rendering several thousand particles, then just stick with doing things on the CPU.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Quote:Original post by OrangyTang
glBegin is slow slow slow. Draw your particles with vertex arrays (the third method), and if needed manually rotate the particles on the cpu.
As a general statement, glBegin being slow actually depends on a few things, if used in conjunction with Display Lists, the state changes can be compiled as well (Yann L stated this, and I have noted it to be true in some situations), and may infact run slightly faster. I would say that is unlikely to hold with this example (because each particle would have a unique positional update). The expensive part of a particle systems are overdraw and state-update (position, colour, intensity, collision etc.) so design your system to take these into account. My planned system is spread across cores for updates and uses GPU processing and GPU memory residency to speed things up.

[EDIT: if you're using DX10/GL2.1 with ext_gpu_program4, you can use attribute streams or Texture buffer objects to do the particle transform/rotation entirely on the GPU]
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
after some research i have thinked on 3 ways to render then
witch one will be more suitable?
(i'm not sure if the 2º way is correct (first contact with displaylists))


eachframe:
..eachparticle:
....colorpointer
....texpointer
....{vertexes rotated on cpu}
....vertexpointer
..glDrawArrays


make displaylist
eachframe:
..eachparticle:
....pushmatrix
....rotate
....translate
....calllist
....popmatrix


eachframe:
..eachparticle:
....pushmatrix
....rotate
....translate
....glbegin(gl_trianglestrip)
....
....glend
....popmatrix

This topic is closed to new replies.

Advertisement