GLSL - Directional Light

Started by
7 comments, last by cherryyosh 15 years, 3 months ago
Hi, I am working on a directional light, but cant seem to get it working right.. Currently The light moves with the camera ( I have both a spot and point light using hte same code that do not do this) and the light only works on side walls. leaving the floor black.. Heres the code
Quote: void DirectionalLight( in int i, in vec3 position, in vec3 normal, inout vec3 color ){ float nDotL, nDotHV; vec3 direction; direction = normalize(gl_LightSource.position.xyz); nDotL = dot( normal, direction ); if( nDotL > 0.0 ){ color *= ( gl_LightSource.diffuse.rgb * nDotL ); nDotHV = max(dot(normal, halfV), 0.0 ); color += 0.2 * gl_LightSource.specular.rgb * pow( nDotHV, 1.0 ); //0.4 = spec power, 1.0 = shineyness } }
Advertisement
Are the normals on the floor correct?

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

Negate the direction, direction = normalize(-gl_LightSource.position.xyz), what happens?
Sorry, I was gone for a bit..

The normals are correct, as is the position, negating the direction only makes the problem worse, full black screen.
nDotL = dot( normal, direction ); // make sure these both are in the same space
(usually world-space)
Did you forget to multiply "normal" with gl_NormalMatrix ?
Quote:Original post by idinev
nDotL = dot( normal, direction ); // make sure these both are in the same space
(usually world-space)
Did you forget to multiply "normal" with gl_NormalMatrix ?


As i've said before i have a point and a spot light function that both work fine. The normal and position are both correct.

EDIT:
Ok, I know both the position and normal are in eye space, the direction and half vectors I am not sure about.

The direction, for point and spot lights, is calculated by normalize( ...position.xyz - position );

and the half vector is by normalize(direction + position)

for the directional, right now I have halfV being
normalize( gl_LightSource[0].halfVector.xyz); in the vertex shader which is then interpolated in the fragment...

[Edited by - cherryyosh on December 27, 2008 12:56:55 PM]
what colour is the floor (not black? haha)?
that that negating the normal in the 'direction' calculation produces no colour at all... this means your normals point into the walls. However, when you calculate the specular, you treat the normals as if they point into the same halfspace as the halfvector; should that maybe not be?:
nDotHV = max(dot(-normal, halfV), 0.0 );
Make sure you apply the lighting AFTER you do the Camera transformations. Lighting is applied to Eye Coordinates. So if you do it before you make your transformations, it will move with them.
Quote:Original post by rewolfer
what colour is the floor (not black? haha)?
that that negating the normal in the 'direction' calculation produces no colour at all... this means your normals point into the walls. However, when you calculate the specular, you treat the normals as if they point into the same halfspace as the halfvector; should that maybe not be?:
nDotHV = max(dot(-normal, halfV), 0.0 );


Would the floor being black matter at all? After all the light should hit everything and thuse effect everything a little making it none black. As I've said before, the normals are correct, using the same code I have a working point and spot light that do as they should. Negating the normal in the nDotHV do nothing but make the whole screen black. (IE, no lighting is applyed )

This topic is closed to new replies.

Advertisement