I'am working on my own graphics engine using OpenGL and GLUT (under Linux, C). The problem is with the sprite loader.Basicaly, I have a structure that holds the data for rendering just a part of the texture, something like this:
struct Sprite {
Vertex start,end,size;
int textureID;
}
And I have a function that renders this to the screen. The textureID represent the id of the texture. Vertices start and end represent uv coordinates(between 0 and 1), they specify the top left(start) and bottom right(end) of the sprite I want to render. Vertex size specifies the actual size of the sprite in screen space.
This is a part of the function that splits the big texture in smaller sprites. This is just the math part of it and I made a C app to test it and it does`nt work how I want. s.u and s.v are the start uv coordinate(top left of the sprite), something like x(s.u) and y(s.v) . e.u and e.v are the end coordinate(bottom right of the sprite). nx and ny are the number of splits that are on the texture( in the image bellow there are 2 horrizontal splits and 2 vertical splits. id represents which sprite I want.
s.u=(1.0f/nx)*(id%nx-1);
s.v=(1.0f/ny)*(ny-id/nx);
e.u=s.u+(1.0f/nx);
e.v=s.v-(1.0f/nx);
For example If I would give my function this numbers: id=1, nx=2, ny=2, it should return this coordinates: start(0;1) and end(0.5;0.5). start(0;1) is the top left coordinate, in the image is a yellow circle with a red circle inside, and end(0.5;0.5) is the bottom right coordinate, in the image is a yellow circle with a green circle inside.
My problem is that my function doesn't map correctly the sprites. I observed that if I add this: if(us<0) s.u=1+s.u;
between the first and the second lines I just end up with the sprites 2 and 4 mixed up.