Transforming directional light to eye space in GLSL
I implement basic diffuse lighting in a shader that uses a directional light which I pass as a uniform. The problem is I can't figure out how to transform it into eye space so that the lighting doesn't follow the camera.
This is my vertex shader:
And these are the steps I take each frame:
This is my vertex shader:
Code:
uniform vec3 lightDir;
uniform float ambient;
uniform vec3 lightColour;
void main()
{
gl_Position = ftransform();
vec3 eyeNormal = normalize(gl_NormalMatrix * gl_Normal);
// Multiplying by gl_ModelViewMatrix seems ineffectual.
// I also tried multiplying by gl_NormalMatrix, which didn't work either.
vec3 realLightDir = normalize(vec3(gl_ModelViewMatrix * vec4(lightDir, 0)));
float intensity = max(ambient, dot(eyeNormal, realLightDir));
vec4 lightColourV4 = vec4(lightColour, 1.0);
gl_FrontColor = gl_Color * lightColourV4 * intensity;
}
And these are the steps I take each frame:
 Enable the shader
 Set the shader's uniform variables
 Set the projection matrix (glMatrixMode(GL_PROJECTION); glLoadIdentity(); Set my projection)
 I do this each frame since I switch between projections a lot, e.g. perspective for the scene, orthographic for the HUD
 Clear the modelview matrix (glMatrixMode(GL_MODELVIEW); glLoadIdentity();)
 Position the camera with gluLookAt
 Draw objects, rotating and translating as needed
At what point in the process do you call glLight? It should be called after the camera transform but before any object transforms.
OpenGL will automatically transform the light into eye space for you, so have you tried just not multiplying it by anything at all?
OpenGL will automatically transform the light into eye space for you, so have you tried just not multiplying it by anything at all?
I'm not using glLight at all. I'm using my own uniform vector.
I suppose I'll try glLight eventually if I can't figure out how to do the transformation on my own.
I suppose I'll try glLight eventually if I can't figure out how to do the transformation on my own.
I you supply your own uniform, and that's supposed to be in "world coordinates", you also need to supply a separate matrix with the camera transform's inverse, which you can then use to transform your global coords into eye space.
Or, alternatively, you can do what OpenGL does with glLight, namely transform before submitting to the GPU, thus the shader will see only stuff in eye space to begin with
Or, alternatively, you can do what OpenGL does with glLight, namely transform before submitting to the GPU, thus the shader will see only stuff in eye space to begin with
So, the light vector has to be multiplied by the inverse of the camera's transform matrix I take it then.
gluLookAt is supposed to be depreciated anyway, right? If it is, I guess I might as well come up with my own matrixusing camera class.
gluLookAt is supposed to be depreciated anyway, right? If it is, I guess I might as well come up with my own matrixusing camera class.
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
OpenGL Blending for 2D Light Effects  metacollin  10  14,029 
Jun 6, 2009 12:51 AM Last Post: NelsonMandella 

light attenuation  NelsonMandella  2  4,098 
Jun 4, 2009 03:28 PM Last Post: reubert 

Help converting normals from global space to tangent space  Muthaias  6  6,314 
Apr 1, 2008 06:28 AM Last Post: Muthaias 

2d light shadows  KiroNeem  2  3,490 
Sep 9, 2005 01:12 PM Last Post: KiroNeem 

Vissible Light (Volumetric?)  hangt5  6  4,549 
Mar 28, 2005 02:25 PM Last Post: OneSadCookie 