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();

1 comment:

  1. Jeff,
    Did you get textures to work with points using vertex/fragment programs? I could do them in fixed functions, but for some reason cannot seem to get the texture coordinates to be generated correctly in the shaders.

    Thanks
    - Remon (from class of CS513)

    ReplyDelete