computer science – game designuc santa cruz adapted from jim whitehead’s slides announcements...

29
Computer Science – Game Design UC Santa Cruz Adapted from Jim Whitehead’s sli Announcements • Prototype submissions will be directly from SVN on Gforge – We will just grab your last commit before due date – Has to work (always maintain a working build on SVN) Evaluation Criteria Working game Milestones for individual members Commenting (SVN logs and in code) Daily logs

Upload: louisa-george

Post on 01-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

Announcements

• Prototype submissions will be directly from SVN on Gforge– We will just grab your last commit before due date– Has to work (always maintain a working build on SVN)Evaluation Criteria

Working game Milestones for individual members Commenting (SVN logs and in code)

Daily logs

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

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

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

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

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

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

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

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

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

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

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

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

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

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)

cameraUpVectorcameraTarget (x,y,z)

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

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)

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

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

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

Vertex Declaration

• XNA requires you to tell the graphics device what kind of vertex data you will be using– Was unclear why XNA couldn’t figure this out in 3.0, or handle

multiple types seamlessly and is has been improved in 4.0– 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(VertexPositionColor.VertexElements);

– IVertexType implementation

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

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

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

Using Basic shader

• Five steps• Create Shader• Copy over camera information• Iterate through EffectPasses

• Examine source code from example in Chapter 9 of XNA 3.0

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

Using Basic shaderFive steps:1. Create Shader

– BasicEffect effect = new BasicEffect(GraphicsDevice, null);2. Copy over camera information

– effect.View = camera.view;– effect.Projection = camera.projection;

3. Set world matrix– effect.World = … (more on this in a few slides)

4. Enable vertex capabilities (varies by Vertex type)– Effect.VertexColorEnabled = true; // for VertexPositionColor– Effect.Texture = myTexture; // for VertexPositionTexture

Effect.TextureEnabled = true; 5. Iterate through EffectPasses

– Call to DrawUserPrimitives inside EffectPass puts triangles on screen

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

Iterating Through Effect Passes• Each Effect has calls to begin()/end()• Effects are comprised of passes

– Each pass requires a call to begin()/end()

effect.Begin(); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Begin(); GraphicsDevice.DrawUserPrimitives<VertexPositionColor>

(PrimitiveType.TriangleStrip, verts, 0, 1); pass.End(); } effect.End();

Draws verticies

Passes

# of triangles (the “primitive shape” in this context) to draw

Index into verts array

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

Triangle Drawing• Examine this line of code

– GraphicsDevice.DrawUserPrimitives<VertexPositionColor>

(PrimitiveType.TriangleStrip, verts, 0, 1);– What is a TriangleStrip?

• Three ways to draw triangles– Triangle List

• Each set of three verticies defines a triangle• Memory inefficient, since triangles often

share edges in complex 3D meshes– Triangle Strip

• Builds triangle out of first three verticies• Each additional vertex creates new triangle

using that vertex, plus previous two verticies– Triangle Fan

• Each additional vertex creates new triable using that vertex, the previous vertex, plus the first vertex

http://escience.anu.edu.au/lecture/cg/surfaceModeling/image/surfaceModeling015.png

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

World Matrix• Each triangle has 3 verticies, and each vertex has an x,y,z

position– This position is given with respect to an origin location– That is, location is with respect to a local coordinate system

• World matrix– Translates from the local coordinate

system to the world (i.e., visible on screen) coordinate system

Local coordinate system offset, no rotation (Note: example uses left handed coordinate system, XNA uses right-handed coordinates) Source: MSDN DirectX documentation

Local coordinate system offset and rotatedwww1.adept.com/main/KE/DATA/V%20Plus/V%20Language%20User/images/World+yaw.gif

local

world

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

Translation

• A translation shifts the local coordinate system relative to the world coordinate system

• XNA provides a method for this– Matrix.CreateTranslation

• 3 parameters are x,y,z movements• Matrix.CreateTranslation(0.01f, 0, 0); // Shift right

(positive) along x axis• Matrix.CreateTranslation(-0.01f, 0, 0); // Shift left

(negative) along x axis

• Multiply world matrix by translation matrix to cause shift– All translations and rotations in 3D graphics accomplished via

matrix multiplication

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

Rotation

• A rotation shifts the local coordinate system by an angle relative to the world coordinate system

• XNA helper methods– Matrix.CreateRotationX, Matrix.CreateRotationY,

Matrix.CreateRotationZ• Rotations around single axes• Matrix.CreateRotationY(angle in radians);

– Matrix.CreateFromYawPitchRoll• Rotate around multiple axes• Matrix.CreateFromYawPitchRoll(yaw rad., pitch rad., roll

rad.)

• Demonstration of example triangle drawing code

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

Triangles are fine, but models are better

• Today’s 3D games have a large number of objects– Theoretically possible to create these objects by manually

writing C# code to create each individual triangle– In practice, this is rarely done.– Far better to use a 3D modeling tool

• Maya, XSI, 3DS Max, Blender

– Allows artists to create objects in world– Allows programmers to focus on behavior of objects– Modeling tools permit much faster creation and editing of

models

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

3D Modeling• Several ways to model a 3D object– Polygonal modeling

• Objects are subdivided into a series of polygons (triangles)• Can only approximate curved surfaces• Dominant modeling form in computer games and computer

graphics due to speed of rendering– NURBS

• Surfaces are defined by spline curves• Curves defined and controlled by control points

– Splines and patches• Curved lines define surfaces. Between polygons and NURBS

– Primitives modeling• Objects built up from primitive shapes (balls, cubes, cylinders, etc.)

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

3D Model formats

• There is a huge number of 3D model formats– No dominant standard– Interchange among models is often lossy

• XNA supports two 3D model formats– .X (DirectX)– .FBX• Originally for FilmBox by Kaydara, then Alias, now

Autodesk

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

Models in XNA• Model

– Represents some entity• A person, a car, or potentially a complex scene

with many parts– Typically used to represent one logical object

(e.g., a person) that has multiple parts (head, arms, legs, etc.)

– A model contains multiple meshes and bones• Mesh

– Represents a single physical object– Triangles, textures, shaders, etc.– XNA ModelMesh class

• Bone– Represents placement of each mesh relative to

other meshes– A transformation matrix

Model

ModelMesh

1

N

Car

Body+bone

Wheel +bone

Door +bone

Bone

1

N

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

Models in XNA (cont’d)• ModelMesh contains:

– List of ModelMeshPart– List of Effects – Verticies for triangles that

comprise this mesh• VertexBuffer

– Also has a bounding sphere• ModelMeshPart

– Represents a set of triangles that share the same materials (e.g., shader, or Effect)

– Has indexes into the ModelMesh – Starting index, number of triangles,

number of primitives to use from parent ModelMesh’s VertexBuffer

Model

ModelMesh

1

N

Bone

1

N

ModelMeshPart Effect

1

N

1

N

VertexBuffer

1

1

Verticies

1

N

Effect1

1

StartIndexNumVerticiesPrimitiveCount

1

1

1

1

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

Working with Models in XNA

• Bring model into XNA project inside Visual Studio

• Load model into XNA via Content manager• Draw model by iterating through all contained

meshes

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

Importing Models into Visual Studio

• Import model into Content directory– Copy .x or .fbx file, along with associated texture

images, into Content directory in XNA project– Inside Visual C#, right-click on Content directory• Add… Add Existing Item• Select .x or .fbx file

– Similar process to adding bitmap textures, etc.

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

Loading Model via Content manager

• Models are loaded via the content manager– Model model = Content.Load<Model>(@”{name of my

model without .x or .fbx extension}”)• XNA parses the model file, and fills in verticies,

textures, and effects in Model, and ModelMeshes– In XNA, this is a robust operation, big time savings– In many open source 3D game engines, model import can

be a big problem– At present, typically is not safe to assume model import will

work smoothly in a given 3D game engine– Need to test your tool chain

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

Drawing a Model• Iterate through all of the meshes

– Iterate through each effect for each mesh• Set lighting, camera, and world for each effect

– Draw each mesh using Draw() method on ModelMesh class

foreach (ModelMesh mesh in model.Meshes) { foreach (BasicEffect be in mesh.Effects) { be.EnableDefaultLighting(); be.Projection = camera.projection; be.View = camera.view; be.World = world * mesh.ParentBone.Transform; } mesh.Draw();}

• Examine example code from Chapter 10 of Learning XNA 3.0

Computer Science – Game DesignUC Santa Cruz

Adapted from Jim Whitehead’s slides

More on Models

• Explanation of parts of XNA models– http://blogs.msdn.com/shawnhar/archive/2006/1

1/20/models-meshes-parts-and-bones.aspx