• Advertisement
Sign in to follow this  

Direct3d C++ - Lighting

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well..

The problem is that only one light is 'active' even if two is created (the last one defined is always active). I've searched but havn't found anything regarding this. :unsure:


Class CLight

void CLight::CreatePointLight(LPDIRECT3DDEVICE9 m_d3dDevice, float range, D3DXVECTOR3 pos);
void CLight::CreateDirectionalLight(LPDIRECT3DDEVICE9 m_d3dDevice);
void CLight::CreateSpotLight(LPDIRECT3DDEVICE9 m_d3dDevice);

void CLight::CreatePointLight(LPDIRECT3DDEVICE9 m_d3dDevice, float range, D3DXVECTOR3 pos)
{
D3DLIGHT9 light;
ZeroMemory( &light, sizeof(D3DLIGHT9) );

light.Type=D3DLIGHT_POINT;

light.Diffuse.r = 10.3f;
light.Diffuse.g = 10.3f;
light.Diffuse.b = 10.3f;

light.Range = range;

light.Position = pos;

light.Attenuation0 = 0.0f;
light.Attenuation1 = 0.018f;
light.Attenuation2 = 0.0f;

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

void CLight::CreateDirectionalLight(LPDIRECT3DDEVICE9 m_d3dDevice)
{
ZeroMemory( &light, sizeof(D3DLIGHT9) );
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.Range = 1000.0f;

D3DXVECTOR3 vecDir;
vecDir = D3DXVECTOR3(0.0f,-0.3f,0.5);
D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir );

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



CLight clight;
CLight clight2;

m_d3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
m_d3dDevice->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_XRGB(50,50,50));

clight.CreatePointLight(m_d3dDevice,1000.0f,D3DXVECTOR3(0.0f,10.0f,0.0f));
clight2.CreateDirectionalLight(m_d3dDevice); // Is the only light showing up, if I remove this line; the pointlight works

Share this post


Link to post
Share on other sites
Advertisement

Well..

The problem is that only one light is 'active' even if two is created (the last one defined is always active). I've searched but havn't found anything regarding this. :unsure:


Class CLight

void CLight::CreatePointLight(LPDIRECT3DDEVICE9 m_d3dDevice, float range, D3DXVECTOR3 pos);
void CLight::CreateDirectionalLight(LPDIRECT3DDEVICE9 m_d3dDevice);
void CLight::CreateSpotLight(LPDIRECT3DDEVICE9 m_d3dDevice);

void CLight::CreatePointLight(LPDIRECT3DDEVICE9 m_d3dDevice, float range, D3DXVECTOR3 pos)
{
D3DLIGHT9 light;
ZeroMemory( &light, sizeof(D3DLIGHT9) );

light.Type=D3DLIGHT_POINT;

light.Diffuse.r = 10.3f;
light.Diffuse.g = 10.3f;
light.Diffuse.b = 10.3f;

light.Range = range;

light.Position = pos;

light.Attenuation0 = 0.0f;
light.Attenuation1 = 0.018f;
light.Attenuation2 = 0.0f;

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

void CLight::CreateDirectionalLight(LPDIRECT3DDEVICE9 m_d3dDevice)
{
ZeroMemory( &light, sizeof(D3DLIGHT9) );
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.Range = 1000.0f;

D3DXVECTOR3 vecDir;
vecDir = D3DXVECTOR3(0.0f,-0.3f,0.5);
D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir );

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



CLight clight;
CLight clight2;

m_d3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
m_d3dDevice->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_XRGB(50,50,50));

clight.CreatePointLight(m_d3dDevice,1000.0f,D3DXVECTOR3(0.0f,10.0f,0.0f));
clight2.CreateDirectionalLight(m_d3dDevice); // Is the only light showing up, if I remove this line; the pointlight works




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



Thats because the first parameter in those two calls determine which Fixed Function pipeline light is updated with the parameters you specified try increasing the 0 to a 1 for the second light you create and it should work. You should have your light creation function take the light number as a parameter to make this easier, also note that the Fixed Function Pipeline only supports 8 lights, if you want more you will have to use shaders.

PS: 8 lights that use forward shading shaders are pretty expensive already if you need more than a few lights for a complex scene and all the objects should interact with those lights, you should start thinking about writing a deffered renderer.


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement