Docsity

Vertex Transformations, Lecture notes of Geometry

Toggle sidebar
bg1
Chapter 6
Vertex Transformations
“There are two worlds; the world that we can measure
with line and rule, and the world that we feel with our
hearts and imaginations.”
Leigh Hunt: Men, Women, and Books, 1847
6.1 Overview
In the previous chapter, we saw how we could define models with vertex buffers,
index buffers and primitives. We also mentioned that dynamic vertex compo-
nents could be implemented with multiple streams.
If we wanted to move a model, we could edit its definition by locking the ver-
tex buffer and modifying the appropriate vertex components. Direct3D provides
the vertex processing pipeline to transform the model as an alternative to edit-
ing every vertex in the model. The applied transformations include translation,
rotation and scaling of vertices.
“Vertex processing” refers to all the computations that occur for each
vertex of each primitive rendered on the device. Each stage of the vertex pro-
cessing section of the pipeline affects one or more components of a vertex as it
is processed.
Direct3D allows for the application to choose between hardware vertex pro-
cessing, software vertex processing or a combination of the two. It is also possi-
ble to instruct Direct3D to perform software vertex processing and capture the
results of the processing without rendering any primitives.
A vertex transformation is represented as a 4x4 matrix of floats. Matrices
performing the basic operations of translation, rotation, and scaling are pre-
sented.
When multiple transformations are applied, the order in which they are
applied can change the outcome of the composite transformation. Examples of
common composite transformations are given.
199
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download Vertex Transformations and more Lecture notes Geometry in PDF only on Docsity!

Chapter 6

Vertex Transformations

“There are two worlds; the world that we can measure with line and rule, and the world that we feel with our hearts and imaginations.” Leigh Hunt: Men, Women, and Books, 1847

6.1 Overview

In the previous chapter, we saw how we could define models with vertex buffers, index buffers and primitives. We also mentioned that dynamic vertex compo- nents could be implemented with multiple streams. If we wanted to move a model, we could edit its definition by locking the ver- tex buffer and modifying the appropriate vertex components. Direct3D provides the vertex processing pipeline to transform the model as an alternative to edit- ing every vertex in the model. The applied transformations include translation, rotation and scaling of vertices. “Vertex processing” refers to all the computations that occur for each vertex of each primitive rendered on the device. Each stage of the vertex pro- cessing section of the pipeline affects one or more components of a vertex as it is processed. Direct3D allows for the application to choose between hardware vertex pro- cessing, software vertex processing or a combination of the two. It is also possi- ble to instruct Direct3D to perform software vertex processing and capture the results of the processing without rendering any primitives. A vertex transformation is represented as a 4x4 matrix of floats. Matrices performing the basic operations of translation, rotation, and scaling are pre- sented. When multiple transformations are applied, the order in which they are applied can change the outcome of the composite transformation. Examples of common composite transformations are given.

200 CHAPTER 6. VERTEX TRANSFORMATIONS

Vertex processing begins with the world transformation. The world trans- formation is applied to model space vertices to map them into world space. Transform properties of the device are introduced with the GetTransform, Set- Transform, and MultiplyTransform methods. Transformations can be used to implement a scene hierarchy. A scene hierar- chy is useful for drawing rigid jointed figures and relative positioning of models. We describe a jointed robot arm as an example of a transformation hierarchy. Multiple world transforms can be applied to a single vertex and the resulting transformed vertices combined in a weighted average in a technique called ver- tex blending. Direct3D provides a variety of vertex blending options which are summarized. The simplest form of vertex blending is called tweening, where a single floating point value weights a vertex transformed by two world transformation matrices. The vertex components for vertex blending weights and their associ- ated FVF code are given. Generalized vertex blending supplies two or three blending weights per vertex allowing three or four matrices to be used for blending. Indexed vertex blending allows for up to 4 different matrices to be used for each vertex. For non-indexed vertex blending the matrices for all vertices of a primitive are the same, only their blending weights are varied per-vertex. For indexed vertex blending, the indices vary per-vertex, allowing different matrices to be applied at different vertices. In the remaining sections we describe the fog, face culling, user clip plane application, view frustum clipping, homogeneous divide, and viewport applica- tion stages of the pipeline. The viewing and projection stages are discussed in chapter 7 and the lighting stage is discussed in chapter 8.

6.2 Vertex Processing

As described in section 2.13, a device can be created with software, mixed or hardware vertex processing. When a device is created with mixed vertex pro- cessing, GetSoftwareVertexProcessing and SetSoftwareVertexProcessing control if the hardware or software is used to process vertices. When this render state is set to TRUE, software vertex processing is selected, otherwise hardware vertex processing is selected. This render state is always set to FALSE or TRUE when the device is created with hardware or software vertex processing, respec- tively. When this render state is changed, the current streams, current indices, and current vertex shader are reset to their default values and will need to be restored. Vertex processing can be summarized as the following pipeline stages: world transformation, texture coordinate generation, texture transformation, view transformation, vertex fog, lighting, projection transformation, primitive assem- bly, face culling, user clip plane application, view frustum clipping, homogeneous divide and finally viewport mapping. The end result of all vertex processing is a so-called “transformed and lit” vertex, with a position component in screen

202 CHAPTER 6. VERTEX TRANSFORMATIONS

with a 3x3 matrix and a 1x3 row matrix.

P ′^ = P M + b

= [ x y z ]

m 11 m 12 m 13 m 21 m 22 m 23 m 31 m 32 m 33

[

b 1 b 2 b 3

]

M defines the rotation and scaling applied to P and b adds the translation. A vertex can also have a surface normal, which must also be transformed. With homogeneous coordinates, we can use a single matrix for the entire transformation instead of using two matrices of different sizes. When P ’s carte- sian coordinates are extended to homogeneous coordinates, a single 4x4 matrix can represent scaling, rotation and translation for three dimensional points. This gives P ′^ = P M′, with M′^ composed of elements of M and b:

[

x′^ y′^ z′^1

]

[

x y z 1

]

m 11 m 12 m 13 0 m 21 m 22 m 23 0 m 31 m 32 m 33 0 b 1 b 2 b 3 1

The upper 3x3 submatrix corresponds to rotation and scaling and the bot- tom row of the matrix corresponds to translation. The homogeneous transfor- mation matrix can also be used to implement perspective foreshortening with the rightmost column, as we will see in the next chapter. The following trans- formation matrices are given for a left-handed coordinate system. For rotations, the angle θ is given in radians with a counterclockwise rotation about the axis corresponding to increasing values of θ.

Translate by (x, y, z) T(x, y, z) =

x y z 1

Uniform scale by s S(s) =

s 0 0 0 0 s 0 0 0 0 s 0 0 0 0 1

Non-uniform scale by (sx, sy , sz ) S(sx, sy , sz ) =

sx 0 0 0 0 sy 0 0 0 0 sz 0 0 0 0 1

Rotation about the X-axis by θ Rx(θ) =

0 cos θ sin θ 0 0 − sin θ cos θ 0 0 0 0 1

6.4. ORDER OF TRANSFORMATIONS 203

Rotation about the Y-axis by θ Ry (θ) =

cos θ 0 − sin θ 0 0 1 0 0 sin θ 0 cos θ 0 0 0 0 1

Rotation about the Z-axis by θ Rz (θ) =

cos θ sin θ 0 0 − sin θ cos θ 0 0 0 0 1 0 0 0 0 1

When a vertex contains a surface normal, the normal vector is specified in the same coordinate frame as the position. When the position is mapped to a new coordinate frame with a transformation matrix, the normal vector must also be mapped into the new coordinate frame. However, the normal vector shouldn’t be distorted when transformed so that the proper surface orientation is preserved. If the position is transformed by M, then transforming the normals by the inverse transpose of M = (M−^1 )T^ preserves their orientation, although they may still be scaled by the transformation.

6.4 Order of Transformations

A complex transformation, such as rotating around a point other than the ori- gin, is built from simple transformations by multiplying the transformations together. Matrix multiplication is not commutative, so the order in which we multiply the transformation matrices is important. For instance, suppose we have two transformation matrices: T contains a translation and R contains a rotation. In general, TR 6 = RT. In this case, translation followed by rotation is not the same thing as rotation followed by translation, as depicted in figure 6.1. The simple scaling and rotation matrices operate with the origin as the center of scaling and rotation, respectively. To perform rotations about a point other than the origin, first translate the point to the origin, perform the desired rotation, and then translate the origin back to the point. Scaling about a point other than the origin is similar to rotation about a point. To rotate about an arbitrary axis, first rotate the axis to coincide with one of the three principal axes

i ,

j , or

k , then perform the desired rotation, and then rotate the principal axis back to coincide with the axis of rotation. Building a complex chain of transformations is a breeding ground for bugs in 3D applications. It is very easy to get one of the transformations wrong and get something that looks tantalizingly close to correct, but has anomolies. A good way to avoid mistakes is to draw a diagram of the composite transforma- tion as a series of simple transformations. Draw one coordinate frame diagram for each simple transformation in the composite. Write down the appropriate transformation matrix for each step in the composite transformation. If the transformation can’t be written as a simple transformation, break it down fur-

6.5. WORLD TRANSFORMATION 205

x

y 6

°μ

z

q

q q

q q

P

Qk Q

s

T(−P )

(a)

x

y 6

°μ

z

q

q q

q q

s

Rz ( π 2 )

(b)

x

y 6

°μ

z

q q q

qq

s

T(P )

(c)

x

y 6

°μ

z

q q q

qq

(d)

Figure 6.2: Illustration of a composite vertex transformation. The letter “F” in the xy plane is rotated clockwise by π 2 about the point P. The letter remains in the xy plane after the composite transformation because the axis of rotation is perpendicular to the plane.

6.5 World Transformation

In chapter 5 we described how models are built from vertices with coordinates in model space. The world transformation is the first operation applied during vertex processing and maps coordinates from model space to world space. The simplest transformation from model space to world space is to multiply the position component by a transformation matrix. The surface normal component of a vertex is also specified in model space. When a vertex is transformed, we must transform not only its position compo- nent but also its normal component. While the position component is a point in space, the surface normal is a vector in space. Direct3D uses a modified transformation matrix when it transforms normals so that the orientation of the normals will be preserved. The modified matrix is the inverse transpose of the world transformation. This matrix can still contain scaling transforma- tions, possibly changing a surface normal’s length. Direct3D can compensate for changes in the length of a surface normal, see chapter 8. The type of world transformation applied to the vertex is controlled by RS Vertex Blend, with values of type D3DVERTEXBLENDFLAGS. When RS Vertex Blend is D3DVBF DISABLE, the vertices are processed through a single world transformation matrix.

typedef enum _D3DVERTEXBLENDFLAGS { D3DVBF_DISABLE = 0, D3DVBF_0WEIGHTS = 256, D3DVBF_1WEIGHTS = 1, D3DVBF_2WEIGHTS = 2, D3DVBF_3WEIGHTS = 3, D3DVBF_TWEENING = 255, } D3DVERTEXBLENDFLAGS;

206 CHAPTER 6. VERTEX TRANSFORMATIONS

The GetTransform and SetTransform methods manipulate the transform properties of the device. The different kinds of transform properties are given by the D3DTRANSFORMSTATETYPE^1 enumeration and associated macros. D3D- TS WORLD specifies the world transformation applied when vertex blending is disabled.

HRESULT GetTransform(D3DTRANSFORMSTATETYPE kind, D3DMATRIX *value); HRESULT SetTransform(D3DTRANSFORMSTATETYPE kind, const D3DMATRIX *value);

typedef enum _D3DTRANSFORMSTATETYPE { D3DTS_WORLD = 256, D3DTS_WORLD1 = 257, D3DTS_WORLD2 = 258, D3DTS_WORLD3 = 259, D3DTS_VIEW = 2, D3DTS_PROJECTION = 3, D3DTS_TEXTURE0 = 16, D3DTS_TEXTURE1 = 17, D3DTS_TEXTURE2 = 18, D3DTS_TEXTURE3 = 19, D3DTS_TEXTURE4 = 20, D3DTS_TEXTURE5 = 21, D3DTS_TEXTURE6 = 22, D3DTS_TEXTURE7 = 23 } D3DTRANSFORMSTATETYPE;

D3DTRANSFORMSTATETYPE D3DTS_WORLDMATRIX(UINT index);

6.6 Transformation Hierarchy

In addition to replacing a transformation matrix property with SetTransform, you can also pre-multiply a matrix onto the existing matrix with Multiply- Transform. If a model is composed of pieces where each piece is positioned relative to another piece, then MultiplyTransform can be used to compose the necessary transformations as the hierarchy is rendered.

HRESULT MultiplyTransform(D3DTRANSFORMSTATETYPE transform, const D3DMATRIX *value);

The diagram in figure 6.3 shows a simple robot arm consisting of two seg- ments L1 and L2, called linkages, and locations where the segments meet an- other object, called joints. The entire ensemble is referred to as a jointed linkage,

(^1) The world matrix symbols are shown as part of the enumeration; the header file defines them as macros with the given values.

208 CHAPTER 6. VERTEX TRANSFORMATIONS

P 0

β 0 =1. 0 β 1 =0. 0

P ′

  1. 4
  2. 6

P 1

  1. 0
  2. 0

PP

@ PPP

B

B

B

BBØ

Ø

Ø

Ø

Ø

Ø

P 2 ØØ

P 1

P 0

P ′^ β^0

β 1

β 2

Q •

Q

Q

QQ

C

C

C

C

C

C°

XXXXX

X

J

J

J

JJ•

P 3

P 2

P ′

P 1

P 0

β 0

β 1

β 2

β 3

(a) (b) (c)

Figure 6.4: Visualization of vertex blending with two, three and four matrices. Pi are the images of the model space vertex position P under the world trans- formation Mi. P ′^ is the resulting blended vertex position. (a) β 0 moves P ′ along the line between P 0 and P 1. (b) β 0 and β 1 move P ′^ inside the triangle defined by P 0 , P 1 and P 2. (c) β 0 , β 1 and β 2 move P ′^ inside the region bounded by P 0 , P 1 , P 2 , and P 3.

and a single weight is used to blend between the two transformed points. This formula is similar to the alpha blending formula presented in section 1.4. Vertex blending with a single weight may be visualized as interpolating along a straight line connecting the two transformed points P 0 and P 1 , with β locating a distance along the line, as shown in figure 6.4(a).

P ′^ = β 0 P 0 + (1 − β 0 )P 1 = β 0 P M 0 + (1 − β 0 )P M 1 #»n ′^ = β 0 #»n 0 + (1 − β 0 ) #»n 1

= β 0 #»n (M− 0 1 )T^ + (1 − β 0 ) #»n (M− 1 1 )T

Assigning a β value to each vertex defines the ratio of transformations at each vertex. Usually the blend weight values will be assigned by a modeling program where a user interface is provided for controlling the appearance of vertex blending. The resulting appearance of a vertex blended model is influenced by the transformation matrices M 0 and M 1 , the distribution of the weights along the model, and the model itself. Figure 6.5 plots six different distributions of blend weights on the interval [0, 1]. Figure 6.6 shows the result of applying a transla- tion using those distributions. The blend weights were computed for the model by normalizing the x coordinate of each vertex into the interval [0, 1] and com- puting β(x). Figure 6.7 shows the result of applying different transformations to the same weight distribution. We can extend this technique to using N matrices and N − 1 blend weights per vertex instead of just two matrices. The final weight is always determined by the system to ensure that the sum of all weights is one. With two blend weights per vertex, each vertex can be positioned inside a triangle defined by

6.7. VERTEX BLENDING 209

β(x)

Blend Weight Distribution Functions 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

××× 2222222222222222

××

××

××

××

××

××

××

××

××

××

××

××

××

××

××

××

××

××

××

××

××

××

××

44 ×

Figure 6.5: Sample blend weight functions β(x) defined for x in [0, 1].

3 : β(x) = 1 − sin(πx) × : β(x) = 1 − x 4 : β(x) = x ? : β(x) = 1 1 + e−25(x−^0 .5)

  • : β(x) =

2 x, x ∈ [0, 0 .5] 2(1 − x), x ∈ (0. 5 , 1]

2 : β(x) =

0, x ∈ [0, 0 .3) 2 .5(x − 0 .3), x ∈ [0. 3 , 0 .7] 1, x ∈ (0. 7 , 1]

6.7. VERTEX BLENDING 211

(a) axis rotation about origin (b) axis rotation about center

(c) scale about center (d) Y translation

(e) Y rotation about left (f) Y rotation about center

Figure 6.7: The effect of the world transformation on vertex blending. The same

mesh is used as in figure 6.6. For all cases the blend function is the “bracketed”

function 2 from figure 6.5. (a) and (b) perform the same rotation about the

origin and the mesh’s center point, respectively. (c) performs a uniform scaling

about the center of the mesh, (d) performs a translation along the Y-axis, (e)

and (f) perform the same rotation around the Y-axis about the minimum and

center points of the mesh, respectively. The differences are more pronounced

when the mesh is put in motion.

212 CHAPTER 6. VERTEX TRANSFORMATIONS

the three transformed points. With three weights per vertex, each vertex can be positioned inside the region bounded by the four transformed points, see figure 6.4. The following formula computes the final position P ′^ and normal #»n ′ for N − 1 blend weights per vertex (N matrices).

βN = 1 −

N∑ − 1

k=

βk

P ′^ =

∑^ N

k=

βkP Mk

#»n ′ (^) =

∑^ N

k=

βk #»n (M− k 1 )T

6.7.1 Basic Vertex Blending

Direct3D exposes fixed-function blending through the D3DVERTEXBLENDFLAGS value in RS Vertex Blend. When this value is D3DVBF 1WEIGHTS, D3DVBF 2- WEIGHTS, or D3DVBF 3WEIGHTS, it indicates that each vertex has 1, 2, or 3 addi- tional floats in the position component. The floats give the β 0 , β 1 and β 2 blend weights for each vertex used with 2, 3, or 4 matrices, respectively. If D3DVBF - 0WEIGHTS is specified, a single matrix with a weight of 1.0 for each vertex is used. This gives the same result as D3DVBF DISABLE. RS Vertex Blend can be set to use fewer weights than are supplied with the vertex. The final weight will still be computed so that all the blend weights sum to one. Setting RS Vertex Blend to use more weights than are defined in the vertex is an error. The number of matrices that can be used at any one time is given by D3DCAPS9- ::MaxVertexBlendMatrices, which may vary based on the device’s creation parameters. When using an FVF to describe vertices, the D3DFVF XYZBn FVF flags spec- ify the number of blend weights stored in the position component of each vertex. With D3DFVF XYZB1, D3DFVF XYZB2 and D3DFVF XYZB3 the final blend weight is computed implicitly from the blend weights given in the vertices. With D3D- FVF XYZB4 the runtime uses β 0 , β 1 , β 2 , and β 3 as the blend weights directly without computing the fourth weight implicitly. With D3DFVF XYZB5 the run- time uses β 0 , β 1 , β 2 , β 3 as the explicit blend weights and β 4 as the blend matrix indices for indexed vertex blending. When using a vertex shader declaration to describe vertices, the blend weights are mapped to the blend weights usage semantic. The blend matrix indices are mapped to the blend indices usage semantic. The blend weights β 0 , β 1 , β 2 and β 3 are associated with the four blend matrices D3DTS WORLD, D3DTS WORLD1, D3DTS WORLD2, or D3DTS WORLD3. The same matrices can be specified with the D3DTS WORLDMATRIX() macro using the arguments 0, 1, 2 or 3, respectively. The blend matrices are manipulated with

214 CHAPTER 6. VERTEX TRANSFORMATIONS

the matrix indices. The indices are stored as BYTEs packed into a single DWORD, giving a range of [0, 255] for blend matrix indices. I 0 , the index associated with β 0 and M 0 , is stored in the least significant BYTE of the DWORD. I 1 is stored in the next signficant BYTE, and so-on.

I 3 I 2 I 1 I 0

31 0

With a fixed-function vertex shader, the matrix index stream data is mapped to the blend indices usage semantic. With a programmable vertex shader, the TODO: Work out stream data can be mapped to any input register. As described in section 5.8, UBYTE4 presence the D3DDECLTYPE UBYTE4 stream data type may not be available. In this sit- uation, the indices are declared as D3DDECLTYPE D3DCOLOR, but they will need to be rescaled from the [0, 1] range, see chapter 9. If the blending indices are declared as D3DDECLTYPE SHORT2 or D3DDECLTYPE SHORT4, then no scaling is required.

6.7.3 Vertex Tweening

Some vertex blending effects can’t be achieved with vertex blending through ma- trices, but can be achived with a method called tweening.^2 The name derives from traditional film animation where a character is drawn with a beginning and ending pose by one animator and another animator drew the “in between” frames that move the character from one pose to another. The fixed-function pipeline provides vertex tweening in a similar fashion to the approach used in film animation. A device supports Vertex tweening when the D3DVTXPCAPS TWEENING bit of D3DCAPS9::VertexProcessingCaps is set.

#define D3DVTXPCAPS_TWEENING 0x00000040L

Each vertex is defined with two positions, and optionally two normals. The two components P 1 and P 2 are combined with a tween factor f before being transformed by a the world transformation matrix M.

P ′^ = [(1 − f )P 1 + f P 2 ]M #»n ′ (^) = [(1 − f ) #»n 1 +^ f^ #»n 2 ](M

− 1 )T

Vertex tweening is enabled when RS Vertex Blend is D3DVBF TWEENING. The tween factor f is supplied by RS Tween Factor and must be in the range [0, 1]. M is the D3DTS WORLD matrix. Fixed-function vertex tweening must be used with a vertex shader decla- ration; it cannot be used with an FVF code shader. Tweening also requires (^2) Also referred to as “morphing”, from the word metamorphosis.

6.7. VERTEX BLENDING 215

that the vertex components for the second position and normal appear last in the vertex. The vertex declaration maps the stream data for P 1 and P 2 to the position usage semantic for usage indices zero and one, respectively. If normals are present, both n 1 and n 2 must be present. n 1 and n 2 are mapped to normal usage semantic for usage indices zero and one, respectively. With vertex tweening, each vertex moves independently of all other vertices, while with vertex blending, they all move under the influence of the same set of transformations. While vertex tweening cannot introduce or remove vertices, vertices can be made coincident to produce degenerate primitives that are not rasterized.

6.7.4 Blending Within Device Limitations

Vertex blending is a powerful technique, but may not be supported natively on the hardware. The device may not have hardware vertex processing, or it might not support more than a single world transformation matrix, or it might not support indexed vertex blending, or it might support too few matrices in the matrix palette. However, even a model with many joints and vertex blending transformations doesn’t use all the transformations for every triangle. Usually it is possible to partition the model into groups of primitives, where all primitives in a group use at most the number of blend matrices supported on the device. For each group, the blend matrices are set appropriately. If a group of primitives uses more blend matrices than are supported in hardware, you can eliminate the transformation with the smallest contribution to the final blend and renormalize the remaining weights. For every primitive in the group find the blend transformation k that contributes least to all the primitives in the group. Remove βk from each vertex for each primitive in the group, and compute new blend weights by renormalizing the remaining weights.

β i′ =

βi 1 − βk

Another approach to device vertex blending limitations is to change the way vertex blending is performed. Software vertex processing fully supports vertex blending and can be used in cases where hardware vertex processing is insuffi- cient. A programmable vertex shader can perfrom vertex blending of arbitrary complexity, limited only by vertex shader capabilities and performance. The runtime uses CPU optimized code for software vertex processing of both fixed- function and programmable vertex shaders, making this a viable alternative for not much effort. When all else fails, an application can simply disable vertex blending when support is lacking, using only rigid transformations for orienting models. D3DX contains functions for converting an arbitrary skinned mesh into a mesh using vertex blending or indexed vertex blending, see chapter 19.

6.8. VERTEX FOG 217

f (z)

Scene Depth z

Fog Factor Functions ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??

××

× × × × × × × × × ×

××

××××××××××××××××××××××××××××××××××××

3 : D3DFOG EXP with d = 3. 33 +: D3DFOG EXP2 with d = 3. 33 2 : D3DFOG EXP with d = 6. 66 ×: D3DFOG EXP2 with d = 6. 66 ?: D3DFOG LINEAR with zs = 0 and ze = 1

Figure 6.8: Fog factor functions f (z) plotted versus scene depth z.

D3DFOG LINEAR f (z) =

1, z < zs ze−z ze−zs ,^ z^ ∈^ [zs, ze] 0, z > ze D3DFOG EXP f (z) = e−dz D3DFOG EXP2 f (z) = e−(dz)

2

Linear fog ramps from the object’s color to the fog color when the depth is in the range [zs, ze]. RS Fog Start and RS Fog End specify zs and ze, respectively. Exponential fog makes a smoother transition between the object’s color and the fog color. Exponential fog has a density parameter d that controls the falloff of the fog factor; d is defined by RS Fog Density. The fog distance is usually computed as the distance to a plane at depth z from the camera. For points distant from the center of the image, this is not the true distance between the camera and the point. Range-based fog computes the true distance from a point to the camera for use with the fog factor formula. If the D3DPRASTERCAPS FOGRANGE bit of D3DCAPS9::RasterCaps is set, the device supports range-based fog. Range-based fog is controlled with RS Range Fog Enable. The coordinate space of the depth value used in fog computations can vary. With vertex fog, the distance is measured in camera space before projection. The range of depth values is [zn, zf ] where zn and zf are the near and far planes

218 CHAPTER 6. VERTEX TRANSFORMATIONS

A

C B

A A A A A A A

AAU

¢Æ¢

#»n 4 æ - (^) − #»n 4

Front Back

Figure 6.9: Computing the plane normal for triangle ABC with the vertices {A, B, C}. The two vectors

AB and

AC are independent vectors in the plane of the triangle. The cross-product of these two vectors is perpendicular to the plane containing the triangle.

of the view volume, as described in chapter 7. With pixel fog, the distance is measured either in the unit interval [0, 1] with Z buffering or in camera space with W buffering. W buffering is an alternative to Z buffering. Both are dis- cussed in chapter 14. The D3DPRASTERCAPS ZFOG and D3DPRASTERCAPS WFOG bits of D3DCAPS9::RasterCaps indicate that the device supports Z buffer fog and W buffer fog, respectively. With fixed-function vertex processing, application supplied fog factors are stored in the alpha channel of the specular color vertex component. With pro- grammable vertex processing, the application is free to store arbitrary per-vertex fog data in the data stream, or generate it entirely within the shader from other vertex data such as position, see chapter 9.

6.9 Face Culling

Most models have about half of their triangles facing the viewer. When the triangle is positioned relative to the viewer, the triangle’s normal vector points towards the viewer if the triangle is visible. The surface normal points away from the viewer when the triangle faces away from the viewer. Triangles facing away from the viewer are “back-facing” triangles. We can use the relative orientation of the triangle normal to cull triangles from further processing. This avoids all the rasterization processing for triangles that won’t be seen. Face culling uses the normal to the plane containing the triangle. Direct3D does not use the surface normal vertex component for culling. Face culling is a property of the triangle, not the smooth surface approximated by the triangle. The normal is computed from the triangle’s vertices. Any plane normal vector is the cross-product of two linearly independent vectors in the plane. “Linearly independent” just means that the two vectors shouldn’t point in the same di- rection, or exactly opposite to each other. The triangle ABC in figure 6.9 has vertices A(xa, ya, za), B(xb, yb, zb) and C(xc, yc, zc), in clockwise order. The vectors

AB and

AC satisfy the cross-product condition. Their cross-product