Skip to content

Morozov-5F/raytracing-weekend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

raytracing-weekend

Simple path renderer written in modern C. Based on Ray Tracing in One Weekend and Ray Tracing: The Next Week books by Peter Shirley.

This project uses PPM image format to simplify image output routines. This is far from optimal but should work.

Here's the sample scene from the cover of the first book:

Sample image

And a showcase from the second:

Showcase image

Apart from the core ray tracer I've decided to add parallel rendering -- right now it has implementation for UNIX-like systems and Windows. Also there are command line options that are available by calling a ray tracer with -h or --help flags.

I've selected C because I wanted some challenge and I think I got some. I decided to make vector and ray routines completely inlined to remove some overhead from function calls. I could have gone with macros but I thought the inlines will suit better. Because of that even in debug mode code compiles with inline optimizations enabled.

Also I had to implement something like polymorphism and encapsulation and I think my implementation is pretty efficient and not completely ugly in terms of code. I also implemented my own simplified version of shared_ptr based on reference counting for all the renderer units. Seem to work fine.

I'm not sure if my render is entirely correct but I'm pretty happy with it considering it's my first raytracer. I know it's slow and not really optimized and accurate but the images look really nice to me.

Building

This project uses CMake to generate build system files.

  1. Create a build directory:
    ? mkdir -p build
  2. Go to new directory:
    ? cd build
  3. Run CMake to generate project. I recommend Ninja as a build system, but UNIX Makefiles will work too.
    # To use Ninja
    ? cmake -G Ninja -DCMAKE_BUILD_TYPE=Release..
    or
    # To use CMake (under Linux)
    ? cmake ..
  4. Build the application:
    # For ninja
    ? ninja build
    or
    # For makefile
    ? make
  5. Launch the application and view image
    ? ./ray_tracing_one_week > image.ppm
    # The following line should open file in image viewer right from the console although it may not work -- depends on the distro settings
    ? xgd-open image.ppm
    On macOS:
    ? open image.ppm