Sign in to follow this  

GLSL Light Coord Problem

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:


[code]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;


[code]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 this post

Link to post
Share on other sites
[quote name='V-man' timestamp='1305899394' post='4813460']
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 this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this