MSDN Magazine - April 2008 - (Page 78) Figure 6 Emulated DirectX Math Classes the projection of the 3D shape onto the monitor’s surface. Silverlight is forced to draw these polygons in back-to-front order by setting the XAML ZIndex property for each Polygon using the Z coordinate of the center of the 3D polygon (scaled to squeeze a float into an int). This is a fairly simplistic way of achieving 3D that will only work if the polygons behave themselves by, for example, not intersecting—something I can get away with in this sample. Smarter forms of 3D involve mechanisms such as A shape in 3D can be depth buffers. As these reidentified as a set of faces quire GPU access, they are out of scope for the Silverthat are identified as a light .NET sandbox. set of points corresponding As a last touch, XAML to corners. allows Polygons to be partially transparent (using the Opacity property), which gives the artistic effect of a partially seethrough polyhedron. And that, as they say, is all there is to it. I’m going to touch lightly on the math used to achieve the animation. If you want to go into more detail, there are many sites on the Web that will help, including wikipedia.com, euclideanspace.com, mathworld.wolfram.com, gamasutra.com, and gamedev.net. I have already introduced some 2D transformations, such as RotationTransform and ScaleTransform, in the XAML samples shown earlier. In these, Silverlight does all the work for you. If you use XAML in a WPF environment, you also have access to 3D transformations, but these are not available in Silverlight. WPF 78 msdnmagazine Silverlight Animations Emulating DirectX Math relies upon DirectX to do the low-level work for it. This makes sense, as DirectX has a nice set of classes for doing the math required for 3D transforms. To do some of the same transformations in Silverlight, I’ve produced a DirectX Math emulation of many of these classes so that they can be used in the Silverlight sandbox. If you have any experience coding in Direct3D®, you’ll be quite at home with the Microsoft implementations of Vector2, Vector3, Matrix, and the like (see Figure 6). If not, I’ll give a very brief and very non-rigorous overview and some examples. Locating a point in 2D requires two coordinates: X and Y. In 3D it requires three coordinates: X, Y, and Z. Although, strictly speaking, points are not vectors, 2D points can be stored in a Vector2 object and 3D points in a Vector3 object. (Vectors are actually entities with a magnitude and a direction, which you can illustrate by having an arrow that starts at the origin and ends at the point. In a sense, you can think of all vectors as starting from the origin.) Shapes in three dimensions can be identified as a set of faces. Faces, in turn, are identified as a set of points corresponding to corners. Nearly everything you might want to do involves transforming these points to another set of points by applying one or more operations such as rotation, translation (or moving), or scaling. Nearly every operation that you might want to do can be represented by a 4-by-4 grid of numbers called a matrix. (Matrices in general can actually have any number of rows and columns, but we’re only interested in the special ones used to manipulate 3D points with what are called homogeneous coordinates.) These operations are referred to as linear transformations because they don’t (overly) distort the shape of the object they are applied to. Very usefully, there is a way of combining two such matrices into one resulting matrix that has the same effect. This operation is called matrix multiplication and can be repeated several times. This means you can chain a whole set of transformations together into a single matrix with the same effect. This is far more efficient than applying each transformation separately. There’s no need to worry about how matrix multiplication is implemented or how it does its transformation magic. Just use it as a tool. To transform a source point into a target point, there is also a multiplication operation defined between a matrix and a vector object. To avoid confusion (with matrix-matrix multiplication), this is implemented in Vector3 using the TransformCoordinate method. A couple of little code snippets should make things more clear. First, the following code in polyhedron.cs is used to fold two faces along their common side: Vector3 axis = axisTo - axisFrom; Matrix foldTransform = Matrix.Translation(-axisFrom) * Matrix.RotationAxis(axis, proportion * _dihedralAngle) * http://wikipedia.com http://euclideanspace.com http://mathworld.wolfram.com http://gamasutra.com http://gamedev.net
Table of Contents Feed for the Digital Edition of MSDN Magazine - April 2008 MSDN Magazine - April 2008 Contents Toolbox CLR Inside Out Basic Instincts Cutting Edge Foundations Test Run Service Station Windows with C++ Going Places { End Bracket } MSDN Magazine - April 2008 MSDN Magazine - April 2008 - (Page Intro) MSDN Magazine - April 2008 - Contents (Page Cover1) MSDN Magazine - April 2008 - Contents (Page Cover2) MSDN Magazine - April 2008 - Contents (Page 1) MSDN Magazine - April 2008 - Contents (Page 2) MSDN Magazine - April 2008 - Contents (Page 3) MSDN Magazine - April 2008 - Contents (Page 4) MSDN Magazine - April 2008 - Contents (Page 5) MSDN Magazine - April 2008 - Contents (Page 6) MSDN Magazine - April 2008 - Contents (Page 7) MSDN Magazine - April 2008 - Contents (Page 8) MSDN Magazine - April 2008 - Contents (Page 9) MSDN Magazine - April 2008 - Contents (Page 10) MSDN Magazine - April 2008 - Toolbox (Page 11) MSDN Magazine - April 2008 - Toolbox (Page 12) MSDN Magazine - April 2008 - Toolbox (Page 13) MSDN Magazine - April 2008 - Toolbox (Page 14) MSDN Magazine - April 2008 - Toolbox (Page 15) MSDN Magazine - April 2008 - Toolbox (Page 16) MSDN Magazine - April 2008 - CLR Inside Out (Page 17) MSDN Magazine - April 2008 - CLR Inside Out (Page 18) MSDN Magazine - April 2008 - CLR Inside Out (Page 19) MSDN Magazine - April 2008 - CLR Inside Out (Page 20) MSDN Magazine - April 2008 - CLR Inside Out (Page 21) MSDN Magazine - April 2008 - CLR Inside Out (Page 22) MSDN Magazine - April 2008 - CLR Inside Out (Page 23) MSDN Magazine - April 2008 - CLR Inside Out (Page 24) MSDN Magazine - April 2008 - Basic Instincts (Page 25) MSDN Magazine - April 2008 - Basic Instincts (Page 26) MSDN Magazine - April 2008 - Basic Instincts (Page 27) MSDN Magazine - April 2008 - Basic Instincts (Page 28) MSDN Magazine - April 2008 - Basic Instincts (Page 29) MSDN Magazine - April 2008 - Basic Instincts (Page 30) MSDN Magazine - April 2008 - Basic Instincts (Page 31) MSDN Magazine - April 2008 - Basic Instincts (Page 32) MSDN Magazine - April 2008 - Basic Instincts (Page 33) MSDN Magazine - April 2008 - Basic Instincts (Page 34) MSDN Magazine - April 2008 - Cutting Edge (Page 35) MSDN Magazine - April 2008 - Cutting Edge (Page 36) MSDN Magazine - April 2008 - Cutting Edge (Page 37) MSDN Magazine - April 2008 - Cutting Edge (Page 38) MSDN Magazine - April 2008 - Cutting Edge (Page 39) MSDN Magazine - April 2008 - Cutting Edge (Page 40) MSDN Magazine - April 2008 - Cutting Edge (Page 41) MSDN Magazine - April 2008 - Cutting Edge (Page 42) MSDN Magazine - April 2008 - Cutting Edge (Page 43) MSDN Magazine - April 2008 - Cutting Edge (Page 44) MSDN Magazine - April 2008 - Cutting Edge (Page 45) MSDN Magazine - April 2008 - Cutting Edge (Page 46) MSDN Magazine - April 2008 - Foundations (Page 47) MSDN Magazine - April 2008 - Foundations (Page 48) MSDN Magazine - April 2008 - Foundations (Page 49) MSDN Magazine - April 2008 - Foundations (Page 50) MSDN Magazine - April 2008 - Foundations (Page 51) MSDN Magazine - April 2008 - Foundations (Page 52) MSDN Magazine - April 2008 - Foundations (Page 53) MSDN Magazine - April 2008 - Foundations (Page 54) MSDN Magazine - April 2008 - Foundations (Page 55) MSDN Magazine - April 2008 - Foundations (Page 56) MSDN Magazine - April 2008 - Foundations (Page 57) MSDN Magazine - April 2008 - Foundations (Page 58) MSDN Magazine - April 2008 - Foundations (Page 59) MSDN Magazine - April 2008 - Foundations (Page 60) MSDN Magazine - April 2008 - Foundations (Page 61) MSDN Magazine - April 2008 - Foundations (Page 62) MSDN Magazine - April 2008 - Foundations (Page 63) MSDN Magazine - April 2008 - Foundations (Page 64) MSDN Magazine - April 2008 - Foundations (Page 65) MSDN Magazine - April 2008 - Foundations (Page 66) MSDN Magazine - April 2008 - Foundations (Page 67) MSDN Magazine - April 2008 - Foundations (Page 68) MSDN Magazine - April 2008 - Foundations (Page 69) MSDN Magazine - April 2008 - Foundations (Page 70) MSDN Magazine - April 2008 - Foundations (Page 71) MSDN Magazine - April 2008 - Foundations (Page 72) MSDN Magazine - April 2008 - Foundations (Page 73) MSDN Magazine - April 2008 - Foundations (Page 74) MSDN Magazine - April 2008 - Foundations (Page 75) MSDN Magazine - April 2008 - Foundations (Page 76) MSDN Magazine - April 2008 - Foundations (Page 77) MSDN Magazine - April 2008 - Foundations (Page 78) MSDN Magazine - April 2008 - Foundations (Page 79) MSDN Magazine - April 2008 - Foundations (Page 80) MSDN Magazine - April 2008 - Foundations (Page 81) MSDN Magazine - April 2008 - Foundations (Page 82) MSDN Magazine - April 2008 - Foundations (Page 83) MSDN Magazine - April 2008 - Foundations (Page 84) MSDN Magazine - April 2008 - Foundations (Page 85) MSDN Magazine - April 2008 - Foundations (Page 86) MSDN Magazine - April 2008 - Foundations (Page 87) MSDN Magazine - April 2008 - Foundations (Page 88) MSDN Magazine - April 2008 - Foundations (Page 89) MSDN Magazine - April 2008 - Foundations (Page 90) MSDN Magazine - April 2008 - Foundations (Page 91) MSDN Magazine - April 2008 - Foundations (Page 92) MSDN Magazine - April 2008 - Foundations (Page 93) MSDN Magazine - April 2008 - Foundations (Page 94) MSDN Magazine - April 2008 - Foundations (Page 95) MSDN Magazine - April 2008 - Foundations (Page 96) MSDN Magazine - April 2008 - Foundations (Page 97) MSDN Magazine - April 2008 - Foundations (Page 98) MSDN Magazine - April 2008 - Test Run (Page 99) MSDN Magazine - April 2008 - Test Run (Page 100) MSDN Magazine - April 2008 - Test Run (Page 101) MSDN Magazine - April 2008 - Test Run (Page 102) MSDN Magazine - April 2008 - Test Run (Page 103) MSDN Magazine - April 2008 - Test Run (Page 104) MSDN Magazine - April 2008 - Test Run (Page 105) MSDN Magazine - April 2008 - Test Run (Page 106) MSDN Magazine - April 2008 - Service Station (Page 107) MSDN Magazine - April 2008 - Service Station (Page 108) MSDN Magazine - April 2008 - Service Station (Page 109) MSDN Magazine - April 2008 - Service Station (Page 110) MSDN Magazine - April 2008 - Service Station (Page 111) MSDN Magazine - April 2008 - Service Station (Page 112) MSDN Magazine - April 2008 - Service Station (Page 113) MSDN Magazine - April 2008 - Service Station (Page 114) MSDN Magazine - April 2008 - Windows with C++ (Page 115) MSDN Magazine - April 2008 - Windows with C++ (Page 116) MSDN Magazine - April 2008 - Windows with C++ (Page 117) MSDN Magazine - April 2008 - Windows with C++ (Page 118) MSDN Magazine - April 2008 - Windows with C++ (Page 119) MSDN Magazine - April 2008 - Windows with C++ (Page 120) MSDN Magazine - April 2008 - Windows with C++ (Page 121) MSDN Magazine - April 2008 - Windows with C++ (Page 122) MSDN Magazine - April 2008 - Going Places (Page 123) MSDN Magazine - April 2008 - Going Places (Page 124) MSDN Magazine - April 2008 - Going Places (Page 125) MSDN Magazine - April 2008 - Going Places (Page 126) MSDN Magazine - April 2008 - Going Places (Page 127) MSDN Magazine - April 2008 - { End Bracket } (Page 128) MSDN Magazine - April 2008 - { End Bracket } (Page Cover3) MSDN Magazine - April 2008 - { End Bracket } (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.