Sign in to follow this  
istallion

Vertex alpha blending

Recommended Posts

I want to fade a texture from 100% transparent to 100% opaque. I've played with the renderstate until I'm blue in the face, but I can't get any transparency. The foreground texture is only getting shaded black in one of the corners. The sample code in MSDN doesn't even mention that you need to set the alpha operations & arguments, but many others have. The results are the same either way.

            CustomVertex.PositionColoredTextured[] verts;

            device.RenderState.Lighting = false;
            device.RenderState.ZBufferEnable = false;

            device.RenderState.AlphaBlendEnable = true;
            device.RenderState.AlphaSourceBlend = Blend.SourceAlpha;
            device.RenderState.AlphaDestinationBlend = Blend.InvSourceAlpha; 

            device.TextureState[0].AlphaOperation = TextureOperation.Modulate;
            device.TextureState[0].AlphaArgument1 = TextureArgument.TextureColor;
            device.TextureState[0].AlphaArgument2 = TextureArgument.Diffuse;
            
            device.VertexFormat = CustomVertex.PositionColoredTextured.Format;

            
            for (int i = 0; i < 256; i++)
            {
                device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Aqua, 1.0f, 0);
                //Begin the scene
                device.BeginScene();
                
                SetupMatrices();

                verts = (CustomVertex.PositionColoredTextured[])vb1.Lock(0, LockFlags.None);
                verts[0].Color = Color.FromArgb(255 - i, 0, 0, 0).ToArgb();
                verts[1].Color = Color.FromArgb(255-i, 255, 255, 255).ToArgb();
                verts[2].Color = Color.FromArgb(255-i, 255, 255, 255).ToArgb();
                verts[3].Color = Color.FromArgb(255-i, 255, 255, 255).ToArgb();
                vb1.Unlock();

                device.SetStreamSource(0, vb1, 0);
                device.SetTexture(0, picTex[currentArrIndex]);
                device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);

                //End the scene
                device.EndScene();
                // Update the screen
                device.Present();
            }

Share this post


Link to post
Share on other sites
device.RenderState.SourceBlend and .DestBlend instead of AlphaSourceBlend and AlphaDestBlend should be used to control how colors blend rather than alphas. ie, you want to use alpha to mix colors, the resulting alpha value in the framebuffer is not important to you.

If lighting were enabled, I'd suggest also looking at RenderState.ColorVertex, and RenderState.DiffuseMaterialSource to ensure that the diffuse is based on your per-vertex diffuse rather than material diffuse settings.

When fading an object, you generally don't want to modify the vertices all the time... look at RenderState.TextureFactor and TextureArgument.TFactor instead. It's a single color value you can program per draw call and use in your texture stages.

Note, all state names may be slightly off as I don't use C#.

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