Hi, I am trying to create a basic engine in DX9 but I'm sort of stuck at the lighting part.
The issue is that the lights have no effect on my meshes. The objects will just render to the ambient color.
I think the easiest way would be to just show you the different parts of the code that has something to say on the lights.
After creating the DX device i enable some different states:
p_dx_Device->SetRenderState(D3DRS_LIGHTING,true);
p_dx_Device->SetRenderState(D3DRS_ZENABLE, true);
p_dx_Device->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB( 255, 255, 255 ) );
That includes setting the ambient color, and enabling lights.
My custom vertex format is defined like this:
struct OURCUSTOMVERTEX
{
float x,y,z;
D3DVECTOR normal;
float u, v;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)
I then create a light using this code:
D3DLIGHT9 light;
ZeroMemory(&light, sizeof(light));
light.Type = D3DLIGHT_DIRECTIONAL;
light.Diffuse.r = 1.0f;
light.Diffuse.g = 1.0f;
light.Diffuse.b = 1.0f;
light.Diffuse.a = 1.0f;
light.Direction.x = 1.0f;
light.Direction.y = 0.0f;
light.Direction.z = 1.0f;
light.Range = 1000;
manager->GetDevice( )->SetLight(0, &light);
manager->GetDevice( )->LightEnable(0, TRUE);
Then the mesh is loaded:
HRESULT hr = D3DXLoadMeshFromX( "cube.x" ,
D3DXMESH_SYSTEMMEM,
device,
NULL,
&matBuffer,
NULL,
&numMaterials,
&mesh);
The actual rendering is done like this:
p_Device -> Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,32,128), 1.0f, 0);
p_Device -> Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 32, 128), 1.0f, 0);
p_Device -> BeginScene();
for (unsigned int i=0; i<objects.size(); i++)
objects[i]->Render( p_Device );
p_Device -> EndScene();
p_Device -> Present(NULL, NULL, NULL, NULL);
And thats about it. I think one possible reason is that when the mesh is loaded it doesn't load the normal data.
Thoughts?
Hi