Sprite Sheet OpenGL

Started by
9 comments, last by Maverick Programmer 15 years, 4 months ago
So, if I have an image, a sprite sheet, and I only to display part of it, i.e. one sprite, how would I do this?
Advertisement
Quote:Original post by Kurushimi
So, if I have an image, a sprite sheet, and I only to display part of it, i.e. one sprite, how would I do this?
Typically you would do this by rendering a textured quad, with the texture coordinates set appropriately so that only the specified part of the texture is visible. Assuming you know the dimensions of the texture, the dimensions of the sprite, and the sprite's location relative to the texture, you can easily compute the texture coordinates using simple arithmetic.
You would specify the texture coordinates as fractional parts. For example, if you had the sheet divided into 4ths, you would specify 0.25f for the texture coordinate on that axis.

Check out Super Play, the SNES inspired Game Engine: http://www.superplay.info

Well, its not quite that simple... What you want to specify for your texture coordinates is the center of the texel -- this will help avoid one texture bleeding into another when filtering and multi-sampling are applied.

So, in other words, the texture coordinate 0.0f,1.0f (keep in mind that OpenGL addresses textures from the lower-left corner) corresponds to the exact corner of the texture, and moving any more left or up moves outside the texture -- This scenario can be helped by texture clamping, but what if you sampled from 0.25, 0.75? Well, you'd take some texture samples from the surrounding sprites, and probably generate some funny colors on your screen.

You want to offset your texture coordinates by a small amount, equal to one-half the width of a texel in the corresponding texture. If you have a texture (the sprite sheet) which is 1024 texels wide, you want to add and offset equal to 1/(2*1024), or about 0.00049.

You may also have to consider how the graphics card handles non-power-of-two textures -- it may support them directly, or may pad them out so that they are hosted in the next power-of-two texture that fits... I'm not certain if that would affect the texture coordinate calculation or whether the API compensates.

throw table_exception("(? ???)? ? ???");

In my engine texture coordinates are on 1:1. So if a texture is 256x256, I access top left texel at 0:0, and the bottom right at 255:255.
- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]
Ah, thanks for the advice dudes. But when I tried implementing textures in my class, I can't get it to work. I think there are a lot of problems but one problem is that glGenTexture() gives me 0 as a handle. The way I set it up, there's a class "Entity" which has a member "textureHandle". In the default constructor, I call glGenTexture(1, &textureHandle). But, textureHandle is always 0...
Make sure you have a context valid and running when invoking glGenTextures. And, of course, does glGenTextures set an error?
The texture coordinates is equal to one divided by the number of frames you have in the sprite sheet.

//Please note this will only work for the x columns double cell_division = 1.0/number_of_frames;double x_cell = current_frame * cell_division;double x_cell2= ( current_frame * cell_division ) + cell_division;glBegin(GL_QUADS);glTexCoord2d(x_cell, 0.0);glVertex3i(0, 0, 0);  glTexCoord2d(x_cell2, 0.0);glVertex3i( sprite_width , 0, 0);  glTexCoord2d( x_cell2 , 1.0 );glVertex3i( sprite_width, sprite_height, 0);  glTexCoord2d( x_cell , 1.0);glVertex3i(0, sprite_height, 0); glEnd();


It would look something like that. :]
Holy crap, you can read!
Quote:Original post by haegarr
Make sure you have a context valid and running when invoking glGenTextures. And, of course, does glGenTextures set an error?



How do I do that? I'm using GLUT, btw.
really what you want is an editor that will give you the texture coordinates of each individual frame (and perhaps build the atlas from a source sheet as well)

This topic is closed to new replies.

Advertisement