OpenGL sprites (was Re: Wish List)

Garrett schrieb am 21 Mar 2000:

So does OGL support just plain blitting sprites onto the screen? Or do they
all have to be treated as 3D objects? I got to learn more about OGL now…

What you can do is:

Create a orthographic projection and the viewport the size of the
window (in pixels):

I use the following code:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, w, 0.0f, h, 0.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, w, h);

Now you can pretend you have a 2D screen with 0,0 in the LOWER left corner.
If you don’t get that into your head (as a math geek, I find it the
natural way), do

glTranslatef(0.0, h - 1, 0.0);
glScalef(1.0, -1.0, 1.0);

Now we want to draw a sprite at screen coordinates (x, y). Say we have loaded
that into a texture with size (cx, cy) before.

The origin is assumed at the LOWER left!

/* load texture and stuff ommited, see reference below */

glBegin(GL_QUADS);

glTexCoord2f(0.0, 0.0);
glVertex2f(x, y);
glTexCoord2f(1.0, 0.0);
glVertex2f(x + cx, y);
glTexCoord2f(1.0, 1.0);
glVertex2f(x + cx, y + cy);
glTexCoord2f(0.0, 1.0);
glVertex2f(x, y + cy);

glEnd();

That’s it.

See http://www.ards.net/Andreas/sdl_textures.html on how to load
a texture from and SDL_Surface into OpenGL).

  • Andreas–
    Probably one of the smallest 3D-Games in the world: http://www.gltron.org
    More than 60’000 Downloads of the latest version (0.53)