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

Transparent Texture

Started by dxdotnet1 Nov 18, 2004 at 2:07 AM 1 replies 450+ views
Original Post
dxdotnet1
dxdotnet1
how to render transparent texture in DX? where to specify that i will use a transparent texture? is it during texture load or during renderin?
Endurion
Endurion
There's two ways for transparent textures.

Simple transparency for sprites, having one color to be invisible. That's done with alpha testing, you need to enable the render state, set the alpha compare function and the alpha reference value. And your texture needs to have an alpha component (1 bit will do).

Blending transparency, as the texture is semi-see-through. This you can do with alpha blending. Enable the state, set the texture stages, set srcblend and destblend and have alpha component in the texture (best 8 bits, fewer will work also).

Here some sample snippets for generally setting the states. The snippets are using DX8, i think it works with DX9 too.

Alphablend only:
    Viewer.SetRenderState( D3DRS_ALPHATESTENABLE, FALSE );    Viewer.SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );    Viewer.SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );    Viewer.SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );    Viewer.SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );    Viewer.SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );    Viewer.SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );    Viewer.SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );    Viewer.SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );    Viewer.SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );    Viewer.SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );    Viewer.SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );


Alphatest only:
    Viewer.SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );    Viewer.SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );    Viewer.SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_DIFFUSE );    Viewer.SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_TEXTURE );    Viewer.SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );    Viewer.SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );    Viewer.SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );    Viewer.SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );    Viewer.SetRenderState( D3DRS_ALPHAREF, 8 );    Viewer.SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL );


Both alpha test and blend:
    Viewer.SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );    Viewer.SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );    Viewer.SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );    Viewer.SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );    Viewer.SetRenderState( D3DRS_ALPHAREF, 8 );    Viewer.SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL );    Viewer.SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );    Viewer.SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );    Viewer.SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );    Viewer.SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );    Viewer.SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );    Viewer.SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );    Viewer.SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );    Viewer.SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
Aldenar
Aldenar
You have many choices for transparency in DX, including:

1. Creating a bitmap with an explicit alpha channel and load it on a texture with a format that supports alpha (such as D3DFMT_A8R8G8B8 for example)

2. Specifying alpha in your vertex colors.

3. Using some kind of computation to output the alpha value from a vertex or pixel shader.

But whatever technique you use, you'll have to enable transparency. This is done by setting the D3DRS_ALPHABLENDENABLE render state to TRUE and setting the D3DRS_SRCBLEND & D3DRS_DESTBLEND to appropriate blending modes (D3DBLEND_SRCALPHA for D3DRS_SRCBLEND and D3DBLEND_INVSRCALPHA for D3DRS_DESTBLEND for normal transparency).

Topic Locked

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

Sign in to reply to this topic.