Issue With D3DXLoadSurfaceFromFile() In Loading Pngs

Started by
7 comments, last by vince0656 11 years, 4 months ago
Hello there,
I'm pulling out my hair here trying to understand what I am doing wrong but I am having issues in loading and rendering pngs using D3DXLoadSurfaceFromFile() using the D3D9 API. They load but not correctly because they seem to be corrupt after rendering when the files are OK. A good example is from the files attached. The one on the top is loaded into a surface and being rendered using StrechRect(). However as you can see from the original on the bottom, its not doing it correctly and that not to mention that the alpha channel is being used. Therefore, I should only be seeing the cleared back buffer and the silhouette of the heli but I'm not. I don't know if there is anything I'm not doing but here is my initialisation and render code from the sprite class I used to render:
[source lang="cpp"]void MySprite::Initialise(wstring fileName)
{
HRESULT hResult;

if( m_bitmap == nullptr)
{

D3DXIMAGE_INFO imgInfo;


hResult = D3DXGetImageInfoFromFile(
fileName.c_str(),
&imgInfo);

m_bitmapSize.X = imgInfo.Width;
m_bitmapSize.Y = imgInfo.Height;

hResult = Graphics::GetInstance()->getDevice()->CreateOffscreenPlainSurface(
m_bitmapSize.X,
m_bitmapSize.Y,
D3DFMT_A8R8G8B8,
D3DPOOL_DEFAULT,
&m_bitmap,
NULL);

if( FAILED( hResult ) )
{
MessageBox(NULL, L"Unable to create Offscreen surface", L"ERROR", MB_OK);
return;
}

if( fileName != L"" )
{
hResult = D3DXLoadSurfaceFromFile(
m_bitmap,
NULL,
NULL,
fileName.c_str(),
NULL,
D3DX_DEFAULT,
0,
NULL );
if( FAILED( hResult ) )
{
MessageBox(NULL, L"Unable to load image into memory", L"ERROR", MB_OK);
return;
}
}
}
.
if( m_backBuffer == nullptr)
{
hResult = Graphics::GetInstance()->getDevice()->GetBackBuffer(
0,
0,
D3DBACKBUFFER_TYPE_MONO,
&m_backBuffer);
if( FAILED( hResult ) )
{
MessageBox(NULL, L"Unable to Unable to obtain BackBuffer", L"ERROR", MB_OK);
return;
}
}
}

void MySprite::Render(const RECT& destRect)
{

if( m_bitmap != nullptr && m_backBuffer != nullptr)
{
Graphics::GetInstance()->getDevice()->StretchRect(
m_bitmap,
NULL,
m_backBuffer,
&destRect,
D3DTEXF_NONE);
}

}[/source]

I don't know if it is the format of the offscreen surface I'm using or what. BTW: the screen is being cleared but that is being called elsewhere. It is being cleared to D3DCOLOR_XRGB(255,100,0). Also bitmaps load fine so that is not an issue. Thing is I'm new to D3D9 and I'm happy something is rendering but its not doing it correctly.

Here are the images I mentioned:
34g7qqo.jpg
2r7239z.jpg
Advertisement
My guess is that you're rendering with alpha blending disabled.

If you compare the original image with the messed up result, I believe the only areas that are 'wrong' are areas where the PNG can be expected to be fully transparent.

Try opening your PNG in an art package (e.g. GIMP) and removing the alpha channel, and see if you get a match.
Just because a texture has data in the alpha channel, doesn't mean it will render transparently. You need to enable the alpha-test render-state.

The colours look like that because PNG files just save random RGB values for transparent pixels, because they assume that you'll never see these values.
I see. I automatically assumed that the API would handle that. That's OK. I've done some research and I found something but when I use the following code the outcome is the same.
[source lang="cpp"]// Enable alpha blending.
lpD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,
TRUE);

// Set the source blend state.
lpD3DDevice->SetRenderState(D3DRS_SRCBLEND,
D3DBLEND_SRCCOLOR);

// Set the destination blend state.
lpD3DDevice->SetRenderState(D3DRS_DESTBLEND,
D3DBLEND_INVSRCCOLOR);[/source]


EDIT: The above states are not working. Question: Does the alpha blending not work if I use StrechRect()?
StretchRect is not a true "rendering" function. I would strongly recommend using ID3DXSprite, which actually renders geometry and supports alpha-blending and transformations (rotation, scaling, etc.).

StretchRect is not a true "rendering" function. I would strongly recommend using ID3DXSprite, which actually renders geometry and supports alpha-blending and transformations (rotation, scaling, etc.).


Thanks for that. I found a site that talked about D3DXSprite (http://celestialcoding.com/tutorials-25/d3d9-tutorial-3-loading-an-image-from-file-and-drawing-it/?wap2) and took the following code:

[source lang="cpp"]//member methods

LPDIRECT3DTEXTURE9 imagetex; //textute our image will be loaded into
LPD3DXSPRITE sprite; //sprite to display our image
D3DXVECTOR3 imagepos; //vector for the position of the sprite

//Initialise

D3DXCreateTextureFromFileW(Graphics::GetInstance()->getDevice(),fileName.c_str(),&imagetex);

D3DXCreateSprite(Graphics::GetInstance()->getDevice(),&sprite);

imagepos.x=0.0f; //coord x of our sprite
imagepos.y=0.0f; //coord y of out sprite

//Render code

Graphics::GetInstance()->getDevice()->BeginScene(); //Call the BeginScene function with our Device and see if it succeeded, all drawing/render code go behind BeginScene and EndScene

sprite->Begin(D3DXSPRITE_ALPHABLEND); //begin our sprite with alphablend so it support alpha, like png use alpha
sprite->Draw(imagetex,NULL,NULL,&imagepos,0xFFFFFFFF); //Draw the sprite, first parameter is the image texture, 4th parameter is the position, last parameter is the color change leave to 0xFFFFFFFF for no change
sprite->End(); //end the sprite
Graphics::GetInstance()->getDevice()->EndScene(); //Call the EndScene function with our Device[/source]

And it works!!!

However. I'm not sure if its best practice. Also its scaling my image now and is bigger than I want it to be which I'm annoyed about.Basically, its not rendering to the original image size but it is rendering correctly now which is good.
D3DXCreateTextureFromFile will scale your image to power-of-2 dimensions by default, in order to support older GPU's with poor non-power-of-2 support. If you don't care about that, you can use D3DXCreateTextureFromFileEx and tell it not to scale the image.
Also, about the render states, these should do the trick:


// Enable alpha blending.
lpD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);

// Set the source blend state.
lpD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
// Set the destination blend state.
lpD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>


D3DXCreateTextureFromFile will scale your image to power-of-2 dimensions by default, in order to support older GPU's with poor non-power-of-2 support. If you don't care about that, you can use D3DXCreateTextureFromFileEx and tell it not to scale the image.


Thank you so much. I now have a working sprite blitting class as a result.

Thanks also to everyone else for their input. I am going to have fun with this now!

This topic is closed to new replies.

Advertisement