Thread: proper way to render particles (OpenGL)

  1. #1
    Registered User
    Join Date
    Feb 2002

    proper way to render particles (OpenGL)

    im working on a particle engine, and im having trouble coming up with a good way to do the rendering.

    the particles are billboarded, which means i have to recreate the vertices every frame. the way i see it, i have 3 options:

    1. call glBegin() and glEnd(). ive heard drawing this way, only one particle at a time, is a great way to bring your framerate to its knees. it is the easiest to impliment, however, and it works perfectly fine in all cases.

    2. compile all of the verticies, texture coords, and colors dynamically, store them in a couple big arrays, and call glDrawArrays() with them. i was doing it this way, and i could get it to work, but it was extremely slow. i wrote my own array class similar to the stl vector but much faster, and yet i still bottleneck during the list creation every frame. i am having to push on at least 12 floats for the verticies, 8 for the tex coords, and 16 colors (4 per vertex) every frame, and i am having to do this for every particle. using method 1 is actually faster...

    3. spend the memory and store all of the variables inside the particle, and use something like glInterleavedArrays() on it. i've considered this method, but it will cause me to have to change my base code way too much for it to be feasable to test it.

    these are the only things i can think of. is there a better way? how do you guys render your particles? am i going down the wrong track? i need speed, but i dont want to change my base too much from what it is...
    I came up with a cool phrase to put down here, but i forgot it...

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    how about a gl list? They can be pre compiled and called using glCallList(listID); their pretty fast.

  3. #3
    Registered User
    Join Date
    Feb 2002
    i think i found a big optimization, but now im running into a different problem...

    this code is crashing

    glEnableClientState( GL_VERTEX_ARRAY );
    // turn off writing to the depth buffer
    glDepthMask( 0 );
    glVertexPointer( VECTOR_SIZE, GL_FLOAT, 0, &particle_verticies[0] );
    glGetFloatv( GL_MODELVIEW_MATRIX, viewMatrix );
    // the up and right vectors
    up_vec = Vector( viewMatrix[1], viewMatrix[5], viewMatrix[9] );
    right_vec = Vector( viewMatrix[0], viewMatrix[4], viewMatrix[8] );
    // topleft, topright, bottomleft, bottomright
    tl_vec = up_vec - right_vec;
    tr_vec = right_vec + up_vec;
    bl_vec = right_vec + up_vec;
    br_vec = right_vec - up_vec;
    	FOR( last_generated_particle )
    		if ( particles[i].life > 0 )
    			Vector loc( p.position[X], p.position[Y], p.position[Z] );
    			// billboarding
    			tl = ( loc + ( tl_vec ) * p.size );
    			tr = ( loc + ( tr_vec ) * p.size );
    			bl = ( loc + ( bl_vec ) * -p.size );
    			br = ( loc + ( br_vec ) * p.size );
    			// compiling the draw list
    			particle_verticies.pushBack( br.GetVerticies(), VECTOR_SIZE );
    			particle_verticies.pushBack( bl.GetVerticies(), VECTOR_SIZE );
    			particle_verticies.pushBack( tl.GetVerticies(), VECTOR_SIZE );
    			particle_verticies.pushBack( tr.GetVerticies(), VECTOR_SIZE );
    	// draw the particles
    	glDrawArrays( GL_QUADS, 0, particle_verticies.size() );
    glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
    glDisable( GL_BLEND );
    glDepthMask( 1 );

    this is my entire particle render function ( well, its simplified a bit, but all the code is the same). it crashes inside of glDrawArrays(). the Vector is a 3D vector class with overloaded operators, and the particle_verticies is a custom array class, but the crash happens when i use stl vector, so thats not the problem...

    if i can get this working, then i think i may have a solution to my problem.
    I came up with a cool phrase to put down here, but i forgot it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  2. OpenGL wont Render
    By Da-Nuka in forum Windows Programming
    Replies: 3
    Last Post: 12-30-2004, 06:55 AM
  3. render OpenGL geomatry on dialog box button press
    By psychopath in forum Windows Programming
    Replies: 0
    Last Post: 08-20-2004, 08:49 PM
  4. OpenGL Particles
    By Speedy5 in forum Game Programming
    Replies: 2
    Last Post: 09-30-2003, 01:24 PM
  5. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 07:57 PM