PDA

View Full Version : alpha channel


Pushapjit
04-17-2006, 02:35 AM
I am trying to render textures with alpha channel. Below is the code. I am getting problem with alpha channel which is showing blue outlining around the object. Grill.dds file is used for grill texture.

HRESULT CMyD3DApplication::DrawMeshData(SMeshData *pMeshData)
{

// Set diffuse blending for alpha set in vertices
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );

// Enable alpha testing (skips pixels with less than a certain alpha.)
if( m_d3dCaps.AlphaCmpCaps & D3DPCMPCAPS_GREATEREQUAL )
{
m_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_ALPHAREF, 0x08 );
m_pd3dDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL );
}


// Set and draw each of the materials in the mesh
for( DWORD iMaterial=0; iMaterial < m_dwNumMaterials; iMaterial++ )
{
m_pd3dDevice->SetMaterial( &m_pMeshMaterials[iMaterial] );
m_pd3dDevice->SetTexture( 0, m_pMeshTextures[iMaterial] );

if( !m_bShowStrips && !m_bShowSingleStrip)
{
pMeshData->m_pMesh->DrawSubset( iMaterial );
}
}

// Restore Alpha state
m_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );

return S_OK;
}


HRESULT CMyD3DApplication::RestoreDeviceObjects()
{

m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );

geon
04-17-2006, 03:28 AM
Do you have a blue background for the texture? Don't. It's simply wrong.

A texture wich is supposed to be transparent with an alpha channel should not use a background color, as you would get if you flatten a transparent layer in photoshop.

Instead, save first the alpha channel, then make the colors 100% opaque. In photoshop, you can achieve this by [ctrl]-clicking the transparent layer, create an alpha channel and fill the selected area with white. Then create a new layer from the transparent one and merge them. Repeat this 8 times, giving you completely a opaque surface. Flatten this and save as png, or any other format that supports alpha.

juhnu
04-17-2006, 11:49 PM
There are some problems with the Photoshops .png saving support. Not all versions save the alpha channel correctly, so it would be a better idea to save as a 32-bit bmp.

It's amazing how crappy alpha-channel supports there are in various imaging tools, image converters and image libraries.