Creating a Simple Ray Tracer in Haskell
17 Oct 2024Introduction
Ray tracing is a technique for generating an image by tracing the path of light as pixels in an image plane. It simulates how rays of light interact with objects in a scene to produce realistic lighting, reflections, and shadows.
In this post, we’ll walk through building a simple raytracer in Haskell. We will start with basic vector math, define shapes like spheres and cubes, and trace rays through the scene to generate an image. By the end, you’ll have a raytracer that can render reflections and different shapes.
What You’ll Learn:
- Basics of raytracing and the math behind it
- How to define math primitives in Haskell
- How to trace rays against shapes (including spheres and cubes)
- How to generate an image from the traced rays
- … a little math
Some Math Primitives
To begin, we need to define some basic 3D vector math. This is essential for all calculations involved in ray tracing: adding vectors, calculating dot products, normalizing vectors, and more.
We’ll define a Vec3
data type to represent 3D vectors and functions for common vector operations.
Defining a Ray
The ray is the primary tool used to “trace” through the scene, checking for intersections with objects like spheres or cubes.
A ray is defined by its origin \(O\) and direction \(D\). The parametric equation of a ray is:
\[P(t) = O + t \cdot D\]Where:
- \(O\) is the origin
- \(D\) is the direction of the ray
- \(t\) is a parameter that defines different points along the ray
Shapes
To trace rays against objects in the scene, we need to define the concept of a Shape
. In Haskell, we’ll use a
typeclass to represent different types of shapes (such as spheres and cubes). The Shape
typeclass will define methods
for calculating ray intersections and normals at intersection points.
ExistentialQuantification and Why We Need It
In Haskell, lists must contain elements of the same type. Since we want a list of various shapes (e.g., spheres and cubes),
we need a way to store different shapes in a homogeneous list. We achieve this by using existential quantification to
wrap each shape into a common ShapeWrapper
.
Sphere
Sphere Equation
A sphere with center \(C = (c_x, c_y, c_z)\) and radius \(r\) satisfies the equation:
\[(x - c_x)^2 + (y - c_y)^2 + (z - c_z)^2 = r^2\]In vector form:
\[\lVert P - C \rVert^2 = r^2\]Where \(P\) is any point on the surface of the sphere, and \(\lVert P - C \rVert\) is the Euclidean distance between \(P\) and the center \(C\).
Substituting the Ray into the Sphere Equation
We substitute the ray equation into the sphere equation:
\[\lVert O + t \cdot D - C \rVert^2 = r^2\]Expanding this gives:
\[(O + t \cdot D - C) \cdot (O + t \cdot D - C) = r^2\]Let \(L = O - C\), the vector from the ray origin to the sphere center:
\[(L + t \cdot D) \cdot (L + t \cdot D) = r^2\]Expanding further:
\[L \cdot L + 2t(L \cdot D) + t^2(D \cdot D) = r^2\]This is a quadratic equation in \(t\):
\[t^2(D \cdot D) + 2t(L \cdot D) + (L \cdot L - r^2) = 0\]Solving the Quadratic Equation
The equation can be solved using the quadratic formula:
\[t = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}\]Where:
- a is defined as: \(a = D \cdot D\)
- b is defined as: \(b = 2(L \cdot D)\)
- c is defined as: \(c = L \cdot L - r^2\)
The discriminant \(\Delta = b^2 - 4ac\) determines the number of intersections:
- \(\Delta < 0\): no intersection
- \(\Delta = 0\): tangent to the sphere
- \(\Delta > 0\): two intersection points
Here’s how we define a Sphere
as a Shape
with a center, radius, color, and reflectivity.
Cube Definition
For a cube, we typically use an axis-aligned bounding box (AABB), which means the cube’s faces are aligned with the coordinate axes. The problem of ray-cube intersection becomes checking where the ray crosses the planes of the box’s sides.
The cube can be defined by two points: the minimum corner \(\text{minCorner} = (x_{\text{min}}, y_{\text{min}}, z_{\text{min}})\) and the maximum corner \(\text{maxCorner} = (x_{\text{max}}, y_{\text{max}}, z_{\text{max}})\). The intersection algorithm involves calculating for each axis independently and then combining the results.
Cube Planes and Ray Intersections
For each axis (x, y, z), the cube has two planes: one at the minimum bound and one at the maximum bound. The idea is to calculate the intersections of the ray with each of these planes.
For the x-axis, for example, we compute the parameter \(t\) where the ray hits the two x-planes:
\[t_{\text{min}, x} = \frac{x_{\text{min}} - O_x}{D_x}\] \[t_{\text{max}, x} = \frac{x_{\text{max}} - O_x}{D_x}\]We do the same for the y-axis and z-axis:
\[t_{\text{min}, y} = \frac{y_{\text{min}} - O_y}{D_y}\] \[t_{\text{max}, y} = \frac{y_{\text{max}} - O_y}{D_y}\] \[t_{\text{min}, z} = \frac{z_{\text{min}} - O_z}{D_z}\] \[t_{\text{max}, z} = \frac{z_{\text{max}} - O_z}{D_z}\]Combining the Results
The idea is to calculate when the ray enters and exits the cube. The entry point is determined by the maximum of the \(t_{\text{min}}\) values across all axes (because the ray must enter the cube from the farthest plane), and the exit point is determined by the minimum of the \(t_{\text{max}}\) values across all axes (because the ray must exit at the nearest plane):
\[t_{\text{entry}} = \max(t_{\text{min}, x}, t_{\text{min}, y}, t_{\text{min}, z})\] \[t_{\text{exit}} = \min(t_{\text{max}, x}, t_{\text{max}, y}, t_{\text{max}, z})\]If \(t_{\text{entry}} > t_{\text{exit}}\) or \(t_{\text{exit}} < 0\), the ray does not intersect the cube.
Final Cube Intersection Condition
To summarize, the cube-ray intersection works as follows:
- Calculate \(t_{\text{min}}\) and \(t_{\text{max}}\) for each axis.
- Compute the entry and exit points.
- If the entry point occurs after the exit point (or both are behind the ray origin), there is no intersection.
Tracing a Ray Against Scene Objects
Once we have rays and shapes, we can start tracing rays through the scene. The traceRay
function checks each ray against all objects in the scene and calculates the color at the point where the ray intersects an object.
Putting It All Together
We can now render a scene by tracing rays for each pixel and writing the output to an image file in PPM format.
Examples
Here’s an example where we render two spheres and a cube:
Conclusion
In this post, we’ve built a simple raytracer in Haskell that supports basic shapes like spheres and cubes. You can extend this to add more complex features like shadows, lighting models, and textured surfaces. Happy ray tracing!
The full code is available here as a gist: