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

  1. Points, Vectors and Normals
  2. Coordinate Systems
  3. Math Operations on Points and Vectors
  4. Matrices
  5. How Does Matrix Work: Part 1
  6. How Does Matrix Work: Part 2
  7. Transforming Points and Vectors
  8. Row Major vs Column Major Vector
  9. Matrix Operations
  10. Spherical Coordinates and Trigonometric Functions
  11. Creating an Orientation Matrix or Local Coordinate System
  12. Transforming Normals
  13. Source Code (external link GitHub)

Coordinate Systems

Reading time: 15 mins.

Exploring Coordinate Systems

Coordinate systems are a fundamental aspect of the graphics pipeline, serving as a straightforward yet crucial concept. Often introduced in early education within the context of geometry, understanding coordinates is a stepping stone towards grasping matrices more thoroughly.

In our preceding discussion, we highlighted that in computer graphics (CG), points and vectors are defined by three real numbers. These numbers signify a signed distance from a line's origin to a point's location on that line. Imagine drawing a line and marking its center as the origin. This mark serves as our reference point, the baseline from which we measure distances to other points. A point situated to the right of the origin is assigned a positive signed distance, indicating it is greater than zero. Conversely, a point on the left has a negative value.

We consider the line to extend indefinitely in both directions from the origin. This implies that the distance between any two points on the line could be boundlessly large. Nevertheless, in computing, the representation of numbers is limited by the number of bits used for encoding, setting a practical cap on the values we can handle. Fortunately, this limit is typically sufficient for constructing the 3D scenes intended for rendering in CG, so the computational constraints are not a significant concern for now.

With our line and origin established, we introduce regular markings (unit lengths) on either side of the origin, transforming our line into a measurable scale. This scale allows us to determine the coordinate of a point relative to the origin, with "coordinate" referring to the signed distance from the origin to the point. In the realms of computer graphics and mathematics, this scale is known as an axis.

Figure 1: the position of a point is defined as the (signed) distance from the point's position to the origin of the axis. The axis extends from minus to plus infinity.

Consider a scenario where a point does not lie directly on the axis. We can still ascertain its coordinate by projecting it onto the axis with a vertical line (assuming the axis is horizontal), typically using a line perpendicular to the axis. The point where this vertical line intersects the axis gives us the coordinate of the point in relation to that axis, teaching us how to locate a point's coordinate along an axis.

Exploring Dimensions and Cartesian Coordinate Systems

Expanding on the concept of coordinate systems, let's delve into how dimensions are incorporated, starting with what we've previously called the x-axis—our horizontal ruler. By introducing another ruler at a right angle to the x-axis at its point of origin, we create the y-axis. This setup allows us to determine both the x- and y-coordinates of any point by drawing perpendicular lines to each axis and measuring their distances from the origin, paralleling the method described earlier. Consequently, we can assign an arbitrary point two coordinates: one for its position along the x-axis and another for its placement on the y-axis. Through this mechanism, a two-dimensional space, or plane, is defined by the intersection of these two axes.

Imagine plotting several points on a sheet of paper; this action takes place within a two-dimensional space. When we draw two axes on this paper—one for each dimension—and measure each point's position using these axes, we effectively establish a coordinate system. If these rulers intersect at right angles, they form what is known as a Cartesian coordinate system.

To denote a point's coordinates within this system, we use an ordered pair, which consists of two numbers separated by a comma. In Cartesian coordinate systems, it is standard practice to list the x-coordinate (horizontal) first, followed by the y-coordinate (vertical). For instance, a point with an x-coordinate of 2.5 and a y-coordinate of 2.25 would be represented as (2.5, 2.25), as seen in Figure 2. This notation simplifies to understanding the point's location as 2.5 units to the right and 2.25 units above the origin. This method of describing a point's coordinates will be used in future discussions.

Figure 2: a 2D Cartesian coordinate system is defined by two perpendicular right-angled axes (represented by the grey square in the middle of the figure). Each axis is divided into regular intervals of unit length. Computing the coordinates of a 2D point is simply an extension of the 1D case (in Figure 1) to the 2D case. We take signed distances from the point to the origin of the coordinate system in both x and y.

With this knowledge, we've established how to create a two-dimensional Cartesian coordinate system and how to define the coordinates of a 2D point within it. It's crucial to recognize that within a given coordinate system, the coordinates of points are unique, meaning the same point cannot have different coordinates simultaneously.

Although any number of coordinate systems can be defined within a plane, for simplicity, let's consider only two Cartesian coordinate systems drawn on a single sheet of paper. The coordinates assigned to a point on this paper will vary depending on the coordinate system used. For example, in Figure 3, point P is assigned coordinates (-1, 3) in coordinate system A and (2, 4) in coordinate system B, illustrating that the physical location of the point remains unchanged despite the differing coordinates.

Figure 3: the same point is defined in two coordinate systems. We can transform the point in the red coordinate system (A) to the green coordinate system (B) by adding the values (3, 1) to its coordinates.
Figure 4: scaling (shown in blue) or translating a point (shown in green) modifies its coordinates. A scale is a multiplication of the point's coordinates by some value. A translation is an addition of some values to the point's coordinates.

To translate a point's coordinates from one system to another—say, from A to B—an essential operation in computer graphics (CG) and mathematics, we simply add the values (3, 1) to its coordinates in system A to obtain its coordinates in system B. Similarly, subtracting these values from the coordinates in system B returns them to their values in system A. This process highlights the concept of additive inverses and their role in navigating between coordinate systems.

Moreover, operations like translation—moving a point to a new location within the same coordinate system—and scale—altering a point's coordinates through multiplication—demonstrate the application of linear operators to point coordinates. Scaling, for instance, shifts a point along the line extending through both the point and the origin, emphasizing the transformational relationship between points and vectors. Further exploration of these concepts will continue in subsequent lessons.

Extending to the Third Dimension

The transition from 2D to 3D coordinate systems is a natural progression that introduces a third axis—known as the z-axis—which is orthogonal (perpendicular) to both the x-axis and the y-axis, thereby adding depth to the previously flat representation. In this setup, the x-axis extends to the right, the y-axis stretches upwards, and the z-axis projects backward, appearing to come out of the screen when viewed with the x-axis pointing rightward. This arrangement, while not the only possible one, will be consistently applied throughout our discussions on Scratchapixel. This three-dimensional framework defines what is more formally recognized as Euclidean space, a cornerstone concept in geometry.

Figure 5: a three-dimensional coordinate system. Three coordinates define a point, one for each axis.

To delve deeper, especially for those interested in the formal aspects of coordinate systems, we turn to the principles of linear algebra. Here, the concept of a basis becomes essential. In the context of a coordinate system—whether it's one-dimensional with a single axis, two-dimensional with two axes, or three-dimensional as now introduced—the axes constitute the system's basis. A basis is a collection of linearly independent vectors that, through linear combinations, can represent any vector (or point) within a specific vector space (the coordinate system itself). Vectors within a set are considered linearly independent if none of them can be expressed as a linear combination of the others within the same set. The operation known as change of basis, or altering the coordinate system, plays a significant role in both mathematics and the graphics pipeline, facilitating the transition between different perspectives and dimensions within a given space.

Understanding Left-Handed vs. Right-Handed Coordinate Systems

The concept of coordinate system handedness introduces a layer of complexity due to varying conventions. This complexity is best illustrated by considering the orientation of the z-axis in relation to the x-axis (right vector) and y-axis (up vector). When visualizing your screen as the XY plane, with the y-axis pointing upwards and the x-axis extending to the right, the z-axis (also known as the forward or down vector) can either point towards the screen or towards you, indicating depth's direction.

To distinguish between these orientations, we refer to the first arrangement as the left-hand coordinate system and the second as the right-hand coordinate system. The distinction between these two types of systems was made clearer by physicist John Ambrose Fleming, who introduced the left-hand and right-hand rules.

The differentiation between left-handed and right-handed systems is straightforward: with the x-axis directed to the right and the y-axis upwards, a z-axis that points away from you defines a left-hand coordinate system. Conversely, if the z-axis points towards you, you're dealing with a right-hand coordinate system.

To easily recall the orientation of each coordinate system, you can use your hands as a mnemonic device. Assign your thumb, index finger, and middle finger to represent the x, y, and z-axes, respectively. This order naturally matches the way we extend our fingers. Perform this with both your left and right hand, orienting your thumb to the right and your index finger upward. Your middle finger will then point in the direction of the z-axis:

This method might require some dexterity but effectively aids in consistently remembering the orientation of the z-axis in these coordinate systems. Thus, the terms "left-hand" and "right-hand" coordinate systems, or the "handedness" of a system, are derived from this physical representation.

Figure 6: orient your left or right hand as depicted in this figure to find out in which direction the z-axis points to when you the left or right hand coordinate systems are used respectively. Hand 3D model courtesy of Artem Manuilov.

Certainly, let's rephrase that section for clarity:

Position your thumb upwards to represent the y-axis, and extend your index finger forward, away from you, to symbolize the z-axis. In this alignment, for a left-hand coordinate system, your middle finger, representing the x-axis, naturally points to the right. Conversely, in a right-hand coordinate system, your middle finger will point towards the left.

This method utilizes the left and right hands to intuitively determine the orientation of the x-axis, with the thumb signifying the upward direction (y-axis) and the index finger indicating the forward direction (z-axis), which is considered forward relative to the viewer's perspective. According to this approach, the x-axis, or the direction indicated by the third finger (middle finger), will point right when using the left hand and left when using the right hand. It's important to note that the terms "left" and "right" in this context refer to the hand used for the demonstration, not to the physical direction of the x-axis. This distinction is made because stating that the middle finger points right in a left-hand coordinate system might seem paradoxical. However, the XYZ-thumb-index-middle finger mnemonic is preferred for its intuitive guidance on axis orientation.

This explanation is provided because it might appear in other resources or discussions, offering a historical perspective on how Scratchapixel originally differentiated between the two coordinate systems.

The handedness of a coordinate system further influences the orientation of normals computed from polygonal faces' edges. In a right-handed system, for example, polygons defined with vertices in a counterclockwise order will face forwards. This principle plays a crucial role in rendering polygonal objects, as will be explored in further lessons.

Deciphering Right, Up, and Forward Vectors in CG Conventions

Figure 7: the most popular convention used in CG defines the up vector as being the y-axis (a). However, it's common to find coordinate systems in many CG-related papers (particularly those related to shading techniques) where the up vector is defined as the z-axis (b). Some authors claim this convention comes from the notation commonly used in physics and mathematics. Both coordinate systems drawn in this figure are right-handed.

In the realm of computer graphics (CG), the Cartesian coordinate system is underpinned by three unit-length vectors that are perpendicular to each other. However, beyond their mathematical description, these axes carry specific meanings that developers must interpret and define. This necessity underscores the importance of distinguishing between the handedness of a coordinate system and the conventions adopted for naming its axes.

The question of whether the up vector is represented by the z-axis or the y-axis hinges on the convention being applied. Adopting the perspective of Figure 7b, where the x-axis is considered the right vector, we can still identify the system as right-handed by orienting our hand accordingly and observing the alignment of our fingers with the designated up and forward vectors. This exercise illustrates that the labeling of axes—whether x, y, or z—does not influence the coordinate system's handedness. It's a common misconception that the choice of label for the up vector (z-axis versus y-axis) might imply a shift in handedness from left to right or vice versa, which is not the case.

Handedness is determined solely by the spatial relationship between the left (or right) vector and the up and forward vectors, independent of the labels assigned to these axes. This distinction between handedness and naming conventions is crucial for clarity and accuracy in discussing and working with coordinate systems.

When working with a renderer or any 3D application, understanding the employed convention for the coordinate system is essential. The industry tends to favor the right-hand XYZ coordinate system, where x points right, y points up, and z extends outward. Tools and APIs like Maya and OpenGL adhere to a right-hand system, while others like DirectX, PBRT, and PRMan adopt a left-hand system. It's notable that Maya and PRMan define the up vector as the y-axis and the forward vector as the z-axis, impacting how coordinates are interpreted and potentially requiring adjustments when exporting geometry between systems. The choice between left-hand and right-hand systems affects operations such as rotation and the calculation of cross-products.

Transitioning between coordinate systems, albeit straightforward, can be cumbersome, requiring adjustments to point coordinates and the camera-to-world matrix, often by scaling these values with (1, 1, -1).

Scratchapixel commits to a right-hand coordinate system to maintain compatibility with Maya and align with the prevalent industry standard. This choice highlights a desire for uniformity in conventions across the CG industry, although variations persist.

Navigating the World Coordinate System

In the journey through 3D applications, we've uncovered how the positions and directions of points and vectors are anchored to the origin of a Cartesian coordinate system, constructed from three mutually perpendicular unit vectors that form its basis. This exploration revealed the possibility to craft numerous coordinate systems, each granting points and vectors a unique set of coordinates within its confines. Yet, paramount to the structure of most 3D environments is the world coordinate system. This master coordinate system sets the stage, offering the foundational x-, y-, and z-axes from which all other coordinate systems derive their orientation. It emerges as the cornerstone in the constellation of coordinate systems employed throughout the rendering pipeline, which includes those dedicated to objects, local environments (particularly in shading contexts), cameras, and screens. The intricacies of these systems will be unraveled progressively.

Core Concepts to Remember

While the fundamentals of basic geometry might already be familiar to many readers, the crux of this discussion leans more towards fostering familiarity with the terminology pervasive across computer graphics (CG) literature. In this narrative, the spotlight shines on coordinates, axes, and the Cartesian coordinate system as key terms. Further enriching this vocabulary is the introduction of linear operators, such as scaling and translation, instrumental in manipulating points and vectors. The takeaways extend beyond mere geometric principles to include an understanding that points are invariably linked to a coordinate system, amidst a realm where endless coordinate systems can be instituted, each bestowing a point with distinct coordinates.

Crucial, too, is recognizing the handedness of the coordinate system in use—be it within your programming endeavors or the APIs employed for image rendering. This discernment aids in navigating the nuances between the handedness of a coordinate system and the conventions adopted for axis labeling, ensuring a seamless integration of concepts across the spectrum of CG endeavors.