c - OpenGL sprite mapping doesn't work -


i'am working on own graphics engine using opengl , glut (under linux, c). problem sprite loader.basicaly, have structure holds data rendering part of texture, this:

struct sprite {    vertex start,end,size;    int textureid; }     

and have function renders screen. textureid represent id of texture. vertices start , end represent uv coordinates(between 0 , 1), specify top left(start) , bottom right(end) of sprite want render. vertex size specifies actual size of sprite in screen space.
part of function splits big texture in smaller sprites. math part of , made c app test , does`nt work how want. s.u , s.v start uv coordinate(top left of sprite), x(s.u) , y(s.v) . e.u , e.v end coordinate(bottom right of sprite). nx , ny number of splits on texture( in image bellow there 2 horrizontal splits , 2 vertical splits. id represents sprite 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 give function numbers: id=1, nx=2, ny=2, should return coordinates: start(0;1) , end(0.5;0.5). start(0;1) top left coordinate, in image yellow circle red circle inside, , end(0.5;0.5) bottom right coordinate, in image yellow circle green circle inside. uv coordinates

my problem function doesn't map correctly sprites. observed if add this: if(us<0) s.u=1+s.u; between first , second lines end sprites 2 , 4 mixed up.

i'm not sure why mixing id within equation. furthermore, structure needs start point , dimensions of sprite:

struct sprite {     vertex start,end;     int textureid; }   // nx , ny indexes of sprite inside atlas need (index starting @ 0 // xsize , ysize standard size of sprite in texture space, example // in example both 0.5 sprite generatesprite(int nx, int ny, int xsize int ysize){     sprite s;     s.start.u = xsize*nx;     s.start.v = ysize*ny;     s.end.u = s.start.u+xsize;     s.end.v = s.start.v+ysize; } 

that's think trying archive, hope helps.

edit: can calculate xsize , ysize easily, example if texture holds 4x4 sprites of same size, xsize = ysize = 1.0/4


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -