Chapter 3

Rasterization

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

conﬁgure 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 diﬀerent 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

classiﬁed into three cases. See Fig. 3.1. (1) Triangle t

1

is completely outside

the view frustum and is discarded. (2) Triangle t

2

is completely inside and is

passed to the next step as is. (3) Triangle t

3

intersects the view frustum and

is then clipped. Only the part of the triangle located inside the view frustum

53

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

proj

presented in Equation (2.23). Unlike aﬃne

transforms, the last row of M

proj

is not (0 0 0 1) but (0 0 −1 0). When M

proj

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

proj

to P

2

and Q

2

.

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

1

and l

2

, which are projection-transformed into l

0

1

and

l

0

2

, respectively. Even though l

1

and l

2

are of the same length in the camera

space, l

0

2

becomes smaller than l

0

1

due to the division. This brings the eﬀect of

perspective projection or foreshortening. Division by w is called perspective

division.

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]

1

. The vertices P

0

1

, Q

0

1

, P

0

2

and Q

0

2

in Fig. 3.2 are

all represented in NDC.

1

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 eﬀect 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.