Texture Coordinates *very* inaccurate

Started by
1 comment, last by MrDoomMaster 18 years, 8 months ago
I'm attempting to use textured quads to blitter my sprites in a 2D game, yet the part of the bitmap it blitters is not correct. It seems to be 1 pixel off on all sides. Here is the structure I use to specify *where* in my sprite sheet the bitmap is for the sprite:

    struct Rect
    {
        int x;
        int y;
        int width;
        int height;
    };

x and y are the top left coordinates of the sprite's bitmap, while width and height are the pixel dimensions of that actual sprite's bitmap. Here is how I setup my texture coordinates:

        Quad[0].tu      = (float)source->x / sd.Width;
        Quad[0].tv      = (float)source->y / sd.Height;

        Quad[1].tu      = (float)(source->x + source->width) / sd.Width;
        Quad[1].tv      = (float)source->y / sd.Height;

        Quad[2].tu      = (float)(source->x + source->width) / sd.Width;
        Quad[2].tv      = (float)(source->y + source->height) / sd.Height;

        Quad[3].tu      = (float)source->x / sd.Width;
        Quad[3].tv      = (float)(source->y + source->height) / sd.Height;

the variable source is of type Rect (above). This specifies the coordinates of the sprite in the sprite sheet to use for the quad. sd is a structure containing information on the width and height of the entire sprite sheet. Below is a link to a screenshot of the improper results. The image has been blown up to show you the detail of the pixels. The image labeled "Bad" is the result I currently obtain when blittering sprites. You'll notice that the image is missing a line of pixels on the bottom and right sides. The image labeled "Good" is the actual sprite bitmap. This is how it *should* look. Note also that I have tested this with D3DXSprite, and it blitters perfectly fine. It is the texture coordinates that are messing up. http://img.photobucket.com/albums/v356/MrDoomMaster/turtles.png PS: Just as a side note, performance between using Textured Quads for blittering bitmaps and D3DXSprite is very different. I was told that Textured Quads would be faster, but my evidence proves this false. In the same situation in my game, Textured Quads gave me 190 FPS, and D3DXSprite gave me 280 FPS. Why, I don't know. All I know is that my FPS counter is greater when I use D3DXSprite.
Advertisement
Your texture coords are valid for OpenGL, but not for DirectX. In DirectX, 0,0 refers to the corner of the topleft pixel, not it's center. Add 0.5/texwidth and 0.5/texheight to your UVs to sample them correctly.
Quote:Original post by Namethatnobodyelsetook
Your texture coords are valid for OpenGL, but not for DirectX. In DirectX, 0,0 refers to the corner of the topleft pixel, not it's center. Add 0.5/texwidth and 0.5/texheight to your UVs to sample them correctly.


Thank you very much. I rated you appropriately. Very excellent help!

This topic is closed to new replies.

Advertisement