I implemented deferred rendering in my little engine using framebuffers but there is no dynamic lighting for now. The g-buffer contains positions and normals in view space and albedo.

To implement real time lighting, I guess that I need to perform one pass for each light. Given the position and shape of a light, how can find all pixels affected by the light? I want to support point lights, spot lights and directional lights.

  • \$\begingroup\$ Since point lights are just sphere, you render a sphere of a size of a spotlight on the position of light. And what you get in pixel shader for this are pixels affected by this light. Spot lights are similar, and directional lights are rendered as fullscreen quad (since everything is affected by them) \$\endgroup\$
    – Kikaimaru
    Commented Mar 27, 2013 at 18:45

1 Answer 1


In deferred shading, one usually just draws some geometry that bounds the light's area of effect in world space, such as a polygonal sphere or cone that encloses the point or spot light's mathematical sphere or cone.

The simplest thing to do is draw the back faces of this geometry (front face culling) with the depth test reversed (greater-than instead of less-equal). Then any pixels that are between the camera and the back faces of the light geometry will get shaded.

This also shades pixels between the camera and the front faces of the light geometry, which are outside the light and therefore don't need to be shaded. There are various optimizations that can be done to remove these pixels, such as a depth bounds test or stencil masking.

For a directional light, you do a full-screen pass.

  • \$\begingroup\$ What comes to my mind for excluding pixels in front of the light volume is to draw the sphere with back face culling again and use the opposite depth test. But how can I prevent shading pixels inside the light geometry which are occluded by walls, for example? \$\endgroup\$
    – danijar
    Commented Mar 27, 2013 at 20:50
  • \$\begingroup\$ Well, once you've already shaded the pixels in front of the light volume you can't un-shade them, so drawing the light a second time doesn't help. As for pixels inside the light geometry that are occluded by walls - now you're talking about shadow casting, which is a whole other can of worms. It can be done using shadow maps for spot lights, and omnidirectional shadow maps for point lights. But that's really a completely separate issue. \$\endgroup\$ Commented Mar 27, 2013 at 21:11
  • \$\begingroup\$ Thanks for your explanations, though I realized that deferred lighting is a hard fake. \$\endgroup\$
    – danijar
    Commented Mar 27, 2013 at 21:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .