How to Write a Ray Tracer Intuitively

How to Write a Ray Tracer Intuitively

13 minute read

In my last blog post, I demonstrated my ray tracer for the BBC Micro:Bit. In that post, I talked a little about what a ray tracer is, but didn’t discuss how you actually implement one. If you don’t know what a ray tracer is, make sure you read that post first.

This post discusses a ray tracer in detail, talking you through the computation step-by-step. It’s not the most advanced ray tracer - in fact it’s about as simple as they come. It fires one ray per pixel, and doesn’t support reflection or refraction. However, you should be able to adapt it to your needs once you understand the maths.

I deliberately haven’t included any code samples. Instead, I explain in words, maths, and pictures. That probably seems scary, but trust me when I say that ray tracing just isn’t that complicated. If I included code samples, you would probably just copy them and not really understand what was going on.

By the end of this post, you should have an intuitive understanding of how ray tracers work. You should be able to write the code yourself, and more advanced readers will be able to extend their implementation with extra features.

Step 1: Define your Scene

Ray tracers simulate light rays to render a 3D scene. Before we can start doing that, we need to define the scene that we want to render.

In our simple implementation, the scene contains one camera and many triangles. We only use triangles in our scene because they are the simplest shape to render. We can convert more complex shapes into triangles in a process called triangulation

Each triangle is a set of 3 coordinates .

One triangle is displayed

Each coordinate is a 3D coordinate .

The X axis runs left-right. The Y axis runs up-down. The Z axis runs in-out. The 3 coordinates of the triangle's corners are {-1, 0, 2}, {0, -1, 3}, and {1, 0, 2}

We add four triangles in a pyramid shape and the camera. The camera’s position is also a 3D coordinate. It dictates where we are looking in the scene.

A camera is on the right-hand side of the screen, pointing at the triangle. More triangles have been added to create a square-based pyramid

The camera is special because it has both a position and a direction. This allows us to turn the camera and look around the scene. The camera’s rotation is given as (yaw) and (pitch). Yaw handles the left-right rotation, while the up-down rotation is determined by the pitch. Together, these let the camera look in any direction.

The yaw and pitch are annotated on an arrow showing the direction the camera is facing. Yaw is rotation about the Y axis, while Pitch is rotation about the X axis.

Step 2: Calculate the Triangle Planes

Triangles are flat and two-dimensional. We can imagine a triangle as a small section of an infinite plane.

The plane is shown as an infinite grid that the triangle is sitting on

We describe a plane as . Any values of that satisfy are on the plane, and vice-versa.

The , , and components of the plane decide its orientation. We can calculate them by finding the angle of the plane’s normal line. The normal line is the line that is orthogonal (at a right angle) to the plane.

Looking at the plane from the side, we see the normal line coming out of the triangle at a right angle

To get the angle of the normal, we will use the cross-product of two vectors. The cross-product is an algebraic function which, given two vectors, outputs a new vector which is orthogonal to both.

This is a visualisation of the cross product

To find the plane’s normal line we need two vectors on the plane that cross. We can pick any two sides of the triangle.

Another image of the triangle from before

I will use the two lines crossing at . We can calculate the angle of those two lines by looking at the difference in coordinates from one end to the other:

For anyone that’s new to vectors, subtracting them works like this:

The bottom side of the triangle is {2, 0, 0}, and the left side is {1, -1, 1}

Now that we have our two lines and , we can calculate the normal line as . This is , the cross product operator.

I’m not new to vectors, but I certainly can’t remember how to do a cross product. For anyone in the same camp, here’s how it works:

If you want a more in-depth explanation of how it works, try this ‘Math is Fun’ page. I’m not ashamed to say that I spent a long time on that site. It may be for children, but it sure is helpful!

The normal line comes out of the bottom-right corner with an angle of {0, -2, -2}

Using the cross product, we now have the angle of the normal line . The values of map to the components of the plane.

To calculate the final component, , we need to go back to the plane formula:

We can substitute in our values from :

Here, is simply any point on the plane. The last two lines are using the dot product, for conciseness. It’s defined as:

Now we know that , we can calculate it by substituting the value of our normal line and any point on the plane . Pick your favourite corner of the triangle and use that.

The same normal line as before is shown on the plane

In our case, this becomes:

Meaning the overall vector for the plane is:

Referring back to the original formula for the plane, we can substitute in our values to get or put simply . That’s it, we have calculated the formula for the plane!

Step 3: Calculate the Ray Lines

It’s time to start simulating the rays. The vector representation of a line, including both position and angle, is:

is the origin point of the ray. In our case, it is always the same as the camera coordinates. is the angle of the ray. It specifies how the , , and coordinates change as we move along the line. The parameter specifies how far along the line we are.

The line starts at S and progresses by M each time lambda increases

You were probably taught a similar formula for 2D graphs in school, . That is equivalent to saying:

In other words, for each that increases, increases by . You can move along the line using . It is simple to see how this gets extended to work in 3D.

Finding the origin of the ray, , is easy. All rays start at the camera position , so we just use that.

Calculating , the line’s angle, is more complex. This is the ratio of how each coordinate changes as we move along the line.

If was then for each increase in , we would see twice the increase in and three times the increase in . Since is just a ratio, it doesn’t matter whether we say or .

We can find by looking at how the coordinates change along a known portion of the line. We will look at the portion of the line from the camera to the view plane. The view plane is a grid that sits in front of the camera. Each cell in the grid is one pixel that we want to paint on the screen.

A 5x5 grid sits in front of the camera

For now, we will just consider the simplest case. The camera has no rotation, and is just looking straight ahead (in the direction). We are calculating the angle of the center ray, which goes through the centermost pixel in the grid.

In that case, the change in coordinates between the camera’s position and the center of the view plane is simply . In this example, we will treat the view plane as having a fixed width of . Since the screen is pixels, each pixel has a width and height of .

For every pixel right of the center, we must add to . That means that for the pixel being the center, is simply:

An arrow goes from the camera's origin to the bottom-right pixel of the view plane

However, this ignores the direction of the camera. Thankfully, rotating vectors is quite simple. We can simply rotate to match the rotation of the camera.

To rotate a vector about the axis (yaw), we do:

And about the axis (pitch), we do:

Therefore, given yaw and pitch :

Now that we know both and , we can calculate the coordinates of any point along the ray’s path.

Step 4: Intersect Rays with Planes

We’re finally ready to simulate our light rays. We know where our light rays start, where they’re going, and everything they might hit. All that’s left to do is to actually simulate the path and find out what each ray hits.

More precisely, we need to find the intersection point of each pairwise ray/plane combo. To be even more precise, for each pairwise combo of ray and plane , what is the intersection point ?

The ray intersects with the plane to the right of the triangle

Obviously, must be on both and . We can substitute into the formula for to get:

And we can do the same for to get:

Then we substitute in the values of from above:

Which can then be rearranged to solve for :

Now that we know , we can substitute it back into the definition above to calculate the coordinates of .

Step 5: Filter Intersection Points

Not all intersection points are actually valid. All we know about is that it is on the same plane as a triangle. However, we only care about the ones that are actually inside the triangle.

Before performing any other checks, note that . If your value is negative, you can stop early. That plane cannot get hit by the ray, as the intersection point is behind the camera.

Then, we can do a quick sanity check, making sure that is inside the bounding box of the triangle. If then cannot be inside the triangle as it is too far left. Repeat this for each combination of or and , , or .

The intersection point is inside the bounding box

We do that first because it’s much faster than the real check and we can often exit early. If is inside the bounding box, we need to do a full check, which works like this:

The outline of a triangle is showing, with the intersection point shown to the right

has 3 sides. We know that is inside the triangle if:

  1. and are on the same side of
  2. and are on the same side of
  3. and are on the same side of

Intuitively, that makes sense.

One corner of the triangle and the intersection point are shown on opposite sides of a side of the triangle

To check whether and are on the same side of the line , we do:

This uses both the cross product and the dot product. The cross product has a special property that is the same as but in the opposite direction.

This goes further, and says that the direction of depends on which side of that is on. If and are on the same side of then the angle between them will be . If they are on different sides, the angle will be .

When the dot product is positive, the angle between the two vectors is , meaning they must have been on the same side.

The A and B vectors are shown in almost opposite directions

if then and are on the same side. We repeat that for each of the three sides of the triangle. Any intersection points that are not in the triangle can be discarded.

Step 6: Rasterise the Rays

Each ray can only intersect with one triangle, so we need to find the triangle that gets hit first. To calculate the distance that the ray travelled, we can use Pythagoras’ theorem in 3D:

For our simple implementation, we will set each pixel’s brightness to , meaning pixels that are closer to the camera are brighter. Any rays that don’t intersect result in a black pixel.

Each pixel of the view plane is coloured based on whether they hit the pyramid

Conclusion

That’s it! You now know everything you need to, and can write your own ray tracer. If you want to know more about 3D rendering, make sure you have a look at these:

If you are still desperate for some example code, you can look at the code from my last post. Either way, you really should give it a try yourself first.

Once you’ve finished your basic ray tracer, here’s some extra features you could add, from easiest to hardest:

  • Colour each triangle a different colour
  • Send multiple rays per pixel and average the result
  • Change the shape of the view plane to a dome
  • Optimise your code, can it run at 60fps?
  • Allow some triangles to behave like mirrors
  • Add transparent materials and simulate refraction
  • Add light sources to your scene, and render a shadow
  • Add a black hole to the scene and simulate gravitational lensing

If you do write your own ray tracer, please show me on twitter (especially if you implement gravitational lensing, you absolute madlad)