CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The...

39
CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas

Transcript of CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The...

Page 1: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

CSE 381 – Advanced Game ProgrammingSkeletal Animation and Skinning

Jack Skellington, from The Nightmare Before Christmas

Page 2: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Static objects are boring

• Look at your pyramids– they just sit there

– we can give them velocities, but they’re still boring

• What about more complicated shapes?– living things

– vehicles

– etc.

• These things are all made up of separate moving parts that are still part of the whole object– where I go, my arms go with me

Page 3: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Player Animation States

• Think about an FPS

• What game states does the player have?– running forward– running backwards– running sideways– jumping– falling– landing– dying (in all sorts of ways)– etc.

Page 4: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Typical Role of Artist

• Create a mesh for a game shape/character/vehicle– geometry– texturing– Materials

• Create animation sequences for different game states for that entity

• NOTE: exported data can be tricky

Page 5: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

11-55

Keyframe Animation In traditional hand-drawn animation:

the senior key artist would draw the keyframes, and the junior artist would fill the in-between frames.

We can use a similar approach

For a 30-fps animation, < 30 frames are defined per second:1. The key data are assigned to the keyframes2. In-between frames are interpolated

Page 6: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

The MD2 Model Format• Created by id for Quake 2

• The Good:– easy to parse and animate

– support with many free modeling tools

– plenty of free models available

• TurboSquid, 3DCafe, 3DTotal

• The not so good:– older format

– inefficiently stores keyframe info

– not good for realistic animations

– time consuming for artists to animate

Page 7: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

The MD2 File Layout

MD2 Header

Skin Data

Texture Coords

Triangles

GL Commands

Keyframes

Skin Offset

Texture Coord Offset

Triangle Offset

GL Command Offset

Keyframe Offset

File End Offset

Data

Page 8: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

The MD2 Headerstruct MD2Header {

char ident[4];

int version;

int skinWidth;

int skinHeight;

int frameSize;

int numSkins;

int numVertices;

int numTextureCoords;

int numTriangles;

int numGLCmds;

int numFrames;

int skinOffset;

int texCoordOffset;

int triangleOffset;

int frameOffset;

int GLCmdOffset;

int eofOffset;

};

Let’s look at Ogro Invasion’s md2model.h

Page 9: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Some More Details

• Data stored in struct chunks

struct TexCoord {

short s;

short t;

};

struct KeyFrame{

float scale[3];

float translate[3]

char name[16];

Vertex vertices[numVertices];

};

struct Vertex{

unsigned char v[3];

unsigned char lightNormalIndex;

};

struct Triangle {

short vertexIndex[3];

short texCoordIndex[3];

};

Page 10: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Again, the positions between frames?

• Must be interpolated (linear interpolation used)

• Assume a time from 0.0 to 1.0– from one keyframe to next– m_interpolation variable in MD2Model class– other important variables to note:

•m_startFrame, m_endFrame, m_currentFrame, m_nextFrame

• vi = v1 + t * (v2 – v1)

Page 11: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Alternative Approaches

• For better results we could use other techniques– more efficient– more realistic

• Frame Hierarchies

• Bone Hierarchies

• Skinning

• Quaternions

Page 12: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Frame Hierarchy

• A means for organizing a mesh into structures that may be moved separately– meshes can consist of other child meshes

• This provides a means for fluid object animation– when artists create meshes, they establish hierarchy

Page 13: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Frame

• Hold the actual hierarchy data– each Frame can contain zero to many child Frames– each Frame can contain zero to many sibling Frames

• Important Properties of a frame– Name– Transformation Matrix– Adjacency data– And others

• All other Frames can be accessed through the root

Page 14: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Example: A Tank

• Gun Turret should be able to rotate

• Gun Barrel should be able to move up and down

• The wheels should be able to spin

Page 15: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.
Page 16: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.
Page 17: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.
Page 18: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

What would be the Tank Hierarchy?

• Tank hull – central parent section– Wheels – children of the hull and siblings to each other– Gun Turret – child of the hull

• Gun Barrel – child of the Gun Turret

• Sub-meshes– wheels vertices would be a sub-mesh of the hull– where the hull goes, the wheels go– when the hull rotates, the wheels rotate

Page 19: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

What do you think a combined Matrix is?

• Our tank will have a location– we translate according to that location, by building a

matrix

• Remember, the sub-meshes may move and rotate additionally, relative to the tank, but independently of the hull

• Combined matrices combine the two– move, scale, and rotate the mesh (along with entire object)– move, scale, and rotate the sub-mesh relative to the parent mesh

Page 20: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

What is skinning?

• Provides a means to animate a model while reducing the "folding" and "creasing" of polygons as the model animates

• Sometimes called skeletal animation

• A model is represented in two parts:– skin– skeleton

Page 21: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

How is skinning implemented?

• Define a bone hierarchy in a model– separate from the mesh (triangle) data– is linked to the model's mesh, so that animating the

bone hierarchy animates the mesh's vertices

• Multiple bones affect each vertex in the skin mesh

• Each bone has a weighting value to determine how much influence it has in proportion to the others

Page 22: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

How are bones moved?

• Matrices– each bone has a transformation matrix for moving it

• Moving a bone has what effect?– moves all children bones (and their children, etc.)– transforms corresponding vertices according to

weighted results

• So, to calculate the final position of a vertex:– each bone transformation is applied to the vertex

position, scaled by its corresponding weight

Page 23: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

11-2323

Hierarchical Model

Associate bones with vertices

Page 24: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

11-2424

Hierarchical Model Example

Page 25: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

What might be a disadvantage of skinning?

• Doesn’t provide realistic muscle movement– Ex: a character flexing an arm muscle– historically more important for movies than games– games are starting to use muscle controllers as well

• attached to bones, mimic muscle movements

Page 26: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Skinned Data

• Necessary data to define relationship between bone hierarchy and the skin:– Number of vertices– Number of bones– Vertex Weights– Vertex Indicies– Inverse Matrices

• Values are extracted from the modeler when loading an animation

Page 27: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Inverse Bone Matrices (IBMs)

• Used to transform from skin space to bone space

• Describe the transformation matrix from "the root" bone in the hierarchy to "each" bone in the hierarchy

Page 28: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Typical Skinning Algorithm

1. Transforms vertex positions from their object space (skin space) into bone space using the IBMs.

2. Performs the key-frame based animation for that frame, which transform the vertex positions back into skin space in its animated pose.

3. The model is then rendered.

Page 29: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Problems With Euler Angles

• Bones are positioned and rotated– pitch, yaw, and roll are Euler angles

• We need to interpolate interim bone transforms

• Euler angles are problematic when interpolating– Can produce incorrect results– Gimbal lock

Page 30: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Quaternions

• An extension of complex numbers

• An alternative for interpolation– don’t suffer from Gimbal Lock

• A rotation can be represented:– using 3 Euler Angles– using 4 Quaternion Components

• 1 real (think scalar)

• 3 imaginary (think vector)

• Alt representation: 4 num vector (real as w)

Page 31: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Quaternion Advantages

• Interpolation is easy between 2 quaternions

• Smoother animation

• Take up less space than matrices

• Can easily be converted to matrices for rendering

Page 32: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

But what is a quaternion?

• A quadruple

Q = (qx, qy, qz, qw)

= qxi + qyj + qzk + qw

Where i2 = j2 = k2 = -1

Alternatively denoted as [w, x, y, z]

Or [s, v]

where s is the scalar (w)

v is the vector (x,y,z)

Page 33: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Quaternion Addition

• All quaternions for a 3D rotation have unit length

• Adding and subtracting quaternions is easy

• Ex:

p = [w1, x1, y1, z1]

q = [w2, x2, y2, z2]

p + q = [w1+w2,x1+x2,y1+y2,z1+z2]

Page 34: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Quaternion Multiplication

• Same as multiplying two rotation matrices:– the rotations will be added together

p = [s1, v1] q = [s2, v2]

p * q = [s1s2 – v1∙v2, s1v2 + s2v1 + v1xv2]

Page 35: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Quaternion Conversions

• What the hell is a quaternion anyway?– Artists can’t relate

• Plus, OpenGL doesn’t support them

• So what do we do?– do all interpolation using quaternions– convert results to matrices

Page 36: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Conversion Calculations

• Euler angles to quaternions:

Qx = [cos(pitch/2),sin(pitch/2),0,0]

Qy = [cos(yaw/2),0,sin(yaw/2),0]

Qz = [cos(roll/2), 0, 0, sin(roll/2)]

• Quaternion to matrix:

1-2y2-2z2 2xy-2wz 2xz+2wy

2xy+2wz 1-2x2-2z2 2yz-2wx

2xz-2wy 2yz+2wx 1-2x2-2y2

Page 37: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

LERPs AND SLERPs

• 2 Ways of Interpolating Quaternions

• LERP– Linear Interpolation

• SLERP– Spherical Linear Interpolation

LERP

SLERP

Page 38: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

Interpolating

LERP(q0,q1,t) = (1-t)q0 + tq1

ϴ = arccos(q0 ∙ q1)

SLERP (q0, q1, t) =

(sin((1-t) ϴ))q0/(sinϴ)

+

(sin(tϴ))q1/(sinϴ)

Page 39: CSE 381 – Advanced Game Programming Skeletal Animation and Skinning Jack Skellington, from The Nightmare Before Christmas.

References

• UV Mapping a Human Head Mesh Utilizing LSCM – http://bgdm.katorlegaz.com/lscm_tute/lscm_tute.htm

• Wikipedia:– http://en.wikipedia.org/wiki/Skeletal_animation

• CADTutor– http://www.cadtutor.net/dd/bryce/anim/anim.html

• Tom’s Hardware– http://graphics.tomshardware.com/graphic/20000717/i

mages/keyframe.gif