# Direction to point for lighting purposes

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

## Recommended Posts

Hi guys

I am working on a directional light system, where the light is collocated with the camera position. As such, the lighting returns will depend on the direction to the surface from the camera position. I want to get the eye direction to this point so I can perform the dot product calculation on it with the surface normal and determine how much light is reflected back.

Would I simply normalize the vertex position after multiplying it by the view matrix?

Thanks
JB

##### Share on other sites

That will give you a normalized direction vector pointing from the camera to the surface, in view space. You actually want a vector pointing from the surface -> light source, so you'd want to negate the vector. You would also need to make sure that the normal is converted to view space, otherwise you'll be working with vectors in two different coordinate spaces which wouldn't work. So you could do this:

float3 surfacePosVS = mul(float4(surfacePosWS, 1.0f), ViewMatrix);
float3 normalVS = mul(float4(normalWS, 0.0f), ViewMatrix);
float3 surfaceToLightVS = normalize(-surfacePosVS);
float lighting = saturate(dot(normalVS, surfaceToLightVS));


Or if you'd rather do everything in world space, you can do it like this:

float3 surfaceToLightWS = normalize(CameraPosWS - surfacePosWS);
float lighting = saturate(dot(normalWS, surfaceToLightWS));

For the second version, you just need to somehow pass your world space camera position through a constant buffer.

##### Share on other sites
Perfect. Thanks a bunch.

1. 1
2. 2
Rutin
19
3. 3
JoeJ
16
4. 4
5. 5

• 35
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631701
• Total Posts
3001811
×