Sign in to follow this  
mojobojo

CreateTexture Outputs Backwards Images

Recommended Posts

I took it upon myself to teach myself some directx and run into a bit of a problem. Until now I have been using "D3DXCreateTextureFromFileEx" to load textures. I wanted to try out a manual approach to get a better understanding of image formats and pixels so I am creating a function that loads a bitmap. I am loading ARGB 32 bit bitmaps and loading them as textures using CreateTexture. However they come out completely upside down for some reason. I was just going to write a function for flipping the pixels but I thought I would get a real solution rather than a quick fix. Here is a code snippet

[CODE]
void Bitmap::ConvertToTexture(LPDIRECT3DDEVICE9 pD3DDevice, IDirect3DTexture9 **ppTexture, D3DSURFACE_DESC *pTextureDesc)
{
pD3DDevice->CreateTexture(pBitmapInfoHeader->biWidth, pBitmapInfoHeader->biHeight, 1, 0, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, ppTexture, NULL);
if (*ppTexture == NULL)
{
throw Exception(L"Bitmap::ConvertToTexture - Failed to create texture");
}
pTextureDesc->Width = pBitmapInfoHeader->biWidth;
pTextureDesc->Height = pBitmapInfoHeader->biHeight;
pTextureDesc->Format = D3DFMT_UNKNOWN;
D3DLOCKED_RECT d3dlr;
(*ppTexture)->LockRect(0, &d3dlr, 0, 0);
memcpy(d3dlr.pBits, pBitmapData + pBitmapFileHeader->bfOffBits, pBitmapInfoHeader->biWidth * pBitmapInfoHeader->biHeight * 4);
(*ppTexture)->UnlockRect(0);
}
[/CODE]

Here is also a file to show what bitmap format I am dealing with.

[url="https://dl.dropbox.com/u/45861203/Fireball.bmp"]https://dl.dropbox.c...03/Fireball.bmp[/url]

Here is what I did to fix it for now.

[CODE]
void Bitmap::ConvertToTexture(LPDIRECT3DDEVICE9 pD3DDevice, IDirect3DTexture9 **ppTexture, D3DSURFACE_DESC *pTextureDesc)
{
pD3DDevice->CreateTexture(pBitmapInfoHeader->biWidth, pBitmapInfoHeader->biHeight, 1, 0, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, ppTexture, NULL);
if (*ppTexture == NULL)
{
throw Exception(L"Bitmap::ConvertToTexture - Failed to create texture");
}
pTextureDesc->Width = pBitmapInfoHeader->biWidth;
pTextureDesc->Height = pBitmapInfoHeader->biHeight;
pTextureDesc->Format = D3DFMT_UNKNOWN;
D3DLOCKED_RECT d3dlr;
(*ppTexture)->LockRect(0, &d3dlr, 0, 0);
//memcpy(d3dlr.pBits, pBitmapData + pBitmapFileHeader->bfOffBits, pBitmapInfoHeader->biWidth * pBitmapInfoHeader->biHeight * 4);
PDWORD pdwDest = (PDWORD)d3dlr.pBits;
PDWORD pdwSrc = (PDWORD)(pBitmapData + pBitmapFileHeader->bfOffBits);
DWORD dwPixels = pBitmapInfoHeader->biWidth * pBitmapInfoHeader->biHeight;
for (int x = 0, rx = (dwPixels) - (pBitmapInfoHeader->biWidth); x < dwPixels; x += pBitmapInfoHeader->biWidth, rx -= pBitmapInfoHeader->biWidth)
{
memcpy(pdwDest + x, pdwSrc + rx, pBitmapInfoHeader->biWidth * 4);
}
(*ppTexture)->UnlockRect(0);
}
[/CODE] Edited by mojobojo

Share this post


Link to post
Share on other sites
As a separate note, you need to take into account the Pitch of the locked rectangle. It is the texture's scanline memory width, and is often greater than width*bytesperpixel, especially if (but not limited to the scenario where) the width is not a power of two.

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