Sign in to follow this  

How to the light a mesh properly

Recommended Posts

I created a simple mesh using the D3DXLoadMeshFromXInMemory() function. The mesh .x file contains normals data. I set up the lighting as follows :
	memset(&m_Light, 0, sizeof(D3DLIGHT9));
	m_Light.Type = D3DLIGHT_POINT;
	m_Light.Diffuse.a = 0.0f;
	m_Light.Diffuse.r = 1.0f;
	m_Light.Diffuse.g = 1.0f;
	m_Light.Diffuse.b = 1.0f;
	m_Light.Ambient = m_Light.Diffuse;
	m_Light.Specular.r = 1.0f;
	m_Light.Specular.g = 1.0f;
	m_Light.Specular.b = 1.0f;

	m_Light.Position.x = 0.0f;
	m_Light.Position.y = 0.0f;
	m_Light.Position.z = -50.0f;
	m_Light.Range = 1000.0f;
	m_Light.Attenuation1 = 1.0f;

	hr = D3D9DEVICE()->SetLight(0, &m_Light);
	hr = D3D9DEVICE()->LightEnable( 0, true);
	hr = D3D9DEVICE()->SetRenderState(D3DRS_LIGHTING,  true);
	hr = D3D9DEVICE()->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(128, 128, 128));
	hr = D3D9DEVICE()->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
	hr = D3D9DEVICE()->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_DIFFUSE);
	hr = D3D9DEVICE()->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT);

The mesh has no texture, only a single material has been applied to it. The object is rendered with the right color but it seems to be affected only by the ambient light setup. When I rotate the object (placed at the origin) the color doesn't change as it should have. I loaded the same file with DX mesh viewer and there the light had the desired effects. What am I doing wrong here ? thx. P.S when I change m_Light.Attenuation1 = 1.0f; to m_Light.Attenuation0 = 1.0f; the mesh is rendered with the white color, why is that ???

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