For your lifelong learning solutions, visit courseptr.com Visit our corporate website at cengage.com Character Animation with Direct3D Carl Granberg Publisher and General Manager, Cours
Trang 2C HARACTER A NIMATION
Charles River Media
A part of Course Technology, Cengage Learning
Trang 3© 2009 Course Technology, a part of Cengage Learning
ALL RIGHTS RESERVED No part of this work covered by the copyright herein may be reproduced, transmitted, stored, or used in any form or by any means graphic, electronic, or mechanical, including but not limited to photocopying, recording, scanning, digitizing, taping, Web distribution, information networks, or information storage and retrieval systems, except
as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the publisher.
Microsoft, Windows, Direct3D, and DirectX are either registered trademarks
or trademarks of Microsoft Corporation in the United States and/or other countries All other trademarks are the property of their respective owners Library of Congress Control Number: 2008931080
ISBN-13: 978-1-58450-570-9 ISBN-10: 1-58450-570-2
Course Technology, a part of Cengage Learning
20 Channel Center Street Boston, MA 02210 USA
Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Kingdom,
Australia, Mexico, Brazil, and Japan Locate your local office at: international.
cengage.com/region
Cengage Learning products are represented in Canada by Nelson Education, Ltd.
For your lifelong learning solutions, visit courseptr.com Visit our corporate website at cengage.com
Character Animation with Direct3D
Carl Granberg
Publisher and General Manager,
Course Technology PTR:
Stacy L Hiquet
Associate Director of Marketing:
Sarah Panella
Content Project Manager:
Jessica McNavich
Marketing Manager: Jordan Casey
Senior Acquisitions Editor: Emi Smith
Project Editor and Copy Editor:
Dan Foster, Scribe Tribe
Technical Reviewer: Henrik Enqvist
CRM Editorial Services Coordinator:
Jennifer Blaney
Editorial Services Coordinator: Jen Blaney
Interior Layout: Jill Flores
Cover Designer: Mike Tanamachi
CD-ROM Producer: Brandon Penticuff
Indexer: Valerie Haynes Perry
Proofreader: Ruth Saavedra and
Mike Beady
Printed in the United States of America
For product information and technology assistance, contact us at
Cengage Learning Customer & Sales Support, 1-800-354-9706
For permission to use material from this text or product,
submit all requests online at cengage.com/permissions
Further permissions questions can be emailed to
permissionrequest@cengage.com
eISBN-10: 1-58450-630-X
Trang 4To Aino… again.
Trang 5About the Author
Carl Granberg has been creating games on a hobby basis since the old DOS-based
Mode 13h graphics, after which he moved on to DirectDraw and finally Direct3D graphics He received his master of science in computing at Curtin University, Perth, Australia, and has since been working in the Finnish game industry for 3 years
He is currently working as a Tools programmer at Remedy Entertainment in Finland He’s also involved with a group of hobby game developers that goes by
the name of BugFactory (www.bugfactory.fi), which has just released its first
title, The Tales of Bingwood.
For matters relating to this book, he can be contacted at carl@bugfactory.fi.
Acknowledgments
As always with a project such as this, there’s a long list of people deserving my thanks Writing a book is not a small feat (yes, patting myself on the back), and it is also some-thing I could not have done alone So first off I must thank the people of Charles River Media for wanting to publish this hunk of technical mumbo jumbo, and especially Emi Smith, my editor Big thanks also to Dan Foster, my project editor, and Henrik Enqvist of Remedy Entertainment, my technical editor Henrik also supplied the code for the Inverse Kinematics chapter and the Wrinkle Maps example, for which I owe him thanks
Next I’d like to thank my friend Markus Tuppurainen for supplying some of the art for this book—sketches and pixel characters—as well as for helping me make the Soldier model
Finally I’d like to thank my wife and my family for their support through this last year, which has been challenging in many ways
Last, but not least, thank you for buying this book I hope you enjoy it and also
learn something from it
Trang 6Introduction .xi
1 Introduction to Character Animation 1
What Is Character Animation? 2
A Brief History of Character Animation 2
Morphing Animation and Skeletal Animation 5
The Soldier 7
Coding Conventions 8
Conclusions 10
Further Reading 10
2 A Direct3D Primer 11
DirectX 9 versus DirectX 10 12
STL and the D3DX Library 13
Setting Up a Project in Visual Studio Express 2008 15
VC++ Directories 15
Creating a New Project 17
Linking DirectX Libraries 18
Application Framework 19
WinMain 21
Creating the Window 22
Basic Rendering 24
Creating the DirectX Device 25
Direct3D Rendering Loop 26
Loading a Mesh 27
Trang 7Loading an Effect 28
Rendering a Mesh with an Effect 30
Conclusions 32
Further Reading 32
3 Skinned Meshes 33
Skinned Meshes Overview 34
Bone Hierarchies 35
The D3DXFRAME Structure 37
Loading a Bone Hierarchy 40
The CreateFrame() Function 41
The CreateMeshContainer() Function 41
The DestroyFrame() Function 42
The DestroyMeshContainer() Function 42
The ID3DXAllocateHierarchy 42
Applying a Mesh to the Bone Hierarchy 47
Software Skinning Overview 49
Hardware Skinning Overview 49
Software Skinning Implementation 50
Hardware Skinning Implementation 59
Rendering Static Meshes in Bone Hierarchies 67
Conclusions 71
Chapter 3 Exercises 72
Further Reading 72
4 Skeletal Animation 73
Keyframe Animation 74
Animation Sets 76
The ID3DXAnimationController Interface 79
Loading the Animation Data 79
Multiple Animation Controllers 82
Conclusions 83
Chapter 4 Exercises 84
vi Contents
Trang 85 Advanced Skeletal Animation Techniques 85
The Track Structure 86
Blending Multiple Animations 88
Compressing Animation Sets 90
Animation Callback Events 92
Motion Capture (Mocap) 96
Optical Motion Capture Systems 97
Magnetic Motion Capture Systems 98
Mechanical Motion Capture Systems .99
Comparison of the Different Mocap Systems 100
Lapland Studio Interview 101
Conclusions 107
Chapter 5 Exercises 107
Further Reading 107
6 Physics Primer 109
Introduction to Rigid Body Physics 110
Forces 111
The Effect of Forces on a Rigid Body 112
Quaternions 114
Describing the World 119
The Oriented Bounding Box Class 120
Physics Simulation 124
Position, Velocity, and Acceleration 126
The Particle 128
The Spring 131
Conclusions 134
Chapter 6 Exercises 135
Further Reading 135
Contents vii
Trang 97 Ragdoll Simulation 137
Introduction to the Bullet Physics Engine 139
Integrating the Bullet Physics Library 140
Download Bullet 140
Build the Bullet Libraries 141
Setting Up a Custom Direct3D Project 142
Hello btDynamicsWorld 144
Constraints 147
Constructing the Ragdoll 150
Updating the Character Mesh from the Ragdoll 158
Getting a Bone’s Position from an OBB 159
Getting a Bone’s Orientation from an OBB 161
Updating the Bone Hierarchy 162
Conclusions 164
Chapter 7 Exercises 165
8 Morphing Animation 167
Basics of Morphing Animation 168
Using Multiple Morph Targets 170
Morphing Animation on the GPU 173
Custom Vertex Formats 174
Creating the Morph Vertex Declaration 177
The Morphing Vertex Shader 180
Combining Skeletal and Morphing Animation 183
Skeletal/Morphing Vertex Format 185
Skeletal/Morphing Vertex Shader 188
Conclusions 191
Chapter 8 Exercises 192
viii Contents
Trang 109 Facial Animation 193
Facial Animation Overview 194
Facial Expressions 194
The Eye of the Beholder 196
The Face Class 198
Loading Multiple Targets from One X File 200
Extracting Meshes from a D3DXFRAME Hierarchy 201
Implementing the Face Class 202
The Face Controller Structure 205
Animation Channels 205
Face Factory 208
Conclusions 215
Chapter 9 Exercises 216
10 Making Characters Talk 217
Phonemes 218
Visemes 221
Basics of Speech Analysis 225
Sound Data 227
The WAVE Format 227
Automatic Lip-Syncing 232
Conclusions 234
Further Reading 235
11 Inverse Kinematics 237
Introduction to Inverse Kinematics 238
Solving the IK Problem 240
Look-At Inverse Kinematics 240
Two-Joint Inverse Kinematics 246
Conclusions 252
Chapter 11 Exercises 253
Further Reading 253
Contents ix
Trang 1112 Wrinkle Maps 255
Introduction to Normal Mapping 256
What Are Normal Maps? 258
Encoding Normals as Color 261
Putting the Normal Map to Use 262
The TBN-Matrix 265
Converting a Mesh to Support Normal Mapping 265
The Normal Mapping Shader 270
Creating Normal Maps 277
Creating Normal Maps in Practice 280
Specular Highlight 281
Specular Maps 284
Wrinkle Maps 288
Conclusions 292
Chapter 12 Exercises 292
Further Reading 293
13 Crowd Simulation 295
Flocking Behaviors 296
Boids 297
Introduction to Crowd Simulation 304
Smart Objects 308
Following a Terrain 310
Conclusions 313
Chapter 13 Exercises 313
Further Reading 313
x Contents
Trang 1214 Character Decals 315
Introduction to Decals 316
Picking a Hardware-Rendered Mesh 318
Creating Decal Geometry 325
Calculating the Exact Hit Position 328
Selecting Triangles for the Decal Mesh 330
Copying the Skinning Information 331
The CharacterDecal Class 337
Calculating Decal UV Coordinates 339
Conclusions 346
Chapter 14 Exercises 347
15 Hair Animation 349
Hair Representation 350
Hair Modeling 351
The Control Hair Class 352
The HairPatch Class 356
Growing the Hair 359
Rendering the Hair Patch 362
Creating a Haircut 367
Animating the Control Hairs 370
The Hair Class 373
Conclusions 376
Chapter 15 Exercises 377
Further Reading .377
Contents xi
Trang 1316 Putting It All Together 379
Attaching the Head to the Body 380
The Character Class 387
Future Work 389
Character Level-of-Detail 390
Root Motion versus Non-Root Motion 392
Animation Trees/Animation Graph 393
Track Masks 395
Separate Mesh and Animation Files 395
Alan Wake Case Study 396
Interview with Sami Vanhatalo, Senior Technical Artist 397
Interview with Henrik Enqvist, Animation Programmer 402
Final Thoughts 408
Further Reading 408
Index .409
xii Contents
Trang 14INTENDED AUDIENCE
This book is primarily aimed at teaching indie and hobby game developers how to create character animation with Direct3D Also, the seasoned professional game developer may find some interesting things in this book
You will need a solid understanding of the C++ programming language as well
as general object-oriented programming skills
As for DirectX, you will need to know the very basics at least In other words, you will need to have completed at least an introductory book on DirectX before starting this one
On top of all these prerequisites, you should also have basic knowledge of the High Level Shading Language (HLSL), since many of the effects done in this book will use it
If you feel that you can’t honestly say you meet these prerequisites, I suggest you brush up on these topics before continuing with this book rather than trying to learn them as you go You will quite quickly be faced with some advanced topics, and, if you are faced with them for the first time, they will be quite hard to handle without trying to learn HLSL or similar topics as well
But, hey, this is just my suggestion After all, that certainly wasn’t how I learned the stuff I know today
USING THIS BOOK
This book has been divided into 16 chapters, each of which usually focuses on one
or a few related components I aim to keep this book very “hands-on,” so a lot of code will be covered throughout You’re probably best off reading the book from cover to cover, since a lot of stuff covered in the earlier chapters will be built upon
in later chapters
Introduction
Trang 15The topic of character animation is a very general one that can be applied to all game genres It doesn’t matter if you are making your own role-playing games (RPG), real-time strategy games (RTS), first-person shooter games (FPS), or a game from another genre As long as you plan to include characters in your game, you will benefit greatly from learning the topics covered in this book
Because the topic is extremely code intensive, you won’t find most of the code written out in full throughout this book Rather, use the book as a manual to un-derstand the code found on the accompanying CD-ROM Also, if you have time I suggest that you try to implement the topics covered here completely on your own, and use the code provided only as guidelines or a helping hand Even though this might seem like a tedious waste of time, I can guarantee that it will greatly increase your understanding of the different techniques (although, of course, I know that 95% of readers will pay no attention whatsoever to this recommendation)
To get to the fun stuff as soon as possible, I won’t waste time covering simple things like basic Direct3D rendering, basic data structures, and so on There are more books available on these topics than absolutely necessary, so if you feel you’re lacking in knowledge about basic DirectX programming, I suggest you go and pick up such a book before getting back to this one Also, I’ll rely heavily on the Standard Template Library (STL) for all basic data structures such as vectors, stacks, queues, etc For all generic 3D math functions, mesh and texture loading, and more, I will be using the D3DX library This is a part of Direct3D and is a great help when developing 3D applications (as you’ll soon see)
You’ll find all the examples on the CD-ROM together with their executables, models, textures, and more The examples are ordered according to the chapter number and the example number Usually the examples are fairly simple and focus only on one specific thing At the end of the book, however, there will be a character that can walk, talk, collide with objects, fall, and more
SYSTEM REQUIREMENTS
Windows Vista/Windows XP DirectX SDK
Graphic card supporting Vertex and Pixelshader version 2.0
A decent processor Not too little RAM
xiv Introduction