Home Source code

RayTracer

Ray trace diagram

Ray Tracing is a technique to render images by simulating a ton of light rays.
Instead of the rays going from the light-source to the camera, we shoot a ray from the camera for each pixel, this saves a lot of computation.
Now if we compute whether the ray hits an object in the scene, we get an image like this:

Mask of spheres

In this scene I used two spheres, but you can use any object where you can define a function to compute the intersection.
Now from this intersection-point we can check if there is a non-obstructed line between the point and a light source,
if not: it's in the shadow,
but if there is: we can calculate the angle and make the brightness dependent on that.
I've given my light-points a color property, so we can apply a color on these light spots.

Colored point-light render

I've also added a simple background so it wouldn't all be black, and gave the spheres a checkerboard pattern.

Now comes the interesting part: ✨reflections✨
By adding a simple rule to this setup we'll create much more realistic images.
"The law of reflection states that the angle of reflection equals the angle of incidence θr=θi."
So on the intersection point we use the angle of income and the normal vector of the object to recursively reflect the ray. Now we can give objects a "reflectiveness" property and combine the color observed from the previous rule and the new one.

Ray trace diagram

The bigger sphere has a high reflectiveness and the smaller none, I've also added an infinite horizontal plane, computing the intersection and normal vector are very easier for this.
Also cool are the shadows created by the three point-lights representing the primary colors.

Ray trace diagram

Since we can render an image, we can also render a video by stitching many rendered images one after another.
(This took 2 hours to render on my laptop)

Finally I wanted to try to use a 360 photo as a background, these images use cylindrical projection so they're just images, it's the same projection used on the world map you're most familiar with.
So far when a ray goes into the void we've assigned a static color, but we can triangulate a pixel from the photo and use it's color. Resulting in this:

Ray trace diagram Ray trace diagram