# C++ DirectX 12 Engine From Scratch (Day 8): Lambertian Diffuse + Ambient Lighting in HLSL

**URL:** <https://forums.unrealengine.com/t/c-directx-12-engine-from-scratch-day-8-lambertian-diffuse-ambient-lighting-in-hlsl/2876149>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, CPP\
**Created:** [December 2, 2026, 4:25pm UTC](https://forums.unrealengine.com/t/c-directx-12-engine-from-scratch-day-8-lambertian-diffuse-ambient-lighting-in-hlsl/2876149 "2026-12-02T16:25:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevinpc\_1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kevinpc_1/32/1215238_2.png) [@kevinpc\_1](https://forums.unrealengine.com/u/kevinpc_1)\
**Post date:** [December 2, 2026, 4:25pm UTC](https://forums.unrealengine.com/t/c-directx-12-engine-from-scratch-day-8-lambertian-diffuse-ambient-lighting-in-hlsl/2876149/1 "2026-12-02T16:25:34Z")

</div>

Day 8 of building a custom 3D game engine from scratch in C++ and DirectX 12 using the Microsoft GDK.

In today’s update, I implemented directional lighting (Lambertian diffuse + ambient) so the rotating cubes show real surface shading and depth while keeping the 40x40 reference grid unlit.

What’s new in Day 8:

- Vertex Normals & Input Layout: Extended the Vertex struct with 3D normals (float normal[3]), added the NORMAL semantic at byte offset 28 in the Input Layout, assigned per-face normals to the cubes, and set upward (0, 1, 0) normals on the reference grid.
- Expanded 256-Byte Constant Buffer: Updated SceneConstantBuffer with the world matrix, directional light vector, light color, ambient color, and a useLighting flag while preserving strict 256-byte alignment.
- HLSL Lighting Shader: Transformed vertex normals into world space in VSMain using gWorld and computed Lambertian diffuse (saturate(dot(N, L))) + ambient lighting in PSMain when useLighting is enabled (set to 0.0f for the grid).
- Root Signature Visibility Fix: Updated constant buffer b0 to D3D12\_SHADER\_VISIBILITY\_ALL so both the Vertex and Pixel Shaders can access SceneConstants without PSO root signature mismatch errors.

Current Engine Controls:

- W / A / S / D: Move forward, left, backward, right
- Q / E: Move down / up
- Right Mouse Button + Move: Free look (Yaw & Pitch)
- Left Shift: 2.5x movement speed boost
- Mouse Wheel: Adjust base camera speed
- R: Reset camera to default position and orientation
- Video Complete Day 8:[https://youtu.be/bBSojk5ZM50](https://youtu.be/bBSojk5ZM50)

Tech Stack: C++, DirectX 12, HLSL, DirectXMath, Win32 API, Microsoft GDK.

Follow the daily engine development journey here on Low-Level Code | USA.

#CPP #DirectX12 #GameEngine #GraphicsProgramming #EngineProgrammer #LowLevelProgramming #GameDev #MicrosoftGDK #hlsl #3DEngine
