Skip to main content
GameDev.net gamedev.net
🔒 Locked

Direct3d9 Material Alpha

Started by Fredericvo May 11, 2012 at 1:42 PM 2 replies 2.7k views
Original Post
Fredericvo
Fredericvo
Hello,

So I've been trying to fool around with transparent materials a bit and have a small problem.
I used the following code from msdn site:

ZeroMemory(&material, sizeof(D3DMATERIAL9)); // clear out the struct for use
material.Diffuse = D3DXCOLOR(1.0f, 0.4f, 0.0f, 1.0f); // Alpha purpusely set to opaque
material.Ambient = D3DXCOLOR(1.0f, 0.4f, 0.0f, 1.0f); // because that's the odd behaviour
material.Emissive = D3DXCOLOR(1.0f, 0.4f, 0.0f, 1.0f); // I want to solve
material.Specular = D3DXCOLOR(1.0f, 0.4f, 0.0f, 1.0f);

// Enable alpha blending.
d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE,
TRUE);
// Set the source blend state.
d3ddev->SetRenderState(D3DRS_SRCBLEND,
D3DBLEND_SRCCOLOR);
// Set the destination blend state.
d3ddev->SetRenderState(D3DRS_DESTBLEND,
D3DBLEND_INVSRCCOLOR);
d3ddev->SetRenderState( D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL ); // Tell D3D to get Alpha from material
d3ddev->SetMaterial(&material); // set the globably-used material to &material

OK so I DID in fact achieve my goal of transparency and see through the objects behind BUT... not with as much flexibility as I expected. I can't seem to regulate the amount of transparency that I want and even with 100% opaque values my object is still fully see-through.
TomKQT
TomKQT
I think you wanted this setting:

d3ddev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);


That's the right way to tell the device to use alpha as the blending factor.
You are using the color in your code and that's why you never get fully opaque model - it would be fully opaque only for white color which has all channels equal to 1.0
Fredericvo
Fredericvo

I think you wanted this setting:

d3ddev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);


That's the right way to tell the device to use alpha as the blending factor.
You are using the color in your code and that's why you never get fully opaque model - it would be fully opaque only for white color which has all channels equal to 1.0


Thanks! That did the job indeed. Not sure why MSDN mentioned that colour blending there. Oh well...
TomKQT
TomKQT
Yea, you're right, I also found the MSDN page with COLOR blend. It's also in the DirectX .chm file.
To be fair, it's not an error, it's a valid setting. But IMHO very unusual.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.