# GLSL Light Coord Problem

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

## Recommended Posts

Hey, can i ask you for a little help finding mistake? The problem is then i'm passing my light's world coordinates to fragment shader - light is moving with camera, but wait... if i multiply it's position with gl_ModelViewMatrix or gl_ModelViewMatrixInverse, it doesn't help it just acting really weird(with gl_ModelViewMatrix it increases fragment color then i'm rotating camera down) , here are simplified examples:

Vertex:

varying vec4 V; varying vec4 N; void main() { gl_TexCoord[0] = gl_MultiTexCoord0; V = gl_ModelViewMatrix * gl_Vertex; N = vec4(normalize(gl_NormalMatrix * gl_Normal), 1.0); gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex; }

Fragment:

uniform sampler2D colorMap; uniform vec4 Ambient, Specular, lightPos, camPos; varying vec4 V; varying vec4 N; void main (void) { vec4 lightDir = lightPos - V; vec4 camDir = normalize(camPos - V); lightDir = normalize(lightDir); vec4 halfVec = normalize(camDir + lightDir); vec4 Final; float Lambert = dot(N, lightDir); float Nhalf = 0.0; if(Lambert > 0.0) { Nhalf = pow(max( dot( halfVec, N), 0.0 ), 64.0); } Final = Ambient + Nhalf * Specular; gl_FragColor = texture2D( colorMap, gl_TexCoord[0].st)*Lambert + Final; }

##### Share on other sites
In your C++ code, transform the light's position by the camera matrix so that it ends up in eye space and then send it to shader.

##### Share on other sites

In your C++ code, transform the light's position by the camera matrix so that it ends up in eye space and then send it to shader.

Thanks for helpful reply, i fixed that problem, but there's one more(i thought it will go away with first one). So lighting with this algorithm is not correct.
Diffuse and Specular seems ok on flat horizontal(or close to flat) surfaces, but on more complex geometry it's obviously wrong, like: specular and diffuse light on the side opposite to light, camera movement affects affects specular too much et cetera... What could be wrong? It look like shader is ok(i tried different examples and faced simillar issues), it looks the same even if i pass light position using glLightfv(GL_LIGHT0, GL_POSITION, LigthData);. So light now stays still, but acts weird. Any idea why?

##### Share on other sites
http://trueimages.ru...7d72e241752.png
Screenshot of how it looks on random bunch of geometry i use to test.

• 15
• 9
• 13
• 41
• 15
×

## Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!