Jump to content
  • Advertisement
Sign in to follow this  
mojobojo

CreateTexture Outputs Backwards Images

This topic is 2072 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 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


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);
}


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

https://dl.dropbox.c...03/Fireball.bmp

Here is what I did to fix it for now.


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);
}
Edited by mojobojo

Share this post


Link to post
Share on other sites
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!