# Directional Light - Always pointing up?

Hi,

I’m fairly new to GLSL, and have been following the set of .pdf tutorials by TyphoonLabs. I’ve had no problems until now; although it is a small one, the maths should work (in my mind) but computer says no…

I am on the directional light tutorial in chapter 3 (located here), and do not quite understand something in the vertex shader:

``````varying vec4 diffuse;

void main()
{
gl_Position    = ftransform();
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;

vec3 normal = gl_Normal;

// directional light direction = its position
vec3 lightVector = normalize(gl_LightSource[0].position.xyz);

// nxDir will be the multiplier in the range of [0,1]
// to act as the diffuse scalar.
float nxDir = max(0.0, dot(normal, lightVector));

diffuse = gl_LightSource[0].diffuse * nxDir;
}
``````

Whether the light’s z position is positive (0,0,1) or negative (0,0,-1), the teapot model is always lit from the bottom. As the light’s position is normalised to give the direction, I would have thought it would change?

Another thing is, if the vector is being normalised, surely the lighting shouldn’t change at all by just changing the z value (e.g. from -1.0 to -0.5) as it’s still pointing in the same direction, the unit vector should too? Yet there are changes.

I’m having to use ShaderMaker as the OpenGL Shader Designer would crash on startup, but surely that wouldn’t affect something as trivial as this!

Simon,

If no one has any suggestions as to how to fix the problem, please can people point me in the direction of a more reliable shader designing program?

What are other people using? Does it work on Windows 7 x64?

Any help appreciated. Thanks,
Simon.

Here’s one problem. gl_Normal is the vertex normal in OBJECT-SPACE. gl_LightSource[0].position is the light source position in EYE-SPACE. You then combine them with math as if they are in the same space already.

You need to transform your vertex normal to EYE-SPACE (the same space as gl_LightSource[0].position) first, so that the math combining them makes sense. You’d do this with:

``````
vec3 normal_eye = normalize( gl_NormalMatrix * gl_Normal );

``````

This will give you the vertex normal in EYE-SPACE. For more details, see:

It seems consistent. The teapot is more lit when the light is forward on the z axis and less when back down the z axis. If you are using shaderDesigner the light is placed in the same modelview matrix as the object, so when the object is moved, the light moves with it.

I’ve been fiddling about with some tools… Lumina is quite cool allowing scripting, but at the end I found it easier just to experiment in the game engine and read a lot. RenderMonkey is still out there. Problem is you end up spending so much time learning how to work the tools.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.