Sign in to follow this  
BloodLust666

GLSL light vector

Recommended Posts

What should I multiply a light vector by so it is in world position? Whenever I have a light position that I pass via a uniform variable, everything works fine, but when I move the camera, it looks as if the position I passed to the shader is in relation to the camera, and not the world. Such as when I use vec3(0.0, 1.0, 0.0) the light only appears to be coming from above when the camera is pointing down the Z axis, but when it rotates about the X axis, the light vector changes in relation to the camera.

Share this post


Link to post
Share on other sites
A world position is brought to eye (camera) space by multiplying it with the view matrix. To transform a vector from eye space to world space, just apply the inverse view matrix.

I guess you provide the light's position and orientation in world space but perform lighting in eye space so you need to transform the light's position/orientation by the view matrix. This can be done in the shader but would be more optimal if you did this prior to setting the uniform, since then the transform would only be done once per frame (or even less if you cache the result and neither the light nor the camera move).

Share this post


Link to post
Share on other sites
You should multiply by the inverse-transpose, not inverse.
Also, if you view matrix doesn't have any scale in it, just multiply by the view matrix and that's all.

uniform mat4 ViewMatrix;
uniform vec4 LightVec;//make sure w = 0.0

vec4 lightVecInViewSpace = ViewMatrix * LightVec;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this