CPU Raytracer

A basic real-time raytracer on the CPU

About this project

For this project, we were tasked with creating a raytracer on the CPU. The goal was to create a real-time raytracer where primitive objects, like spheres and spheroids, can be rendered in real time. Primitive objects can contain reflective and refractive materials. In order to accomplish real-time framerates (around 20fps), a custom BVH system was implemented, as well as a multithreading system for spreading rendering workload across multiple threads.

This is a solo student project made during my 1st year at Breda University of Applied Sciences.

My contributions to the project

Since this is a personal project, everything has been done by me. A brief list of systems that have been implemented will be listed below:
Sphere materials: Three types of material have been added to the project. These materials can be applied to any of the spheres in a level.
Diffuse material: A basic material where an object either uses a flat color or a texture.  
Reflective material: A material where an object has a reflective surface, allowing the material to redirect incident light rays.  
Refractive material: A material where an object has a refractive surface, like a glass ball, allowing the material to manipulate the path of light rays passing through it .  
Custom BVH: In order to decrease the performance cost of ray-object intersection tests in a scene with a lot of spheres, a BVH system was implemented. When a scene is loaded, the BVH is automatically constructed. 
The way the construction of the BVH works is that all objects are wrapped in axis-aligned bounding boxes (AABBs). All these AABBs are then grouped as small sets within larger bounding boxes. These AABBs are then grouped into other (larger) bounding boxes, which is repeated until the entire scene is contained within one single bounding box. Once this is done, a complete tree structure has been made for all primitives in the scene, facilitating efficient spatial organization. 
When a ray is cast into the scene, rather than checking for collisions with each primitive (which is very expensive), it checks if the ray collides with the bounding boxes instead. If an intersection occurs, it descends the BVH tree to check for collisions with the bounding box of a smaller subset of primitives. This recursive process continues until a leaf node is reached, which warrants an individual collision calculation for a primitive. By using the hierarchical structure of the BVH, the program greatly reduces the number of primitive collision tests, therefore resulting in improved performance. 
In order to test if my BVH implementation was working correctly, I decided to profile my program before- and after using a BVH. In a scene with 256 spheres, enabling the BVH resulted in a 10.4x performance increase. In another scene with 15.960 spheres, enabling the BVH resulted in a 163x performance increase. 
Multithreaded rendering:  To further improve the performance of the raytracer, I implemented a multithreading system. This system works by partitioning the screen into several small tiles, which are then assigned to an idle thread for raytracing. When a thread is idle, it picks up a tile, raytraces it, and moves on to the next available tile until the entire frame is rendered. Doing this resulted in a ~10x performance increase.
More details about these contributions can be found below.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Quick overview

Project Type:  Raytracer
Platform:  Windows
Tools:  C++
Role in Team:  Programmer
Project Duration:  8 weeks
Release Date:  January 28, 2022
Team size:  1
Role in Team:  Engine programmer and Lead