Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


CreateTexture Outputs Backwards Images


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 mojobojo   Members   -  Reputation: 116

Like
0Likes
Like

Posted 17 November 2012 - 05:57 AM

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, 17 November 2012 - 06:37 AM.


Sponsor:

#2 Nik02   Crossbones+   -  Reputation: 2882

Like
1Likes
Like

Posted 17 November 2012 - 07:59 AM

The bmp format stores scanlines bottom to top, while D3D textures are top to bottom.

Niko Suni


#3 Nik02   Crossbones+   -  Reputation: 2882

Like
0Likes
Like

Posted 17 November 2012 - 08:04 AM

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.

Niko Suni





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS