Advertisement Jump to content
Sign in to follow this  

Texture Coordinates *very* inaccurate

This topic is 4920 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post

Link to post
Share on other sites
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!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!