Hello! Welcome to the first lesson of our second module, "Rays, Pixels, and Image Output."
In the last module, we set up a solid foundation for our project. You learned essential C++ skills and, crucially, how to organize a project into a professional structure with header and source files. We now have a blueprint that can grow with our ray tracer's complexity.
Today, we take the first real step into the world of ray tracing by focusing on its most fundamental component: the ray itself. Everything we do from this point forward will involve creating and manipulating rays. This lesson addresses the learning outcome: Define a ray mathematically as an origin point and a direction vector. We will explore the simple but powerful equation that describes a ray's path through our 3D scene. This concept is the absolute core of how renderers like Arnold or Karma begin their work for every pixel.
1. From Camera to Scene: The Role of a Ray
In the real world, light travels from sources (like the sun), bounces off objects, and eventually enters our eyes or a camera. Simulating this forward path for trillions of photons would be computationally infeasible.
Instead, ray tracing works in reverse. For each pixel in our final image, we imagine a ray traveling from our virtual camera, through that pixel's position on a "viewport" plane, and out into the 3D scene. The color of the first object that this ray hits determines the color of the pixel. This is the central idea of ray tracing.
To learn more about this "reverse" tracing approach, the following resource provides a great high-level overview.
Basic Raytracing - Computer Graphics from Scratch
The article 'Basic Raytracing' from Gabriel Gambetta's 'Computer Graphics from Scratch' explains the core concept of tracing rays from the camera into the scene.
Read the section titled 'Tracing Rays' (up to the subsection 'The Ray Equation'). This will give you the conceptual model for what we're about to define mathematically.
2. The Parametric Equation of a Ray
To trace a ray, we need a way to describe it mathematically. The most common and useful way is with a simple parametric equation.
A ray can be thought of as a function that returns a 3D point for any given value of the parameter . The function is defined as:
Let's break this down:
- is a 3D point along the ray.
- is the origin of the ray—a 3D point where the ray begins. In our case, this will be the camera's position.
- is the direction of the ray—a 3D vector indicating the direction in which the ray travels.
- is a scalar (a real number). It acts like a distance parameter. As you vary , moves along the line defined by the origin and direction.
Since you have a strong background in linear algebra, you'll recognize this as the standard vector equation of a line. We are simply starting at point and moving some distance along the direction vector .

To see this definition put into the context of our project, please read the following section from the "Ray Tracing in One Weekend" book, which our course is based on.
Ray Tracing in One Weekend Series
This section from Peter Shirley's 'Ray Tracing in One Weekend' provides the formal definition of a ray and shows how directly it maps to a C++ class structure, which we'll be building in the next lesson.
Read the subsection 'The ray Class'. Focus on the mathematical definition and how the text breaks down the meaning of A, b, and t. Notice how simple the corresponding class structure is.
3. Understanding the Parameter t
The parameter is more than just an abstract variable; it has a very practical meaning. Given our ray equation :
- If , then . The point is at the ray's origin.
- If , the point is "in front" of the origin, along the ray's direction. These are the points we care about, as they represent locations in the scene visible to the camera.
- If , the point is "behind" the origin. These intersections are usually ignored, as the camera can't see what's behind it.
When we later calculate intersections with objects (like spheres), our calculations will solve for . The value of will tell us where along the ray the intersection occurred. If we find multiple intersections with different objects, the one with the smallest, positive value is the closest one to the camera and is the one that's visible.
The following reading explains the significance of the t parameter very clearly.
Basic Raytracing - Computer Graphics from Scratch
Let's return to 'Computer Graphics from Scratch' to get a deeper insight into the role of the 't' parameter.
Read the text associated with 'Table 2-1: Subdivisions of the Parameter Space' and the accompanying diagram. This provides an excellent summary of what different ranges of 't' represent.
Test your understanding!
A ray originates from the camera at point and has a direction vector .
This ray intersects two objects:
- A red sphere, resulting in an intersection parameter of .
- A blue plane, resulting in an intersection parameter of .
Which object does the camera "see"? What is the 3D coordinate of the visible intersection point?
Show answer
The camera sees the red sphere.
Both and are positive, so both intersections are in front of the camera. However, , meaning the intersection with the red sphere is closer to the camera's origin. The red sphere therefore occludes the blue plane.
The 3D coordinate of the intersection point is calculated using with :
The visible intersection occurs at the point .
Conclusion
In this lesson, we established the mathematical and conceptual foundation for our entire ray tracer. The simple parametric equation is the engine that will drive our renderer.
Key Takeaways:
- Ray tracing works by casting rays from a virtual camera, through pixels, and into a 3D scene.
- A ray is mathematically defined by a parametric function , where is the origin, is the direction, and is a scalar parameter representing distance along the ray.
- The value of is critical: positive values represent points in front of the camera, and the smallest positive for an intersection corresponds to the closest (and therefore visible) object.
Next Steps
Now that we have the mathematical theory, the next logical step is to translate it into code. In the next lesson, we will implement a C++ class for a ray, creating ray.h and ray.cpp files within the project structure we designed. This will be our first functional component of the ray tracer.