Jump to content
  • Advertisement
Sign in to follow this  
Kurushimi

Sprite Sheet OpenGL

This topic is 3585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Make sure you have a context valid and running when invoking glGenTextures. And, of course, does glGenTextures set an error?

Share this post


Link to post
Share on other sites
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. :]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!