Calculating sprite coords in sprite sheet texture ?

Started by
3 comments, last by vNeeki 12 years ago
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 ?
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.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.
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.


//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.

...

//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 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

This topic is closed to new replies.

Advertisement