390 likes | 500 Views
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?
E N D
CSE 381 – Advanced Game ProgrammingSkeletal 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
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.
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
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: • The key data are assigned to the keyframes • In-between frames are interpolated
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
The MD2 File Layout MD2 Header Skin Offset Skin Data Texture Coord Offset Texture Coords Triangle Offset Triangles GL Command Offset GL Commands Data Keyframe Offset Keyframes File End Offset
The MD2 Header struct 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
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]; };
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)
Alternative Approaches • For better results we could use other techniques • more efficient • more realistic • Frame Hierarchies • Bone Hierarchies • Skinning • Quaternions
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
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
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
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
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
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
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
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
Hierarchical Model • Associate bones with vertices
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
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
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
Typical Skinning Algorithm • Transforms vertex positions from their object space (skin space) into bone space using the IBMs. • Performs the key-frame based animation for that frame, which transform the vertex positions back into skin space in its animated pose. • The model is then rendered.
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
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)
Quaternion Advantages • Interpolation is easy between 2 quaternions • Smoother animation • Take up less space than matrices • Can easily be converted to matrices for rendering
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)
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]
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]
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
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
LERPs AND SLERPs • 2 Ways of Interpolating Quaternions • LERP • Linear Interpolation • SLERP • Spherical Linear Interpolation SLERP LERP
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ϴ)
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/images/keyframe.gif