Sign in to follow this  
sinyester

Direct3d C++ - Lighting

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:

[code]
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 );
}
[/code]

[code]
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
[/code]

Share this post


Link to post
Share on other sites
[quote name='sinyester' timestamp='1307121715' post='4819142']
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:

[code]
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 );
}
[/code]

[code]
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
[/code]
[/quote]

[code]
m_d3dDevice->SetLight( 0, &light );
m_d3dDevice->LightEnable( 0, TRUE );
}
[/code]


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

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