Why does the GLSL lighting code shift the light spot using a camera?

I am trying to create a custom light shader and trying to do many different things over time. Some of the solutions I found work better, others worse. For this question, I am using the solution that has worked best so far.

My problem is that if I move the β€œcamera” around, the light positions seem to be moving too. This solution has a very small but noticeable movement in it, and the position of the light seems to be higher where it should be.

The default OpenGL lighting (without any shaders) works fine (stable light positions), but I need a shader for multitexturing, and I plan to use parts of it for lighting effects after it works.

Vertex source:

varying vec3 vlp, vn; void main(void) { gl_Position = ftransform(); vn = normalize(gl_NormalMatrix * -gl_Normal); vlp = normalize(vec3(gl_LightSource[0].position.xyz) - vec3(gl_ModelViewMatrix * -gl_Vertex)); gl_TexCoord[0] = gl_MultiTexCoord0; } 

Source of fragment:

 uniform sampler2D baseTexture; uniform sampler2D teamTexture; uniform vec4 teamColor; varying vec3 vlp, vn; void main(void) { vec4 newColor = texture2D(teamTexture, vec2(gl_TexCoord[0])); newColor = newColor * teamColor; float teamBlend = newColor.a; // mixing the textures and colorizing them. this works, I tested it w/o lighting! vec4 outColor = mix(texture2D(baseTexture, vec2(gl_TexCoord[0])), newColor, teamBlend); // apply lighting outColor *= max(dot(vn, vlp), 0.0); outColor.a = texture2D(baseTexture, vec2(gl_TexCoord[0])).a; gl_FragColor = outColor; } 

What am I doing wrong?

+7
source share
1 answer

I cannot be sure that any of these problems is a problem, but they can cause it.

First, you need to normalize your per-vertex vn and vlp (BTW, try using more descriptive variable names. viewLightPosition much easier to understand than vlp ). I know that you normalize them in the vertex shader, but interpolating the fragment shader will denormalize them.

Secondly, it is not so bad as redundant. vec3(gl_LightSource[0].position.xyz) . "Position.xyz" is already vec3 since the swizzle mask (".xyz") has only 3 components. You do not need to enter it again in vec3.

+5
source

All Articles