Sign in to follow this  
pcmattman

Alpha blending issues

Recommended Posts

I've got a little test program I wrote to learn Direct3D, and it works well (considering I knew noting about Direct3D before) and I now understand it all. However, I'm a bit confused as to why, when I set my alpha to 0xFF (ie. 100%) the vertices disappear. At first I thought that it was because I was getting my intensities wrong, but when I tried 0 I had the same problem! The only place where I can get the shape to show properly is at 50% alpha. The color I set was simple, 0xFFFFFFFF. Yet, it didn't work. I'm really confused! Edit: actually, it seems to be by normals. Can anyone give any hints as to how the normals are calculated? Also, I'm using D3DXMatrixRotationYawPitchRoll to rotate my viewport, which probably doesn't help. Any ideas?

Share this post


Link to post
Share on other sites
I'm not entirely sure what you're describing here - it sounds a bit like the colorkey used when loading via D3DX, but not quite. Some short/relevant code snippets maybe?

The normals (and by implication lighting calculations) only indirectly affect the blending result, so again - some more info on how you've come to this conclusion would be useful. Normal generation is a fairly simple case of vector maths and if you're loading a mesh from a file it'll probably already have the normals pre-computed and stored...


hth
Jack

Share this post


Link to post
Share on other sites
I'm not loading a mesh yet, just outputting vertices (to make a square-based pyramid).

I have alpha blending turned on, so the diffuse color of each vertex has an extra byte at the front to govern alpha (instead of 0xFFFFFF for white, 0xFFFFFFFF). Unfortunately, I can't get the shape to show with anything but 0x7F alpha.

When I turn off lighting (hence removing the normals) and disable alpha blending, I don't have a problem.

The only other possible reason I can think of is my method of rotating the camera (D3DXMatrixRotationYawPitchRoll), but I don't think that could be the cause.

As for source, here's my vertex initialization (for the four triangles that make the pyramid):

// the triangles
D3DVERTEX aTriangle[] = {
// x is left/right
// y is up/down
// z is how close it is to me
// normals are which direction the shape's face points
// diffuse is the color
// u is the texture's x
// v is the texture's y

// x y z nx ny nz diffuse u v
{ 0.0f, 1.0f, 0.0f,/* 0.0f, 1.0f, -1.0f,*/ DEFAULT_VERTEX_COLOR, 0.5f, 0.0f},
{ -1.0f, -1.0f, -1.0f,/* 0.0f, 1.0f, -1.0f,*/ DEFAULT_VERTEX_COLOR, 0.0f, 1.0f},
{ 1.0f, -1.0f, -1.0f,/* 0.0f, 1.0f, -1.0f,*/ DEFAULT_VERTEX_COLOR, 1.0f, 1.0f},
{ 0.0f, 1.0f, 0.0f,/* -1.0f, 1.0f, 0.0f,*/ DEFAULT_VERTEX_COLOR, 0.5f, 0.0f},
{ -1.0f, -1.0f, 1.0f,/* -1.0f, 1.0f, 0.0f,*/ DEFAULT_VERTEX_COLOR, 0.0f, 1.0f},
{ -1.0f, -1.0f, -1.0f,/* -1.0f, 1.0f, 0.0f,*/ DEFAULT_VERTEX_COLOR, 1.0f, 1.0f},
{ 0.0f, 1.0f, 0.0f,/* 0.0f, 1.0f, 1.0f,*/ DEFAULT_VERTEX_COLOR, 0.5f, 0.0f},
{ 1.0f, -1.0f, 1.0f,/* 0.0f, 1.0f, 1.0f,*/ DEFAULT_VERTEX_COLOR, 0.0f, 1.0f},
{ -1.0f, -1.0f, 1.0f,/* 0.0f, 1.0f, 1.0f,*/ DEFAULT_VERTEX_COLOR, 1.0f, 1.0f},
{ 0.0f, 1.0f, 0.0f,/* 1.0f, 1.0f, 0.0f,*/ DEFAULT_VERTEX_COLOR, 0.5f, 0.0f},
{ 1.0f, -1.0f, -1.0f,/* 1.0f, 1.0f, 0.0f,*/ DEFAULT_VERTEX_COLOR, 0.0f, 1.0f},
{ 1.0f, -1.0f, 1.0f,/* 1.0f, 1.0f, 0.0f,*/ DEFAULT_VERTEX_COLOR, 1.0f, 1.0f}
};



DEFAULT_VERTEX_COLOR is defined as 0x7FFFFFFF, which uses a macro I wrote:

#define D3D_RGBA( r, g, b, a ) ( (DWORD) ( ( a << 24 ) | ( r << 16 ) | ( g << 8 ) | b ) )



You can see I've commented out the normal's positions, but while I no longer have alpha issues, I'm now stuck with 'invisible' faces on the pyramid... Any ideas?

Share this post


Link to post
Share on other sites
What is your texturing and render-state setup?

Obviously you'll need alpha blending enabled (D3DRS_ALPHABLENDENABLE = TRUE) and the appropriate source/destination blending states.

But for vertex alpha you need to modify the texture stage state to modulate between texture alpha and vertex alpha or, iirc, it'll just fetch the alpha from the texture and ignore any interpolated vertex alpha.

Also, you could try using the standard D3DCOLOR_ARGB() macro instead of your own - just to make sure you don't have something wrong with your implementation...

hth
Jack

Share this post


Link to post
Share on other sites
Quote:

Obviously you'll need alpha blending enabled (D3DRS_ALPHABLENDENABLE = TRUE) and the appropriate source/destination blending states.


That's how I'm enabling/disabling alpha blending.

Quote:

But for vertex alpha you need to modify the texture stage state to modulate between texture alpha and vertex alpha or, iirc, it'll just fetch the alpha from the texture and ignore any interpolated vertex alpha.


Ok, here's how I setup all this stuff:

// setup the perspective
D3DXMatrixPerspectiveFovLH( &m_matProjection, m_fFOV, m_fAspectRatio, m_fNearPlane, m_fFarPlane );
m_pD3DDev->SetTransform( D3DTS_PROJECTION, &m_matProjection );

// setup the sampler
for( int i = 0; i < 8; ++i )
{
// set each sampler
m_pD3DDev->SetTextureStageState( i, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
m_pD3DDev->SetTextureStageState( i, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
m_pD3DDev->SetTextureStageState( i, D3DTSS_MIPFILTER, D3DTEXF_LINEAR );
m_pD3DDev->SetTextureStageState( i, D3DTSS_MAXANISOTROPY, m_dwAnisotropy );
}

// setup the render state
m_pD3DDev->SetRenderState( D3DRS_AMBIENT, RGB( 0, 0, 0 ) ); // ambient light
m_pD3DDev->SetRenderState( D3DRS_LIGHTING, true ); // we want lighting
m_pD3DDev->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE); // don't cull
m_pD3DDev->SetRenderState( D3DRS_ZENABLE, D3DZB_FALSE ); // disable depth buffering
m_pD3DDev->SetRenderState( D3DRS_ALPHABLENDENABLE, true ); // alpha blending
m_pD3DDev->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ); // the source blend
m_pD3DDev->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ); // destination blend

// setup alpha
m_pD3DDev->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );

// set the vertex format
m_pD3DDev->SetVertexShader( D3DFVF_CUSTOMVERTEX );

// setup a light
D3DLIGHT8 Light;

// set up the light settings (white light, facing the triangle, 1000 unit range)
ZeroMemory( &Light, sizeof( Light ) );
Light.Type = D3DLIGHT_DIRECTIONAL;
Light.Diffuse.r = 1.0f;
Light.Diffuse.g = 1.0f;
Light.Diffuse.b = 1.0f;
Light.Direction.x = 0.0f;
Light.Direction.y = 0.0f;
Light.Direction.z = 1.0f;
Light.Range = 1000.0f;

// install and enable the light
m_pD3DDev->SetLight( 0, &Light );
m_pD3DDev->LightEnable( 0, true );





Quote:

Also, you could try using the standard D3DCOLOR_ARGB() macro instead of your own - just to make sure you don't have something wrong with your implementation...


OK, I was looking for the D3D version but didn't find one, hence the reason I wrote my own. Thanks.

Edit: Don't know if it helps, but this code is what I wrote from the tutorial at [url]http://www.two-kings.de/tutorials/dxgraphics/[/url]...

Edit 2: Actually, I've been doing some testing and it seems that my lighting isn't right. I've only got one directional light pointing at the left side of the triangle, and I assumed that when I used D3DXMatrixRotationYawPitchRoll the light wouldn't move. It did. So I had parts of my model in darkness and parts in light, which made it look really odd.

[Edited by - pcmattman on June 10, 2007 6:53:06 PM]

Share this post


Link to post
Share on other sites
Actually, the answer was right in front of my eyes:

m_pD3DDev->SetRenderState( D3DRS_ZENABLE, D3DZB_FALSE ); // disable depth buffering



I have no idea why the tutorial said to disable depth buffering, but I've since enabled it with perfect results.

Sorry for wasting everyone's time!

Share this post


Link to post
Share on other sites

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