Tải bản đầy đủ (.pdf) (20 trang)

Character Animation with Direct3D- P1 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (332.38 KB, 20 trang )

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

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
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.
12 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
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
14 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
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
16 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
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
INTENDED 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
xiii
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
The 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
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 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. You
won’t get to do any coding in this chapter, but toward the end I’ll include a brief
overview of the coding 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
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
WHAT IS CHARACTER ANIMATION?
This somewhat silly question may seem pretty easy to answer at a first glance, 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 that better 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 covered in this book.
New ways of animating character models pop up each year, pushing the evolu-
tion of the field forward. The techniques covered in this book are by no means
cutting edge; rather, they are the foundational techniques that all (or most) cutting-
edge technologies are based upon. Techniques such as skeletal animation, morphing,

ragdoll physic simulation, and inverse kinematics have already existed for a long time
(in terms of game evolution at least).
Still, at the end of this book you will have all the tools you need to create your
own game featuring realistic character animation.
A BRIEF HISTORY OF CHARACTER ANIMATION
Let’s start from the beginning! Say hello to one of the first well-known computer
game characters of our time: Pac-Man (Figure 1.1).
This 28 ϫ 28 pixel character (developed by Namco) was released in Japan in
1980 and is still today the most famous arcade game of all time. This character
(looking more like a pizza missing a slice) slowly gave way to more humanoid
characters. Four years later, Sierra On-Line released Kings Quest: Quest for the
Crown, staring Sir Graham (Figure 1.2).
2
Character Animation with Direct3D
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 1 Introduction to Character Animation 3
FIGURE 1.1
Pac-Man.
FIGURE 1.2
Sir Graham.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Sir Graham might not feature many more pixels than Pac-Man did, but at least
he was more colorful 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 ’80s to early ’90s) pretty much shared the same complexity—Figure 1.3
shows an example.
Suddenly the ’90s hit, and with the new decade the first 3D games brought a

whole new set of problems. Some of the more famous games included Wolfenstein
3D and, later, DOOM—although these games can’t really be called proper 3D games
since they still used 2D sprites for enemies and characters (usually drawn from eight
angles, depending on their orientation to the player). The first real 3D character was
seen in the game Alone in the Dark, which was released in 1992. It featured charac-
ters 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
back then there was usually no lighting of the models and the resolution was so
small that these gaps were often hidden from the player.
Jumping ahead in time a few years, we reach 1996, when 3dfx launched the first
Voodoo 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.
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 this watermark.
MORPHING ANIMATION AND SKELETAL ANIMATION
Morphing animation (or per-vertex animation) works by blending two (or more)
meshes together on a per-vertex basis. The two meshes need to have the same
amount of vertices, and their polygons need to be arranged in the same way for this
technique to work. Each mesh representing a pose of the character is referred to as
a morph target. More than one morph target may be used to blend 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 ANGRY frames are the actual target meshes. The meshes in between
are created by interpolating the vertex positions smoothly over time.
Chapter 1 Introduction to Character Animation 5
FIGURE 1.4
A character built from blocks.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

×