Sign in to follow this  

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

This topic is 4198 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

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[i] = mtrl[i].Material3D;
                    meshMats[i].Ambient = meshMats[i].Diffuse;
                    meshMats[i].Diffuse = Color.FromArgb(127, meshMats[i].Diffuse);

                    if ((mtrl[i].TextureFilename != null) && (mtrl[i].TextureFilename != string.Empty))
                        meshTexts[i] = TextureLoader.FromFile(d, @"..\..\Models\" + mtrl[i].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[i];
                device.SetTexture(0, textures[i]);
                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? :(

Share this post


Link to post
Share on other sites
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 :).

Share this post


Link to post
Share on other sites
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[i];
device.SetTexture(0, textures[i]);
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?

Share this post


Link to post
Share on other sites

This topic is 4198 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.

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