We stand with Ukraine!
Introduction to Ray Tracing: a Simple Method for Creating 3D Images
Keywords: ray-tracing, perspective projection, conductor, dielectric, forward tracing, backward tracing, shadow ray, primary ray, eye ray, path tracing, reflection, refraction, Appel, Whitted, index of refraction, Fresnel equation, transmission, recursive, recursion depth, image file format.Want to report bugs, errors or send feedback? Write at feedback@scratchapixel.com. We do this for free, please be kind. Thank you.

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

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 will be able to utilize a computer to simulate an "artificial" image by similar methods. We like to think of this section as the theory that more advanced CG is built upon.

In the second section of this lesson, we will introduce the ray-tracing algorithm and explain, in a nutshell, how it works. We have received email from various people asking why we are focused on ray-tracing rather than other algorithms. The truth is, we are not. Why did we chose to focus on ray-tracing in this introductory lesson? Simply because this algorithm is the most straightforward way of simulating the physical phenomena that cause objects to be visible. For that reason, we believe ray-tracing is the best choice, among other techniques, when writing a program that creates simple 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, for example, we will show how to do that as well.

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.

Although it seems unusual to start with the following statement, the first thing we need to produce an image, is a two-dimensional surface (this surface needs to 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 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 upon. Although it may seem obvious, what we have just described is one of the most fundamental concepts used to create images on a multitude of different apparatuses. For example, an equivalent in photography is the surface of the film (or as just 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 that it 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, then we draw a line from c0' to c1'. If c0-c2 defines an edge, then 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 have then created our first image using perspective projection. If we continually repeat this process for each object in the scene, what we get is an image of the scene as it appears from a particular vantage point. It was only at the beginning of the 15th century that painters started to understand the rules of perspective projection.

Light and Color

Once we know where 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 skeleton.

An object's color and brightness, in a scene, is mostly the result 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. Photons are emitted by a variety of light sources, the most notable example being the sun. If a group of photons hit an object, three things can happen: they can be either absorbed, reflected, or transmitted. The percentage of photons reflected, absorbed, and transmitted varies from one material to another and generally dictates how the object appears in the scene. However, the one rule that 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 surface of the object, 60 might be absorbed and 40 might be reflected. The total is still 100. In this particular case, we will never tally 70 absorbed and 60 reflected, or 20 absorbed and 50 reflected because the total 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 things such as 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. Both the glass balls and the plastic balls in the image below are dielectric materials. Every material is in a way or another transparent to some sort of electromagnetic radiation. X-rays for instance can pass through the body.

An object can also be made out of a composite, or a 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 will 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. Now, the reason why we see the object at all is 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 ray 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 is then able to use these signals to interpret the different shades and hues (how we are not exactly sure). 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 and Physics for Computer Graphics.

Figure 3: al-Haytham's model.

Like 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!