TFactor and Diffuse

Started by
5 comments, last by Namethatnobodyelsetook 15 years, 8 months ago
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]
Advertisement
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?
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).

My free book on Direct3D: "The Direct3D Graphics Pipeline"
My blog on programming, vintage computing, music, politics, etc.: Legalize Adulthood!

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.
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]
If you're fuzzy on how the texture stages work, consult my pipeline poster to give yourself a mental picture of the dataflow through the entire pipeline and then read Chapter 11. Basic Texturing from my book "The Direct3D Graphics Pipeline", which you can read for free.

My free book on Direct3D: "The Direct3D Graphics Pipeline"
My blog on programming, vintage computing, music, politics, etc.: Legalize Adulthood!

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.

This topic is closed to new replies.

Advertisement