Introduction to 3D in XNA Game Design Experience Professor Jim Whitehead February 27, 2009 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0
Announcements Partially operational game prototype ► Due today Can turn in to box by my office door by 5pm ► Submit on CDROM, USB Drive, or URL to Subversion project Project progress report ► Due Monday ► Take your project work breakdown and schedule (done previously) Update this schedule based on your (now improved) understanding of what still needs to be done Cut project scope, if necessary Remember, you have assignments due in other classes too
Announcements 3D modeling homework ► Assignment text not yet on web ► Will be up soon ► Due Monday, March 9 ► Assignment will involve: Create a simple 3D model (e.g., in Blender) –Something slightly (but not by much) more complex than a cube will be fine Make this model show up in XNA Extra credit for making model rotate, applying bitmap textures Goal is to exercise a model import pathway ► Intended to be a straightforward assignment
2D to 3D Many current games use 3D graphics ► Much more complex than 2D graphics ► This course will provide a basic overview of 3D graphics ► CMPS 160, 161, 164 provide greater depth Ratchet and Clank: Future
3D Camera Analogy ► 2D is like putting stickers on a page Place sticker (sprite) at x,y coordinate If a sticker is placed at 50,50, you see it ► 3D is like recording a video with a camera What is recorded (shown on screen) is what camera sees Can have objects in a scene that aren’t visible –Can have 3D object at 50,50,50, but if camera is pointing in the opposite direction, won’t see it! Introduces rotation –Camera can potentially be rotated around all 3 axes –Objects can also be rotated around 3 axes –Affects what shows up on screen
3D Coordinate System 3D graphics requires use of x,y,z coordinates So, which direction is positive z? ► Is it back away from you, or towards you? ► Either choice would work, need to pick one Right handed vs left handed coordinate systems ► XNA uses right handed coordinate system Place hands, palms up ► Point fingers in direction of positive X ► Curl fingers in direction of positive Y ► Thumb is pointing in direction of positive Z Right-handed coordinate system
Camera Camera is comprised of two Matrix objects ► View matrix holds information on Location of camera in world Camera direction Camera orientation ► Projection matrix holds information on View angle Aspect ratio Near and far plane Location (x,y,z) Orientation Direction
Matrix Structure XNA provides a Matrix structure ► A 4x4 matrix, in row vector layout Row vector matrices view vectors as a row from left to right column vector matrices view vectors as a column from top to bottom ► Built-in matrix operations +, -, *, /, == ► Also, convenience matrices Identity, Up, Down, Left, Right ► Large number of convenience methods Rotations, views into 3D world, determinants, invert
Vector3 Structure Represents either: ► An X, Y, Z coordinate, or, ► Distances along X, Y, Z coordinates (e.g., a vector) Often a unit vector –all values between 0 and 1 ► X, Y, Z properties (floats) Built-in operators ► +, -, *, /, ==, != Convenience vectors ► UnitX, UnitY, UnitZ, Up, Down, Left, Right Many convenience methods ► Interpolations, rotations, distance, dot product, normalization (x,y,z) coordinate x y z (x,y,z) vector
Creating an XNA Camera – View Matrix View matrix ► Use CreateLookAt method of Matrix structure ► Parameters (all Vector3) cameraPosition – location of camera (x,y,z) cameraTarget – coordinate of point where camera is looking cameraUpVector – vector indicating up position cameraPosition (x,y,z) cameraUpVector cameraTarget (x,y,z)
Creating an XNA Camera – Projection Matrix Projection Matrix ► Use CreatePerspectiveFieldOfView method ► Parameters (all floats) fieldOfView – angle of camera view, in radians –Typically 45degrees – pi/2 radians aspectRatio –Typically width of screen divided by height of screen nearPlaneDistance –Distance from camera to near viewing plane –Objects between camera and near plane are not shown! farPlaneDistance –Distance from camera to far viewing plane –Objects beyond far plane are not shown! cameraPosition (x,y,z)
Drawing Triangles All complex 3D shapes seen in games are composed of a series of triangles ► A triangle has 3 points, one for each corner Points are more typically known as verticies Minimum number of points to unambiguously define a plane VertexPositionColor object ► Represents the x,y,z location of a vertex ► Also has a color for the vertex ► VertexPositionColor v = new VertexPositionColor(new Vector3(0,1,0), Color.Blue); ► Need 3 verticies to draw a triangle
Vertex Declaration XNA requires you to tell the graphics device what kind of vertex data you will be using ► Unclear why XNA can’t just figure this out, or handle multiple types seamlessly ► Probably due to structure of DirectX API, or capabilities of graphics hardware ► For now, treat as a must-do, black box ► Put following in your main, derived from Game class ► GraphicsDevice.VertextDeclaration = new VertexDeclaration(GrahpicsDevice, VertexPositionColor.VertexElements);
Actually drawing the triangles In XNA, all 3D rendering is handled by a shader ► Shaders defined using High Level Shader Language (HLSL) ► Permits creation of wide range of visual effects ► More on shaders in a few classes XNA provides a default shader ► Called BasicEffect ► Will use this for now BasicEffect is a type of effect ► Effects contain a series of EffectPass ► Each pass handles some aspect of putting things on screen
Using Basic shader Three steps Create Shader Copy over camera information Iterate through EffectPasses Examine source code from example in Chapter 9 of XNA 3.0