Jump to content
  • Advertisement
Sign in to follow this  
vNeeki

OpenGL Calculating sprite coords in sprite sheet texture ?

This topic is 2339 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

I have created a small sprite sheet(64x256) with all the frames of an animated sprite(64x64 each) , and im uploading that
texture into opengl in order to avoid binds for each animation state change.

But im facing a little problem with the mapping , and particurarly with the texture coordinates.
When i attempt to map anything but the first i simply get random parts of the frame rendered.

This is how i calculate my coords :


void CalculateCoordsForQuad(vector<Vector2F>& coords,const Vector2F& frame_position,const Vector2F& frame_dimensions,
const Vector2F& sprite_sheet_dimensions)
{
//Calculate offset of sprite in sprite sheet
float x1 = frame_position.X / sprite_sheet_dimensions.X;
float x2 = (frame_position.X + frame_dimensions.X) / sprite_sheet_dimensions.X;
float y1 = frame_position.Y / sprite_sheet_dimensions.Y;
float y2 = (frame_position.Y + frame_dimensions.Y) / sprite_sheet_dimensions.Y;

//Map Quad coordinates (2 GL_TRIANGLES)
coords.clear();

//Triangle 1
coords.push_back(Vector2F(x1,y1));
coords.push_back(Vector2F(x2,x1));
coords.push_back(Vector2F(y1,y2));

//Triangle 2
coords.push_back(Vector2F(x1,y2));
coords.push_back(Vector2F(x2,y1));
coords.push_back(Vector2F(x2,y2));
}


And how i display them :


vector<Vector2F> coords;
const Vector2F first_frame(0,0);
const Vector2F second_frame(64,0);
const Vector2F frame_dim(64,64);
const Vector2F sheet_dim(256,64);

CalculateCoordsForQuad(coords,second_frame,frame_dim,sheet_dim);

//Now map + upload vertices with immediate mode


But like i mentioned already it will only display correctly the first sprite which is frustrating.

...Anyone seeing anything wrong with my math ?

Share this post


Link to post
Share on other sites
Advertisement
your math looks good, your x1/x2 should be 0.25/0.5, and y1/y2 should be 0.0/1.0. what are you getting when u look what is written into coords?

also, can we see how the image is uploaded, and how your drawing it.

Share this post


Link to post
Share on other sites
Thanks.

I think that the problem is with the way im mapping the triangle(i got it to work with deprecated GL_QUADS).
...Seems like i will have to play around with vertices a little more.

Share this post


Link to post
Share on other sites
Hidden


//Triangle 1
coords.push_back(Vector2F(x1,y1));
coords.push_back(Vector2F(x2,x1));
coords.push_back(Vector2F(y1,y2));

//Triangle 2
coords.push_back(Vector2F(x1,y2));
coords.push_back(Vector2F(x2,y1));
coords.push_back(Vector2F(x2,y2));


and then

I think that the problem is with the way im mapping the triangle(i got it to work with deprecated GL_QUADS).
...Seems like i will have to play around with vertices a little more.


GL_QUAD expects 4 vertices per quad. Hence you need 4 UV coordinate pairs. But you compute 6 pairs, because you handle the quad as 2 triangles.

Share this post


Link to post

...

//Triangle 1
coords.push_back(Vector2F(x1,y1));
coords.push_back(Vector2F(x2,x1));
coords.push_back(Vector2F(y1,y2));

...

At least: The 2nd UV pair consists of x co-ordinates only, and the 3rd pair of y co-ordinates.

Share this post


Link to post
Share on other sites

[quote name='vNeeki' timestamp='1332595982' post='4924875']
...

//Triangle 1
coords.push_back(Vector2F(x1,y1));
coords.push_back(Vector2F(x2,x1));
coords.push_back(Vector2F(y1,y2));

...

At least: The 2nd UV pair consists of x co-ordinates only, and the 3rd pair of y co-ordinates.
[/quote]

Fixed it.Thank you ! smile.png

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!