SDL2-based wireframe demo that rotates and projects simple 3D primitives (cube, pyramid) to 2D. The window is resizable and input is mapped to simple state changes.
![]() |
|---|
| Cube demo |
- Build system: Makefile — targets for debug (
all), run, release, and AddressSanitizer. - Entry point: main.cpp — initializes
RenderState, picks a primitive, starts the render loop. - SDL render loop API: 2d_renderer.h —
renderer_rundeclaration and frame-callback type. - SDL render loop impl: 2d_renderer.cpp — creates the resizable window and VSync renderer, polls events, clears/presents, and invokes the per-frame callback.
- 3D math + callback API: 3d_renderer.h — rotation/project utilities and the
frame_callbacksignature. - 3D math + callback impl: 3d_renderer.cpp — rotation around X/Y/Z, perspective projection, and the per-frame render callback.
- 3D objects API: objects_3d.h — primitive builders (cube, pyramid).
- 3D objects impl: objects_3d.cpp — implementations populating
RenderState. - Shared app state and types: app_state.h —
Point3d,Point2d,Edge3d, andRenderState(geometry, projection, rotation, timing, lifecycle). - Input mapping API: input.h —
handleEventdeclaration. - Input mapping impl: input.cpp — translates SDL events to state changes (quit, resize, spawn cube/pyramid).
Prereqs: SDL2 development headers installed and pkg-config available.
- You can install the SDL2 development headers on Linux with:
sudo apt update
sudo apt install libsdl2-dev- Linux (bash) using Makefile:
make # debug build to output/main
make run # build and launch
make release # optimized build
make asan # debug with AddressSanitizerIf you don't use make:
mkdir -p output && g++ -std=c++17 -Wall -Wextra -g \
$(pkg-config --cflags sdl2) -I. -Isrc \
$(find src -name '*.cpp' -print) \
-o output/main \
$(pkg-config --libs sdl2)
./output/main- Esc: Quit
- c or 1: Spawn cube
- p or 2: Spawn pyramid
Rotation also animates with configured per-axis speeds in main.cpp.
- Window is resizable; resizes update the projection viewport.
- Event handling occurs once in the main loop; rendering callback is pure draw.
- Projection guards extremely small denominators to avoid infinities near the camera.
- To stop the 3d objects from rotating, set the rotation speed for all axis to 0.
- To adjust the starting angle of the objects, change the angle X, Y and Z in main.cpp.
