Skip to main content

Beyond Ray Tracer - Part 1

The simple ray tracer has been completed. Now what? It seems that I had already some spare time, so I have decided to move a bit further with the Ray Trace Renderer.
My goal is to create a Ray Trace Renderer to render a Cornell Box (Should I consider it as the equivalent to a Diploma in computer graphics?)
This Renderer will feature both Direct and Global Illumination.
Keep in mind that, in order to keep it simple, I will not include reflections and caustics. The whole Rendering will be processed by the CPU (multi-threaded at most).
It's not my intention to use GPU in this iteration.

Getting hands dirty


First step is to restructure the code, like making it more object oriented, and splitting the project into different files (not a single file, as it was currently)

Second step is to extend the variety of Graphic Objects, not only to having Spheres, but in general for any kind of triangle based meshes.

Graphic Objects

A Graphic Object interface will hold some generic parameters such as Color/Material, position, rotation and scaling.
Additionally, a function to determine if a ray intersects the object, and the hitpoint. (The hit point contains properties such as Position, Normal, and t - the parameterized value in the ray-)

The Intersect function for Spheres has been described in previous posts.

The Intersect function for Meshes will loop through all triangles finding the closest hitpoint to the ray origin. (Notice there's room for optimization here, as it's not necessary to check all triangles).
A function has been implemented using the Műller-tumblemore line- triangle intersection algorithm. That way, We cover all the cases for ray trace intersections.

Orthographic VS Perspective Projection 

The first implementation of the ray tracer featured an orthographic projection where all rays were shot parallel to each other.
This projection works fine for determining proportions and dimensions of objects, but it fails on providing a realistic perception of them . Besides, the Cornell Box is shot in a perspective projection.
For the sake of flexibility, I will keep both, and provide Z way to toggle between both modes.

In perspective projection, rays are shot from a point of view(the eye)
The following image depicts how rays are traced from the viewpoint (Please ignore the drawings below, it's hard to keep focus when you have kids around)

In order to compute the new direction these rays will take, I have manually estimated a function to determine the ending point of the ray.
There are some restrictions to the Perspective View:
1. The frustum projection is located in Z= O
2. The point of view is located along the Z axis
3. The frustum can't be transformed

Formula to calculate the ray's end-point.
Notice the constant values. They can be cached for speed

Output

The images below display objects in both projections as well as depth rendering.


Orthographic Projection



Perspective Projection


Depth





Comments