Showing posts with label pointsprites. Show all posts
Showing posts with label pointsprites. Show all posts

Monday, October 5, 2009

Assignment #4

Simple particle system:

Movie of particle system




Program built on a MacBook Pro running OS X Leopard with an NVidia card:

wget http://cs.unm.edu/~jbowles/cs513/assign4-jbowles.tar.bz2
tar jxf assign4-jbowles.tar.bz2
cd assign4-jbowles/ps
make
./ps

You can move the view around by holding down the left button and dragging. Shaders can be reloaded by pressing 'r'. You can quit with 'Q' or escape.

This particle system models particles being launched out of a gravity well at just over escape velocity. The particles make a couple of passes past the gravity well and then escape.

Once they've reached a reasonable distance away, the particle is reinitialized and the process starts over.

The particles are modeled as point sprites and passed to the GPU in a vertex buffer object.

The vertex shader does the usual: it tranforms the model coordinates into world space and leaves the texture coordinates alone. The vertex shader also calculate a luminance value based on the distance of the particle from the gravity well. This value is passed on TEXCOORD1 to the pipeline.

The fragment shader uses an exponential function to determine the alpha component of the fragment. The input to the exponential is the distance of the fragment from the center of the point sprite.

The color (not the alpha) of the fragment is determined by the distance of the point sprite from the gravity well. As the sprite gets farther from the well, it "cools off" and the red component goes to zero.

It is interesting to see how ordering has an effect on the alpha blending of the particles. Since these particles are not ordered, the blending is not always correct. I'm looking forward to putting the particles onto the GPU and sorting them there.

On my MacBook Pro, this ran at about 60 FPS when using 1, 20, 2000, or 100000 particles. I suspect that this is because I'm using synchronized swap buffers. When I remove this constraint, FPS goes up to 600 with 200 particles. If I use 20000 particles, the frame rate is about 100 without synchronized buffers.

Tuesday, September 29, 2009

Displaying a point sprite in OpenGL

After looking at a bunch of examples on how to use point sprites in OpenGL and seeing some with blatant errors and leaving out key things like setting the point size, I've decided to cut and paste my point sprite code snippet from PointSprite::draw()


// Get the max point size
glGetFloatv(GL_POINT_SIZE_MAX_ARB, &_maxSize );

// Get the minimum point size. Not necessary for this example, but interesting to know
glGetFloatv(GL_POINT_SIZE_MIN_ARB, &_minSize );

// Enable point sprites
glEnable(GL_POINT_SPRITE);

// I've also seen the following used to enable point sprites:
// glEnable(GL_POINT_SPRITE_ARB);
// but I only needed the first one. Maybe it's because I'm using GLEW

// Replace the texture coordinates across the point sprite
glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);

// Set the point size.
glPointSize(_maxSize);

// I saw some examples doing the following:
// glPointParameteri(GL_POINT_SIZE, _maxSize);
// But this didn't do anything for me. glPointSize was
// the only thing that actually set the point size

// Set a color. You'll want to use a nice texture map that you blend
// using a shader, but this is a simple example to get you started
glColor3f(1.0, 1.0, 1.0);

// Begin points. In real life, you'd use a vertex array.
glBegin(GL_POINTS);

// A point
glVertex3f(0.0, 0.0, -1.0);

// Done.
glEnd();