Skip to content

qbojj/raytracer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple OBJ Ray Tracer (Embree/KD backend + MTL textures)

This project implements a ray tracing assignment with an extensible acceleration layer:

  • Loads geometry from Wavefront OBJ (v, vt, f) and MTL (Kd, Ks, Ns, d/Tr, map_Kd)
  • Supports texture sampling with generated mip levels and trilinear filtering
  • Supports two acceleration backends:
    • Embree backend
    • Custom backend (currently baseline traversal, prepared for KD-tree evolution)
  • Includes OpenGL scene viewer with fly camera controls
  • Uses GLM for camera/vector math
  • Writes output as JPG via stb_image_write

Build

On Arch Linux:

sudo pacman -S embree glm stb cmake
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j

On Ubuntu/Debian-like systems:

sudo apt install libembree-dev libglm-dev libstb-dev cmake
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j

This creates executable build/raytracer (compile-time Embree backend).

Raytracing loops are parallelized with OpenMP. You can control thread count with:

OMP_NUM_THREADS=8 ./build/raytracer ...

To build both backend-specific binaries:

cmake --build build --target backends -j

This creates:

  • build/raytracer-embree
  • build/raytracer-kdtree

Build OpenGL viewer target:

cmake --build build --target raytracer-viewer -j

This creates:

  • build/raytracer-viewer

Build interactive raytraced viewer target:

cmake --build build --target raytracer-rtviewer -j

This creates:

  • build/raytracer-rtviewer

The interactive raytraced viewer also uses OpenMP for per-frame pixel traversal.

Benchmark Workflow

Run automated Embree vs KD benchmark cases:

cmake --build build --target benchmark -j

This generates:

  • renders in renders/bench/
  • CSV metrics in report/benchmark_results.csv

CSV columns:

  • scene path
  • backend
  • resolution
  • build/trace times
  • total/shadow ray counts
  • rays/s metrics

Usage

./build/raytracer <input.obj> <output.jpg> [options]
./build/raytracer <input.obj> -o <output.jpg> [options]

Assignment-style options:

  • -vp <x y z> camera position
  • -vd <x y z> camera viewing direction
  • -up <x y z> camera up vector
  • -fovy <deg> vertical field of view in degrees
  • -ltcol <r g b> current light color
  • -ltpos <x y z> add point light (uses last -ltcol)
  • -r <n> recursion depth (0 primary only, 1 adds shadow rays, >=2 adds reflections)
  • -normals <default|invert|auto> normal orientation mode (auto tries to detect inward-facing winding)
  • -res <width height> output resolution
  • -o <path> output image path
  • -accel embree|kdtree runtime backend switch (only in non-forced builds)

Legacy aliases (still available):

  • --width, --height, --fov, --eye, --target, --up, --lightdir, --background

The eye/target/up/fov parameters map directly to an OpenGL-style camera setup.

Example Commands

./build/raytracer scenes/single_triangle.obj -o out_triangle.jpg \
  -vp 0.0 1.0 3.0 -vd 0.0 -0.2 -1.0 -up 0 1 0 \
  -fovy 55 -ltcol 1 1 1 -ltpos 4 6 2 -r 2 -res 1280 720
./build/raytracer scenes/pyramid_room.obj out_room.jpg \
  -vp 2.8 1.7 3.2 -vd -2.8 -0.9 -3.2 -up 0 1 0 \
  -fovy 52 -ltcol 1 1 1 -ltpos 5 7 2 -r 2 -res 1600 900

CMake Render Targets

Generate all bundled scene renders:

cmake --build build --target render-all -j

Generate a single scene render:

cmake --build build --target render-single-triangle -j
cmake --build build --target render-pyramid-room -j

Outputs are written to renders/.

The program prints required stats at the end:

  • acceleration structure build time
  • ray tracing time
  • total computed rays
  • number of shadow rays
  • throughput in rays/second (with and without build time)

External OBJ Scenes

To download additional internet scenes for experiments:

cmake --build build --target fetch-scenes

Downloaded files are placed in scenes/external/. Track attribution and license notes in scenes/external/SOURCES.md before submission.

OpenGL Viewer

Run:

./build/raytracer-viewer scenes/pyramid_room.obj

Controls:

  • W/S: forward/backward
  • A/D: strafe left/right
  • Q/E: down/up
  • Shift: speed boost
  • hold right mouse button and move mouse: look around
  • P: print camera as -vp/-vd/-up arguments for offline raytracing
  • Esc: quit

Interactive Raytraced Viewer

Run:

./build/raytracer-rtviewer scenes/external/spider.obj \
  -vp -19.01 23.4072 109.266 -vd 0.0207327 -0.29089 -0.956532 -up 0 1 0 \
  -res 640 360 -r 1 -normals auto

Controls:

  • W/S: forward/backward
  • A/D: strafe left/right
  • Q/E: down/up
  • Shift: speed boost
  • hold right mouse button and move mouse: look around
  • P: print camera as -vp/-vd/-up arguments
  • Esc: quit

Project Structure

  • src/main.cpp thin executable entrypoint
  • src/raytracer_app.cpp render orchestration and tracing loop
  • src/raytracer_app.hpp public entrypoint declaration for raytracer module
  • src/raytracer_types.hpp shared domain types (scene/material/ray/camera/stats)
  • src/raytracer_io.hpp / src/raytracer_io.cpp CLI and OBJ/MTL loading
  • src/raytracer_accel.hpp / src/raytracer_accel.cpp acceleration backends (Embree + custom KD-tree)
  • src/raytracer_texture.cpp texture loading and mipmap generation
  • src/viewer.cpp OpenGL viewer application
  • scenes/*.obj example scenes
  • CMakeLists.txt build configuration

About

simple ray tracer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors