Skip to main content
GameDev.net gamedev.net
🔒 Locked

Texture transparency

Started by ludo8x Jul 5, 2004 at 4:39 PM 3 replies 1.3k views
Original Post
ludo8x
ludo8x
Hello. I'm loading a texture with this function: D3DXCreateTextureFromFileEx(g_d3d_device, "can.jpg", 0, 0, 0, 0,D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &g_pTexture); I want black color (0,0,0) to be transparent. What textureStageType should i set to achieve this ? I tried this but don't work: g_d3d_device->SetTextureStageState(0, D3DTSS_ALPHAOP,D3DTOP_SUBTRACT);
Weston
Weston
Here are some of the stages I use:

m_D3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);m_D3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);m_D3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT);m_D3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);m_D3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);m_D3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_CURRENT);

Alpha values and color values in this case are taken from both the texture and the vertex colors, which is excellent in my case. Even if I don't specify the vertecies to have a color, it assumes they are opaque white, which will look just fine, and if I don't have a texture, it just uses colors from the vertecies. The D3DTOP_MODULATE flag tells the alpha operation or color operation to muiltiply the two arguments (Which in this case is D3DTA_TEXTURE and D3DTA_CURRENT. D3DTA_CURRENT is the value of the previous blending stage. There is also a D3DTA_DIFFUSE you can use.) So if you want to make something slightly transparent, you can change the alpha in the vertecies. Textures will also be able to use color keys for transparencies as well.
---Will DDR for food.
_Flecko
_Flecko
The poster above offered a more complex solution than I think you're looking for (though I personally think it's a better way of doing things, using images with an alpha channel is far easier than dealing with color keys). There's a fairly comprehensive guide to both methods here: http://www.drunkenhyena.com/docs/d3d_tutorial.phtml#DHColourKey
--------------Trans2D - 2D library for C# / Managed DirectX
IFooBar
IFooBar
To get black to not show up, when you load an image specify opaque black as the color key, which you've already done. Then enable the alpha testing render state

look for D3DRS_ALPHATESTENABLE

What it does when enabled is that it will reject or acespt a pixel, based on the pixels alpha value. So if you set the threshold to say 50, then all pixels with alpha value of 50 will not be drawn to the screen (depending on the comparison function of course). You'll understand the deal with the threshold and the comparison function once you look into the render states:

D3DRS_ALPHAREF

and

D3DRS_ALPHAFUNC
[size=2]aliak.net
Supernat02
Supernat02
Also note that alpha testing is a better solution to alpha blending because it is much faster.

The default state of the D3DRS_ALPHAREF render state is 0. The default state of the D3DRS_ALPHAFUNC render state is D3DCMP_ALWAYS, so you'll likely want to change that one.

pDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_EQUAL);pDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);orpDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_LESSEQUAL);pDevice->SetRenderState(D3DRS_ALPHAREF, 10);pDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);


The first one will throw out alpha values exactly 0. The second one will throw out alpha values less than or equal to 10.

Good luck,
Chris
Chris ByersMicrosoft DirectX MVP - 2005

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.