Jump to content
  • Advertisement
Sign in to follow this  
SingularOne

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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

Sign me up!