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

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

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