Ray tracing is one of the most fascinating techniques in computer graphics, allowing for the creation of realistic images by simulating the way light interacts with objects. As part of my 42 school journey, I recently completed the miniRT project with my good friend Flynn from my campus, which involved implementing a simple ray tracer from scratch. This experience deepened my understanding of 3D rendering, mathematics, and optimization techniques.
Understanding Ray Tracing
At its core, ray tracing works by casting rays from a camera into a scene, determining which objects they intersect, and computing the color of each pixel based on lighting, reflection, and other optical effects. Unlike rasterization (commonly used in real-time graphics like video games), ray tracing provides high levels of realism but is computationally expensive.
Rasterization
Rasterization is the dominant technique in real-time rendering, as it quickly converts 3D objects into 2D images using a pipeline-based approach. It processes polygons by projecting them onto the screen and filling in pixels based on textures and shading models. This method excels in speed and efficiency, making it ideal for applications like video games. However, rasterization struggles with producing realistic lighting, reflections, and shadows without additional techniques like screen-space reflections, shadow mapping, or ambient occlusion.
Ray Tracing
Ray tracing simulates the physics of light by tracing rays from the camera into the scene, calculating how they interact with surfaces, and determining how light bounces between objects. This method allows for natural reflections, soft shadows, accurate global illumination, and other complex optical effects. Unlike rasterization, which relies on approximations, ray tracing produces photorealistic results. However, this approach requires significantly more computational power, making it traditionally suited for offline rendering in movies, CGI, and high-end visualization.
Path Tracing
Path tracing is an advanced extension of ray tracing that simulates light transport more accurately by modeling the behavior of light as it bounces around a scene. Unlike traditional ray tracing, which typically stops after a few bounces, path tracing traces the path of light rays over multiple bounces, capturing effects like global illumination, caustics, and diffuse inter-reflections. This technique is based on Monte Carlo integration, where random samples are used to approximate the lighting in a scene. While computationally intensive, path tracing produces highly realistic images.
In the miniRT project, we ultimately implemented path tracing as our rendering algorithm. This allowed us to achieve more realistic lighting and shadows compared to basic ray tracing. By simulating the way light interacts with surfaces over multiple bounces, we were able to create images with natural soft shadows, realistic reflections, and subtle lighting effects that closely mimic real-world physics.
Implementing miniRT
The miniRT project required implementing a basic ray tracer with fundamental rendering capabilities. We had to construct a scene parser, handle ray-object intersections, and compute lighting using basic shading models. The project was divided into two main parts:
Parsing and Scene Description: I focused on developing the parser, which reads a scene description file and converts it into an internal representation that the ray tracer can use. This included defining objects like spheres, planes, and cylinders, as well as handling light sources and camera settings.
Ray Tracing Algorithm: Flynn worked on the core algorithm, implementing ray-object intersection calculations, shading models, and optimizations like bounding volume hierarchies (BVH) to speed up rendering.
Features Implemented
Phong Reflection Model
The Phong reflection model is a widely used shading model that improves the realism of rendered objects by simulating how light interacts with surfaces. It consists of three main components:
- Ambient Reflection: This accounts for the general illumination present in the scene, ensuring that objects are not completely dark even when not directly lit.
- Diffuse Reflection: Based on Lambert’s cosine law, this models the way light scatters uniformly when hitting a rough surface, producing a matte appearance.
- Specular Reflection: This simulates the bright highlights seen on glossy surfaces. It depends on the angle between the viewer and the reflected light, creating sharp highlights on smooth materials.
Bounding Volume Hierarchy (BVH)
BVH is a spatial acceleration structure used to improve ray tracing performance. Instead of checking every object in the scene for ray intersections, BVH organizes objects into a tree of bounding volumes:
- Bounding Volumes: Groups of objects are enclosed within simple geometric shapes (e.g., boxes or spheres) that are easier to test for intersection than the actual objects.
- Hierarchical Structure: The scene is divided recursively into a tree structure, allowing the ray tracer to quickly eliminate large groups of objects that do not intersect with a given ray.
- Performance Gain: By reducing the number of intersection tests per ray, BVH significantly accelerates rendering, especially in complex scenes with many objects.
Bump Mapping
This technique simulates surface detail by modifying normal vectors based on a texture map, creating the illusion of intricate surface features without altering geometry.
Multi-spot Lights
We extended our lighting model to support multiple light sources, allowing for more dynamic and realistic illumination in scenes.
Custom Objects (OBJ Files)
We added support for loading external 3D models from OBJ files, enabling users to incorporate more complex objects into their rendered scenes.
Final Thoughts
miniRT was an incredibly rewarding project that provided hands-on experience with rendering techniques. While this was just a basic ray tracer, it inspired me to explore more advanced topics related to computer graphics. A huge thanks to Flynn for making math and ray tracing look easy—this project wouldn’t have been the same without his expertise!
Recommended Resources
If you're interested in learning more about ray tracing, I highly recommend the following resources that helped us during this project:



