Introduction to Raytracing: A Simple Method for Creating 3D Images

Distributed under the terms of the CC BY-NC-ND 4.0 License.

  1. How Does it Work
  2. The Raytracing Algorithm in a Nutshell
  3. Implementing the Raytracing Algorithm
  4. Adding Reflection and Refraction
  5. Writing a Basic Raytracer
  6. Source Code (external link GitHub)

How Does it Work

Reading time: 8 mins.

Note: This lesson is more of a general introduction to 3D rendering. You can also check the lesson An Overview of the Ray-Tracing Rendering Technique if you are interested in learning about ray-tracing specifically.

To begin this lesson, we will explain how a three-dimensional scene is made into a viewable two-dimensional image. Once we understand that process and what it involves, we can utilize a computer to simulate an "artificial" image by similar methods. This chapter describes the foundations upon which CGI is built.

In the second chapter of this lesson, we will introduce the ray-tracing algorithm and explain, in a nutshell, how it works. We have received emails from various people asking why we are focused on ray tracing rather than other algorithms. The truth is, we are not. Why did we choose to focus on ray tracing in this introductory lesson? This algorithm is the most straightforward way of simulating the physical phenomena that cause objects to be visible. For that reason, ray tracing is the best choice, among other techniques, when writing a simple program that creates 3D or computer-generated images.

To start, we will lay the foundation with the ray-tracing algorithm. However, as soon as we have covered all the information we need to implement a scanline renderer, we will also show how to do that.

How Does an Image Get Created?

Figure 1: we can visualize a picture as a cut made through a pyramid whose apex is located at the center of our eye and whose height is parallel to our line of sight.

The first thing we need to produce an image is a two-dimensional surface (this surface must be of some area and cannot be a point). With this in mind, we can visualize a picture as a cut made through a pyramid whose apex is located at the center of our eye and whose height is parallel to our line of sight (remember, to see something, we must view along a line that connects the eye to that object). We will call this cut, or slice, mentioned before, the image plane (you can see this image plane as the canvas used by painters). An image plane is a computer graphics concept, and we will use it as a two-dimensional surface to project our three-dimensional scene. Although it may seem obvious, what we have just described is one of the most fundamental concepts used to create images on various apparatuses. For example, an equivalent in photography is the surface of the film (a sensor for a digital camera) or, as mentioned before, the canvas used by painters.

Perspective Projection

Let's imagine we want to draw a cube on a blank canvas. The easiest way of describing the projection process is to start by drawing lines from each corner of the three-dimensional cube to the eye. To map out the object's shape on the canvas, we mark a point where each line intersects with the surface of the image plane. For example, let us say that c0 is a corner of the cube and is connected to three other points: c1, c2, and c3. After projecting these four points onto the canvas, we get c0', c1', c2', and c3'. If c0-c1 defines an edge, we draw a line from c0' to c1'. If c0-c2 defines an edge, we draw a line from c0' to c2'.

Figure 2: projecting the four corners of the front face on the canvas.

If we repeat this operation for the remaining edges of the cube, we will end up with a two-dimensional representation of the cube on the canvas. We created our first image using perspective projection. If we continually repeat this process for each object in the scene, we get an image of the scene as it appears from a particular vantage point. Painters started understanding the rules of perspective projection at the beginning of the 15th century.

Light and Color

Once we know how to draw the outline of the three-dimensional objects on the two-dimensional surface, we can add colors to complete the picture.

To summarize quickly what we have just learned: we can create an image from a three-dimensional scene in a two-step process. The first step consists of projecting the shapes of the three-dimensional objects onto the image surface (or image plane). This step requires nothing more than connecting lines from the features of the object to the eye. An outline is then created by going back and drawing on the canvas where these projection lines intersect the image plane. As you may have noticed, this is a geometric process. The second step consists of adding colors to the picture's wireframe (a process called shading).

In a scene, an object's color and brightness are mostly the results of lights interacting with an object's materials. Light is made up of photons (electromagnetic particles) that have, in other words, an electric component and a magnetic component. They carry energy and oscillate like sound waves as they travel in straight lines. Various light sources emit photons, the most notable example being the sun. If a group of photons hits an object, three things can happen: they can be either absorbed, reflected, or transmitted. The percentage of photons reflected, absorbed, and transmitted varies from material to another and generally dictates how the object appears in the scene. However, the one rule all materials have in common is that the total number of incoming photons is always the same as the sum of reflected, absorbed, and transmitted photons. In other words, if we have 100 photons illuminating a point on the object's surface, 60 might be absorbed, and 40 might be reflected. The total is still 100. In this case, we will never tally 70 absorbed, and 60 reflected or 20 absorbed and 50 reflected because the sum of transmitted, absorbed, and reflected photons has to be 100.

In science, we only differentiate two types of materials, metals which are called conductors and dielectrics. Dielectrics include glass, plastic, wood, water, etc. These materials have the property to be electrical insulators (pure water is an electrical insulator). Note that a dielectric material can either be transparent or opaque. The glass and plastic balls in the image below are dielectric materials. Every material is, in one way or another, transparent to some electromagnetic radiation. X-rays, for instance, can pass through the human body.

An object can also be made out of a composite or multi-layered material. For example, one can have an opaque object (let's say wood, for example) with a transparent coat of varnish on top of it (which makes it look both diffuse and shiny at the same time as the colored plastic balls in the image below).

Let's consider the case of opaque and diffuse objects for now. To keep it simple, we assume that the absorption process is responsible for the object's color. White light is made up of "red", "blue", and "green" photons. If a white light illuminates a red object, the absorption process filters out (or absorbs) the "green" and the "blue" photons. Because the object does not absorb the "red" photons, they are reflected. This is the reason why this object appears red. We see the object at all because some of the "red" photons reflected by the object travel towards us and strike our eyes. Each point on an illuminated area or object radiates (reflects) light rays in every direction. Only one beam from each point strikes the eye perpendicularly and can therefore be seen. Our eyes are made of photoreceptors that convert light into neural signals. Our brain can then use these signals to interpret the different shades and hues (how we are still determining). This is a very simplistic approach to describing the phenomena involved. Everything is explained in more detail in the lesson on color (which you can find in the section Mathematics for Computer Graphics).

Figure 3: al-Haytham's model.

Similar to the concept of perspective projection, it took a while for humans to understand light. The Greeks developed a theory of vision in which objects are seen by rays of light emanating from the eyes. An Arab scientist, Ibn al-Haytham (c. 965-1039), was the first to explain that we see objects because of the sun's rays of light; streams of tiny particles traveling in straight lines were reflected from objects into our eyes, forming images (Figure 3). Now let us see how we can simulate nature with a computer!


Found a problem with this page?

Want to fix the problem yourself? Learn how to contribute!

Source this file on GitHub

Report a problem with this content on GitHub