lecture 11: more xna basics prof. hsien-hsin sean lee ...agomes/tjv-2008-09/praticas/anx-02.pdfprof....

27
Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute of Technology

Upload: others

Post on 03-Mar-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

Lecture 11: More XNA Basics

Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering

Georgia Institute of Technology

Page 2: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

2

The Big Picture Initialize()

LoadContent()

Camera setup

Update()

Game Logic

Draw()

Initialization phase

Texture load

Model load

Graphics Device

Content Pipeline

Vertex data, commands

Page 3: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

3

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 weight

Page 4: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

4

VECTOR Example

Vectors (See Demo in Visual Studio)

Page 5: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

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.Invet(Matrix); // find its inverse – Matrix TR = Matrix.Transpose(Matrix); // transpose – Matrix IM = Matrix.Identity; // return an identity matrix

Page 6: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

6

MATRIX Example

SimpleMatrix (See Demo in Visual Studio)

Page 7: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

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 msdn

Page 8: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

8

Difference Between Drect3D 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 BasicEffect

Page 9: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

9

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 processed

Page 10: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

10

DrawUserPrimitives

Give vertex list

Inform the gfx device the type of vertices

Declare the type of vertices

# of Triangles

Offset

Page 11: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

11

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 data

Page 12: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

12

DrawPrimitves Example

FirstTriangle (See Demo in Visual Studio)

Page 13: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

13

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]

Page 14: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

14

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]

Page 15: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

15

Indexed Vertex Structure Define vertices of a Cube

VertexPositionNormalTexture

In Draw()

Texture Coordinates

(0,0) (0,1)

(1,0) (1,0)

Page 16: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

16

Texture Mapping for Sphere •  Warping the map could lead to distortion •  Geometric calculation involved

Globe image source: P. Bourke from uwa.edu.au

Page 17: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

17

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);

Page 18: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

18

Movement Control

Composite (*) into “one matrix” for effect file

Be cautious on operation ordering

Page 19: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

19

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.Up

Page 20: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

20

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/height

Page 21: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

21

DrawUserIndexedPrimitves Example

DrawIndexedCube (See Demo in Visual Studio)

Note that, in this example, all controls move the “Object,” *not* the viewer.

Page 22: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

22

Rotate Camera (Looking Around)

move

+x

- z Top view for XNA RHS

Page 23: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

23

Moving Camera Example

MovingCam (See Demo in Visual Studio)

Page 24: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

24

Loading an .X model myDuck = Content.Load<Model>("ducky_highres");

.x model’s Asset name

foreach (ModelMesh mesh in myDuck.Meshes) { foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting();

effect.View = viewMatrix; effect.Projection = projectionMatrix; effect.World = worldMatrix;

} mesh.Draw(); }

Draw all the meshes in the model

Page 25: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

25

Ducky Example

DuckBasicEffect (See Demo in Visual Studio)

Page 26: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

26

Draw a Subset of a Model

Draw only MeshPart[0] and [1]

Set up mesh IB and VB yourself

Use your own Draw call instead of mesh.Draw()

Page 27: Lecture 11: More XNA Basics Prof. Hsien-Hsin Sean Lee ...agomes/tjv-2008-09/praticas/anx-02.pdfProf. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute

27

Teapot Example

TeapotBasicEffect (See Demo in Visual Studio)