0

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. uv coordinates

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.

4

1 回答 1

1

我不确定您为什么要在方程式中混合id 。此外,您的结构只需要 Sprite 的起点和尺寸:

struct Sprite {
    Vertex start,end;
    int textureID;
} 

// nx and ny are indexes of which sprite inside the atlas do you need (index starting at 0
// xsize and ysize are the standard size of an sprite in texture space, for example
// in your example both would be 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;
}

这就是我认为您要存档的内容,希望对您有所帮助。

编辑:您可以轻松计算 xsize 和 ysize,例如,如果您的纹理包含相同大小的 4x4 精灵,则 xsize = ysize = 1.0/4

于 2013-05-04T09:06:55.680 回答