Chapter 3
The vertices processed by the vertex program enter a hard-wired stage. First
of all, they are assembled to build primitives such as triangles, using the con-
nectivity information that accompanies the input stream of vertices. (Not only
a triangle but also a line segment or a point can be a primitive. A primitive is
processed as an independent entity in the rendering pipeline.) Each primitive
is further processed to determine its 2D form appearing on the screen, and
is rasterized into a set of fragments. The per-vertex data such as the vertex
normals are interpolated across each primitive, and each fragment is assigned
the interpolated data. The hard-wired stage is generally named primitive as-
sembly and rasterization. Direct3D simply calls this stage rasterization or
rasterizer, and this book follows it just for the sake of simplicity.
The substages composing the rasterizer include clipping, perspective divi-
sion, back-face culling, viewport transform, and scan conversion. The raster-
izer is not programmable. For some substages, it is enough just to understand
what they do. An example is clipping. On the other hand, some other sub-
stages require us to understand how they work. An example is back-face
culling. Without understanding its working principle, it would not be easy to
configure the substage. Another example is scan conversion, which is taken as
the key feature of the real-time graphics and needs to be fully understood for
writing the fragment program. This chapter focuses on the triangle primitives
and presents the substages in different depths.
3.1 Clipping
Clipping refers to the process of cutting the polygons against the cuboidal
view volume. The clipping algorithm is implemented in hardware, and we
cannot modify the algorithm. Just for intuitive understanding, this section
presents the idea of polygon clipping “against the view frustum in the camera
space.” The spatial relationship between a triangle and the view frustum is
classified into three cases. See Fig. 3.1. (1) Triangle t
is completely outside
the view frustum and is discarded. (2) Triangle t
is completely inside and is
passed to the next step as is. (3) Triangle t
intersects the view frustum and
is then clipped. Only the part of the triangle located inside the view frustum
54 3D Graphics for Game Programming
Fig. 3.1: Examples of clipping a triangle against the view frustum.
proceeds to the next step in the rendering pipeline. As a result of clipping,
vertices are added to and deleted from the triangle.
3.2 Perspective Division
Fig. 3.2 shows a view frustum and the cuboidal view volume produced
by the projection matrix M
presented in Equation (2.23). Unlike affine
transforms, the last row of M
is not (0 0 0 1) but (0 0 1 0). When M
is applied to (x, y, z, 1), the w-coordinate of the transformed vertex is z,
which is not necessarily 1. See the results of applying M
to P
and Q
In order to convert from the homogeneous (clip) space to the Cartesian
space, each vertex should be divided by its w-coordinate. It equals z and
is a positive value representing the distance from the xy-plane of the camera
space. Therefore, division by w makes distant objects smaller. In Fig. 3.2,
consider line segments l
and l
, which are projection-transformed into l
, respectively. Even though l
and l
are of the same length in the camera
space, l
becomes smaller than l
due to the division. This brings the effect of
perspective projection or foreshortening. Division by w is called perspective
By the perspective division, a vertex in the clip space can be represented
in so-called normalized device coordinates (NDC). The coordinates are named
normalized because the x - and y-coordinates are in the range of [1,1] and
the z -coordinate is in [0,1]
. The vertices P
, Q
, P
and Q
in Fig. 3.2 are
all represented in NDC.
Direct3D has the same range. In contrast, OpenGL’s z -range is [1,1]. See the shaded
note in Section 2.4.2.
Rasterization 55
Fig. 3.2: Projection transform produces the vertices in the homogeneous
clip space. Dividing each vertex by its w-coordinate converts the homoge-
neous coordinates into the Cartesian coordinates. This brings the effect of
perspective projection and therefore is called perspective division.

Get 3D Graphics for Game Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.