• 13
• 18
• 19
• 27
• 9

# Lighting won't work with vertex colors

This topic is 4399 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to get lighting working and for the life of me I can't get it to work. I've tried everything I can possibly think of to get it to work properly but it just won't work. Here's the problem: when I turn on lighting it always uses the material as the source color instead of the vertex color and so everything is being drawn as white. I'm trying to use a white directional light that's pointing straight down onto the map with a white material so 100% of the vertex color shines through. The vertex colors exist: when I turn off lighting the map is colored by vertex colors properly. The vertex normals are correct and even if they weren't I tried setting all normals to up just to see what would happen and everything still stayed white. Here's the relevant rendering code, minus error checking for compactness (errors aren't a problem either):
/*Vertex info
struct Vertex
{
float x,y,z;
float nx,ny,nz;
D3DCOLOR col;
};

#define D3DFVF_MYVERTEX ( D3DFVF_XYZ  | D3DFVF_NORMAL | D3DFVF_DIFFUSE )
*/
pD3DDev->SetRenderState( D3DRS_LIGHTING, TRUE );
pD3DDev->SetRenderState( D3DRS_COLORVERTEX, TRUE );
pD3DDev->SetRenderState( D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1 );
pD3DDev->SetRenderState( D3DRS_AMBIENT, 0 );
pD3DDev->SetRenderState( D3DRS_SPECULARENABLE, FALSE );

D3DLIGHT9 light;
ZeroMemory( &light, sizeof(D3DLIGHT9) );
light.Direction = D3DXVECTOR3(0,-1,0);
light.Ambient.r = light.Diffuse.r = 1.f;
light.Ambient.g = light.Diffuse.g = 1.f;
light.Ambient.b = light.Diffuse.b = 1.f;
light.Ambient.a = light.Diffuse.a = 1.f;
light.Type = D3DLIGHT_DIRECTIONAL;

pD3DDev->SetLight( 0, &light );
pD3DDev->LightEnable( 0, TRUE );

D3DMATERIAL9 mtrl;
ZeroMemory( &mtrl, sizeof(mtrl) );
mtrl.Diffuse.r = mtrl.Ambient.r = 1.f;
mtrl.Diffuse.g = mtrl.Ambient.g = 1.f;
mtrl.Diffuse.b = mtrl.Ambient.b = 1.f;
mtrl.Diffuse.a = mtrl.Ambient.a = 1.f;

pD3DDev->SetMaterial( &mtrl );

D3DXMATRIX temp;

//Removed: Matrix operations on temp

pMxStack->MultMatrixLocal( &temp );
pD3DDev->SetTransform( D3DTS_WORLD, pMxStack->GetTop() );
pD3DDev->SetFVF( D3DFVF_MYVERTEX );

//Removed: Map drawing function call
pD3DDev->SetRenderState( D3DRS_LIGHTING, FALSE );


So, can anyone help me with this problem? I've been working on it for the last three hours and it's driving me crazy.

##### Share on other sites
I've never used the D3D fixed function lighting (always calculated it myself), so this is just a guess, but it's not the

pD3DDev->SetRenderState( D3DRS_COLORVERTEX, FALSE );

is it? Shouldn't that be set to true?

##### Share on other sites
Quote:
 Original post by Anonymous PosterI've never used the D3D fixed function lighting (always calculated it myself), so this is just a guess, but it's not the pD3DDev->SetRenderState( D3DRS_COLORVERTEX, FALSE );is it? Shouldn't that be set to true?

Yes, and the code I copied was me testing what would happen if I turned it off. Either way it still breaks but I've corrected the code in the original post now.

##### Share on other sites
Try setting the ambient to something other than pure white. It could be that the lighting calculation is just saturating out because of this.

##### Share on other sites
Try this:

IDirect3DDevice9::SetRenderState( D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1 );

##### Share on other sites
Quote:
 Original post by Anonymous PosterTry setting the ambient to something other than pure white. It could be that the lighting calculation is just saturating out because of this.
Nope, already tried that. I tried not adding a light and the map is rendered as black. I add a light and don't add a material and the map renders as black. I tried setting all light values to .5 but it just made everything gray. I tried setting material values to .5 and light back at 1 and it was the same gray color. I tried both, it was darker gray, which means the material is being treated as the source color, even though I'm explicitly setting the renderstate to use the vertex color.I've tried doing it with alpha on and alpha off to see if that would do anything. I tried turning culling off as well as turning the zbuffer off. Nothing caused any change. I tried changing the light to a point light and what I ended up with was a big white circle surrounded by black. I've tried changing the direction vector to be pointing in all sorts of directions, but there's no change. Nothing's working, so I'm going to try using vertex declarations instead of FVF and see if that helps.

Edit: Using vertex declarations doesn't help either. I might just have to give up on my goal of having this work on Fixed-Function-Only video cards.

##### Share on other sites
Hi!

I don't know about DirectX, but in OpenGL, vertex colors don't work with lighting. It won't probably work on DirectX either, after all, they work on the same hardware with the same specs.

In OpenGL, however, there is glColorMaterial, an option to make current material properties track to the vertex colors. It first needs to be enabled and then the user has to define which material properties are tracked. For example:
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);

There seems to be some cryptic notes on the glColorMaterial manual page, which seem to say that glColorMaterial might not work properly with vertex arrays under certain conditions.

I suggest you stop using vertex colors and go with the material functionality of DirectX.

-Richardo

##### Share on other sites
RichardoX is quite right, if you specify pD3DDev->SetRenderState( D3DRS_LIGHTING, TRUE );, then D3D will attempt to calculate the lighting, even though you're specifying it. Set it to false before rendering.

##### Share on other sites
Quote:
 Original post by _goatRichardoX is quite right, if you specify pD3DDev->SetRenderState( D3DRS_LIGHTING, TRUE );, then D3D will attempt to calculate the lighting, even though you're specifying it. Set it to false before rendering.
So what you're saying is this part from the GameDev Forums DirectX FAQ is incorrect:
Quote:
 How is the color at a vertex eventually determined?The color at a vertex depends on three things: whether there is a D3DFVF_DIFFUSE element in the vertex structure, whether the D3DRS_COLORVERTEX state is set, and whether the D3DRS_LIGHTING state is set:D3DRS_LIGHTING| D3DRS_COLORVERTEX| | D3DFVF_DIFFUSE in vertex| | | Result---------------------------- 0 0 0 White0 1 0 White0 1 0 White0 1 1 Vertex Color1 0 0 Material Color * Light1 0 1 Material Color * Light1 1 0 Material Color * Light1 1 1 (Material Or Vertex Color) * LightThe last of those - (Material Color or Vertex Color) * Light – is determined by a set of four render states:- D3DRS_DIFFUSEMATERIALSOURCE- D3DRS_AMBIENTMATERIALSOURCE- D3DRS_SPECULARMATERIALSOURCE- D3DRS_EMISSIVEMATERIALSOURCEEach of them can be set to:(a) D3DMCS_MATERIAL, which uses the value from the material(b) D3DMCS_COLOR1, which uses the vertex's diffuse color value(c) D3DMCS_COLOR2, which uses the vertex's specular valueThere is no way to get material color effects without enabling lighting.

If what you're saying is correct, the last line where it's 1 1 1 should be corrected to say "Material Color * Light."