Jump to content
  • Advertisement
Sign in to follow this  
Teldan

[C#] problem with transparent material

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

Share this post


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

Share this post


Link to post
Share on other sites
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?

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Great. The last one helped me out:

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

Thanks a lot.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!