Jump to content
  • Advertisement
Sign in to follow this  

Vertex alpha blending

This topic is 5392 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

                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();

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

                //End the scene
                // Update the screen

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!