Sign in to follow this  
punmaster

Semitransparent textures (without lighting)

Recommended Posts

I am using Directx 9 in MS Visual Studio .NET 2K5 with C#. I was wondering if it was possible to make an object appear semitransparent without using lighting. The texture will be a red gradient, if it matters. Thanks.

Share this post


Link to post
Share on other sites
Yes.

If you want a fixed amount of transparency, set the render state called "TextureFactor" to a color with the alpha you want. Change your textureStageStates alphaarg to TFactor instead of diffuse. If using shaders, just output whatever alpha value you want.

If you want per-vertex transparency, put the alpha in the diffuse color of the vertices.

If you want per-pixel transparency, include alpha in the texture you're using, or add a second texture stage with an alpha texture.

As you currently have transparency is tied to lighting, I'm assuming you were setting the Material.Diffuse.A, which is a fixed amount over the entire mesh. To get the same result, just use the tfactor method.

Share this post


Link to post
Share on other sites
Sorry to sound like an idiot but what do I have to do to implement the "TFactor Method"? I tried this code:

device.RenderState.TextureFactor = Color.FromArgb(128, 200, 0, 0).ToArgb();
device.SetTextureStageState(0, TextureStageStates.AlphaArgument0, (int)TextureArgument.TFactor);

But nothing happened. What am I doing wrong? Thanks.

Share this post


Link to post
Share on other sites
You can either set the color for each vertex diffuse value, or use the TextureFactor.

Below is the code I use setting the alpha to 128 which is half transparent.

device.SetRenderState(RenderStates.TextureFactor, System.Drawing.Color.FromArgb(128, 255, 255, 255).ToArgb());

device.SetTextureStageState(0, TextureStageStates.ColorArgument1, (int)TextureArgument.TextureColor);
device.SetTextureStageState(0, TextureStageStates.AlphaArgument1, (int)TextureArgument.TextureColor);
device.SetTextureStageState(0, TextureStageStates.AlphaArgument2, (int)TextureArgument.TFactor);
device.SetTextureStageState(0, TextureStageStates.AlphaOperation, (int)TextureOperation.Modulate);

Share this post


Link to post
Share on other sites
Quote:
Original post by punmaster
Sorry to sound like an idiot but what do I have to do to implement the "TFactor Method"? I tried this code:

device.RenderState.TextureFactor = Color.FromArgb(128, 200, 0, 0).ToArgb();
device.SetTextureStageState(0, TextureStageStates.AlphaArgument0, (int)TextureArgument.TFactor);

But nothing happened. What am I doing wrong? Thanks.


Most operations use arg1 and arg2. arg 0 is only used in a few operations that need 3 arguments (lerp and multiplyadd). I have no idea why they called it arg0 instead of arg3, or why the docs refer to them as args1-3 while the code doesn't.

Share this post


Link to post
Share on other sites
Quote:
Original post by devronious
Can't you just put the transparency into the alpha component of the verticy's color?

That's probably the easiest way to do it if using the fixed function pipeline.

-Devin


The disadvantage with this method is that you need to modify all the vertices to change the alpha, such as fading something in. Using TFactor you can fade the alpha value by changing a single state.

Share this post


Link to post
Share on other sites
Quote:
Original post by Namethatnobodyelsetook
The disadvantage with this method is that you need to modify all the vertices to change the alpha, such as fading something in. Using TFactor you can fade the alpha value by changing a single state.

True, but for sprite engines and other applications that are using a dynamic vertex buffer, such effects are basically free since the vertices need to be rewritten anyways. I'm sure you know this, just pointing it out for others. One the coolest things about using a dynamic VB for is that there's no frame coherence required; you can scale, fade, rotate, color, and blend however you want whenever you want.

Plus, you can batch together 100 such objects of varying opacity. Using the TFACTOR method, it seems to me you would have to break the batch to get this effect, right?

Share this post


Link to post
Share on other sites
Quote:
Original post by MasterWorks
Quote:
Original post by Namethatnobodyelsetook
The disadvantage with this method is that you need to modify all the vertices to change the alpha, such as fading something in. Using TFactor you can fade the alpha value by changing a single state.

True, but for sprite engines and other applications that are using a dynamic vertex buffer, such effects are basically free since the vertices need to be rewritten anyways. I'm sure you know this, just pointing it out for others. One the coolest things about using a dynamic VB for is that there's no frame coherence required; you can scale, fade, rotate, color, and blend however you want whenever you want.

Plus, you can batch together 100 such objects of varying opacity. Using the TFACTOR method, it seems to me you would have to break the batch to get this effect, right?

Yup. All true, but I don't think the OP is there yet. I was going for simplest solution. Most sprite engines will use dynamic VBs, and the vertex color is always handy. Good for coloring text, changing alphas independantly, etc. You could even put in a per-sprite "light vector" for semi-accurate normal mapped sprites that would work even on older hardware.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this