Delayed rendering and highlighting of moving points

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!

+4
source share
1 answer

In addition to Nobody commenting that all the vectors you are calculating must be normalized, you have to make sure that they are all in the same space. If you use the position of the representation space as a viewing vector, the normal vector must also be in the viewing space (must be transformed using the model's transpose matrix before being written to the G-buffer in the first pass). And the vector of light should also be in the space of sight. Therefore, you need to transform the position of the light according to the view matrix (or the model viewing matrix, if the position of the light is not in world space) instead of its transposition.

 shader->set("lightPosition", viewMatrix * modelMatrix * vec4(0, 10, 0, 1.0)); 

EDIT: For directional light, reverse transposition is actually a good idea if you specify the direction of light as the direction of light (e.g. vec4(0, 1, 0, 0) for light pointing in the -z direction).

+3
source

Source: https://habr.com/ru/post/1369387/


All Articles