Table Of ContentPRACTICAL ALGORITHMS FOR
3D COMPUTER
GRAPHICS
S E C O N D E D I T I O N
K18939_FM.indd 1 11/15/13 1:51 PM
TThhiiss ppaaggee iinntteennttiioonnaallllyy lleefftt bbllaannkk
PRACTICAL ALGORITHMS FOR
3D COMPUTER
GRAPHICS
S E C O N D E D I T I O N
R. STUART FERGUSON
THE QUEEN’S UINVERSITY OF BELFAST
UK
Boca Raton London New York
CRC Press is an imprint of the
Taylor & Francis Group, an informa business
AN A K PETERS BOOK
K18939_FM.indd 3 11/15/13 1:51 PM
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2014 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business
No claim to original U.S. Government works
Version Date: 20131112
International Standard Book Number-13: 978-1-4665-8253-8 (eBook - PDF)
This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to publish reliable data and information, but
the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to
trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained.
If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical,
or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without
written permission from the publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.copyright.com/) or contact the Copyright
Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a
variety of users. For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to
infringe.
Visit the Taylor & Francis Web site at
http://www.taylorandfrancis.com
and the CRC Press Web site at
http://www.crcpress.com
Contents
Preface ix
I Basic principles 1
1 Introduction 3
1.1 A note on mathematics for 3D computer graphics . . . . . . . . 4
1.2 Getting up to speed and following up. . . . . . . . . . . . . . . 5
1.3 Assumed knowledge . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4 Computer graphics and computer games . . . . . . . . . . . . . 8
1.5 The full spectrum. . . . . . . . . . . . . . . . . . . . . . . . . . 9
2 Basic theory and mathematical results 11
2.1 Coordinate systems . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 Homogeneous coordinates . . . . . . . . . . . . . . . . . . . . . 14
2.4 The line in vector form . . . . . . . . . . . . . . . . . . . . . . . 16
2.5 The plane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.6 Intersection of a line and a plane . . . . . . . . . . . . . . . . . 18
2.7 Closest distance of a point from a line . . . . . . . . . . . . . . 19
2.8 Closest distance of approach between two lines . . . . . . . . . 20
2.9 Reflection in a plane . . . . . . . . . . . . . . . . . . . . . . . . 21
2.10 Refraction at a plane . . . . . . . . . . . . . . . . . . . . . . . 22
2.11 Intersection of a line with primitive shapes. . . . . . . . . . . . 23
2.12 Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.13 Parametric curves . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.14 Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.15 B´ezier curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.16 Splines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.17 Parametric surfaces . . . . . . . . . . . . . . . . . . . . . . . . 56
2.18 Angular interpolation (quaternions) . . . . . . . . . . . . . . . 58
v
vi CONTENTS
3 Data structures for 3D graphics 69
3.1 Integer coordinates . . . . . . . . . . . . . . . . . . . . . . . . 69
3.2 Vertices and polygons . . . . . . . . . . . . . . . . . . . . . . . 70
3.3 Algorithms for editing arrays of structures . . . . . . . . . . . 76
3.4 Making an edge list from a list of polygonal faces . . . . . . . 79
3.5 Finding adjacent polygons . . . . . . . . . . . . . . . . . . . . 81
3.6 Finding polygons adjacent to edges . . . . . . . . . . . . . . . 84
4 Basic visualization 87
4.1 The rendering pipeline . . . . . . . . . . . . . . . . . . . . . . . 88
4.2 Hidden surface drawing and rasterization . . . . . . . . . . . . 92
4.3 Anti-aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
4.4 Lighting and shading . . . . . . . . . . . . . . . . . . . . . . . . 114
4.5 Materials and shaders . . . . . . . . . . . . . . . . . . . . . . . 128
4.6 Image and texture mapping . . . . . . . . . . . . . . . . . . . . 134
4.7 Perlin noise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
4.8 Pseudo shadows . . . . . . . . . . . . . . . . . . . . . . . . . . 149
4.9 Line drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
4.10 Tricks and tips . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
5 Realistic visualization 165
5.1 Radiometric lighting and shading . . . . . . . . . . . . . . . . 167
5.2 Ray tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
5.3 Ray tracing optimization. . . . . . . . . . . . . . . . . . . . . . 171
5.4 Multi-threading and parallel processing . . . . . . . . . . . . . 184
6 Computer animation 187
6.1 Keyframes (tweening) . . . . . . . . . . . . . . . . . . . . . . . 187
6.2 Animating rigid motion . . . . . . . . . . . . . . . . . . . . . . 189
6.3 Character animation . . . . . . . . . . . . . . . . . . . . . . . . 202
6.4 Inverse kinematics . . . . . . . . . . . . . . . . . . . . . . . . . 215
6.5 Physics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
6.6 Animating cloth and hair . . . . . . . . . . . . . . . . . . . . . 246
6.7 Particle modeling . . . . . . . . . . . . . . . . . . . . . . . . . 252
II Practical 3D graphics 259
7 Real-time 3D: OpenGL 263
7.1 The basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
7.2 Native programming . . . . . . . . . . . . . . . . . . . . . . . . 273
7.3 The GL shading language . . . . . . . . . . . . . . . . . . . . . 277
7.4 The P-buffer and framebuffer objects . . . . . . . . . . . . . . . 290
7.5 Rendering a particle system using OpenGL . . . . . . . . . . . 290
CONTENTS vii
7.6 Summing up . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
8 Mobile 3D: OpenGLES 293
8.1 OpenGLES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
8.2 3D on iOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
8.3 3D on Android . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
8.4 Summing up . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
9 The complete package: OpenFX 319
9.1 Using OpenFX . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
9.2 The OpenFX files and folders structure . . . . . . . . . . . . . 324
9.3 Coordinate system and units . . . . . . . . . . . . . . . . . . . 327
9.4 User interface implementation . . . . . . . . . . . . . . . . . . 328
9.5 The Animation module . . . . . . . . . . . . . . . . . . . . . . 331
9.6 The Designer module . . . . . . . . . . . . . . . . . . . . . . . . 341
9.7 The Renderer module . . . . . . . . . . . . . . . . . . . . . . . 350
9.8 Adding to the software . . . . . . . . . . . . . . . . . . . . . . . 362
9.9 Continuing to dissect OpenFX . . . . . . . . . . . . . . . . . . 369
III Practical algorithms for modeling and procedural textures 371
10 Modeling with polygonal datasets 375
10.1 Triangulating polygons . . . . . . . . . . . . . . . . . . . . . . 376
10.2 Triangulating polygons with holes . . . . . . . . . . . . . . . . 385
10.3 Subdividing polygonal facets . . . . . . . . . . . . . . . . . . . 391
10.4 Lofting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
10.5 Surfaces of revolution . . . . . . . . . . . . . . . . . . . . . . . 400
10.6 Beveling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
10.7 Orienting surface normals . . . . . . . . . . . . . . . . . . . . . 405
10.8 Delaunay triangulation . . . . . . . . . . . . . . . . . . . . . . 407
10.9 Boolean modeling . . . . . . . . . . . . . . . . . . . . . . . . . 413
10.10Metaball modeling and marching cubes . . . . . . . . . . . . . 429
10.11Texture coordinate generation . . . . . . . . . . . . . . . . . . . 442
10.12Building polygonal primitives . . . . . . . . . . . . . . . . . . . 451
11 Algorithms for procedural textures 453
11.1 A standard interface . . . . . . . . . . . . . . . . . . . . . . . . 454
11.2 CPU textures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
11.3 GPU textures . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
11.4 Fur and short hair . . . . . . . . . . . . . . . . . . . . . . . . . 497
Bibliography 499
Index 504
TThhiiss ppaaggee iinntteennttiioonnaallllyy lleefftt bbllaannkk
Preface
Taken as a whole, the topics covered in this book will enable you to create a
completesuiteofprogramsforthree-dimensionalcomputeranimation,modeling
andimagesynthesis. Itisaboutpracticalalgorithmsforeachstageinthecreative
process. Thetexttakesyoufromtheconstructionofpolygonalmodelsofobjects
(real or imaginary) through rigid body animation into hierarchical character
animation and finally down the rendering pipeline for the synthesis of realistic
images of the models you build.
Thecontentofthefirsteditionofthebook,publishedin2001,arosefrommy
experienceofworkingontwocomprehensivecommercial3Danimationandmod-
elingapplicationprograms(Envisage 3DandSoftFX)forthepersonalcomputer
inthe1990s. Inthattimethecapabilitiesofboththehardwareandsoftwarefor
creating computer graphics increased almost unimaginably.
Back in 2001 it was hard to envisage how radically the graphics scene would
changeagain asthespecialpurposegraphicsprocessors(GPUs)rolledout, with
ever increasing capabilities. Since 2001 we have been finding new and exciting
ways to take advantage of the advancements in graphics technology through
an open source 3D animation and modeling program called OpenFX and in
investigating how to enhance the immersive experience with virtual reality [59].
I am sure that the computer games of the future will have to interact with all
the human senses and not just our sight. Glimpses of this are here now in the
Nintendo Wii and the Microsoft Kinect.
Getting the opportunity to bring this book up to date in a second edition
is a marvelous opportunity to include some interesting algorithms that were
either still to be found useful or had a very low profile back in 2001. Whilst
algorithms and most of the basic principles of computer graphics remain the
same, the practicalities have changed unrecognizably in the last twelve years.
So the second edition allows us to look at implementations in a new way and
partIIofthefirsteditionhasbeencompletelyre-writtenwiththreenewchapters
coveringthemodernapproachtoreal-time3Dprogrammingandanintroduction
into 3D graphics for mobile devices.
I’ve chosen in part II to focus on OpenGL as the preferred API for gaining
accesstothehigh-speedhardware,primarilybecauseofitssimplicity, longpedi-
greeandplatformindependence. ManybookscoverOpenGLingreatdepthand
ix
Description:""A valuable book to accompany any course that mixes the theory and practice of 3D graphics. The book's web site has many useful programs and code samples.""-Karen Rafferty, Queen's University, Belfast""The topics covered by this book are backed by the OpenFX modeling and animation software. This is