Skip to content

RobinLmn/cuda-raytracer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GPU-Accelerated Raytracer

rAI is a real-time raytracer built with CUDA and OpenGL interoperability. It features progressive rendering, anti-aliasing, and depth of field effects. The engine supports both spheres and triangle meshes, and includes a physically-inspired material system with diffuse, specular, glossy, and emissive surfaces. Lighting includes a procedural sky with sun and ground gradients, and the final image is rendered in HDR with tone mapping.


The tool window

Getting Started

Download

Download for Windows: Download Link
A CUDA-capable NVIDIA GPU is required.

Building Prerequisites

  • CUDA Toolkit 11.0+
  • CMake 3.15+
  • C++20-compatible compiler
  • NVIDIA GPU with CUDA support

Key Features and Implementation

CUDA-OpenGL Interoperability

The raytracer is built on CUDA-OpenGL interoperability, allowing us to render directly to an OpenGL texture. This setup allows us to:

  • Create an OpenGL texture that can be displayed as an ImGui::Image in the viewport
  • Map this texture to CUDA memory for direct writing
  • Launch a CUDA kernel with a 2D grid matching the image dimensions for parallel pixel processing
  • Write rendered pixels directly to GPU memory without CPU transfer

Foundational Raytracing

The initial implementation focused on raytracing fundamentals:

  • Camera-based ray generation
  • Ray-sphere intersection
  • Diffuse shading
  • Simple lighting model

Camera controls (WASD and right-click drag) allow you to move around the scene and rotate the view. On the right panel, Save allows you to save the render as a png image.


First pass: basic shading and intersection

Progressive Rendering

Progressive rendering with frame accumulation reduces noise and improves visual fidelity. Over time, the image converges to a clean result. Clicking Render on the right panel freezes the camera controls and starts the accumulation process. Stop stops the accumulation and resets it. It also unfreezes camera controls.


Without progressive rendering

After progressive accumulation

Anti-Aliasing & Depth of Field

Multi-sample anti-aliasing is achieved by jittering ray origins. Results are averaged to produce smooth edges. The Diverge Strength setting controls jitter intensity, acting like a blur.


Multi-sample anti-aliasing

Depth of field is implemented by perturbing ray directions across a virtual aperture and adjusting convergence at a configurable focal plane, producing realistic bokeh effects. This is controlled by the Defocus Strength and Focus Distance.


Focus: background

Focus: mid

Focus: foreground

Mesh Support

Mesh rendering was added to go beyond primitives:

  • Ray-triangle intersection with backface culling
  • OBJ file loading
  • Bounding box acceleration for performance

Single triangle

Imported mesh (OBJ)

Physically-Inspired Materials

Materials include:

  • Mirror-like specular reflection
  • Glossy highlights with adjustable roughness
  • Emissive surfaces to simulate area lights

Cornell box with a variety of materials

Tone Mapping & HDR

To support HDR lighting:

  • Exposure control
  • Gamma correction
  • Tone mapping (Reinhard and ACES)

Reinhard tone mapping

ACES tone mapping

License

This project is released under the MIT License. See the LICENSE file for details.

About

A gpu-accelerated raytracer built with CUDA and OpenGL

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors