Sign in to follow this  
j_smith4

Lighting question

Recommended Posts

I've exported a simple sphere that has normals and a texture as a .x file and load it with D3DXLoadMeshFromX(). And have setup a light as follows:
        m_D3DDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
	// Fill in a light structure defining our light
	D3DLIGHT9 light;
	ZeroMemory( &light, sizeof(D3DLIGHT9) );
	light.Type    = D3DLIGHT_POINT;
	light.Diffuse.r = 0.0f;
	light.Diffuse.g = 0.0f;
	light.Diffuse.b = 1.0f;

	light.Ambient.r  = 1.0f;
	light.Ambient.g  = 1.0f;
	light.Ambient.b  = 1.0f;
	light.Specular.r = 1.0f;
	light.Specular.g = 1.0f;
	light.Specular.b = 1.0f;

	// Point lights have no direction but do have a position
	light.Position = D3DXVECTOR3(0, 100, 0);
  
	// Tell the device about the light and turn it on
	light.Attenuation0 = 1.0f;
	light.Range=2000.0f;
	m_D3DDevice->SetLight( 0, &light );
	m_D3DDevice->LightEnable( 0, TRUE ); 

Here is the mesh rendering code:
for (DWORD i=0; i<numMaterials; i++)
	{
		// Set the material and texture for this subset
		m_D3DDevice->SetMaterial(&meshMaterials[i]);
		m_D3DDevice->SetTexture(0, meshTextures[i]);

		m_D3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
		    
		// Draw the mesh subset
		m_object->DrawSubset(i);
	}

I get no lighting at all with this. Even m_D3DDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(0, 0, 0)) doesn't seem to have any effect either.

Share this post


Link to post
Share on other sites
Have you tried a directional light. A directional light will immediately let you know if there are any irregularities.

I hope this helps.
Take care.

Share this post


Link to post
Share on other sites
I can't tell by just looking at it, but if you want some more documentation on lighting in general, you could always take a look at this and this. It may help you find, where you may be going wrong. The second link, would be more what I believe you're trying to accomplish.

Best of luck.

Share this post


Link to post
Share on other sites
What are the values of the material you're setting? It acts as a multiplier of the light's properties, so if it were all zero'd out you'd get completely black.

Also, any transformations including scaling? Scaling affects normals so you might want to try D3DRS_NORMALIZENORMALS = TRUE.

I second Pieter's suggestion about trying a directional light - they have no attenuation and are easier to debug with. At least that way you can eliminate your geometry and material properties...

hth
Jack

Share this post


Link to post
Share on other sites
Setting:
m_D3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
means that you are just using the texture without "modulating" the light.

Try setting:
m_D3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
m_D3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_CURRENT);
m_D3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_TEXTURE);


Share this post


Link to post
Share on other sites
Seems the problem is I was trying to use a blue light. With blue directional light you can very faintly see the models. If I use a white light then it seems to work as it should. But the lighting in general doesn't seem to work as it should. Tried to cast a yellow light but nothing really changed. I even hard coded the ambient and diffuse for the textures to one.

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