|
|

楼主 |
发表于 2007-1-10 23:37:00
|
显示全部楼层
Re:关于文字贴图,怎么去背景?
终于找到方法了,可是看不懂,效果达到了,也就是给texture设置colorkey,真不明白D3D为什么抛弃了colorkey,难道还有别的方法?
HRESULT D3DUtil_SetColorKey( LPDIRECT3DTEXTURE9 pTexture, DWORD dwColorKey )
{
// Get colorkey's red, green, and blue components
DWORD r = ((dwColorKey&0x00ff0000)>>16);
DWORD g = ((dwColorKey&0x0000ff00)>>8);
DWORD b = ((dwColorKey&0x000000ff)>>0);
// Put the colorkey in the texture's native format
D3DSURFACE_DESC d3dsd;
pTexture->GetLevelDesc( 0, &d3dsd );
if( d3dsd.Format == D3DFMT_A4R4G4B4 )
dwColorKey = 0xf000 + ((r>>4)<<8) + ((g>>4)<<4) + (b>>4);
else if( d3dsd.Format == D3DFMT_A1R5G5B5 )
dwColorKey = 0x8000 + ((r>>3)<<10) + ((g>>3)<<5) + (b>>3);
else if( d3dsd.Format != D3DFMT_A8R8G8B8 )
return E_FAIL;
// Lock the texture
D3DLOCKED_RECT d3dlr;
if( FAILED( pTexture->LockRect( 0, &d3dlr, 0, 0 ) ) )
return E_FAIL;
// Scan through each pixel, looking for the colorkey to replace
for( DWORD y=0; y<d3dsd.Height; y++ )
{
for( DWORD x=0; x<d3dsd.Width; x++ )
{
if( d3dsd.Format==D3DFMT_A8R8G8B8 )
{
// Handle 32-bit formats
if( ((DWORD*)d3dlr.pBits)[d3dsd.Width*y+x] == dwColorKey )
((DWORD*)d3dlr.pBits)[d3dsd.Width*y+x] = 0x00000000;
}
else
{
// Handle 16-bit formats
if( ((WORD*)d3dlr.pBits)[d3dsd.Width*y+x] == dwColorKey )
((WORD*)d3dlr.pBits)[d3dsd.Width*y+x] = 0x0000;
}
}
}
// Unlock the texture and return OK.
pTexture->UnlockRect(0);
return S_OK;
}
|
|