Lighting the Forge:
PBR, Image-Based Lighting, and Shadows
In DevBlog #7, we split RoboEngine into a clean runtime library and editor app, giving ReSceneRenderer two distinct ways to draw the world: Forward and Deferred. Now comes the fun part: making what those pipelines actually draw look grounded, tactile, and alive.
Today, in Part 2 of our breakdown, we’re opening up the visual engine room: metallic-roughness Physically Based Rendering (PBR), HDR Image-Based Lighting (IBL), and shadow maps for every light type in the scene.
Everything lives inside modular GLSL headers under Engine/shaders/include/, shared verbatim by both pipelines, alongside two new C++ heavyweights: ReIblBaker and ShadowRenderSystem.
Physically Based Materials
(Without Extra Vertex Bloat)
One Shading Function to Rule Both Pipelines
The centerpiece of our shader library is surface_lighting.glsl. Forward rendering invokes it per mesh fragment; deferred rendering invokes it per G-buffer pixel, guaranteeing that both pipelines produce the exact same picture:
vec3 shadeSurface(SurfaceMaterial material, vec3 positionWS, vec3 N, vec3 geometricN)
{
vec3 V = normalize(ubo.inverseViewMatrix[3].xyz - positionWS);
vec3 radiance = evaluateEnvironmentLight(material, N, V);
for (int i = 0; i < ubo.numLights; i++)
{
vec3 L;
vec3 lightRadiance = incomingRadiance(ubo.lights[i], positionWS, L);
if (dot(lightRadiance, lightRadiance) <= 0.0) { continue; } // Skip if outside spot cone or range
lightRadiance *= shadowFactor(ubo.lights[i], positionWS, geometricN);
radiance += evaluateBSDF(material, N, V, L, lightRadiance);
}
return radiance;
}
The BSDF Breakdown
Inside bsdf.glsl, we implemented the industry-standard metallic-roughness PBR model used by glTF 2.0 and modern real-time engines:
| Term | Mathematical Model |
| Diffuse | Lambert (baseColor / π), weighted by (1 − F) and (1 − metallic) |
| Distribution (D) | GGX / Trowbridge-Reitz |
| Visibility (G) | Smith height-correlated GGX |
| Fresnel (F) | Schlick approximation (F0 = 0.04 for dielectrics, baseColor for metals) |
Artist-facing “perceptual” roughness gets squared into GGX alpha and clamped at 0.045 so mirror-smooth specular highlights never shrink below a single pixel and shimmer with aliasing.
Tangent-Free Normal Mapping
Every material binds three texture maps: Base Color, Normal, and Metallic-Roughness (packing roughness in green and metallic in blue, matching glTF). Empty texture slots bind 1×1 fallback textures (white, flat normal, white), allowing the shader to sample unconditionally while untextured materials cleanly evaluate their scalar factors.
Instead of forcing every 3D mesh loader to compute and store per-vertex tangent vectors, material.glsl reconstructs a cotangent frame on the fly per pixel using screen-space derivatives of world position and UVs (Christian Schüler’s classic 2013 technique, check out Real-Time Rendering for more), automatically falling back to the smooth vertex normal if UVs don’t vary.
Image-Based Lighting:
Bottling the Sky
Nothing makes PBR metals gleam quite like reflecting a real HDR environment panorama. Using Brian Karis’s classic split-sum approximation (SIGGRAPH 2013), ReIblBaker takes a single equirectangular HDR image and bakes four GPU textures via compute shaders in one submit:
| Baked GPU Image | Dimensions | Purpose |
| Environment Cube | Up to 1024² per face (with mips) | Renders the skybox and feeds the baker |
| Irradiance Cube | 32² per face | Captures soft, omnidirectional diffuse light |
| Prefiltered Cube | 256², 6 mips (roughness 0.0 → 1.0) | Blurry-to-sharp specular reflections |
| BRDF LUT | 256² (baked once at startup) | Stores the split-sum GGX reflectance terms (A, B) |
At shading time, diffuse light comes straight from the irradiance cube, specular reflections sample the prefiltered cube at roughness × maxLod, and the 2D LUT combines them via F0 × A + B:
float lod = clamp(m.roughness, 0.0, 1.0) * ubo.environmentMap.z;
diffuse += texture(irradianceMap, toEnvironmentSpace(N)).rgb * intensity;
specular += textureLod(prefilteredMap, toEnvironmentSpace(R), lod).rgb * intensity;
vec2 envBRDF = texture(brdfLut, vec2(NdotV, m.roughness)).rg;
You can even rotate the environment map around +Z (toEnvironmentSpace), and scenes without an HDR sky fall back to a smooth horizon gradient blending sky and ground colors. Finally, the linear radiance passes through Narkowicz’s ACES tone-mapping curve so bright highlights roll off naturally instead of clipping harshly.
Casting Shadows Across Every Light Type
Shadows are rendered from each light’s perspective into two layered depth texture arrays managed by ShadowRenderSystem:
| Shadow Array | Dimensions | Allocation |
| Cascades | 2048² × 4 layers | Dedicated to the primary shadow-casting directional light |
| Local | 512² × 48 layers | Handed out in scene order: 1 layer per spot light, 6 layers per point light |
Directional Cascades:
- The camera frustum is sliced into four depth ranges. Each slice fits tightly to a bounding sphere and snaps to whole texel increments so shadow edges stay rock-solid without shimmering as you move the camera—fading out smoothly past 85% of max shadow distance.
Spot & Point Lights:
- Spot lights render a single perspective depth map over their cone, while point lights render six
90°faces (selecting the dominant light-to-surface axis during shader lookup).
Best of all, ShadowRenderSystem doesn’t care how meshes are stored—anything implementing the lightweight ShadowCaster interface can cast shadows:
class ShadowCaster
{
public:
virtual ~ShadowCaster() = default;
// Inside the shadow render pass, viewport set: draw depth from viewProj
virtual void renderShadowDepth(FrameInfo &frameInfo, const glm::mat4 &viewProj) = 0;
};
// Inside ReSceneRenderer::render:
shadowSystem->render(frameInfo, {staticMeshSystem.get(), skeletalMeshSystem.get()});
To banish shadow acne without floating shadows off the ground, the depth pass applies a slope-scaled bias while the fragment lookup pushes the sample point along the geometric normal by 1.5 texels—scaling with distance for local lights as texels cover larger world footprints:
float distanceToLight = length(positionWS - lightPosition);
vec3 offset = N * texelAtUnitDistance * distanceToLight * NORMAL_OFFSET_TEXELS;
(That’s the exact reason our deferred G-buffer stores the geometric normal right beside the normal-mapped normal—pushing along bumpy tangent-space normals would make shadows crawl across surfaces!)
Traps & Dragons Along the Way
Shadow layers are a finite resource.
- Our local shadow array holds 48 layers. Since point lights devour 6 layers apiece, 8 shadow-casting point lights will fill the pool—any extras gracefully skip shadows while logging a single
warnedOutOfLayersalert instead of spamming the console every frame.
Normal maps point green-up; image UVs point down.
- Vulkan image coordinates place
v = 0at the top, whereas glTF/OpenGL normal maps treat positive green as “up” the texture.cotangentFrameflips the vertical UV (vec2(uv.x, -uv.y)), forget that minus sign and every normal-mapped bump appears lit upside-down!
Let the GPU decode sRGB.
- Base color textures upload as
*_SRGBformats so hardware sampling converts them to linear space automatically. Our shaders never run manualpow(2.2)conversions, keeping all lighting math strictly linear until the sRGB swap chain encodes the final pixels.
IBL baking stalls the pipeline.
ReIblBaker::bake()waits for the GPU to finish, meaning swapping an environment map mid-frame causes a brief hitch. That’s totally fine when loading a scene or tweaking settings in the editor, though something to schedule asynchronously if we ever change skies during live gameplay.
Cull shadows before sampling them.
shadeSurfacechecks whether a light actually illuminates the pixel (such as falling inside a spotlight’s cone) before running its3×3PCF shadow lookup—saving nine texture taps per light across the dark parts of the scene.
Next Up on the Quest
We’ve built the engine split, the dual pipelines, and a full PBR lighting and shadow suite—now let’s bring animated creatures into the world! In DevBlog #9, we’ll wrap up our trilogy on commit 18dd4b4 by diving into glTF model loading with cgltf, CPU keyframe animation, and GPU-skinned skeletal meshes that bring the classic animated Fox sample to life!
