434 10. The Rendering Engine
The camera’s position and orientation can be specifi ed using a view-to-
world matrix , just as a mesh instance is located in the scene with its model-to-
world matrix. If we know the position vector and three unit basis vectors of
camera space, expressed in world-space coordinates, the view-to-world ma-
trix can be writt en as follows, in a manner analogous to that used to construct
a model-to-view matrix:
0
0
0.
1
V
V
VW V
V
→
⎡⎤
⎢⎥
=⎢⎥
⎢⎥
⎢⎥
⎢⎥⎣⎦
i
j
M k
t
When rendering a triangle mesh, its vertices are transformed fi rst from
model space to world space, and then from world space to view space. To
perform this latt er transformation, we need the world-to-view matrix , which
is the inverse of the view-to-world matrix. This matrix is sometimes called the
view matrix:
MM MWV→→==()VW−^1 view.
Be careful here. The fact that the camera’s matrix is inverted relative to the
matrices of the objects in the scene is a common point of confusion and bugs
among new game developers.
The world-to-view matrix is oft en concatenated to the model-to-world
matrix prior to rendering a particular mesh instance. This combined matrix is
called the model-view matrix in OpenGL. We precalculate this matrix so that the
rendering engine only needs to do a single matrix multiply when transform-
ing vertices from model space into view space:
M MM MMV→==MW→→ (^) WV model view-.
10.1.4.2. Projections
In order to render a 3D scene onto a 2D image plane, we use a special kind
of transformation known as a projection. The perspective projection is the most
common projection in computer graphics, because it mimics the kinds of im-
ages produced by a typical camera. With this projection, objects appear small-
er the farther away they are from the camera—an eff ect known as perspective
foreshortening.
The length-preserving orthographic projection is also used by some games,
primarily for rendering plan views (e.g., front, side, and top) of 3D models or
game levels for editing purposes, and for overlaying 2D graphics onto the
screen for heads-up displays (HUDs) and the like. Figure 10.31 illustrates how
a cube would look when rendered with these two types of projections.