Deferred Shading

Description

Tech demo showcasing deferred shading in OpenGL. The demo uses Crytek’s Sponza scene and lights the scene with hundreds of randomly moving lights. The model is loaded using a custom format.

Render Target Setup

DeferredRenderTargets

  • RT Zero: Holds the pixel color.
  • RT One:  Holds the the normal and emissive val at each pixel. The emissive value is a multiplier onto the Diffuse color. 1.0f being max emissive (Added diffuse color) and 0.0f being no emissive addition.
  • RT Two: Holds the depth at the current fragment and is later used to rebuild the fragment’s world space position. Specular multiplier is placed in RT2.

    *All render targets can be switched to RGBA16 for memory concerns.

    *Depth can be place in RT0.a, allowing us to use RT2.rgb for a specular color. It’s currently in this setup to experiment with alpha blending.

Rendering Pipeline

DeferredPipeline

  • Populate GBuffers: Draw all geometry to populate render targets and the depth buffer.
  • Light Pass: Using the populated render targets, calculate the light accumulation buffers (diffuse and specular).
  • Final Pass: Using RT0, RT1, RT3, and RT4 blend the accumulated light with the fragment’s diffuse and emissive colors.
  • If Post Process or Reflectibe Pass: If any post processing is needed, use the final scene that was rendered to RT2 and render it to RT0. The other render targets are now open for ping-ponging for different post-processing effects.
Source

  • Renderer.h

    Browse code on Github!

  • Renderer.cpp

    Browse code on Github!

  • DeferredRenderer.h

    Browse code on Github!

  • DeferredRenderer.cpp

    Browse code on Github!

  • PointLight.h

    Browse code on Github!

  • PointLight.cpp

    Browse code on Github!

  • Deferred GBuffer Fragment Shader

    Browse code on Github!

  • Deferred Final Pass Fragment Shader

    Browse code on Github!

  • Deferred Point Light Fragment Shader

    Browse code on Github!