Closed ytgui closed 4 years ago
a screen-space shading technique.
no shading is actually performed in the first pass of the vertex and pixel shaders: instead shading is "deferred" until a second pass.
reduce fragment counts
Forward
for object in world:
for vert in object:
frag = rasterize(vert, texture, normal, light, shadow)
framebuffer = blend(frame, frag)
Deferred
for object in world:
for vert in object:
GBuffer += vert, depth, ...
GBuffer = backface_culling(GBuffer)
for vert in GBuffer:
frag = rasterize(vert, texture, normal, light, shadow)
framebuffer = blend(frame, frag)
Diffuse Color G-Buffer
Z-Buffer
Surface Normal G-Buffer
Final Compositing.
(To calculate the shadows shown in this image, other techniques such as shadow mapping, shadow feelers or a shadow volume must be used together with deferred shading.)
// VS
out vec4 gl_Position;
out vec3 FragPos;
out vec2 TexCoords;
out vec3 Normal;
// FS
layout (location = 0) out vec3 gPosition;
layout (location = 1) out vec3 gNormal;
layout (location = 2) out vec4 gAlbedoSpecular;
// VS
out vec4 gl_Position;
out vec2 TexCoords;
// FS
void main() {
// retrieve data from gbuffer
vec3 FragPos = texture(gPosition, TexCoords).rgb;
vec3 Normal = texture(gNormal, TexCoords).rgb;
vec3 Diffuse = texture(gAlbedoSpecular, TexCoords).rgb;
float Specular = texture(gAlbedoSpecular, TexCoords).a;
}
https://learnopengl-cn.readthedocs.io/zh/latest/05%20Advanced%20Lighting/08%20Deferred%20Shading/