PDA

View Full Version : Directional Light - Always pointing up?



shwhjw
07-20-2011, 08:29 AM
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 (http://www.opengl.org/sdk/docs/tutorials/TyphoonLabs/Chapter_3.pdf)), 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?

http://img833.imageshack.us/img833/3729/teapot.png

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!

Any ideas? Thanks in advance,
Simon,

shwhjw
07-21-2011, 02:20 AM
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.

Dark Photon
07-22-2011, 02:05 PM
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));


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:

* GLSL Tutorial: OpenGL Directional Lights I (Lighthouse) (http://www.lighthouse3d.com/opengl/glsl/index.php/index.php?ogldir1)

ste3e
08-05-2011, 10:54 PM
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.