[C#] problem with transparent material

Started by
5 comments, last by Teldan 17 years ago
Hi, i tried to render a sphere with a transparent materiel. But i can't get it to work. The sphere is rendered correct (right position, right color, etc.) but not transparent.
// set up material
Material mat;
mat.Ambient = Color.FromArgb(64, 0, 127, 255);
mat.Diffuse = Color.FromArgb(64, 0, 127, 255);

// the mesh
mesh = Mesh.Sphere(device, radius, 18, 18);

// while rendering
device.RenderState.AlphaBlendEnable = true;
device.RenderState.DiffuseMaterialSource = ColorSource.Material;
device.RenderState.CullMode = Cull.None;
device.Material = mat;
device.SetTexture(0, null);
mesh.DrawSubset(0);
Am i doing something wrong? Any idea what could cause this problem? Thx in advance.
Greetings,Teldan
Advertisement
The code you've shown seems correct. What is it that isn't transparent about it? are you drawing other objects that should be behind it but it is "covering" them?

In any case, translucent objects need to be rendered last, after all opaque objects have been rendered, and preferably in a back-to-front order. Make sure the sphere is that last thing you draw, so it can blend with things behind it correctly.

Hope this helps.
Sirob Yes.» - status: Work-O-Rama.
I was just implementing the collision detection with vounding spheres and wanted to visualize it for testing purpose. So there should be an object inside the sphere. And yes, the sphere is rendered last, just before the EndScene command.

Is there something else that must be set up special, like the backbuffer format or something, before you can use AlphaBlending? Or has the zBuffer to be a special format or in a special state?
Greetings,Teldan
The problem might be that your card doesn't handle the null texture like most cards do, as the code you posted should typically work. But in fact, the results of using a null texture on any card are undefined (more info), so you'll have to set up your texture stage states appropriately to use only the material color source. I've also had some problems getting the material color to work with alpha, so a simple workaround would be to use the TFactor constant to set the sphere's color (note that you'll also have to set up your texture state accordingly for this).

Hope this helps :)
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!
Quote:Original post by remigius
The problem might be that your card doesn't handle the null texture like most cards do, as the code you posted should typically work. But in fact, the results of using a null texture on any card are undefined (more info), so you'll have to set up your texture stage states appropriately to use only the material color source. I've also had some problems getting the material color to work with alpha, so a simple workaround would be to use the TFactor constant to set the sphere's color (note that you'll also have to set up your texture state accordingly for this).

Hope this helps :)


Hi, and thanks for this hint. Unfortunatly i have no idea how to do this ^^ (im absolutly new in game programming).

Greetings,Teldan
Here's a little snippet that sets up the texture states to use only the diffuse material color for your mesh:

device.TextureState[0].ColorArgument1 = TextureArgument.Diffuse;device.TextureState[0].ColorArgument2 = TextureArgument.TextureColor;device.TextureState[0].ColorOperation = TextureOperation.SelectArg1;// render mesh here


Alternatively, the code to use the TFactor constant would be:

device.TextureState[0].ColorArgument1 = TextureArgument.TFactor;device.TextureState[0].ColorArgument2 = TextureArgument.TextureColor;device.TextureState[0].ColorOperation = TextureOperation.SelectArg1;device.RenderState.TextureFactor = System.Drawing.Color.Blue.ToArgb();// render mesh here


And finally, don't forget to set your texture states back to normal after you render your mesh, using:

device.TextureState[0].ColorArgument1 = TextureArgument.Diffuse;device.TextureState[0].ColorArgument2 = TextureArgument.TextureColor;device.TextureState[0].ColorOperation = TextureOperation.Modulate;


--- edit ---

Just noticed you are enabling AlphaBlending, but you aren't telling your device how you want it to blend. For typical alpha blending, you should apply these settings:

device.RenderState.AlphaBlendEnable = true;device.RenderState.SourceBlend = Blend.SourceAlpha;device.RenderState.DestinationBlend = Blend.InvSourceAlpha;



Hope this helps :)

[Edited by - remigius on April 24, 2007 2:25:41 PM]
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!
Great. The last one helped me out:

device.RenderState.SourceBlend = Blend.SourceAlpha;device.RenderState.DestinationBlend = Blend.InvSourceAlpha;

Thanks a lot.
Greetings,Teldan

This topic is closed to new replies.

Advertisement