Sign in to follow this  
MrDoomMaster

Texture Coordinates *very* inaccurate

Recommended Posts

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

Share this post


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this