Skip to main content
gamedev.net
🔒 Locked

Beginner with lights

Started by White Scorpion Jan 15, 2005 at 8:50 AM 0 replies 400+ views
Original Post
White Scorpion
White Scorpion
Hi everyone ! I started reading on lights using DX SDK tutorials but there are things I don't quite understand. Consider the following code:
void SetupLight()
{
	D3DMATERIAL9 mtrl;
	ZeroMemory(&mtrl, sizeof(D3DMATERIAL9));
	mtrl.Diffuse.a = 1.0f;
	mtrl.Diffuse.b = 0.0f;
	mtrl.Diffuse.r = 1.0f;
	mtrl.Diffuse.g = 0.0f;
	D3DDev->SetMaterial(&mtrl);

	D3DLIGHT9	light;
	ZeroMemory(&light, sizeof(D3DLIGHT9));
	light.Type = D3DLIGHT_DIRECTIONAL;
	light.Diffuse.b = 1.0f;
	light.Diffuse.r = 1.0f;
	light.Diffuse.g = 1.0f;
	
	D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, 
						&D3DXVECTOR3(cosf(timeGetTime() / 350.0f), 0.0f, sinf(timeGetTime() / 350.0f)));
	light.Range = 1000.0f;
	D3DDev->SetLight(0, &light);
	D3DDev->LightEnable(0, true);
	D3DDev->SetRenderState(D3DRS_LIGHTING, true);
	D3DDev->SetRenderState(D3DRS_AMBIENT, 0x00202020);
}
Why does everything turn black if I change ANY variable of light.Diffuse to something else than 1.0f ? Could someone explain the whole D3DXVec3Normalize() line ? I don't understand why the author used timeGetTime() to set the light's direction... and I don't know what normalizing is (I'm taking physic classes but it's a bit ahead of me for now...) In this example, after 1000.0f (Z axis) the light will be completely off right ? How would it be possible to have many different lights at the same time ? Is it hard ? What does the last line do ? Finally, what's the difference between those 2 lines ?
	D3DDev->LightEnable(0, true);
	D3DDev->SetRenderState(D3DRS_LIGHTING, true);
Thank you a lot guys/girls.
Specchum
Specchum
Quote:
Original post by White Scorpion
Hi everyone ! I started reading on lights using DX SDK tutorials but there are things I don't quite understand. Consider the following code:*** Source Snippet Removed ***Why does everything turn black if I change ANY variable of light.Diffuse to something else than 1.0f ?



Your material is setup to reflect only the diffuse red component (mtrl.Diffuse.r) of any light. Which means, unless your light has a corresponding red diffuse component (the blue and green won't light up the material since it's not reflecting any of those components) the material will appear completely black.

Quote:

Could someone explain the whole D3DXVec3Normalize() line ? I don't understand why the author used timeGetTime() to set the light's direction... and I don't know what normalizing is (I'm taking physic classes but it's a bit ahead of me for now...)


The Vec3Normalize is used to make the vector a unit vector (normalisation). A good primer on Vectors will help here.
As for the timeGetTime(), it is being used to rotate the light in a circular motion (unless I'm mistaken about the equation).

Quote:

In this example, after 1000.0f (Z axis) the light will be completely off right ?


Yes.

Quote:

How would it be possible to have many different lights at the same time ? Is it hard ?


Not at all. You can have many lights lighting up the scene.

Quote:

Finally, what's the difference between those 2 lines ? D3DDev->LightEnable(0, true);
D3DDev->SetRenderState(D3DRS_LIGHTING, true)
<!--QUOTE-->


This ties in with your earlier question. The first parameter in the LightEnable function is the element of light you want to enable in the light list. Say, you set up three lights in your program (like u did in ur example). Then you can do:

D3DDev->SetLight (0, &light1); //assign light1 to zeroth index in the light list
D3DDev->SetLight (1, &light2); //assign light2 to first index in the light list
D3DDev->SetLight (2, &light3); //assign light3 to second index in the light list

Then to selectively enable any or all of these lights you simply use the LightEnable function:
D3Dev->LightEnable(0, true); //enable the zeroth index light
D3Dev->LightEnable(1, false); //turn off the first index light
...and so on

The SetRenderState function enables/disables global lighting. If you set it to false, all lights are turned off and if you set it to on then all lights that you have enabled using LightEnable will be turned on.

I don't want to sound offensive, but I think you would be better off picking up a good directX book. The SDK simply doesn't cover all the ground for a beginner. A good book that I highly recommend is "Introduction to 3d Game Programming using DirectX 9".
"There is no dark side of the moon." - Pink Floyd

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.