Introduction to 3D Game Programming with DirectX 90c: A Shader Approach Part I Solutions Note : Please email to frank@moon-labscom if ou find an errors Note : Use onl after ou have tried, and struggled with, the problems ourself Chapter Vector Algebra Let u (,, 0) and v ( 3, 4, 0) Compute u+ v and draw the vectors relative to a coordinate sstem v, u v, u+ v, and u + v u+ v,, 0 + 3, 4, 0 + 3, + 4, 0 + 0 4,, 0 u v,, 0 3, 4, 0 3, 4, 0 0, 6,0 u+,,0 + 3, 4,0 + 3,4+,0+ 0 7,,0 u+ v,,0 + 3, 4,0 + 3, 4+ 4,0+ 0, 8,0 Frank Luna Page 6/7/006
Let u (,, 4) and v ( 3, 4, ) Normalize u and v u n v n u,, 4 (,, 4) 4,, u + + 4 v 3, 4, ( 3, 4, ) 3 4,, v 3 + 4 + 6 6 6 6 3 Show u u has a length of one unit (Hint: Compute the length of u u ) he length of u u ( u u, u u, uz u ) is given b: u u u u u z + u + uz + u + uz u + + u u u u u u 4 Is the angle between u and v orthogonal, acute, or obtuse? a u (,, ), v (,, ) b u (,, 0), v (,, 0) u,, v 3,, 0 c, Use the geometric properties of the dot product given on page 0: (,, ) (,, ) + + 6 > 0 acute angle (,, 0) (,, 0) ( ) + + 0 0 0 orthogonal (,, ) ( 3,, 0) ( ) 3+ ( ) + ( ) 0 4 < 0 obtuse angle 5 Let u (,, 4) and v ( 3, 4, ) Find the angle θ between u and v Use Equation 4: Frank Luna Page 6/7/006
u v u v cosθ u v cosθ u v u v θ cos u v (,, 4) ( 3, 4,) θ cos + + 4 3 + 4 + 6 4+ 4 θ cos 6 6 θ cos 546 θ 0488 6 Let u ( u, u, uz), v ( v, v, vz), and w ( w, w, wz) 3 properties are true for : a u v v u b u ( v + w ) u v + u w k u v ku v u kv d v v v e 0 v 0 c (Hint: Just use the definition, for eample, v v v v + v v + v v v + v + v v + v + v v z z z z, show that the following ) a) b) u v u u u v v v (,, z) (,, z) uv + uv + uv z z vu + vu + vu z z ( v, v, vz) ( u, u, uz) v u Frank Luna Page 3 6/7/006
u v+ w u u u v v v + w w w (,, ) (,, ) (,, ) z z z ( u, u, uz) ( v + w, v + w, vz + wz) u ( v + w ) + u ( v + w ) + u ( v + w ) z z z uv + uw + uv + uw + uv + uw z z z z ( uv uv uv z z) ( uw uw uw z z) + + + + + u v+ u w c) k u v k u v u v u v ( ) ( + + z z) ku v + ku v + ku v ( ku ) z z ku v + ku v + ku v v z z k u v k u v u v u v ( ) ( + + z z) ku v + ku v + ku v z z u kv + u kv + u kv u z z ( kv) d) v v vv + vv + vzvz v + v + vz ( v + v + vz ) ( v ) 0 v 0,0,0 v, v, v 0v + 0v + 0v 0 e) z z 7 Use the Law of Cosines ( c a + b abcosθ, where a, b, and c are the lengths of the sides of a triangle and θ is the angle between sides a and b) to show uv + uv + uv u v cosθ (Hint: Draw a picture and set c u v, a u, z z and b v, and use the dot product properties from the previous eercise) Frank Luna Page 4 6/7/006
he figure shows the setup, then b the Law of Cosines: c a + b ab cosθ u v u + v u v cosθ ( ) ( u v) u v u v u v u + v u v cosθ u u u v + v v u + v u v cosθ u u v + v u + v u v cosθ u v u v cosθ cosθ uv + uv + uv u v cosθ z z 8 Let v ( 4, 3, 0) and n ( 5, 5,0) Show that n is a unit vector and find the orthogonal projection, p, of v on n hen find a vector w orthogonal to n such that v p+ w (Hint: Draw the vectors for insight, what does v p look like?) We have, n 5 + 5 + 0 + 4 5 5 So n is a unit vector o find the projection, we use the equation below Figure 8: p v n n ( 4, 3, 0) ( 5, 5, 0) 8 3 ( 5 5) n + n If v p+ w 5 (,,0) 5 5 5 (,,0) 5 5 n, then ( 0 5 4,3,0,,0,,0 ) (,,0 ) ( w v p, 4,0 ) Moreover, because to n 9 Let u (,, 4) 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5,,0,,0 + 0, we have that w is orthogonal and v ( 3, 4, ), and show wu 0 and wv 0 Find w u v Frank Luna Page 5 6/7/006
Appl Equation 5: u v (,, 4) ( 3, 4,) ( 7,4, 5) ( 4 4,4 3, 4 3) wu 7,4, 5,, 4 7 + 4 + 5 4 0 wv 7,4, 5 3, 4, 7 3 4 4 + 5 0 0 Let the following points define a triangle relative to some coordinate sstem: A 0, 0, 0 B 0,, 3 C 5,, 0 Find a vector orthogonal to this triangle,, and (Hint: Find two vectors on two of the triangle s edges and use the cross product) he two vectors on the edges of the triangle are: u B A ( 0,, 3) v C A 5,, 0 hen a vector orthogonal to this triangle is given b: u v ( 0,, 3) ( 5,, 0) ( 3,5, 5) Suppose that we have frames A and B Let p (,, 0) and q (,, 0) represent a point and force, respectivel, relative to frame A Moreover, let O ( 6,, 0), u (,,0), v (,,0), and w ( 0, 0,) describe p,, z q,, z that describe the frame A relative to frame B Find point and force relative to frame B B A and pb u v+ 0w+ O + 6, +,0,,0 q u+ v+ 0 w, +,0,,0 B Let p() t (, ) + t(, ) on the ra at t 00, 05, 0, 0, and 50 he points are: p t + p t + (, ) 0 (, ) (, ) () (, ) (, ) (, 3 ) 3 6 ( ) ( ) 3 B be a ra relative to some coordinate sstem Plot the points A Frank Luna Page 6 6/7/006
And the plot: p t + p t + () (, ) (, ) ( 3, ) () (, ) (, ) ( 5, 3) p() t (, ) + 5 (, ) (, 6) 3 Let p 0 and p define the endpoints of a line segment Show that the equation for a line segment can also be written as p ( t) ( t) p 0 + tp for [ ] 0, p t p + t p p p + tp tp t p + tp () ( ) 0 0 0 0 0 t 4 Rewrite the program in 8 twice; first using D vectors (D3DXVECOR) and second using 4D vectors (D3DXVECOR4) (Hint: Search the inde for these kewords in the DirectX SDK documentation: D3DXVECOR, D3DXVECOR4, D3DXVec, and D3DXVec4) We rewrite the program using D3DXVECOR; the other case is analogous #include <d3d9h> #include <iostream> using namespace std; // Overload the "<<" operators so that we can use cout to // output D3DXVECOR objects ostream& operator<<(ostream& os, D3DXVECOR& v) { os << "(" << v << ", " << v << ")"; return os; } int main() { // Using constructor, D3DXVECOR(FLOA, FLOA ); D3DXVECOR u(0f, 0f); Frank Luna Page 7 6/7/006
// Using constructor, D3DXVECOR(CONS FLOA *); float [] {-0f, 0f}; D3DXVECOR v(); // Using default constructor, D3DXVECOR(); D3DXVECOR a, b, c, d; // Vector addition: D3DXVECOR operator + a u + v; // Vector subtraction: D3DXVECOR operator - b u - v; // Scalar multiplication: D3DXVECOR operator * c u * 0; // u float length D3DXVecLength(&u); // d u / u D3DXVecNormalize(&d, &u); // s u dot v float s D3DXVecDot(&u, &v); cout << "u " << u << endl; cout << "v " << v << endl; cout << "a " << a << endl; cout << "b " << b << endl; cout << "c " << c << endl; cout << "d " << d << endl; cout << " u " << length << endl; cout << "u dot v " << s << endl; } return 0; Chapter Matri Algebra Let 3 0 0 0 0 0 0 0 0 0 S 0 0 0, 0 0 4 0 0 0 0 0 0 0 5 Compute the following matri products: S, S, us, u, and u [ ], and u( S) Just use the definition of matri multiplication (ie, Equation ): Does S S? Frank Luna Page 8 6/7/006
3 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 S 0 0 4 00 0 0 0 0 4 0 0 0 0 5 5 0 0 0 3 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 S 0 0 00 0 4 0 0 0 4 0 5 0 0 0 6 0 4 3 0 0 0 0 0 0 us [ ] [ 6 4 ] 0 0 4 0 0 0 0 0 0 0 0 0 0 u [ ] [ 4 6 0 ] 0 0 0 5 3 0 0 0 0 0 0 0 0 0 0 0 0 u S 8 3 3 0 0 4 00 0 0 0 0 0 5 S [ ] [ ] S v Show uv u, u, u z v u v v z he notation uv means the product of a 3 row vector u with a 3 column vector v so that the matri product is defined B the definition of matri multiplication we have that the product of a 3 matri with a 3 matri is a matri; we can think of a matri as just a scalar Appling Equation we obtain: v u, u, u z v uv uv uv + + z z uv + uv + uv z z u v v z his eercise simpl shows that we can epress a dot product in matri notation using matri multiplication Frank Luna Page 9 6/7/006
3 Using S and from Eercise, compute? Does S S? S,, and ( ) S and S What is S 3 0 0 0 3 0 0 0 0 0 0 0 0 0 S 0 0 4 0 0 0 4 0 0 0 0 0 0 0 Observe for this special matri, is said to b smmetric S S When a matri equals its transpose, the matri 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 5 0 0 0 ( S ) ( S ) ( ) 3 0 0 0 3 0 0 0 0 0 0 0 5 0 0 4 0 0 0 4 5 0 0 0 3 0 0 0 3 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 S 0 0 4 0 0 0 4 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 5 So if we take one transpose, we interchange the rows and columns If we take another transpose, we interchange the rows and columns again and end up back to where we started Frank Luna Page 0 6/7/006
S 0 0 3 0 0 0 3 0 0 0 0 5 0 0 0 0 0 5 0 0 0 0 4 0 0 0 4 0 0 0 0 0 0 0 0 0 ( S) 4 Using S and from Eercise, verif that D3DXMatriInverse to do the calculations) Using D3DXMatriInverse, we find that: S S (Use ( S ) 3 0 0 0 0 0 0, 0 0 0 4 5 3 4 S 3 0 0 0 0 0 0, 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 5 Using the definition of matri multiplication, we compute S 3 0 0 0 0 0 0 0 0 4 0 5 3 4 We see that indeed S S 5 Write the following linear combination as a vector-matri multiplication: v,,3 + 4 5,0, + 3,, 3 Observe: v + +,,3 4( 5,0, ) 3, (, 3) (, 4, 6) ( 0, 0, 4) ( 6, 6, 9) ( 8,, ) + + B Equation, we can write this linear combination as a vector-matri product: 3 v 3 [, 4, 3] 5 0 [ 8,, ] Frank Luna Page 6/7/006
6 Redo Eercise from Chapter using Equation 3 he idea here is to just epress the change of frame calculation b a matri equation, namel Equation 3 o change from frame A to frame B, we stick the frame A vectors, with coordinates relative to frame B in homogeneous coordinates, u, v, w, and O into the rows of a matri C hen, given the vector/point pa (,, z, w) that specifies a vector/point relative to a frame A, we obtain the same vector/point, identified b pb (,, z, w) relative to frame B, b performing the vector-matri multiplication: pb pc A From Eercise of Chapter, we have, in homogenous coordinates, the frame A vectors (relative to frame B): u (,,0,0), v (,,0,0), w ( 0, 0,, 0) A A hen the change of frame transformation ma be computed as follows: And, again in homogeneous coordinates, p (,, 0,) and q (,, 0, 0), O ( 6,, 0,) p B q B 0 0 0 0 0 6 0 0 0 3 6 [,, 0, ] (,, 0, ) 0 0 0 0 0 6 0 0 0 3 [,, 0, 0 ] (,, 0, 0) 7 Show that u u u3 v v v3 urow urowb AB u u u v v v u v v v u B [ ] 3 3 row col col col3 row u3 u3 u 33 v3 v3 v 33 u row3 urow3b his result shows that a matri-matri multiplication can be viewed as several linear combinations; specificall, in this case, the matri product AB is essentiall the three linear combinations u B, u B, and u 3B row row row Frank Luna Page 6/7/006
One wa to do this is to just do the matri multiplication brute force, but it is tedious even for small 3 3 matrices o make things simpler, we will onl calculate the ith row of AB, where i is arbitraril,, or 3 AB u v u v u v row i row i col row i col row i col3 v v v [ u u u ] v v v v v v u B 3 i i i3 3 row i 3 3 33 Now we just let i var over,, and 3 to obtain the result: u u u3 v v v3 urowb AB u u u v v v u B 3 3 row u3 u3 u 33 v3 v3 v 33 urow3b Chapter 3 ransformations; Planes Show that the -ais rotation transformation given b Equation 37 is a linear transformation Recall R ( u) (, cosθ zsin θ, sinθ + zcosθ) We must show that this transformation satisfies the linearit condition, which is Equation 3 (o save horizontal space, we will use column vector notation) Frank Luna Page 3 6/7/006
αu + βv R( αu+ βv) ( αu + βv) cosθ ( αuz + βvz) sinθ ( αu βv) sinθ ( αuz βvz) cosθ + + + αu + βv αu cosθ + βvcosθ αuzsinθ βvzsinθ αusinθ βvsinθ αuz cosθ βvzcosθ + + + αu βv αucosθ αuzsinθ+ βvcosθ βvzsinθ αusinθ + αuz cosθ βvsinθ βvzcosθ + u v α ucosθ uzsinθ+ β vcosθ vzsinθ usinθ uz cosθ vsinθ vz cosθ + + αr u + βr v We have shown R ( u ) to be a linear transformation Show that the identit function, defined b I ( u) u, is a linear transformation, and show that its matri representation is the identit matri, so the definition 3 of a linear We have I ( αu+ βv) αu+ βv α I( u) + β I( v) transformation is satisfied B appling the linear transformation to each of the basis vectors and then putting them into the rows of a matri, we obtain the matri representation of the identit function: I( i ) i 0 0 I( j) j 0 0 0 0 I( k ) k 3 Show that the row vectors in the -ais rotation matri R are orthonormal Recall R cosθ 0 sinθ 0 0 sinθ 0 cosθ Frank Luna Page 4 6/7/006
o show the row vectors are orthonormal, we must show the all are of unit length and that the are all mutuall orthogonal Let r ( cos θ, 0, sinθ ), r ( 0,, 0), and r3 ( sin θ, 0, cosθ ) be the row vectors of the matri R B the well known trig identit cos θ sin θ +, we have: r θ θ cos + 0 + sin r 0 + + 0 r3 sin θ + 0 + cos θ So all the row vectors are of unit length Now a straightforward application of the dot product shows: r r 0 r r3 0 r r3 0 hus the row vectors are all orthogonal to each other We have shown the row vectors to be orthonormal 4 Let R be the -ais rotation matri Show that the transpose of this matri is its inverse; that is, show R R R R I R cosθ 0 sinθ 0 0 sinθ 0 cosθ R R R R cosθ 0 sinθ cosθ 0 sinθ 0 0 0 0 0 0 0 0 I sinθ 0 cosθ sinθ 0 cosθ 0 0 cosθ 0 sinθ cosθ 0 sinθ 0 0 0 0 0 0 0 0 I sinθ 0 cosθ sinθ 0 cosθ 0 0 he inverse is unique so we must have R R 5 In 34, we showed how the -ais, -ais, and z-ais rotation matrices could be derived directl Another perspective is to think of these rotation matrices as special Frank Luna Page 5 6/7/006
cases of the arbitrar ais rotation matri Show that the arbitrar ais rotation matri reduces to the -ais, -ais, and z-ais rotation matrices when q equals, i, j, and k (ie, the standard basis vectors), respectivel We have ( ) ( ) ( ) ( ) ( ) ( ) ( ) + ( ) + ( ) c+ c c + zs z c s R c zs c c z c s z c s z c s c z c q + + aking q i (, 0, 0) to: ( c), we have in the above matri, z 0, and matri reduces c+ 0 0 0 0 R 0 c s 0 cosθ sinθ R i 0 s c 0 sinθ cosθ he process is analogous to obtain the -ais, and z-ais rotation matrices 6 Show that the translation matri affects points, but not vectors Using the definition of matri multiplication (Equation ), we have: 0 0 0 0 0 0,,, 0 0 0 b b bz z + b, + b, z+ bz, [ z ] and [,, z,0] ( z,,,) (,0,0, b), ( z,,,) ( 0,,0, b), ( z,,,) ( 0,0,, b), ( z,,,) ( 0,0,0,) 0 0 0 0 0 0 0 0 0 b b bz z ( z,,,0) (,0,0, b), ( z,,,0) ( 0,,0, b), ( z,,,0) ( 0,0,, b), ( z,,,0) ( 0,0,0,) [,, z,0] Frank Luna Page 6 6/7/006
7 Verif that the given scaling matri inverse is indeed the inverse of the scaling matri; that is, show, b directl doing the matri multiplication, that SS S S I Similarl, verif that the given translation matri inverse is indeed the inverse of the translation matri; that is, show, b directl doing the matri multiplication, that I SS S S s 0 0 0 0 0 0 0 0 0 s s s 0 0 0 s 0 s 0 0 0 0 0 0 0 0 0 0 0 s s I 0 0 s 0 z 0 0 0 sz 0 0 0 0 0 0 s z sz 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 s s 0 0 0 s 0 0 0 s 0 0 0 s 0 s 0 0 0 s 0 0 0 0 0 0 0 0 s I 0 0 0 0 0 sz 0 sz 0 0 0 0 0 0 sz sz 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 b b bz b b bz b b b b bz bz 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 00 0 0 0 0 0 0 0 0 b b bz b b bz b b b b bz bz 0 0 0 0 0,, 0 these points define, 3, 6 8, 5, 3 8 Let p, p ( ), and p be three points Find the plane wo vectors on the plane are given b: u p p0 v p p 0 (, 3, 6) ( 0,, 0) (,, 6) ( 8, 5, 3) ( 0,, 0) ( 8, 4, 3) Now take the cross product to get a vector perpendicular to the plane (ie, the plane normal): Frank Luna Page 7 6/7/006
n u v (,, 6) ( 8, 4, 3) ( 8, 5, 0) d n p 0 5 Moreover, hen the plane consists of all the points p ( z,, ) satisf the equation: n ( p p 0) n p ( n p 0) 8+ 5 0z 5 0 that Remark: Because we didn t normalize the plane normal, the value d is no longer the signed distance from the origin, but some scaled distance 9 Let,,, 5 3 3 3 to the plane: ( 3 3,5 3,0 ), ( 3, 3, 3 ), and ( 3, 3, 0) π be a plane Define the localit of the following points relative he plane equation is: + + z 5 0 Plugging these points into the left-hand 3 3 3 side of the equation gives: 3 3 + 5 3 + 0 5 3 In front of the plane 3 3 3 3 + 3 + 3 5 0 On the plane 3 3 3 3 + 3 + 0 5 5 In back of the plane 3 3 3 0 Let,,, 5 3 3 3 π be a plane, and let r( t) (,, ) + t(,0,0) be a ra Find the point at which the ra intersects the plane hen write a short program using the D3DXPlaneIntersectLine (see the SDK documentation for the prototpe) function to verif our answer ( n p0 ) ( n u) ( 3 ) d 5 t 5 3+ ( 5 3 ) (,, ) ( 5 3 )(,0,0) (,, ) + ( 5 3 +, 0, 0) ( 5 3,, ) r + + + 3 We plug ( 5 3,, ) into the plane equation to verif it indeed lies on the plane: Frank Luna Page 8 6/7/006
5 3 + 5 0 On the plane 3 3 3 he code is given b: #include <d3d9h> #include <iostream> using namespace std; // Overload the "<<" operators so that we can use cout to // output D3DXVECOR3 objects ostream& operator<<(ostream& os, D3DXVECOR3& v) { os << "(" << v << ", " << v << ", " << vz << ")"; return os; } int main() { D3DXVECOR3 p0(-0f, 0f, -0f); D3DXVECOR3 u(0f, 00f, 00f); // Construct plane b specifing its (A, B, C, D) // components directl float s 0f / sqrtf(3); D3DXPLANE plane(s, s, s, -50f); // Function epects a line segment and not a ra; so we just // truncate our ra at p0 + 00*u to make a line segment D3DXVECOR3 isect; D3DXPlaneIntersectLine(&isect, &plane, &p0, &(p0 + 00*u)); cout << isect << endl; } return 0; he output is: (86605,, -) Press an ke to continue We note 5 3 86605, so the computer result agrees with our calculation Frank Luna Page 9 6/7/006