[mdx] Can't get alpha blending to work on meshes

Started by
5 comments, last by fervent 17 years, 10 months ago
Hi, I'm trying to draw a few of my meshes with varying opacity (MDX 1.1). I read a few tutorials and such, set the alpha value of the mesh's material's diffuse color, and turned on alphablending before drawing. Yet, the meshes appear 100% black (on a black background). I spent some time tweaking the render and texture states, but I can't seem to get the model to appear 50% transparent. Here's my load mesh function. As you can see, I'm manually resetting the diffuse color on-load to give the model 50% alpha.

private static Mesh loadMesh(Device d, string file, ref Material[] meshMats, ref Texture[] meshTexts)
        {
            ExtendedMaterial[] mtrl;
            Mesh tempMesh = Mesh.FromFile(file, MeshFlags.SystemMemory, d, out mtrl);

            if ((mtrl != null) && (mtrl.Length > 0))
            {
                meshMats = new Material[mtrl.Length];
                meshTexts = new Texture[mtrl.Length];

                for (int i = 0; i < mtrl.Length; i++)
                {
                    meshMats = mtrl.Material3D;
                    meshMats.Ambient = meshMats.Diffuse;
                    meshMats.Diffuse = Color.FromArgb(127, meshMats.Diffuse);

                    if ((mtrl.TextureFilename != null) && (mtrl.TextureFilename != string.Empty))
                        meshTexts = TextureLoader.FromFile(d, @"..\..\Models\" + mtrl.TextureFilename);
                }
            }

            return tempMesh;
        }

Here's my draw function, where I made a feeble attempt to set the correct render and texture states...

        public void Draw()
        {
            device.RenderState.AlphaBlendEnable = true;
            device.RenderState.SourceBlend = Blend.SourceAlpha;
            device.RenderState.DestinationBlend = Blend.DestinationAlpha;
            device.SetTextureStageState(0, TextureStageStates.AlphaOperation, true);
            device.SetTextureStageState(0, TextureStageStates.AlphaArgument1, true);
            device.SetTextureStageState(0, TextureStageStates.AlphaArgument2, true);

            for (int i = 0; i < materials.Length; i++)
            {
                device.Material = materials;
                device.SetTexture(0, textures);
                mesh.DrawSubset(i);
            }
            device.RenderState.AlphaBlendEnable = false;
        }

If it makes a difference to the solution of my problem, I have lighting enabled and have various point lights in the scene. What am I missing? :(
Advertisement
The calls to SetTextureStageState shouldn't have a boolean as the value. In fact, I'm not too sure why that overload is even there.

When defining the TextureStageStates, you're telling DX how to get a color and alpha value for a pixel. In your case, you want to tell it to use the diffuse alpha. The Op defines which operation to use (you want to Select a single argument, other options are Modulate and Add, for example). Then, you provide an argument that's the Diffuse color (a combination of material and vertex color). Heres how you do it:

For the first call, when you are setting the AlphaOp, you'd want to use TextureOperation.SelectArg1 (this would be an int value).
For AlphaArgument1, you'd want to use TextureArgument.Diffuse to get the alpha value out of the material and vertex color (if the vertices have a color value).
AlphaArgument2 doesn't matter right now, since you are selecting Arg1, and ignoring Arg2 completely.

Hope this helps some :).
Sirob Yes.» - status: Work-O-Rama.
Hmm, thanks for the reply.

I modified the texturestate code in the draw function as follows:
        public void Draw()        {            device.RenderState.AlphaBlendEnable = true;            device.RenderState.SourceBlend = Blend.SourceAlpha;            device.RenderState.DestinationBlend = Blend.InvSourceAlpha;            // device.SetTextureStageState(0, TextureStageStates.AlphaOperation, true);            // device.SetTextureStageState(0, TextureStageStates.AlphaArgument1, true);            // device.SetTextureStageState(0, TextureStageStates.AlphaArgument2, true);            device.TextureState[0].AlphaOperation = TextureOperation.SelectArg1;            device.TextureState[0].AlphaArgument1 = TextureArgument.Diffuse;            for (int i = 0; i < materials.Length; i++)            {                device.Material = materials;                device.SetTexture(0, textures);                mesh.DrawSubset(i);            }            device.RenderState.AlphaBlendEnable = false;        }


But I still get 100% solid black meshes (on a black background) :(

What else could be going wrong?
oops, misread the post.
No worries.

I spent some time tweaking render and texture states. Still can't get the model to show up non-black.

Any other thoughts?
Just a guess: Did you either set up a light or set device.RenderState.Lighting to false?

Sorry, poor reading :) Does the mesh render correctly when you disable alpha blending?
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!
Yes, when I disable alphablending, it shows up just fine

This topic is closed to new replies.

Advertisement