# Directional light... position or direction?

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

## Recommended Posts

Hi all, I'm having some troubles trying to understand directional lights. I'm using the official directx (11) sdk's samples as reference, plus some online resources.

All of them describe a directional light with 2 vectors: direction and color. I got the whole thing working now (device setted, model loaded, shader done), but for me the direction attribute is a kind of position, not direction at all.

I mean, if I set the direction as (0,1,0) I think at a ray of light going up, toward the sky. Hence, for a typical sunlight, I'll use (0,-1,0).

However, this seems to be the exact reverse. So what is that vector, an actual direction, or a position?

And, if it's a position, what is the direction? is it supposed to be toward the origin (like it seems to)?

I'm using a kind of try, observe and understand, playing with values, but I think I still continue to miss something...

##### Share on other sites

For what I've seen in various examples and game engines, both are valid ways to go. As you say, position and direction are basically two sides of the same thing. It's mainly a matter of perspective.

So, there are lighting / shading algorithms that take a light's (world/view/screen space) position into account (as in coordinates), and there are others using the direction from the point of view (i.e. the camera) to the light's origin for their calculations. It's mainly the same vector, but in reverse order/direction.

Thus, if you are to implement an engine or application yourself, you can do it both ways.

Edited by Meltac

##### Share on other sites

Your topic title says "light direction" - you should treat the vector as a direction.

Your questions seem to revolve around the actual direction - i.e., the real-world light direction for an overhead sun is (0, -1, 0) (unit vector). During shader calcs, e.g., one normally dots the light direction with the object normal to determine the direction of the light relative to the surface. Using (0, -1, 0) as the light direction will result in a negative dot product for an "upward" facing surface. You can negate the dot product or use (0, 1, 0) as the light direction to get the proper signed lighting values.

by L. Spiro

Edited by Buckeye

##### Share on other sites

Generally you think of a position as an origin/source, directional lights dont consider source usually, in the sense that they often represent the moon or sun, something so far away that its light rays are parallel by the time they hit the thing being lit. Where as directional light is just a dir (ray/vec), point lights are just positions (origin/source) and spot lights are positions and directions (plus cut off angle).

A directional lights position will need to exist when that light casts a shadow, in the case of shadow maps, the shadow map would need a location to project to (as the scene needs a camera position to project to).

So for shading you have a location/position/point that needs to be shaded, and a normal that can be thought of as pointing out from this position. The directional lights direction, in the world sense, points AT this point.

Lighting would be done using the negative of this incoming light direction. This allows you to use the dot product to determine how much light this point is receiving, because two parallel vectors have a dot of 1.0, and 0.0, when at 90 degrees, and negative dot when pointing in opposite direction, which is why youll see dot products in lighting clamped/maxed to cut off anything below 0.0, which means behind, because we are only concerned with lighting hitting the front, usually.

Think of the negating of the incoming light dir as giving the light dir the same origin as the normal youre shading against, allowing the dot product result to make sense as a measure of the amount of incoming light from that direction.

##### Share on other sites

A directional lights position will need to exist when that light casts a shadow, in the case of shadow maps, the shadow map would need a location to project to (as the scene needs a camera position to project to).

Not true. Commonly, an orthographic projection is used and the view is set "looking" in the light direction at a position not determined by the light's "position" (directional lights don't have a position) but a position convenient to bring objects of interest into the frustum.

##### Share on other sites

It is a direction, not a position.  It is standard practice to pass to the shader the inverse light direction because that is what makes the math against the normal work.  If you look at any lighting-example diagram you will see all vectors (the incoming light vector, half-vector, normal, and outgoing-light vector) going away from the surface.  The only way the math works and the diagrams make sense is if the “incoming” light vector is reversed.

L. Spiro

I found this thing of passing the inverse in some examples only, so I wasn't sure it was right, but it makes sense with all my doubts. I will try as soon as possibile.

Edited by arka80

##### Share on other sites

A directional lights position will need to exist when that light casts a shadow, in the case of shadow maps, the shadow map would need a location to project to (as the scene needs a camera position to project to).

Not true. Commonly, an orthographic projection is used and the view is set "looking" in the light direction at a position not determined by the light's "position" (directional lights don't have a position) but a position convenient to bring objects of interest into the frustum.

Thats sorta what I mean, the position/positioning associated with a directional light is more related to offsetting from a specific point being shadowed (if shadowed at all). The orthographic projections view volume is still positioned.

##### Share on other sites

Not true. Commonly, an orthographic projection is used and the view is set "looking" in the light direction at a position not determined by the light's "position" (directional lights don't have a position) but a position convenient to bring objects of interest into the frustum.

Thats sorta what I mean, the position/positioning associated with a directional light is more related to offsetting from a specific point being shadowed (if shadowed at all). The orthographic projections view volume is still positioned.

Even when generating shadow maps you never “generate” a position for a directional light. The orthographic projection only needs 6 planes (left, right, top, bottom, near, and far), and all of these are determined without information on the “position” of a directional light.

The volume is not “positioned”. The planes are generated based off object bounding spheres within the camera’s view frustums and a series of min/max operations in the 3 orthogonal directions. The orthogonal directions are determined by the direction of the light.

At no point does a directional light have a position, not even for shadows.

L. Spiro

##### Share on other sites

It is a direction, not a position.

At no point does a directional light have a position, not even for shadows.

I was just about to say that this was not true when I realized we're talking directional lights, not spot lights.

I have seen (and used) a couple of shader/lighting/shadowing algorithms taking a light's world space *position* into account, sometimes converting it into view or screen space for the lighting calculations. So there *are* cases where it's completely valid to use a light's position, not direction.

However as you say correctly a directional light is defined by - as the name already says - the direction of light, where the light's position is considered being somewhere in very far to infinite distance. That's the way sunlight physically behaves from the earth's perspective - although the sun itself has a defined position somewhere in universe, the light rays coming to earth are nearly parallel so it is much more precisely to treat sunlight as directional than as spot light.

Edited by Meltac

1. 1
Rutin
69
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633431
• Total Posts
3011834
• ### Who's Online (See full list)

There are no registered users currently online

×