Jump to content
  • Advertisement
Sign in to follow this  
Subconscious

Textured object transparency?

This topic is 5406 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How exactly would I go about making a 3D textured object fade out? I suppose I could lock the texture and set the alpha component of every pixel lower and lower, but that would be rediculous to do every frame. I added a color component to the vertex format along with the texture and played with the alpha component, but it doesn't seem to do anything unless the texture isn't being rendered with it. Is there some obvious way I'm missing?

Share this post


Link to post
Share on other sites
Advertisement
you might be missing a textuire stage state call. Set the ALPHAARG to DIFFUSE and then select that ARG.

SetTextureStateState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
SetTextureStateState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );

This will tell d3d to use the alpha values from the vertices.

Share this post


Link to post
Share on other sites
Look into SetTextureStageState. This allows you to choose which colors and alphas to mix, and how to mix them. It's very much like a pixel shader program, and works on all hardware.

If you look at your possible alpha arguments, you'll see everywhere you can possibly get alpha values from.

Constant - a DX9 (ie: newer cards only) color value which can be set per stage.
Current - from the previous stage
Diffuse - vertex color, or output of lighting if light is enabled
Specular - vertex color, or output of lighting if light is enabled
Temp - DX8 feature. Like current but recalls value from a stage which used RESULTARG, TEMP
Texture - From the current stage's texture
TFactor - a DX7 feature. A single color value used for the entire Draw call (SetRenderState(D3DRS_TEXTUREFACTOR, x))

Basically for each stage you have a colorop (color operation) and an alphaop. These operations may have several arguments, most use 2 arguments such as add, and modulate(multiply).

To get your texture, and your texture's alpha:
SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);

SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);

Note that we end with a disable. Also note that you must have an alphaop and colorop, it's illegal to try to create half a stage. For example, setting stage 0's alphaop to disable will cause undefined behaviour.

Now you want to mix in your vertex colors and your vertex alpha. This will do the trick:
SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);

SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);

Note that if you're using lighting, you have to tell the lighting system whether to get color(and alpha) from vertices or from the material settings.

SetRenderState(D3DRS_COLORVERTEX, true);
SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
or
SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);

The material is set with SetMaterial and takes a D3DMATERIAL9 structure. The material is used only when lighting is enabled.

Materials are a quick way to set the full mesh's alpha, however if you're already using vertex color, material alpha can't be used.

Using TFACTOR is a good solution because it works on the majority of hardware.

Another solution is to use a second texture stage, set it up to have invalid texture coordinates, set the texture border color to transparent, and set the addressing mode to border.

Another solution would be to use a vertex shader or a pixel shader to output your own alpha values.

Share this post


Link to post
Share on other sites
The root of my problem is that I had absolutely no understanding of how SetTextureStageState worked or what it did. After reading what you wrote, sleeping on it, then reading it again today, I think I have a pretty good grasp on it. Thanks for the help [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!