Loading [MathJax]/extensions/tex2jax.js
Carna  Version 3.3.2
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Coordinate Systems

This page explains the typical OpenGL coordinate systems. If you are interested in how these coordinate systems are related to the rendering pipeline, refer to its documentation.

Homogeneous Coordinates

The projective space \(\mathbb R\mathrm P^3\) is the set of all vectors \(\vec p' \in \mathbb R^4\), that represents each vector \(\vec p = \left(x, y, z\right)^\mathrm T \in \mathbb R^3\) with the straight line of points \(\vec p' = \left(wx, wy, wz, w\right)^\mathrm T, w \in \mathbb R\) and obeys the following rules:

Each affine transformation \(f : \mathbb R^3 \to \mathbb R^3\) with \(f\left(\vec x\right) = A\vec x + \vec t\) can be written as a single matrix-vector product using homogeneous coordinates. This unifies the concatenation of rotations, scalings and translations to the multiplication of matrices.

Coordinate Transformations

Carna uses a scene graph to organize objects in 3D space. Within this approach the location of each spatial object is specified relatively to another object that is its parent. Since this parent-child relationship induces a tree-like structure, there always will be a node without a parent. This node is the root of the scene. Its coordinate system is called world space.

The location (and rotation, scaling) of a spatial object's local coordinate system can be written as a matrix using homogeneous coordinates. For example, the point \(\vec p\) whose coordinates are given within the coordinate system \(T_A\), is transformed to the world space as follows:

\[ \vec p_W = A \vec p_{T_A} \]

The matrix \(A\), that maps from the local model space to world space, is called the model matrix. For a particular node \(x\) with a parent \(p\) it is \(A_x = A_p \, L_x\), where \(L_x\) denotes the local transform of node \(x\) relatively to its parent. Now lets further assume that \(\vec p_{T_A}\) and \(\vec p_{T_B}\) were representations of the same point, given in the coordinate systems \(T_A\) and \(T_B\) respectively:

\[ A \vec p_{T_A} = B \vec p_{T_B} \]

From this it is easy to derive a rule on how to transform the coordinates \(\vec p_{T_A}\) to the coordinate system \(T_B\):

\[ \vec p_{T_B} = B^{-1} A \vec p_{T_A} = B^{-1} \vec p_W \]

Camera Coordinates

The process of rendering a scene from a particular point of view requires the transformation of all coordinates within the scene into the coordinate system of the camera. This coordinate system is often called view space or eye space. The inverse of the camera's world matrix, i.e. the matrix that maps world space coordinates to view space, is the view matrix \(V\):

\[ \vec p_V = V \vec p_W \]

Note that most of the time neither the pure model matrix, nor the pure view matrix are of interest, but their concatenation, that the term model-view matrix refers to. All coordinate systems described up to here were right-handed. The coordinate systems introduced below are all left-handed, meaning that their z-axis is flipped in comparison to the view space.

Clipping Coordinates

The projection matrix \(C\) maps the view space coordinates to so-called clipping coordinates, i.e. \(\vec p_C = C \vec p_V\). A typical perspectival projection matrix looks as follows:

\[ C = \begin{pmatrix} \frac{2n}{r-l} & 0 & \frac{r+l}{r-l} & 0 \\ 0 & \frac{2n}{t-b} & \frac{t+b}{t-b} & 0 \\ 0 & 0 & \frac{f+n}{n-f} & \frac{2nf}{n-f} \\ 0 & 0 & -1 & 0 \end{pmatrix} \]

The six parameters determine the six clipping planes of the perspectival view frustum:

The term frustum culling refers to the testing whether a coordinate vector \(\vec p_C = \left(x_C, y_C, z_C, w_C\right)\) lies within the view frustum or not. The tested condition is the following:

\[ -w_C \leq x_C \leq w_C \enspace\wedge\enspace -w_C \leq y_C \leq w_C \enspace\wedge\enspace -w_C \leq z_C \leq w_C \]

The vector \(\vec p_C\) is inside the frustum if the above condition is true. It cannot be visible if it is false.

Normalized Device Coordinates

With \(\vec p_\text{nd} = \left(x_C, y_C, z_C\right)^\mathrm T / w_C\) the visible, frustum-shaped volume is morphed into a box-shaped \(\left[-1, +1\right]^2 \times \left[0, 1\right]\).

Window Coordinates

The normalized window coordinates are mapped linearly to pixel-based window coordinates, s.t.:

Note that the direction of the y-axis is flipped in comparison to the coordinate systems before.

Frame Coordinates

The frame coordinates are obtained from the window coordinates by shifting the viewport's base::Viewport::marginLeft and base::Viewport::marginTop location.