Game Development
3D Game Rendering: meshes, PBR, lighting, camera
A modern AAA game like Cyberpunk 2077 renders 5-10 million triangles, runs hundreds of light sources, and reflects geometry not present on screen - all in 16 ms per frame. The pipeline doing this work has barely changed in 25 years: triangles, materials, lights, camera. What has changed is the physics in each step.
- **Unreal Engine 5 Nanite**: virtualized geometry with billions of source triangles, cluster-level culling, and software rasterization for sub-pixel detail - same triangle primitive, new traversal.
- **id Tech 7 (DOOM Eternal)**: forward+ rendering at 60 FPS with hundreds of dynamic lights using clustered shading, 4K HDR on PS5 with no dropped frames.
- **Hardware ray tracing**: NVIDIA RTX, AMD RDNA 2/3, and Intel Arc all expose BVH traversal in hardware - hybrid rasterize-plus-trace pipelines now run in shipped titles like Alan Wake 2.
Historical context
In 1973, Bui Tuong Phong at the University of Utah published 'Illumination for Computer Generated Pictures', introducing the Phong reflection model that decomposed light into ambient, diffuse, and specular components. The model was cheap enough for real-time rendering and dominated the game industry for three decades. Phong died of leukemia in July 1975 at age 32, before seeing his model become the standard shading algorithm that powered every major 3D game engine through the early 2000s.
Mesh: geometry as triangles
A **3D mesh** is a collection of triangles defined by vertices and an index buffer. Vertices store per-point attributes: position (XYZ), normal (XYZ), UV texture coordinates, and optionally tangent vectors and bone weights for skeletal animation. The **index buffer** allows vertex reuse - a cube has 8 vertices but 12 triangles; without an index buffer, each triangle would need its own 3 vertex copies.
**Winding order and backface culling**: triangles defined counter-clockwise (CCW) from the camera's perspective are considered front-facing. The GPU's backface culling stage discards clockwise triangles before the fragment shader, halving the number of fragments to shade. This optimization assumes closed meshes (no holes) - single-sided surfaces like leaves or grass blades must either disable culling or duplicate with flipped winding.
Why do GPUs render triangles rather than quads (quadrilaterals) or n-gons?
PBR materials: physically based reflection
**PBR (Physically Based Rendering)** models surface appearance using properties from physics: **albedo** (base color), **metallic** (0=dielectric like plastic, 1=metal like gold), **roughness** (0=mirror, 1=matte), and **ambient occlusion**. The Cook-Torrance BRDF computes specular reflection using microfacet theory - the surface is modeled as millions of tiny mirrors (microfacets) with a statistical roughness distribution.
**Why PBR over Phong**: Phong's specular power is an artist-tuned exponent with no physical meaning. PBR's roughness and metallic are physically meaningful: roughness=0 is a polished mirror, metallic=1 means the tint color is applied to specular reflections (not diffuse, since metals absorb diffuse light). This makes PBR materials portable across different lighting environments without re-tuning.
Roughness=0 vs Roughness=1 in a PBR material. What is the visual difference?
Lighting: from Phong to ray tracing
**Forward shading** computes lighting per-fragment: for each triangle fragment, loop over all lights. Cost: O(pixels * lights). **Deferred shading** separates geometry and lighting into two passes: (1) render all geometry to a G-buffer (normals, albedo, roughness, depth), (2) apply all lights using the G-buffer data. Cost: O(pixels + lights * screen_pixels), independent of scene complexity. Standard for games with 100+ dynamic lights.
**Ray tracing in games (DXR/Vulkan RT)**: rasterization approximates global illumination with baked lightmaps, screen-space ambient occlusion, and cube map reflections. Ray tracing computes accurate shadows (shadow rays), reflections (reflection rays), and indirect illumination (GI rays) at the cost of 4-16 rays per pixel per frame. Modern hybrid rendering uses rasterization for primary visibility and selective ray tracing for glossy reflections and soft shadows.
Deferred Shading renders lighting after geometry. What is the main advantage over Forward Shading?
Camera: projection, FOV, and post-processing
The **camera** transforms 3D world coordinates to 2D screen pixels through two matrices: **view matrix** (world relative to camera) and **projection matrix** (3D to 2D perspective). **Field of View (FOV)** determines how wide the viewing frustum is. Standard FPS games use FOV 90-100 degrees for a natural feel. FOV 130+ degrees creates a fish-eye distortion that can cause disorientation during fast movement.
**Post-processing pipeline**: after the main render pass, games apply screen-space effects - Bloom (bright areas glow), Depth of Field (out-of-focus blur simulating camera lens), Motion Blur (camera movement blur), Temporal Anti-Aliasing (TAA, averages frames to reduce aliasing), and Color Grading (LUT-based tone mapping). TAA has largely replaced MSAA/SSAA because it costs O(1) samples per pixel regardless of scene complexity.
Which FOV value typically causes a fish-eye effect and potential disorientation during fast movement?
Key ideas
- **Triangle meshes** with vertex + index buffers, with LOD layers swapped by camera distance.
- **PBR materials**: albedo, metallic, roughness, ambient occlusion - microfacet BRDF models real surfaces in any lighting.
- **Forward vs deferred shading**: forward scales with overdraw, deferred decouples geometry and lighting at the cost of G-buffer bandwidth.
- **Camera and projection**: view + perspective matrices, FOV 90-110 degrees for FPS, post-processing (TAA, bloom, DOF) finishes the frame.
- **Ray tracing**: hybrid pipelines selectively trace shadows, reflections, and global illumination on top of rasterization.
Вопросы для размышления
- Forward+ shading is preferred on tile-based mobile GPUs while desktop AAA titles default to deferred or visibility-buffer pipelines. What hardware property of mobile GPUs flips the trade-off?
- TAA averages frames over time and largely replaced MSAA. What artifacts does TAA introduce that MSAA never did, and what mitigations (DLSS, FSR, XeSS) exist?
- Ray-traced reflections in Cyberpunk 2077 cost ~30% of the frame budget on RTX 3080. Which scene structures benefit enough from RT to justify that cost, and which look effectively the same with cubemaps?
Связанные уроки
- gd-03 — Linear algebra (matrices, vectors, transformations) is required for understanding the rendering pipeline
- arvr-04 — VR rendering builds directly on this pipeline: stereo cameras, foveated shading, reprojection
- cgeom-04 — Terrain mesh generation uses Delaunay triangulation; LOD systems use the same triangle-based representation
- cv-04 — Game AI uses CNNs for visual perception (object detection, terrain classification in procedural generation)
- devops-04 — Game build pipelines containerize asset compilation, shader compilation, and platform packaging in Docker
- gd-05
- sci-04
- dsp-04
- la-06-transformations