Sunday, December 8, 2019

Tech Art Project: Ray Tracer

For my project, I decided to tackle writing a ray tracer in C++ to learn more about graphics programming. I read Peter Shirley's Ray Tracing in a Weekend and studied HillsoftCode's video in order to better understand how to tackle the project.

I began with writing a loop to output a .ppm file pixel by pixel in the main function. Then I set up the Vector3 class and the Ray class to start firing out rays from a camera.



I added a shape class for my sphere and shape list to inherit from and then created a material class and referenced it in the hit data. By doing so, I would be able to tell how to scatter the rays based on the material. The first material was a simple Lambert that scattered based on a random position in a unit sphere and attenuated an RGB color.



Then I added the metal material that calculates the reflected vector of incoming rays and multiplied the resulting color with it's attenuation. Also multiplied a scalar with a random point in a unit sphere to blur the reflection for more matte looking materials.



Creating glass was a real struggle as calculating refraction was challenging for someone who doesn't know much about physics but thankfully I was able to follow the formula provided in Ray Tracing in a Weekend for refraction and the approximation by Christopher Schlick. There are two spheres, one with a negative radius to create a glass ball look.