Sign in to follow this  
nsto119

Making an entire primitive transparent, dynamically [MDX]

Recommended Posts

So, say I have a triangle. How can I make the whole have the same alpha value, regardless of texture or color data? For example, if I want the triangle to be fully opaque when the mouse is over it and transparent when it's not, what do I need to do in the triangle's rendering method?

Share this post


Link to post
Share on other sites
You'll have to modulate the alpha from the texture or color with another alpha value to achieve this effect. There are a number of ways to do this, but the easiest is probably by using the TextureFactor, which can serve as a constant in the texture stages during rendering. The following code should modulate your texture color with the alpha you specify:

[source lang=C#]
// set up blending as usual
device.RenderState.AlphaBlendEnable = true;
device.RenderState.SourceBlend = Blend.SourceAlpha;
device.RenderState.DestinationBlend = Blend.InvSourceAlpha;

// set up texture stage states
device.TextureState[0].AlphaArgument1 = TextureArgument.TFactor;
device.TextureState[0].AlphaArgument2 = TextureArgument.TextureColor;
device.TextureState[0].AlphaOperation = TextureOperation.Modulate;

// set up our global alpha as the texture factor
float alphaPerc = 0.25f; // alphaPerc from 0.0f to 1.0f
int alpha = (int)(alphaPerc * 255.0f); // alpha from 0 to 255
alpha = alpha << 24; // shift to correct position in ARGB int
device.RenderState.TextureFactor = alpha;

// render stuff here

// reset stages
device.RenderState.AlphaBlendEnable = false;
device.TextureState[0].AlphaOperation = TextureOperation.SelectArg2;





I pieced it together form some other code I had lying around, so it might not work out of the box. Still, it should give you an idea of how this can be done, I hope :)

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