Overview
For my second rendering project, I moved from CPU rasterization to ray tracing on the GPU. I built a C++ application around an OpenGL compute shader that traces rays through a scene, simulates several light bounces, and progressively accumulates the results into an image.
The application also includes a simple scene editor and a conventional rasterized preview, so I can change objects and materials and switch between the two rendering approaches. The ray tracer uses a deliberately simplified material model rather than a physically validated PBR implementation.
Path tracing and progressive accumulation
The compute shader generates a camera ray for each pixel and finds the nearest intersection with the scene. At each hit, it samples a new direction and continues tracing up to a configurable bounce limit. Materials combine diffuse scattering and reflection, with controls for reflexivity, smoothness, surface color, and emission. A path gathers light when it hits an emissive surface or escapes into the procedural sky.
Because the bounce directions are sampled randomly, one path per pixel produces a noisy image. Instead of trying to resolve the image in a single pass, I use progressive accumulation: each dispatch adds one new sample per pixel to a running average. The shader reads the previous result from one floating-point texture, writes the updated average into another, and alternates between them.
The update follows a simple running average: newAverage = (sampleCount * previousAverage + newSample) / (sampleCount + 1).
When I move the camera or edit the scene, the application resets the accumulation count so the image is sampled again for the new view. This allows scene interaction while each stationary view is progressively refined.
Ray intersections: spheres, tori, and triangles
I implemented intersections for several kinds of geometry. Spheres are handled analytically with a quadratic equation, while planes, cubes, and boxes are represented as triangles. Their vertices are transformed on the CPU and uploaded to the compute shader through a shader storage buffer (SSBO).
One of the more involved mathematical parts was the torus intersection. Substituting the ray equation into the implicit equation of a torus produces a fourth-degree polynomial. I worked out the ray–torus equation and used an existing quartic solver referenced in the repository to find its roots. This lets the shader intersect the curved torus surface analytically rather than approximating it with triangles.
The ray tracer tests these primitive types and keeps the nearest hit for each ray. It does not use an acceleration structure, so it is intended for relatively small scenes rather than large, complex environments.
Editing the scene
I also made an editor using Dear ImGui. It lets me add or remove built-in primitives, change their transforms, and edit material properties such as color, emission, smoothness, and reflexivity. Changes to geometry are sent back to the GPU, and edits restart progressive accumulation.
The application can switch between a rasterized view and the compute-shader ray-traced view. The rasterizer uses a simpler local lighting model, so the modes are useful as two different ways to inspect the scene, not as an identical-quality comparison. Scene descriptions can also be saved to a text file and loaded when the application starts.


