Overview
The camera system in Cub3D handles the rendering of the 3D world from a character’s perspective. It uses multi-threaded raycasting to efficiently render walls and billboards.s_camera Structure
The camera structure stores rendering configuration for a character.Fields
Pointer to the character this camera is rendering for
Field of view in degrees (default: PLAYER_FOV = 66.0)
Number of rays to cast (typically matches canvas width)
Array storing the distance each ray traveled. Used for billboard depth sorting and rendering. Length equals
rays.s_render_config Structure
Configuration structure for rendering operations.Fields
The image buffer to render to
Pointer to the game state
The character to render from
Field of view for rendering
Number of rays to cast
Multi-Threaded Rendering
The camera system uses parallel processing to improve performance.Thread Configuration
Number of threads used for raycasting. Defined in
cub3d.h:96.- Thread 0: rays 0 to (rays/4)
- Thread 1: rays (rays/4) to (rays/2)
- Thread 2: rays (rays/2) to (3*rays/4)
- Thread 3: rays (3*rays/4) to rays
Thread Data Structure
Starting ray index for this thread
Ending ray index (exclusive) for this thread
Core Functions
render_camera()
Main camera rendering function that orchestrates the rendering pipeline.Game state containing entities, map, and configuration
Image buffer to render into
Character whose view to render
- Creates a temporary camera structure
- Allocates ray_distances array
- Calls
render_walls()to cast rays and draw walls - Calls
render_billboards()to render sprites - Frees the ray_distances array
src/utils/render/camera/camera.c:15
render_walls()
Multi-threaded wall rendering using DDA raycasting.CAMERA_THREADS threads. Each thread:
- Calculates ray angle for each column
- Performs DDA raycasting to find wall intersections
- Handles transparent walls recursively
- Stores distances in
camera->ray_distances - Draws vertical wall slices to the canvas
src/utils/render/camera/walls/render.c:95
render_billboards()
Renders all billboard entities (sprites, characters, items).- Sorts billboards by distance from camera (back to front)
- Iterates through sorted billboards
- Calculates screen position and size based on distance
- Checks visibility using ray_distances for depth testing
- Renders visible billboard slices
src/utils/render/camera/billboards/render0.c:88
Rendering Pipeline
The complete rendering sequence:Camera Configuration Constants
Default field of view in degrees. Defined in
cub3d.h:58.Maximum ray distance when no wall is hit. Defined in
cub3d.h:57.Number of sub-rays for anti-aliasing or precision. Defined in
cub3d.h:59.Related Structures
See also:- Raycasting - DDA algorithm and ray calculations
- Sprites - Billboard and sprite rendering