This topic is now archived and is closed to further replies.


Transparency in D3D

Recommended Posts

NightZero    122
Hi! How can I get a color in my sprites (BMP-images) to transparent. I would like the color 255,0,255 to be transparent and are using it for the background. This works fine in DirectDraw, but now I''m trying Direct3D and i use the function: g_lpD3DSprite->Draw( g_lpImageTexture,NULL, NULL, NULL, 0.0, &pos, color); to get transparency. But I can''t get it to work. In the best case the backgroundcolor which I would like to get transparent gets black. But this doesn''t look good when I paint my sprites on a blue background :-). I have done like this: //changes the background (255,0,255) to black! D3DCOLOR color = D3DCOLOR_ARGB(0xFF,0,0xFF,0); //Changes everything _except_ the background to black! Background is still (255,0,255) D3DCOLOR color = D3DCOLOR_ARGB(0xFF,0xFF,0,0xFF); //Changes everything except the background to black! Background is still (255,0,255) D3DCOLOR color = D3DCOLOR_XRGB(255,0,255); //changes the background (255,0,255) to black! D3DCOLOR color = D3DCOLOR_XRGB(0,255,0); Regards, Mattias

Share this post

Link to post
Share on other sites
Doolwind    213

Here are a couple of things to check...

1. When you load in your picture set the color key to the colour you want to be transparent.

if( FAILED(D3DXCreateTextureFromFileEx(g_pd3dDevice,
0, //Mip levels (0 = complete chain)
0, //usage
D3DFMT_UNKNOWN, //Image format (set to loaded image)
D3DX_DEFAULT, //Filter
D3DX_DEFAULT, //Mip fliter
D3DCOLOR_XRGB(250,0,0), //Colorkey, this colour is transparent
NULL, //src info goes here (nowehere if null)
NULL, //Pallete to use (none)
return E_FAIL;

Here I have set (250, 0, 0) to be transparent. This means any pixels that are this colour (red) will be treated as transparent.

2. Also make sure you have set your render state and texture states to something similar to:

g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);

g_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_ALPHAREF, 0x02 );
g_pd3dDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL );


This will set up transparency and tells Direct3D how you want the transparency to work. There are plenty of tutorials to explain what is going on here so i will just give you the code to make sure you haven''t missed a small part.

This may help you a bit, but if you are still stuck be a little more specific with what is wrong. Good luck.


Share this post

Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Doolwind is right about color key, but since NightZero uses D3DXSprite he doesn''t need all SetRenderState calls. D3DX takes care of it automaticaly.

Share this post

Link to post
Share on other sites