Overview
This repository contains an interactive CPU ray tracer. The implementation demonstrates classic ray-tracing techniques and a number of usability and performance features (async worker renderer, responsive interactive mode, and a contest-quality scene with animation).
Highlights
- Pinhole camera with configurable vertical field-of-view (FOV) and aspect-correct ray generation.
- Analytic geometry: ray-sphere and finite, capped ray-cylinder intersections.
- Triangle meshes and plane primitives for scene construction.
- Phong lighting with shadows (shadow rays + bias to avoid self-intersections).
- Recursive reflections and refractions (Snell’s law) with Schlick/Fresnel blending.
- Multithreaded asynchronous render worker with request/response serials and cancellation.
- Interactive (coarse) mode while moving and full-quality rendering after the camera settles.
- Fly-camera controls, mouse-look, fullscreen/resize robustness, and an advanced Scene 3 with an animated spinning glass cube.
Build (Windows / CMake)
Requirements: CMake (3.16+), a C++17-capable toolchain (MSVC/Visual Studio recommended), OpenGL development headers. The repository includes the third-party libraries under thirdparty/ for convenience.
Recommended build (PowerShell / Developer Prompt):
cmake -S . -B build -A x64
cmake --build build --config Release
# Run the produced binary (Release or Debug as built)
.\build\Release\CpuRayTracing
Or build Debug:
cmake --build build --config Debug
.\build\Debug\CpuRayTracing
Running & Controls
W/S/A/D: Move forward / back / left / rightR/F: Move up / down- Arrow keys : Turn (yaw/pitch)
Tab: Toggle mouse lock (when locked, mouse-look is active)- Right mouse (hold) : Mouse-look when unlocked
- Left mouse : Re-lock mouse (when unlocked)
[/]: Decrease / increase vertical FOV1,2,3: Switch scenesC: Reset cameraQ: QuitEsc: Unlock mouse- Hold
Shift: Increase movement speed
Render modes
- Interactive mode: coarse sampling (pixel stepping), few or zero bounces, shadows disabled, used while the camera is moving for responsiveness.
- Full-quality mode: per-pixel sampling, higher bounce limits, shadows enabled, produced once the camera has been idle for a short settle time.
Implementation notes
- Camera: pinhole ray generation with vertical FOV and aspect correction to produce perspective rays from the camera eye.
- Geometry: analytic ray-sphere intersection, finite capped cylinder intersection with cap tests, and triangle meshes for arbitrary shapes.
- Shading: Phong lighting model for local shading, shadow rays determine whether a surface sees the light or gets ambient-only shading.
- Reflection & refraction: recursive ray tracing with depth limit, refractive indices, Snell’s law, and Schlick’s Fresnel approximation are used to mix reflection and transmission.
- Multithreading: a single worker thread consumes
RenderRequestjobs and producesRenderResults, serial numbers enable cancellation of stale work. - Responsiveness: the scheduler issues interactive frames frequently while input is active, and transitions to full-quality frames after a settle timeout.
References
- Phong, B.-T. “Illumination for Computer Generated Pictures” (1975) - Phong reflection model used for local shading.
- Schlick, C. “An Inexpensive BRDF Model for Physically-Based Rendering” (1994) - Schlick’s approximation for Fresnel reflectance.
- Pharr, M., Jakob, W., & Humphreys, G. “Physically Based Rendering: From Theory to Implementation” - comprehensive reference for physically-based rendering concepts.
- Shirley, P. “Ray Tracing in One Weekend” series - practical ray tracing tutorials and implementation patterns.
- Standard optics resources for Snell’s law and refraction-based derivations.
- Common computational-geometry references for analytic ray-sphere and ray-cylinder intersection formulas.
Third-party libraries
- GLFW - https://www.glfw.org/
- GLAD - https://glad.dav1d.de/
- GLM - https://glm.g-truc.net/
- stb (single-file headers) - https://github.com/nothings/stb
- ImGui - https://github.com/ocornut/imgui
- fmt - https://fmt.dev/ (local patch applied for MSVC compatibility)
Notes & Known issues
- The repository contains a small local patch to
thirdparty/fmt-7.0.3to address MSVC/C++17 compatibility, see that folder for details. - You may see compiler warnings (e.g., C4244) coming from third-party headers depending on compilation flags, these are non-blocking.













