Jump to content
• Advertisement

# Direction to point for lighting purposes

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

If you intended to correct an error in the post then please contact us.

## 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 this post

##### Share on other sites
Advertisement

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 this post

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

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
20
3. 3
khawk
18
4. 4
A4L
14
5. 5
• Advertisement

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633760
• Total Posts
3013721
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!