# proper GLslang lighting

This topic is 5405 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello... I render a heightmap in GLslang vertex and pixel shaders. I've also been playing with ARB_vertex_program and NV_vertex_program for long, but I never managed to implement proper lighting. I calculate the (diffuse) light Intensity with:
vec4 clipCoord = gl_ModelViewProjectionMatrix * gl_Vertex;

vec3 N = normalize( gl_NormalMatrix * gl_Normal );
vec3 L = normalize( gl_LightSource[0].position - clipCoord.xyz );
float intensity = dot( N, L );
and set the light position directly before rendering. However the lighting results are dependant to the viewer. Things that will be on the top of the screen will be dark, like things on the left, while things right and bottom are lit. Could anyone tell me what I'm doing wrong? this is how it looks and here the camera is rotated about the x-axis, but still on the same position edit: shrunk your images to a more reasonable size. - BB

##### Share on other sites
The light position is in eye coordinates, and so must be the vertex position. So, you must muliply gl_Vertex with the modelview matrix for lighting calculations,not the MVP matrix

Also, remember that GL automatically multiplies the light position with the modelview matrix when you call glLightf(). You must set the light positions after you have setup the camera transformations.

##### Share on other sites
Since I am having basically the same problem I'll ask for details in this thread instead of the original one.

1) The modelview is the concatenate of the {view|camera|eye} matrix and of the {model|object|mesh} matrix?

2) The light is transformed by the current modelview matrix when you call glLight -> You have to call glLight for all your lights for each object you render?

3) Since you hopefully want your light to be independent of your model you cannot call glLight with it's {world|absolute} position but you have to account for the model matrix and supply (light_position * inverse_model_matrix) to glLight? Otherwise how can the model orientation affect the lighting? (That is assuming 1) is true.)

I'm avoiding the issue right now by passing my own light position to my shaders but I don't get it so, sorry for asking again (;.

##### Share on other sites
Hi, do not use gl_LightSource, use a simple uniform instead. That's my advice..

uniform vec3 LightPosition;varing vec3 N;varing vec3 L;void main(){    ...    L = LightPosition - gl_Vertex.xyz;    N = gl_Normal;}

varing vec3 L;varing vec3 N;void main(){    ...    float Diffuse = dot(normalize(N), normalize(L)); // opt: dot(N,L) * inverse_sqrt(dot(N,N) * dot(L,L))    gl_FragColor = ...}

I transform the light position into object space on the CPU if necessary (dynamic objects).

##### Share on other sites
lighting with glsl aint really much different to normal lighting

grab the glsl demo from here
http://www.delphi3d.net/
and see how tom's doing it

• 13
• 18
• 29
• 11
• 20