Directional Lighting

Started by
3 comments, last by SamuraiProgrammer 12 years, 8 months ago
I need to implement directional lighting for a very basic terrain engine. I have a skydome with a texture that includes clouds and the sun. I have some generated terrain with all calculated normals. The sun has a 3D position on the skydome of ( -86.6f, 72.9f, 491.2f). So I need my directional light to be from the direction of the sun to (0, 0, 0).
The documentation for the glLightfv command with pname GL_POSITION states that if the w component of the position is 0.0 then the light is treated as a directional source. But you are still only giving the position of a point in 3D space to OpenGL. To specify a direction you need to have two points. Or does OpenGL already have a default second point? If so is this (0.0, 0.0, 0.0)? Please explain just how the direction is calculated.
The commands I use at the moment are:

GLfloat LightPosition[] = {-86.6f, 72.9f, 491.2f, 0.0f};
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
Advertisement
If the light is directional, the vector specifies the direction vector.
But the coordinates given in the glLightfv command specify a point in space not a vector. Unless you mean its treated as a vector as soon as you add the 0.0 w ordinate. I could test this by dividing all the i, j and k components of the vector by 2 and should see the same directional lighting.
Thanks for the reply.
A 3-component vector can represent direction. If you supplied 2 vectors, all that would happen is they would be subtracted and you would be left with a 3-component direction vector. The 4th component informs OGL as to whether the light source is positional or directional. The lighting equations differ slightly but with the w component set to 0.0f (directional), the translation part of the modelview matrix will be negated, specifying the light source as an infinite distance away.
Yeah, I completely understand now. I was only seeing the 3 components that were being given to glLightfv as a point in space but didn't realise it can also be seen as a vector. When I said I would divide the vector components by 2, I wasnt going to supply the command with a 2 vectors but instead divide the vector by 2. e.g. The vector (2, 4, 6) has the the same direction as (1, 2, 3). And they should give the same lighting result if used for a directional vector. That's what I tested and it turned out the same in my program.

This topic is closed to new replies.

Advertisement