I know that there are several threads on the network for the same problem, but I have no help from them, because my implementation is different.
I represent the colors, normals and depth in the space of the eye in the texture. In the second, I associate the textures with a full-screen square and calculate the lighting. Directional light works fine, but spotlights move with the camera.
I share the corresponding shader code:
Vertex shader step
in vec2 inVertex; in vec2 inTexCoord; out vec2 texCoord; void main() { gl_Position = vec4(inVertex, 0, 1.0); texCoord = inTexCoord; }
Lighting Step Fragment Shader
float depth = texture2D(depthBuffer, texCoord).r; vec3 normal = texture2D(normalBuffer, texCoord).rgb; vec3 color = texture2D(colorBuffer, texCoord).rgb; vec3 position; position.z = -nearPlane / (farPlane - (depth * (farPlane - nearPlane))) * farPlane; position.x = ((gl_FragCoord.x / width) * 2.0) - 1.0; position.y = (((gl_FragCoord.y / height) * 2.0) - 1.0) * (height / width); position.x *= -position.z; position.y *= -position.z; normal = normalize(normal); vec3 lightVector = lightPosition.xyz - position; float dist = length(lightVector); lightVector = normalize(lightVector); float nDotL = max(dot(normal, lightVector), 0.0); vec3 halfVector = normalize(lightVector - position); float nDotHV = max(dot(normal, halfVector), 0.0); vec3 lightColor = lightAmbient; vec3 diffuse = lightDiffuse * nDotL; vec3 specular = lightSpecular * pow(nDotHV, 1.0) * nDotL; lightColor += diffuse + specular; float attenuation = clamp(1.0 / (lightAttenuation.x + lightAttenuation.y * dist + lightAttenuation.z * dist * dist), 0.0, 1.0); gl_FragColor = vec4(vec3(color * lightColor * attenuation), 1.0);
I send light attributes to shaders in the form:
shader->set("lightPosition", (viewMatrix * modelMatrix).inverse().transpose() * vec4(0, 10, 0, 1.0));
viewmatrix is ββthe camera matrix, and modelmatrix is ββjust an identity.
Why are spotlights not translated with cameras with models?
Any suggestions are welcome!