Sign in to follow this  
Headkaze

TFactor and Diffuse

Recommended Posts

Is it possible to have both TFactor and Diffuse to control the alpha value? Say I have an array of quads and I would like to control the alpha value of each of them individually I would use the diffuse value of the vertexes to do that. But say I want to fade all of the quads out maintaining their diffuse alpha value as well can I apply a TFactor to do that? Is it possible to use both of these at the same time? Ie. Combining these states
device.SetRenderState(RenderStates.AlphaBlendEnable, true);
device.SetRenderState(RenderStates.SourceBlend, (int)Blend.SourceAlpha);
device.SetRenderState(RenderStates.DestinationBlend, (int)Blend.InvSourceAlpha);
device.SetRenderState(RenderStates.TextureFactor, System.Drawing.Color.FromArgb(alpha, 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);

and

device.SetTextureStageState(0, TextureStageStates.ColorArgument1, (int)TextureArgument.TextureColor);
device.SetTextureStageState(0, TextureStageStates.AlphaArgument1, (int)TextureArgument.TextureColor);
device.SetTextureStageState(0, TextureStageStates.AlphaArgument2, (int)TextureArgument.Diffuse);
device.SetTextureStageState(0, TextureStageStates.AlphaOperation, (int)TextureOperation.Modulate);
[Edited by - Headkaze on August 12, 2008 4:55:23 PM]

Share this post


Link to post
Share on other sites
Forgot to mention I also need to maintain the alpha channel in the texture as well. Edited my original post to show the full render states I need to combine.

Is this possible or will I have to use diffuse and set the alpha for all the quads individually?

Share this post


Link to post
Share on other sites
Yes, you can do the double modulation. The texture cascade just defines a series of stages that allow a certain amount of math in each stage. You're only using two stages here, so it should work fine in all but the oldest of hardware (that which only supports a single texture stage).

Share this post


Link to post
Share on other sites
As most people don't understand the texture stages, I'll expand on Richard's reply a bit. Building off your first set of stages.

Set stage 1's colorop to selectarg1, and colorarg1 to current. This keeps the same RGB value.

Set stage 1's alphaop to modulate, alphaarg1 to current, and alphaarg2 to diffuse. This multiples diffuse by "current". "Current" was the output of stage 0.

Set stage 2's colorop and alphaop to disable.

If you're not in the habit of putting in disabled stages at the end of each effect setup, you'll want to ensure you set stage 1 to disabled after rendering using this method. If you don't stage 1 will still be active and performing the extra diffuse modulate you've asked for.

Share this post


Link to post
Share on other sites
Thanks guys, it's much appreciated and seems to work perfectly :)

Can I just confirm that I'm setting the correct render states and turning them off correctly?

device.SetTextureStageState(0, TextureStageStates.ColorOperation, (int)TextureOperation.Modulate);
device.SetTextureStageState(0, TextureStageStates.ColorArgument1, (int)TextureArgument.TextureColor);
device.SetTextureStageState(0, TextureStageStates.ColorArgument2, (int)TextureArgument.Diffuse);

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

device.SetTextureStageState(1, TextureStageStates.ColorOperation, (int)TextureOperation.SelectArg1);
device.SetTextureStageState(1, TextureStageStates.ColorArgument1, (int)TextureArgument.Current);

device.SetTextureStageState(1, TextureStageStates.AlphaOperation, (int)TextureOperation.Modulate);
device.SetTextureStageState(1, TextureStageStates.AlphaArgument1, (int)TextureArgument.Current);
device.SetTextureStageState(1, TextureStageStates.AlphaArgument2, (int)TextureArgument.Diffuse);


And switching the render states back to normal..

device.SetTextureStageState(0, TextureStageStates.ColorOperation, (int)TextureOperation.Modulate);
device.SetTextureStageState(0, TextureStageStates.ColorArgument1, (int)TextureArgument.TextureColor);
device.SetTextureStageState(0, TextureStageStates.ColorArgument2, (int)TextureArgument.Diffuse);

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

device.SetTextureStageState(1, TextureStageStates.ColorOperation, (int)TextureOperation.Disable);
device.SetTextureStageState(1, TextureStageStates.AlphaOperation, (int)TextureOperation.Disable);


[Edited by - Headkaze on August 13, 2008 10:42:23 AM]

Share this post


Link to post
Share on other sites
Looks fine. Personally I try not to rely on existing state, and would set stage 2's colorop and alphaop to disable when setting up the effect. This makes this effect setup more self contained, rather than hoping that whatever was drawn previously had stage 2 already set to disabled.

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