Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. CHARACTER ANIMATION WITH DIRECT3D ® CARL GRANBERG Australia, Brazil, Japan, Korea, Mexico, Singapore, Spain, United Kingdom, United States Charles River Media A part of Course Technology, Cengage Learning Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. © 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 1 2 3 4 5 6 7 11 10 09 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 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. To Aino… again. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. About 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. iv 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. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. v Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .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 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Loading 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 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 5 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 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 7 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 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 9 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 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... conventions used in this book In this first chapter, you’ll find the following: What is character animation? A brief history of character animation Comparison of skeletal animation and morphing animation Coding conventions ease purchase PDF Split-Merge on www.verypdf.com to remove this watermark 1 2 Character Animation with Direct3D W HAT I S C HARACTER A NIMATION ? This somewhat silly question may seem... more humanoid characters Four years later, Sierra On-Line released Kings Quest: Quest for the Crown, staring Sir Graham (Figure 1.2) Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Chapter 1 Introduction to Character Animation FIGURE 1.1 Pac-Man FIGURE 1.2 Sir Graham ease purchase PDF Split-Merge on www.verypdf.com to remove this watermark 3 4 Character Animation with Direct3D... version 2.0 A decent processor Not too little RAM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 1 Introduction to Character Animation Hello, dear reader, and welcome to this book about character animation! I hope you enjoy it and find it useful In this chapter, I’ll start you off slow by looking at character animation in general as well as a brief history of the same topic... chipset and with it brought affordable 3D accelerator cards to the masses One of the first reputable games taking advantage of this new technology was the game Quake With Quake came seamless characters (albeit low-poly) animated using vertex morphing Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Chapter 1 Introduction to Character Animation 5 FIGURE 1.4 A character built... The first real 3D character was seen in the game Alone in the Dark, which was released in 1992 It featured characters in full 3D with interpolated animations These characters had an extremely low polygon count and were built from several blocks (one for each limb) An example character from this era can be seen in Figure 1.4 You can easily see the obvious gaps between the joints in this character, but... answers the question, “What is the goal of character animation? ” Historically, characters were drawn (or pixeled) and animated by making multiple pictures showing the character at a slightly different pose These pictures would then loop to give the impression of movement With today’s video cards, it is possible to have full three-dimensional characters and animate them with some of the various techniques... 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... but is it really? Wikipedia defines it as follows: Character animation is a specialized area of the animation process concerning the animation of one or more characters featured in an animated work.” -Wikipedia Animated work Well, I guess games falls under that category However, I would probably have tried to define it along the lines of “Making a character move in a realistic way.” Although, I suppose... the final mesh The main use of morphing animation these days is facial animation But in the past it was also used to create full-body character animations For instance, the Quake I and II engines used this approach for their characters using the popular but slightly outdated MD2 file format [Schoenblum07, Leimbach02] See Figure 1.5 for an example of morphing animation In this figure, only the HAPPY... and had a great set of animations Characters continued along the same lines through the late ’80s, with steadily increasing pixel count and/or color In 1987, LucasArts developed its first version of the SCUMM engine (Script Creation Utility for Maniac Mansion) and with it they released several adventure games, including Maniac Mansion, Monkey Island, Loom, and many more The characters of this era (late . following: What is character animation? A brief history of character animation Comparison of skeletal animation and morphing animation Coding conventions Please purchase PDF Split-Merge on www.verypdf.com. RAM xiv Introduction Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 1 Introduction to Character Animation 1 Hello, dear reader, and welcome to this book about character animation! I hope you. vertex morphing. 4 Character Animation with Direct3D FIGURE 1.3 An animation sequence of Tom (The Tales of Bingwood). © BugFactory 2008. Please purchase PDF Split-Merge on www.verypdf.com to remove