Jump to content
  • Advertisement
Sign in to follow this  
BloodLust666

GLSL light vector

This topic is 3682 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

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
Advertisement
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
If you put all the transformation in the standard modelview matrices, yes you can use gl_ModelViewMatrix

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!