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

Texture Transparency

Started by MatthewEva Oct 12, 2001 at 5:27 AM 3 replies 900+ views
Original Post
MatthewEva
MatthewEva
Hi, I have been searching high and low through the MSDN DX help, and although there is alot about texture transparency I dont really get it at all. I have two meshes I render on the screen, both with their own textures and I want one of the mesh objects to appear 50% opaque, however, reading the doc, It talks about using: pDevice->SetRenderState( passing in color op and blend flags and such things, does anyone know of a site that explains this better than the normal Microsoft ''We know, so lets write a complex doc about it'' syndrome Many thanks in advance Matthew Eva
ifndef _DIRECTX PostQuitMessage(0);#endif
Mark Duffill
Mark Duffill
The base render states I use are as follows:-

DXDisplay->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );

DXDisplay->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE2X );

DXDisplay->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );

DXDisplay->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );

DXDisplay->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );

DXDisplay->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

DXDisplay->SetTextureStageState( 0, D3DTSS_MAGFILTER ,D3DTEXF_LINEAR );

DXDisplay->SetTextureStageState( 0, D3DTSS_MINFILTER ,D3DTEXF_LINEAR );

DXDisplay->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );

Then use the following modes to set up colour add, subtract and normal blending ops.

switch( BlendOp )
{
case PCDX_LF_COLADD:
DXDisplay->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE2X );
DXDisplay->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
DXDisplay->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
break;

case PCDX_LF_COLSUB:
DXDisplay->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SUBTRACT );
DXDisplay->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ZERO);
DXDisplay->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCCOLOR );
break;

case PCDX_LF_COLNORM:
default:
DXDisplay->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE2X );
DXDisplay->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
DXDisplay->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
break;
}

Note you may only want to do modulate, not modulate 2.

The "PCDX_LF_COLNORM" is the normal case you would use.


The Jackal
MatthewEva
MatthewEva
Many thanks for that timely reply,

Is there a way of changing the opacity of the alpha state, say from 10% to 90%, is this defined by the current alpha state of the material being used on the surface with the current texture ?

The MFXTex.EXE tool has a field called blend factor, that seems to be a integer value, I wondered if this is it.

Regards
ifndef _DIRECTX PostQuitMessage(0);#endif
Mark Duffill
Mark Duffill
If the texture has a solid alpha (0xff) you can modify the underlying vert alpha directly in the vert ARGB, or more useful is a global ambient applied to all the primitives drawn.

We use

DXDisplay->SetRenderState(D3DRS_AMBIENT , 0xffffffff);

But it only affects the r,g,b components to modify the alpha you have to use the texture factor stage as follows:-

Set up texture stage 1 like this

DXDisplay->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_CURRENT );

DXDisplay->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_MODULATE );

DXDisplay->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_TFACTOR );

DXDisplay->SetTextureStageState( 1, D3DTSS_ALPHAARG1, D3DTA_CURRENT );

DXDisplay->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_MODULATE );

DXDisplay->SetTextureStageState( 1, D3DTSS_ALPHAARG2, D3DTA_TFACTOR );

This passes through the vert/texture colours from stage 0.

Then set the alpha as follows :-

DXDisplay->SetRenderState( D3DRS_TEXTUREFACTOR, D3DCOLOR_ARGB( a, 255,255,255 ) );

Note if you require a texture also on stage 1 you would have to change the states as follows

DXDisplay->SetTextureStageState( 1, D3DTSS_COLORARG1, UseTex ? D3DTA_TEXTURE : D3DTA_CURRENT );

DXDisplay->SetTextureStageState( 1, D3DTSS_COLOROP, UseTex ? D3DTOP_SELECTARG1 : D3DTOP_MODULATE );

I hope this helps.

Why you can''t put a global alpha in the ambient I have no idea.
(maybe that will be in DX 9

The Jackal
Blaster
Blaster
About the first post of Mark Duffill, I would like to know if that subtract operator will be applied between the mesh and the frame buffer or between the different texture stages of the mesh?

Because I am trying to do ''subtractive blending'' of a texture (only 1 stage) over the frame buffer and I can''t find how with DirectX 7.

In DX8 it''s easy you just have to change the D3DRS_BLENDOP but there is no such thing in DX7.

Topic Locked

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

Sign in to reply to this topic.