CSE 381 - PowerPoint PPT Presentation

1 / 54
About This Presentation
Title:

CSE 381

Description:

But the frustum moves What do we do with our plane equations? Make your decision Yaw, Pitch, & Roll Quaternion So what is a quaternion mathematically speaking? Think ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 55
Provided by: elvisp2
Category:
Tags: cse | frustum

less

Transcript and Presenter's Notes

Title: CSE 381


1
CSE 381 Advanced Game Programming3D Mathematics
Pong by Atari, released to public 1975
2
So what math do games use?
  • All types
  • geometry trig for building moving things
  • linear algebra for rendering things
  • calculus for performing collision detection
  • and lots, lots more
  • Numerical methods
  • Quaternions
  • Curves
  • Surfaces
  • NURBS
  • Etc.

3
What math should we cover?
  • Today
  • Vector Math
  • Matrix Math
  • Another time
  • Plane equations and frustum culling
  • Quaternions

4
What unit of measurement should we use?
  • Most games use
  • meters
  • precision down to millimeters
  • maximum range to 100 kilometers
  • Get on the same page
  • programmers
  • artists
  • level designers

5
3D Coordinate Systems
  • Use
  • x, y, z to locate items (like vertices)
  • floating point values

http//img139.imageshack.us/i/viewportgy6.gif/q3
d20coordinate20system20maya
6
Right Handed vs. Left Handed
y
y
x
x
z
z
  • Vertices arranged counter-clockwise
  • Vertices arranged clockwise

7
Coordinate System Conversions
  • Problem
  • art program built models using right-handed
    system
  • game engine uses left-handed system
  • Solution
  • Reverse the order of vertices on each triangle
  • Multiply each Z-coordinate by -1

8
It all starts with vectors
  • Whats a vector?
  • a direction
  • What are vectors used for?
  • everything
  • graphics physics calculations
  • Things we must learn
  • Unit vectors
  • Vector normalization
  • Vector mathematics

(3, 4, 0)
9
Whats a Unit Vector?
  • Any vector that has a length of 1.0
  • may be created by vector normalization
  • Think of it as a direction with a standard
    magnitude
  • its useful for many computations
  • Ex inputs to cross dot products
  • How might we normalize a vector?
  • divide the vector by its length

10
Vector Normalization Example
  • V (3, 4, 0)
  • LengthV square_root(32 42 02)
  • square_root(25)
  • 5
  • Unit VectorV (3/5, 4/5, 0/5)
  • (.6, .8, 0)

11
And now for some Vector Math
  • Vector Arithmetic
  • addition Subtraction
  • for combining vectors
  • Dot Product
  • for calculating angles
  • Cross Product
  • for calculating direction (another vector)

12
Vector Arithmetic
  • Adding or subtracting each component of 2 vectors
  • Useful for
  • combining velocities in physics calculations
  • collision detection algorithms
  • V1 V2 (V1x V2x), (V1y V2y), (V1z V2z)
  • V1 - V2 (V1x - V2x), (V1y - V2y), (V1z - V2z)

13
Dot Product
  • Projects one vector onto the other and calculates
    the length of that vector
  • Useful for
  • determining whether an angle is acute, obtuse, or
    right
  • Is a surface facing toward the camera or not?
  • V1 V2 (V1x V2x) (V1y V2y) (V1z V2z)
  • arccos of V1 V2 gives you the angle

14
Dot Product Results to Note
  • V1 V1 1
  • V1 V2 0 if
  • V1 is orthogonal to V2, meaning V1 V2 form a
    right angle to each other and they are the same
    length
  • V1 V2 -1 if
  • V1 and V2 are the same length and are pointing
    away from each other

15
Dot Product Visualization
V1
V1
V2
V2
V1 V2 -1
V1 V2 gt 0
V1
V1
V2
V2
V1 V2 0
V1 V2 lt 0
V1 V2 V2 V1
16
Dot Product Back Face Culling
  • Camera has look-at vector (V1)
  • unit vector
  • All surfaces have a normal vector (V2)
  • orthogonal to plane of polygon
  • If V1 V2 lt 0, the polygon is facing the camera
  • And so should be drawn

17
Cross Product
  • Produces a vector orthogonal to the plane formed
    by two input vectors
  • Useful for
  • Calculating normal vector of a polygon
  • V1 X V2 (V1.y V2.z) (V2.y V1.z),
  • (V1.z V2.x) (V2.z V1.x),
  • (V1.x V2.y) (V2.x V1.y)

18
Cross Product Visualization
V3
V1
V1
V2
V1 X V2 V3
V2
V2 X V1 V3
V1
V3
V2
V1 X V2 NULL
V1 X V2 ? V2 X V1
19
The Need for Matrix Mathematics
  • We store model vertices normals with their
    original modeled values
  • We filter them through transformation matrices
  • moves it from model to world space
  • Each model has a transform that factors
  • translation
  • rotation
  • scaling

20
Multiple Similar Models
  • Suppose I want 2 ogres
  • What will they have in common?
  • geometry
  • texturing (perhaps some variations)
  • Animations
  • etc.
  • What will they have thats unique?
  • transform matrix
  • animation state
  • etc.

21
Asset Design Pattern
  • Use 2 classes
  • ModelType
  • stores everything common to all models
  • vertex buffers, index buffers, texture
    coordinates, etc.
  • Model
  • stores everything common to a single model
  • position, rotation, etc.
  • has unique transform matrix built from position,
    etc.
  • update matrix each frame

22
Think of it this way
  • To render 2 models, each frame
  • Update transform matrix for model 1
  • Load the transform matrix for model 1
  • Render model 1
  • Update transform matrix for model 2
  • Load the transform matrix for model 2
  • Render model 2
  • Etc.

23
So whats a models transform matrix?
  • A 4 X 4 array of floating point numbers
  • They are really shorthand for representing linear
    equations
  • Components well see
  • Translation
  • moves the object to a location in world space
  • Rotation
  • rotates the object around an origin

24
Translation
1 0 0 0 0 1 0 0 0 0 1 0 T.x T.y
T.z 1
  • T.x, T.y, T.z will move the object to that
    location in world space

25
Rotations are more complicated
  • There are 3 kinds of rotation matrices
  • one around the x-axis
  • one around the y-axis
  • one around the z-axis
  • The object would be rotated about each axis by
    some angles ?x, ?y, ?z
  • We need to factor all 3 rotations of course

26
X-axis Rotation Matrix
1 0 0 0 0 cos(?) sin(?) 0 0 sin(?)
cos(?) 0 0 0 0 1
27
Y-axis Rotation Matrix
cos(?) 0 sin(?) 0 0 1 0 0 sin(?)
0 cos(?) 0 0 0 0 1
28
Z-axis Rotation Matrix
cos(?) -sin(?) 0 0 sin(?) cos(?) 0 0 0 0
1 0 0 0 0 1
29
Now we need to combine them
  • Ultimately, for each object, we want only one
    matrix
  • encode all operations into it
  • How do we do this?
  • matrix multiplication
  • A 4X4 Matrix X A 4X4 Matrix gives you another 4X
    4 Matrix

30
Note, order of operations matters
  • Start with identity matrix
  • Multiply by rotation matrices first
  • Multiply by translation matrix last

31
Matrix Multiplication
  • How about M X N?

M11 M12 M13 M14 M21 M22 M23 M24 M31 M32 M33
M34 M41 M42 M43 M44
N11 N12 N13 N14 N21 N22 N23 N24 N31 N32 N33
N34 N41 N42 N43 N44

P11 P12 P13 P14 P21 P22 P23 P24 P31 P32 P33
P34 P41 P42 P43 P44

32
How does that work?
M11 M12 M13 M14 M21 M22 M23 M24 M31 M32 M33
M34 M41 M42 M43 M44
N11 N12 N13 N14 N21 N22 N23 N24 N31 N32 N33
N34 N41 N42 N43 N44

P11 P12 P13 P14 P21 P22 P23 P24 P31 P32 P33
P34 P41 P42 P43 P44

33
Calculating P
  • P11 M11N11 M12N21 M13N31 M14M41
  • P12 M11N12 M12N22 M13N32 M14M42
  • P13 M11N13 M12N23 M13N33 M14M43
  • P14 M11N14 M12N24 M13N34 M14M44
  • P21 M21N11 M22N21 M23N31 M24M41
  • P22 M21N12 M22N22 M23N32 M24M42
  • P23 M21N13 M22N23 M23N33 M24M43
  • P24 M21N14 M22N24 M23N34 M24M44
  • P31 M31N11 M32N21 M33N31 M34M41
  • P32 M31N12 M32N22 M33N32 M34M42
  • P33 M31N13 M32N23 M33N33 M34M43
  • P34 M31N34 M32N24 M33N34 M34M44
  • P41 M41N11 M42N21 M43N31 M44M41
  • P42 M41N12 M42N22 M43N32 M44M42
  • P43 M41N13 M42N23 M43N33 M44M43
  • P44 M41N14 M42N24 M43N34 M44M44

34
What do we do with our matrix?
  • Transform Points
  • put point into 4 X 1 vector
  • put 1 in 4th cell
  • multiply transform by point matrix
  • result is point in world space coordinates
  • Transform Normal Vectors
  • put vector into 4X1 vector
  • put 0 in 4th cell
  • multiply transform by point matrix
  • result is vector in world space coordinates

35
Transforming a Point
M11 M12 M13 M14 M21 M22 M23 M24 M31 M32 M33
M34 M41 M42 M43 M44
Px Py Pz 1

P11 P21 P31 P41

36
So were does this come in?
  • Full Game World

Scene Graph Culling
Models Near Camera
Frustum Culling
Models In Frustum
  • Rendering
  • Transform vertices
  • Texturing
  • Etc.

37
Frustum Culling
  • Try reading the following
  • http//www.lighthouse3d.com/opengl/viewfrustum/ind
    ex.php?intro
  • http//www2.ravensoft.com/users/ggribb/plane20ext
    raction.pdf
  • http//www.flipcode.com/archives/Frustum_Culling.s
    html

38
So how do we do frustum culling?
  • One approach
  • First, extract frustum planes
  • calculate 8 frustum points
  • use cross-products to calculate orthogonal
    vectors
  • Second, calculate the orthogonal vector from each
    plane to the objects center
  • this steps a bit tricky
  • Third, determine which side the object is on for
    each plane
  • dot product

39
A better approach
  • First, extract frustum planes
  • calculate 8 frustum points
  • use cross-products to calculate orthogonal
    vectors
  • Second, calculate the signed distance from the
    point to the plane
  • much easier
  • Third, determine which side the object is on for
    each plane
  • examine sign of result from step 2

40
How do we extract the frustum planes?
  • What do we know?
  • camera position
  • camera look-at-vector
  • camera up-vector
  • viewport (front clipping plane) width height
  • distances from camera to near far clipping
    planes
  • What do we need to know?
  • 8 frustum points
  • front-top-right, front-bottom-right,
    front-top-left, front-bottom-left
  • back-top-right, back-bottom-right, back-top-left,
    back-bottom-left

41
How can we calculate those points?
  • Assumptions
  • right-handed coordinate system
  • camera at origin (0,0,0)
  • camera look-at is (1, 0, 0)
  • camera up is (0, 1, 0)
  • Easy, simple arithmetic
  • front-top-right (near, height/2, width/2)
  • front-bottom-right (near, -height/2, width/2)
  • front-top-left (near, height/2, -width/2)
  • front-bottom-left (near, -height/2, -width/2)
  • back-top-right (far, height/2, width/2)
  • back-bottom-right (far, -height/2, width/2)
  • back-top-left (far, height/2, -width/2)
  • back-bottom-left (far, -height/2, -width/2)

Calculate these values once, at start of game
42
How do we get the plane normals?
  • Cross Product
  • Using what vectors?
  • those between 3 points on each plane
  • Note, be careful, remember for cross-product
  • A X B ? B X A

43
What happens when the camera moves?
  • We need to update stuff. Like what?
  • look at vector
  • up vector
  • right vector
  • frustum points (8 corners)
  • frustum normals
  • Note beware floating point error
  • dont change the original values
  • use copies of original each frame
  • recalculate from same base point each frame

44
How do we update these values?
  • Using a transformation matrix
  • Whats the translation for this matrix?
  • camera position
  • Whats are the rotations for this matrix?
  • cameras rotation

45
And plane to point distance?
  • First we need plane equations
  • We can define a plane as
  • Ax By Cz D 0
  • A, B, C are the planes normal vector
    components
  • D is the distance from origin
  • 0 for left/right/top/bottom planes
  • near for near plane, far for far plane
  • but this is for a camera at origin

46
But the frustum moves
  • Same old wrinkle
  • Solution
  • extract plane information from transformation
    matrix
  • See http//www2.ravensoft.com/users/ggribb/plane2
    0extraction.pdf

47
What do we do with our plane equations?
  • Simply plug-in the coordinates of the objects
    center into the plane equation
  • The result is the signed distance from the plane
    to the point
  • To get the true distance, then normalize the
    vector
  • What we really care about is the sign of the
    distance

48
Make your decision
  • If distance lt 0 , then the point p lies in the
    negative halfspace.
  • If distance 0 , then the point p lies in the
    plane.
  • If distance gt 0 , then the point p lies in the
    positive halfspace.

49
Yaw, Pitch, Roll
  • An object can be rotated about all 3 axes

http//mtp.jpl.nasa.gov/notes/pointing/Aircraft_At
titude2.png
50
Quaternion
  • An alternative for representing rotations
  • Can provide certain advantages over traditional
    representations
  • require less storage space
  • concatenation of quaternions require fewer
    arithmetic operations
  • more easily interpolated for producing smooth
    animation

51
So what is a quaternion mathematically speaking?
  • A 4th dimension vector
  • q (w,x,y,z) w xi yj zk
  • Often written as
  • q s v
  • Where
  • s is the scalar component (w)
  • v is the vector component (x,y,z)

52
Think of it this way
  • In 2D space, an object rotates around a point
  • In 3D space, an object rotates around a line
  • our quaternion vector
  • x,y,z provides the vector, w provides the angle
    of rotation

53
What are quaternions really good for?
  • Interpolations
  • well see this with animations
  • Models may have 2 keyed animation states
  • Interpolation can calculate interim locations
  • Quaternion calculations allow for smooth rotation
    interpolation

54
References
  • Game Coding Complete by Mike McShaffry
  • Frustum Culling by Dion Picco
  • http//www.flipcode.com/archives/Frustum_Culling.s
    html
  • Vector Math for 3D Computer Graphics
  • http//chortle.ccsu.edu/VectorLessons/vectorIndex.
    html
  • GameDev.Net Quaternions FAQ
  • http//www.gamedev.net/reference/articles/article1
    691.aspQ47
Write a Comment
User Comments (0)
About PowerShow.com