More XNA Basics Prof. Hsien-Hsin Sean Lee Prof. Aaron Lanterman School of Electrical and Computer Engineering Georgia Institute of Technology2 The Big Picture Initialize() LoadContent() Camera setup Update() Game Logic Draw() Initialization phase Texture load Model load Graphics Device Content Pipeline Vertex data, commands3 VECTOR • Various types – VECTOR2 vec2 = new Vector2(100, 100) – VECTOR3 vec3 = new Vector3(10, 10, 10) – VECTOR4 vec4 = new Vector4(1, 1, 10, 2) • Some Useful common vector functions – float len = v3.Length() – float dp = Vector3.Dot(Vector3, Vector3) – Vector3 cp = Vector3.Cross(Vector3, Vector3) – Vector3 lerp = Vector3.Lerp(Vector3, Vector3, float) – Vector3 dist = Vector3.Distance(Vector3, Vector3) – Vector3 nv = Vector3.Normalize(Vector3) – Check out http://msdn.microsoft.com for more 2nd vector’s weight4 VECTOR Example Vectors (See Demo in Visual Studio)5 MATRIX Related • MATRIX (4x4) • Common matrix manipulations – Vector4 Rmat41= Vector4.Transform (Vector4, Matrix); // [4x1]*[4x4] – Matrix Rmat44 = Matrix.Multiply(Matrix, Matrix); // [4x4]*[4x4] – It is overloaded with operator *, thus Rmat44=M1*M2 – Matrix INV = Matrix.Invert(Matrix); // find its inverse – Matrix TR = Matrix.Transpose(Matrix); // transpose – Matrix IM = Matrix.Identity; // return an identity matrix6 MATRIX Example SimpleMatrix (See Demo in Visual Studio)7 Plane • PLANE plane1 = new Plane(Vector3, d) vector3 describes a normal vector d = plane’s distance from the origin • Useful plane function – Plane.intersects(boundingbox box) – Many others, check msdn8 Difference Between Direct3D and XNA • You always need an “effect file” (.fx) • An effect file is a shader code consisting of – Vertex Shader – Pixel Shader • Need to communicate varibales between your C# code and the shader code, e.g., matrices • Even rendering a simple triangle requiring an effect file – Use a BasicEffect9 Specifying an Effect • Either use a BasicEffect or specify a provided .fx file An effect file called effects.fx is given Passes specified in the shader will be processed10 Set Up BasicEffect11 BasicEffect: Just a Shader • Internally set via the “ShaderIndex” parameter: – 0: Flat color – 1: Vertex colors – 2: Textured – 3: Textured + Vertex colors – 4: Lit – 5: Lit + Vertex colors – 6: Lit + Textured – 7: Lit + Textured + Vertex colors – 8: Per-pixel lit – 9: Per-pixel lit + Vertex colors – 10: Per-pixel lit + Textured – 11: Per-pixel lit + Textured + Vertex colors12 BasicEffect.fx (the end of the file)13 BasicEffect Handles 3 Directional Lights • Parameters – AmbientLightColor – DirLight0Direction – DirLight0DiffuseColor – DirLight0SpecularColor – DirLight1Direction – DirLight1DiffuseColor – DirLight1SpecularColor – DirLight2Direction – DirLight2DiffuseColor – DirLight2SpecularColor14 More BasicEffect Parameters • Material properties (colors range 0 to 1) – DiffuseColor – EmissiveColor – SpecularColor – SpecularPower – Alpha • Fog properties – FogEnabled (0 to disable, 1 to enable) – FogStart – FogEnd – FogColor15 DrawUserPrimitives Give vertex list Inform the gfx device the type of vertices Declare the type of vertices # of Triangles Offset16 Use Vertex Buffer (DrawPrimitives) • Create a container Vertex Buffer for allocating vertices Set starting source vertex for gfx device One less argument than DrawUserPrimitives Create VertexBuffer Indicate source of data17 VertexBuffer • Contains list of vertices – 4-D colors – 3-D positions – 2-D texture coordinates – 3-D normals • Structures – VertexPositionColor – VertexPositionColorTexture – VertexPositionNormalTexture – VertexPositionTexture – Can make your own with VertexElement; See “How to: Create and Use a Custom Vertex” http://msdn.microsoft.com/en-us/library/bb976065.aspx18 PrimitiveType Enumeration19 DrawPrimitves Example FirstTriangle (See Demo in Visual Studio)20 Create Indexed Vertex Buffer • To eliminate the repetitive definition of vertices • Use a vertex buffer to store unique vertices • Use an index buffer to store Index Vertex buffer Index buffer V[0] V[1] V[2] V[3]21 DrawUserIndexedPrimitves Primitive Type T[] vertex data Vertex offset # of Vertices Short[] index data Index Offset # of Primitive to render V[0] V[1] V[2] V[3]22 Indexed Vertex Structure Define vertices of a Cube VertexPositionNormalTexture In Draw() Texture Coordinates (0,0) (0,1) (1,0) (1,0)23 Texture Mapping for Sphere • Warping the map could lead to distortion • Geometric calculation involved Globe image source: P. Bourke from uwa.edu.au24 MATRIX Method Examples • Translation (by amount to translate) Matrix.CreateTranslation(Vector3 position); • Scaling (by amount to scale in x, y, and z) Matrix.CreateScale(Vector3 scales); • Rotation Matrix.CreateRotationX(float radians); Matrix.CreateRotationY(float radians); Matrix.CreateRotationZ(float radians); • Special function Matrix.CreateShadow(Vector3 lightDir, Plane plane);25 Movement Control Composite (*) into “one matrix” for effect file Be cautious on operation ordering26 MATRIX Method for View Transformation • View Transformation Matrix.CreateLookAt(Vector3 cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector); x -z y CameraTarget(4, -1, 3) cameraTarget(-3, 3, -1) Usually (0, 1, 0) the world is upward Use Vector3.Up27 MATRIX Method for Projection Transformation • Build a Look-at Matrix Matrix.CreatePerspectiveFieldOfView( float fieldOfView, // In radians float aspectRatio, float nearPlaneDistance, float farPlaneDistance); • Can use MathHelper.ToRadians(degree) forfieldOfView Eye fovy/2 far near Height aspectRatio = width/height28 DrawUserIndexedPrimitves Example DrawIndexedCube (See Demo in Visual Studio) Note that, in this example, all controls move the “Object,” *not* the viewer.29 Rotate Camera (Looking Around) move +x - z Top view for XNA RHS30 Moving Camera Example MovingCam (See Demo in Visual Studio)31 How to Render a Model in XNA Content Pipeline http://msdn.microsoft.com/en-us/library/bb203933.aspx32 How to Peek at
View Full Document