# Transparency

## Recommended Posts

I'm trying to familiarize my self with the direct3d api and have rendered a simple scene with two spheres, a cube mesh and a directional light. I set material properties and I've experimented with different settings to see how it changes the appearances of the meshes. I now want to try making one of the spheres a bit transparent. As I understand it you need to change the alpha blending state of the renderer. I've enabled alpha blending, set blendmode, blendoperation etc. I've enabled the z-buffer, and per-pixel alpha testing. The alpha component of the diffuse, emissive, specular color of material is set to 100. Nothing happens. What am I missing?

##### Share on other sites
Check this tutorial out. Generally, stuff from CodeSampler is pretty good.

Values for the material members should be between 0.0 and 1.0. For example:

alphaMaterial.Diffuse.r = 1.0f;
alphaMaterial.Diffuse.g = 1.0f;
alphaMaterial.Diffuse.b = 1.0f;
alphaMaterial.Diffuse.a = 0.5f;
d3dDevice->SetMaterial( &alphaMaterial );

If you have stuff set up to 100, this may be your problem. Also, have you set the following texture states?

d3dDevice->SetRenderState( D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL );d3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );d3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );d3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );d3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );

##### Share on other sites
Using managed directx I could choose to specify color component are specified in the range 0-255. I tried setting texture stage but nothing happened. I use Mesh.Sphere() and Mesh.Box() to construct the spheres and the box.

##### Share on other sites
Be certain that Mesh.Sphere and Mesh.Box create normals and turn lighting on. If they don't create normals, you'll have to do that. If you don't want to mess with all of that, you can make the object alpha several other ways. One would be to change the vertex DIFFUSE alpha value and set the texture stage states to use DIFFUSE. You'd have to change every vertex which is annoying. Another way (and there's 2 ways of doing it) is to set a constant in the texture stage state and use it for alpha. (instead of setting it to use DIFFUSE, you'd set it to use CONSTANT and set the CONSTANT value to the alpha value). The second way if you aren't using DX 9, you'll need to set the TFACTOR render state to the value and then tell the proper texture stage state to use the TFACTOR value for alpha. I'm in a hurry, so I may try to expand on this tonight when I get home.

Chris

##### Share on other sites
I would like to pose a related question, does the same apply for 2D sprites and making them transparent? Sorry to butt in but i was wondering and making a new post seems dumb.

Zwig

##### Share on other sites
Quote:
 Original post by Supernat02Be certain that Mesh.Sphere and Mesh.Box create normals and turn lighting on.

I make sure normals are computed for the meshes and lighting is turned on. This is driving me nuts.. Is there a simple alpha blending example written in C# somewhere?

##### Share on other sites
Zwig, it depends on what you mean by 2D. If you are using an ortho projection with un-transformed vertices, then yes. If you are using XYZRHW, or calling the ID3DXSprite 2D functions, then no. XYZRHW represents pre-lit, pre-transformed vertices, so it won't transform them and it won't light them. You can't use materials and lighting to make them blend. However, you can use the alpha value of the texture or the alpha value of the diffuse color from the vertex. You would still need to enable alpha blending. Check out Drunken Hyena for more details.

Opwiz, you might find that code useful as well. He has some C# code, but the alpha tutorial is done in C. You should be able to easily convert though. Also, can you post the code that enables the z buffer and alpha testing? Or some other important code would be good. Maybe we can help further.

## 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

• ### Forum Statistics

• Total Topics
627719
• Total Posts
2978796

• 10
• 9
• 21
• 14
• 12