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.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


Issue With D3DXLoadSurfaceFromFile() In Loading Pngs


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
8 replies to this topic

#1 vince0656   Members   -  Reputation: 111

Like
0Likes
Like

Posted 20 November 2012 - 05:13 PM

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:
Posted Image
Posted Image

Sponsor:

#2 C0lumbo   Crossbones+   -  Reputation: 2377

Like
0Likes
Like

Posted 21 November 2012 - 02:03 AM

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.

#3 Hodgman   Moderators   -  Reputation: 31047

Like
0Likes
Like

Posted 21 November 2012 - 02:45 AM

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.

#4 vince0656   Members   -  Reputation: 111

Like
0Likes
Like

Posted 21 November 2012 - 02:54 AM

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()?

Edited by vince0656, 21 November 2012 - 12:42 PM.


#5 MJP   Moderators   -  Reputation: 11584

Like
1Likes
Like

Posted 21 November 2012 - 04:09 PM

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.).

#6 vince0656   Members   -  Reputation: 111

Like
0Likes
Like

Posted 21 November 2012 - 04:52 PM

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 methodsLPDIRECT3DTEXTURE9 imagetex; //textute our image will be loaded intoLPD3DXSPRITE sprite; //sprite to display our imageD3DXVECTOR3 imagepos; //vector for the position of the sprite//InitialiseD3DXCreateTextureFromFileW(Graphics::GetInstance()->getDevice(),fileName.c_str(),&imagetex);D3DXCreateSprite(Graphics::GetInstance()->getDevice(),&sprite);imagepos.x=0.0f; //coord x of our spriteimagepos.y=0.0f; //coord y of out sprite//Render codeGraphics::GetInstance()->getDevice()->BeginScene(); //Call the BeginScene function with our Device and see if it succeeded, all drawing/render code go behind BeginScene and EndScenesprite->Begin(D3DXSPRITE_ALPHABLEND); //begin our sprite with alphablend so it support alpha, like png use alphasprite->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 changesprite->End(); //end the spriteGraphics::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.

#7 MJP   Moderators   -  Reputation: 11584

Like
2Likes
Like

Posted 21 November 2012 - 07:24 PM

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.

#8 Endurion   Crossbones+   -  Reputation: 3652

Like
0Likes
Like

Posted 21 November 2012 - 11:47 PM

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>

#9 vince0656   Members   -  Reputation: 111

Like
1Likes
Like

Posted 22 November 2012 - 06:24 AM

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!




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