Upload
dangngoc
View
216
Download
0
Embed Size (px)
Citation preview
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 1 of 51
Appendix D Selected Solutions
Chapter 1
1. Let and . Perform the following computations and draw the vectors relative to a 2D coordinate system.
a) b)
c)
d) Solution:
a)
b)
c)
d) 3. This exercise shows that vector algebra shares many of the nice properties of real numbers (this is not an exhaustive list). Assume , , and
. Also assume that and are scalars. Prove the following vector
properties. a) (Commutative Property of Addition) b) (Associative Property of Addition) c) (Associative Property of Scalar Multiplication) d) (Distributive Property 1) e) (Distributive Property 2) Solution:
a)
b)
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 2 of 51
c)
d)
e)
5. Let and . Normalize and . Solution:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 3 of 51
7. Is the angle between and orthogonal, acute, or obtuse? a) , b) , c) ,
a) b) c)
9. Let , , and . Also let and be scalars.
Prove the following dot product properties. a) b) c) d) e) Solution:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 4 of 51
11. Let . Decompose the vector into the sum of two orthogonal vectors, one parallel to and the other orthogonal to . Also, draw the vectors relative to a 2D coordinate system. Solution:
13. Let the following points define a triangle relative to some coordinate system: , , and . Find a vector orthogonal to this triangle. Hint: Find two vectors on two of the triangle’s edges and use the cross product. Solution:
15. Prove that gives the area of the parallelogram spanned by and ; see Figure below.
[Figure D.1: Parallelogram spanned by two 3D vectors and ; the parallelogram has base
and height .] Solution:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 5 of 51
The area is the base times the height:
Using trigonometry, the height is given by . This, along with the application of Exercise 14, we can conclude:
17. Prove that the cross product of two nonzero parallel vectors results in the null vector; that is, . Hint: Just use the cross product definition. Solution:
Chapter 2
1. Solve the following matrix equation for :
.
Solution:
3. Compute the transpose of the following matrices:
a) , b)
, c)
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 6 of 51
Solution:
5. Show that
Solution: Let be an arbitrary row in . By definition of matrix multiplication, we know that the ith row is given by
However, by definition of matrix multiplication, this is equal to the vector-matrix product . That is,
Since was an arbitrary row, we just substitute to complete the proof:
7. Prove that the cross product can be expressed by the matrix product:
Solution:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 7 of 51
9. Let
. Is
the inverse of ?
Solution:
Since we can conclude that is not the inverse of .
11. Find the inverse of the following matrices:
Solution:
For the matrix, we have the formula:
To verify:
For the matrix, we use the formula:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 8 of 51
The cofactor matrix is:
So
and
To verify:
13. Show that , assuming is invertible. Solution:
Therefore, is the inverse of .
15. Prove that the 2D determinant
gives the signed area of the parallelogram spanned
by and . The result is positive if can be rotated counterclockwise to
coincide with by an angle , and negative otherwise.
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 9 of 51
[Figure D.2: Parallelogram spanned by two 2D vectors and ; the parallelogram has base
and height .]
Solution:
The area is given by base times height:
Squaring both sides:
Taking the square root:
17. Let
,
, and
. Show that .
This shows that matrix multiplication is associative for matrices. (In fact, matrix multiplication is associative for general sized matrices, whenever the multiplication is defined.) Solution: For matrices, we will just do the computations:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 10 of 51
Comparing the terms element-by-element, we see
Chapter 3
1. Let be defined by . Is a linear transformation? If it
is, find its standard matrix representation.
Solution:
If is linear, then we must have:
1. 2.
Let and .
So ; therefore is not a linear transformation.
3. Assume that is a linear transformation. Further suppose that , , and . Find .
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 11 of 51
Solution:
We are given that is a linear transformation and its behavior on the standard basis vectors
. Therefore, the standard matrix representation of is:
Then
5. Build a rotation matrix that rotates along the axis .
Solution:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 12 of 51
7. Build a single transformation matrix that first scales 2 units on the x-axis, -3 units on the y-
axis, and keeps the z-dimension unchanged, and then translates 4 units on the x-axis, no units on
the y-axis, and -9 units on the z-axis.
Solution:
Then the desired transformation matrix is obtained by the product
9. Redo Example 3.2, but this time scale the square 1.5 units on the x-axis, 0.75 units on the y-
axis, and leave the z-axis unchanged. Graph the geometry before and after the transformation to
confirm your work.
Solution:
The corresponding scaling matrix is:
Now to actually scale (transform) the square, we multiply both the minimum point and
maximum point by this matrix:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 13 of 51
[Figure D.3: Scaling transform.]
11. Redo Example 3.4, but this time translate the square -5 units on the x-axis, -3.0 units on the
y-axis, and 4.0 units on the z-axis. Graph the geometry before and after the transformation to
confirm your work.
Solution:
The corresponding translation matrix is:
Now to actually translate (transform) the square, we multiply both the minimum point and
maximum point by this matrix:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 14 of 51
[Figure D.4: Translation transform.]
13. Prove that the rows of are orthonormal. For a more computational intensive exercise, the
reader can do this for the general rotation matrix (rotation matrix about an arbitrary axis), too.
Solution:
Let , , and .
First we show all the rows are unit length:
Next we show the rows are mutually orthogonal:
14. Prove the matrix is orthogonal if and only if .
Solution:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 15 of 51
For concreteness, we will prove for matrices since 3D rotation matrices are the only kind
of orthogonal matrices we care about in this book. However, the same argument we present
generalizes to matrices.
Suppose is a orthogonal matrix:
Then, by the definition of matrix multiplication . But, because is orthogonal
we have that
It is true that only for the diagonal elements of the matrix. Thus the resulting matrix has
1’s along the diagonal and zeros everywhere else. But this is exactly the identity matrix. Hence,
A similar argument shows . Therefore, we must have that .
Now suppose that . In particular, this implies that . In turn, this means:
Therefore, the row vectors or are mutually orthogonal and unit length; thus, is orthogonal.
15. Compute:
and
Does the translation translate points? Does the translation translate vectors? Why does it not
make sense to translate the coordinates of a vector in standard position?
Solution:
The translation translates points.
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 16 of 51
The translation does not translate vectors. Translation does not make sense for vectors because a
vector only describes direction and magnitude, independent of location.
17. Suppose that we have frames A and B. Let and represent a
point and force, respectively, relative to frame A. Moreover, let ,
,
, and describe frame A with coordinates relative to
frame B. Build the change of coordinate matrix that maps frame A coordinates into frame B
coordinates, and find and . Draw a picture on graph paper to verify
that your answer is reasonable.
Solution:
From Equation 3.9, the change of coordinate matrix is:
Then to transform points and vectors from frame A into frame B we multiply the frame A coordinate
vectors by the matrix:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 17 of 51
[Figure D.5: Change of coordinates from frame A to frame B.]
19. Consider the triangle defined by the points , , and . Graph the following points:
a)
b)
c)
d)
e)
f)
What is special about the point in part (a)? What would be the barycentric coordinates of and
the point in terms of ? Can you make a conjecturer about where the point
will be located relative to the triangle if one of the barycentric coordinates is negative?
Solution:
a)
b)
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 18 of 51
c)
d)
e)
f)
The point in part (a) is the centroid. so the barycentric coordinates are
.
so the barycentric coordinates are
. For negative
barycentric coordinates, the points lie outside the triangle.
[Figure D.6: Plotting barycentric coordinates.]
21. Consider Figure 3.16. A common change of coordinate transformation in computer graphics
is to map coordinates from frame A (the square ) to frame B (the square where the
y-axes aims opposite to the one in Frame A). Prove that the change of coordinate transformation
from Frame A to Frame B is given by:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 19 of 51
[Figure D.7: Change of coordinates from frame A (the square ) to frame B (the square
where the y-axes aims opposite to the one in Frame A)]
Solution:
From Equation 3.9, the change of coordinate matrix is:
That is, we need to describe the coordinate system of frame A (origin and axes) with coordinates relative
to frame B. (We use homogeneous coordinates.) The origin in frame A is the center of the square, which
has coordinates . The x-axis of frame A aims in the same direction as frame B, and,
along this axis, one unit in frame A is half a unit in frame B; therefore, . The y-axis of
frame A aims in the opposite direction as frame B, and along this axis, one unit in frame A is half a unit in
frame B; therefore, . Since this is a 2D problem, we will just assume that the z-axes
are the same in both coordinate systems; therefore, . Substituting these numbers into the
above yields the desired transformation matrix:
23. Consider the transformation that warps a square into a parallelogram given by:
Find the standard matrix representation of this transformation, and show that the determinant of
the transformation matrix is equal to the area of the parallelogram spanned by and .
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 20 of 51
[FigD.8.bmp: Transformation that maps square into parallelogram.]
Solution:
The area of the parallelogram spanned by by and is given by:
The cross product is not defined in 2D, but we can do a trick and augment to 3D space with
so that we can use the cross product. Note that using does not affect the magnitude
or direction of the vectors.
Also see Chapter 2, Exercise 15. So observe that the area of the parallelogram (square) defined
by the standard basis vectors and is 1, and the area of the parallelogram spanned by and
is 5. Therefore, the transformation changed the volume (or area in 2D) from 1 to 5 when it
warped it from a unit square to the parallelogram spanned by and .
25. A rotation matrix can be characterized algebraically as an orthogonal matrix with
determinant equal to 1. If we reexamine Figure 3.7 along with Exercise 24 this makes sense; the
rotated basis vectors , , and are unit length and mutually orthogonal; moreover,
rotation does not change the size of the object, so the determinant should be 1. Show that the
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 21 of 51
product of two rotation matrices is a rotation matrix. That is, show (to show is orthogonal), and show .
Solution:
Using the fact that :
27. Find a scaling, rotation, and translation matrix whose product transforms the line segment
with start point and endpoint into the line segment with length 2,
parallel to the vector , with start point .
Solution:
The line segment currently aims along the z-axis with a length of 1. To make it have a length of
2, we first scale it 2-units on the z-axis. In order to rotate the line segment so that it is parallel to
we need to rotate by an angle in the plane that contains the vectors and
(see Figure). The axis of rotation is given by . The angle we need to rotate is given by the angle between the vectors and :
Finally, we must apply a translation .
[FigD.9: To rotate so that it aims in the same direction as , we must rotate by an angle
about the axis .]
Chapter 5
1. Construct the vertex and index list of a pyramid, as shown in Figure 5.35.
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 22 of 51
Solution:
[FigD.10: Vertices of the pyramid.]
Vertex vertices[5] = {v0, v1, v2, v3, v4};
UINT indices[] = {
0, 1, 2,
1, 3, 2,
1, 4, 0,
0, 4, 2,
2, 4, 3,
3, 4, 1
};
3. Relative to the world coordinate system, suppose that the camera is positioned at
and looking at the point . Compute the view matrix assuming describes the “up” direction in the world.
Solution:
Figure 5.20 shows the setup. Let , and . The
direction the camera is looking is given by:
This vector describes the local z-axis of the camera. A unit vector that aims to the “right” of is
given by:
This vector describes the local x-axis of the camera. Finally, a vector that describes the local y-
axis of the camera is given by:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 23 of 51
The view matrix is:
You can verify the answer by writing the following code and inspecting the matrix elements:
XMMATRIX K = XMMatrixLookAtLH(
XMLoadFloat3(&XMFLOAT3(-20.0f, 35.0f, -50.0f)),
XMLoadFloat3(&XMFLOAT3(10.0f, 0.0f, 30.0f)),
XMLoadFloat3(&XMFLOAT3(0.0f, 1.0f, 0.0f)));
5. Suppose that the view window has height . Find the distance from the origin the view
window must be to create a vertical field of view angle .
Solution:
7. Suppose that you are given the following perspective projection matrix with fixed :
Find the vertical field of view angle the aspect ratio , and the near and far plane values that
were used to build this matrix in terms of . That is, solve the following equations:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 24 of 51
Solving these equations will give you formulas for extracting the vertical field of view angle
the aspect ratio , and the near and far plane values from any perspective projection matrix of the
kind described in this book.
Solution:
Dividing the second equation by the first equation yields:
The second equation implies:
We solve the third equation for :
Now we plug into the fourth equation and solve for :
Now,
In summary,
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 25 of 51
8. For projective texturing algorithms, we multiply an affine transformation matrix after the
projection matrix. Prove that it does not matter if we do the perspective divide before or after
multiplying by . Let, be a 4D vector, be a projection matrix, be a affine
transformation matrix, and let a subscript denote the -coordinate of a 4D vector, prove:
Solution:
Using the properties of matrix algebra, we have:
But is given as an affine transformation matrix, which means it does not modify the w-
coordinate. Hence, we have . Hence,
10. Let be the coordinates of a point in view space, and let be the
coordinates of the same point in NDC space. Prove that you can transform from NDC space to
view space in the following way:
Explain why you need the division by . Would you need the division by if you were
transforming from homogeneous clip space to view space?
Solution:
Recall from Equation 5.1 and §5.6.3.5 that:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 26 of 51
where are view space coordinates.
The steps to go from view space to NDC space are multiplying by the projection matrix,
followed by a division by . Therefore, to transform back from NDC space to view space,
we multiply by the inverse projection matrix followed by a division by (which is
equivalent to multiplying by ).
If you are given points in homogeneous clip space (before the divide by ), then you
do not need to divide by in the inverse transformation. In homogeneous clip space, the point has coordinates :
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 27 of 51
13. Consider the 3D shear transform given by . This
transformation is illustrated in Figure 5.37. Prove that this is a linear transformation and has the
following matrix representation:
[FigD.11: The x- and y-coordinates sheared by the z-coordinate. The top face of the box lies in
the plane. Observe that the shear transform translates points in this plane.]
Solution:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 28 of 51
So is a linear transformation. To find the matrix representation, we apply it to the standard
basis vectors:
The matrix representation is found by inserting the above three vectors into the rows of a matrix:
Chapter 8
6. Let , , and be the vertices of a 3D triangle with respective texture coordinates , ,
and . Recall from §8.2 that for an arbitrary point on a 3D triangle where , its texture coordinates are found
by linearly interpolating the vertex texture coordinates across the 3D triangle by the same parameters:
a) Given and , , and , solve for in terms of and (Hint: Consider the vector
equation . b) Express as a function of and ; that is, find a formula . c) Compute and and give a geometric interpretation of what these vectors mean.
Solution:
a) Let , , and :
From Example 2.10, the inverse of a matrix
is given by:
We can solve for by multiplying by the inverse:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 29 of 51
In other words, given the texture coordinates of a point on a triangle, we can solve for the
parametric coordinates that yield the 3D point corresponding to the texture point .
b) Part (a) showed that and . Therefore: can be expressed in terms of :
c) First, note that:
Now the partial derivatives are given by:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 30 of 51
where we use the notation:
Similarly,
In summary,
Compare this result to the one derived in §18.3; we have simple derived the forumla in §18.3 in a
different way.
The 3D vector
gives us the “velocity” we move in 3D space when we move in the u-
direction in texture space. Put another way, it tells us the direction of the texture space u-axis in
3D space. Likewise, the 3D vector
gives us the “velocity” we move in 3D space when we
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 31 of 51
move in the v-direction in texture space (i.e., the direction of the texture space v-axis in 3D
space).
7. See “TexturedColumns” on the DVD.
Chapter 10
2. Prove that
by doing the matrix multiplication for each
component, as was done in §10.5.1 for directional lights.
Solution:
Let . For , the ith coordinate of is given by:
and the fourth coordinate is given by:
Doing the homogeneous divide we obtain:
But this is exactly the ith coordinate of
, so
7. See “DepthStencilBolt” on the DVD.
Chapter 11
2. See “GSSubdivision” on the DVD.
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 32 of 51
Chapter 12
5. See “CSGpuWavesDemo” on the DVD.
Chapter 14
1. Given the world space axes and origin in world coordinates: , , and , and the view space axes and origin in world coordinates:
, , and , derive the view matrix
form
using the dot product. (Remember, to find the change of coordinate matrix from world space to
view space, you just need to describe the world space axes and origin with coordinates relative to
view space. Then these coordinates become the rows of the view matrix.)
Solution:
[Figure D.12: Finding the world space coordinates in view space.]
All the given vectors have coordinates in world space, and all the axis vectors are unit vectors.
However, using the dot product, we can obtain view space coordinates of the world space. From
the figure, we see:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 33 of 51
Augmenting to homogeneous coordinates and putting these vectors into the rows of a matrix
yields the view matrix.
Chapter 15
2. The plane equations in NDC space take on a very simple form. All points inside the view
frustum are bounded as follows:
In particular, the left plane equation is given by and the right plane equation is given by
in NDC space. In homogeneous clip space before the perspective divide, all points inside
the view frustum are bounded as follows:
Here, the left plane is defined by and the right plane is defined by . Let
be the view-projection matrix product, and let be a point in world space
inside the frustum. Consider to
show that the inward facing frustum planes in world space are given by:
Left
Right
Bottom
Top
Near
Far
[Notes]
a) We ask for inward facing normals. That means a point inside the frustum has a positive
distance from the plane; in other words, for a point inside the frustum.
b) Note that , so the above dot product formulas do yield plane equations of the form
.
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 34 of 51
c) The calculated plane normal vectors are not unit length; see Appendix C for how to normalize
a plane.
[/Notes]
Solution:
We assume is a point inside the frustum. Therefore, its coordinates
in homogeneous space are bounded as
follows:
We can make substitutions to find the plane bounds in world space.
Left Plane:
That is, is in the positive half-space of the world space plane .
Right Plane:
That is, is in the positive half-space of the world space plane .
Bottom Plane:
That is, is in the positive half-space of the world space plane .
Top Plane:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 35 of 51
That is, is in the positive half-space of the world space plane .
Near Plane:
That is, is in the positive half-space of the world space plane .
Far Plane:
That is, is in the positive half-space of the world space plane .
4. An OBB can be defined by a center point , three orthonormal axis vectors , , and
defining the box orientation, and three extent lengths , , and along the box axes , ,
and , respectivey, that give the distance from the box center to the box sides.
a) Consider Figure 15.13 (which shows the situation in 2D) and conclude the projected “shadow”
of the OBB onto the axis defined by the normal vector is , where
b) In the previous formula for , explain why we must take the absolute values instead of just
computing ?
c) Derive a plane/OBB intersection test that determines if the OBB is in front of the plane,
behind the plane, or intersecting the plane.
d) An AABB is a special case of an OBB, so this test also works for an AABB. However, the
formula for simplifies in the case of an AABB. Find the simplified formula for for the
AABB case.
Solution:
b) If one of the terms is negative, the sum will not give the
“radius” of the OBB. To explain it another way (in 2D), let the vectors from the box center to
the corners be given by:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 36 of 51
[Figure D.13: The corner vectors. Note that these are not the corner points, but the vectors from
the center point to the corner points.]
The OBB “radius” is the corner vector that gives the largest projection onto :
The maximum will be the one where all terms are positive (or all terms are negative) so that
there is no cancellation between the terms. In other words, the maximum will be equal to:
The same argument generalizes to 3D where there are 8 corner vectors.
c) The signed distance from the center of the OBB to the plane is . If then
the sphere intersects the plane. If then the OBB is behind the plane. If then the
OBB is in front of the plane and the sphere intersects the positive half-space of the plane.
d) In the case of an AABB, , , and ; therefore:
Chapter 18
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 37 of 51
3. See “DisplacementMappedWaves” on the DVD.
Chapter 21
6. Derive the matrix that maps the box . This is
an “off center” orthographic view volume (i.e., the box is not centered about the view space
origin). In contrast, the orthographic projection matrix derived in §21.2 is an “on center”
orthographic view volume.
Solution:
For all three coordinates, we need to remap an interval onto another interval. We can solve this
problem generally once, and then apply it to each coordinate. We want to map . We assume the mapping takes the form (i.e., a scaling and translation). We have
the conditions and , which allow us to solve for and :
The first equation implies . Plugging this into the second equation we get:
And so:
Therefore,
Applying this formula to our specific intervals, we obtain the transformations:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 38 of 51
Or in terms of matrices:
7. In Chapter 16 we learned about picking with a perspective projection matrix. Derive picking
formulas for an off-centered orthographic projection.
Solution:
Let be the picked point in screen space. Inverting the viewport transformation, we get
the corresponding point in NDC space:
The orthographic projection matrix transforms the view volume from view space to NDC space.
The off-centered orthographic projection transformation is:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 39 of 51
In particular, this gives the two equations:
We can then solve for the view space coordinates in terms of :
Then our picking ray in view space is given by . Note that in an
orthographic projection, all the rays are parallel to the z-axis direction .
Sample code:
Ray3 CalcWorldPickRay(XMMATRIX P, Point s, Size viewport)
{
float sx = (float)s.X;
float sy = (float)s.Y;
float w = (float)viewport.Width;
float h = (float)viewport.Height;
float x = (2.0f*sx/w - 1.0f)/P(0,0) - P(3,0)/P(0,0);
float y = (-2.0f*sy/h + 1.0f)/P(1,1) – P(3,1)/P(1,1);
Ray3 pickRay;
pickRay.Origin = XMFLOAT3(x, y, 0.0f);
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 40 of 51
pickRay.Direction = XMFLOAT3(0.0f, 0.0f, 1.0f);
return pickRay;
}
Note: This works for an off-centered orthographic projection matrix and a centered one since a
centered one is just a special case of the off-centered one.
Chapter 24
4. Rotate the vector using complex number multiplication.
Solution:
We can verify this by seeing if we get the same answer with a rotation matrix:
6. Let . Show .
Solution:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 41 of 51
7. Let be a matrix. Prove that and if and only if
. That is, if and only if is a rotation matrix. This gives us a way of testing if
a matrix is a rotation matrix.
Solution:
Let
and suppose and .
along with
imply the following equations:
Now,
Therefore,
Both terms are positive; hence, we must have:
and
So our matrix has the form:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 42 of 51
Finally, because there exists a such that and , and
we get our result:
Now suppose
. Then
Therefore, and . In other words, all rotation matrices have determinant 1
and are orthogonal, and every orthogonal matrix with determinant 1 must be a rotation matrix.
8. Let and be quaternions. Perform the indicated quaternion
operations.
a.
b.
c.
d. e. f.
g.
h.
i.
j.
Solution:
a. b.
c.
d. e.
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 43 of 51
f.
g.
h.
i.
j.
XMVECTOR p = XMVectorSet(1.0f, 2.0f, 3.0f, 4.0f);
XMVECTOR q = XMVectorSet(2.0f, -1.0f, 1.0f, -2.0f);
XMVECTOR a = p + q;
XMVECTOR b = p - q;
XMVECTOR c = XMQuaternionMultiply(q,p );
XMVECTOR d = XMQuaternionConjugate(p);
XMVECTOR e = XMQuaternionConjugate(q);
XMVECTOR f = XMQuaternionMultiply(XMQuaternionConjugate(p),p);
XMVECTOR g = XMQuaternionLength(p);
XMVECTOR h = XMQuaternionLength(q);
XMVECTOR i = XMQuaternionInverse(p);
XMVECTOR j = XMQuaternionInverse(q);
cout << "p + q = " << a << endl;
cout << "p - q = " << b << endl;
cout << "pq = " << c << endl;
cout << "p* = " << d << endl;
cout << "q* = " << e << endl;
cout << "p*p = " << f << endl;
cout << "||p|| = " << g << endl;
cout << "||q|| = " << h << endl;
cout << "invP = " << i << endl;
cout << "invQ = " << j << endl;
10. Write the unit quaternion
in polar notation.
11. Find the unit quaternion that rotates about the axis .
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 44 of 51
The division by 2 is to compensate for the in Equation 24.3 because we want to rotate by the
angle , not .
XMVECTOR p = XMQuaternionRotationAxis(
XMVectorSet(1.0f, 1.0f, 1.0f, 0.0f),
XMConvertToRadians(45.0f));
cout << "p = " << p << endl;
12. Find the unit quaternion that rotates about the axis .
The division by 2 is to compensate for the in Equation 24.3 because we want to rotate by the
angle , not .
XMVECTOR p = XMQuaternionRotationAxis(
XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f),
XMConvertToRadians(60.0f));
cout << "p = " << p << endl;
13. Let
and
. Compute
and verify it is a unit
quaternion.
Solution:
It is easy to verify that . The angle between the quaternions is given by:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 45 of 51
The interpolated quaternion is easily seen to be unit length:
.
Observe that rotates about the axis by an angle
,
that rotates about the axis by an angle
, and that
rotates about the axis by an angle
. With the
interpolation parameter being the midpoint
, this makes sense, as is right in the middle
between and .
XMVECTOR p = XMVectorSet(0.5f, 0.0f, 0.0f, sqrt(3.0)/2.0f);
XMVECTOR q = XMVectorSet(sqrt(3.0)/2.0f, 0.0f, 0.0f, 0.5f);
XMVECTOR r = XMQuaternionSlerp(p, q, 0.5f);
cout << "r = " << r << endl;
15. Prove that
Solution:
Let .
Because the quaternion has zero vector part, we convert it to a real number as discussed in
§24.2.4. Similarly, it can be shown
.
18. Prove the following properties:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 46 of 51
a. b. c. for
d.
Solution:
Let and so that and .
a)
b)
c)
d)
19. Prove
algebraically.
Solution:
The key component is to apply the trig identity:
Now,
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 47 of 51
Appendix C
1. Let be a ray relative to some coordinate system. Plot the points on the
ray at t = 0.0, 0.5, 1.0, 2.0, and 5.0.
Solution:
[FigD.14: Plotting points on a line]
3. For each part, find the vector line equation of the line passing through the two points.
a) b)
Solution:
a)
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 48 of 51
b)
5. Let be a line. Find the distance from the following points to the
line:
Solution:
Apply the formula from the previous exercise.
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 49 of 51
7. Let
be a plane. Define the locality of the following points relative to the
plane: , , and .
Solution:
The plane equation is:
. Plugging these points into the left-hand side of the
equation gives:
9. Let
be a plane. Find the reflection of the point about the plane.
Solution:
From the previous exercise, we know that is a point on the plane.
Because
, we have that
Now we can apply the formula from §C.4.9:
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 50 of 51
10. Let
be a plane, and let be a ray. Find the
point at which the ray intersects the plane. Then write a short program using the
XMPlaneIntersectLine function to verify your answer.
Solution:
We plug into the plane equation to verify it indeed lies on the plane:
The code is given by:
#include <windows.h> // for FLOAT definition
#include <xnamath.h>
#include <iostream>
using namespace std;
// Overload the "<<" operators so that we can use cout to
// output XMVECTOR objects.
ostream& operator<<(ostream& os, FXMVECTOR v)
{
XMFLOAT4 dest;
XMStoreFloat4(&dest, v);
os << "(" << dest.x << ", " << dest.y << ", " << dest.z <<
", " << dest.w << ")";
return os;
}
int main()
Appendix D Selected Solutions, Introduction to 3D Game Programming with DirectX 11, by
Frank D. Luna
Page 51 of 51
{
XMVECTOR p0 = XMVectorSet(-1.0f, 1.0f, -1.0f, 1.0f);
XMVECTOR u = XMVectorSet(1.0f, 0.0f, 0.0f, 0.0f);
// Construct plane by specifying its (A, B, C, D)
// components directly.
float s = 1.0f / sqrtf(3);
XMVECTOR plane = XMVectorSet(s, s, s, -5.0f);
// Function expects a line segment and not a ray; so we just
// truncate our ray at p0 + 100*u to make a line segment.
XMVECTOR isect = XMPlaneIntersectLine(plane, p0, p0 + 100*u);
cout << isect << endl;
return 0;
}
The output is (in homogeneous coordinates so for points):
(8.66025, 1, -1, 1)
Press any key to continue . . .
We note , so the computer result agrees with our calculation.