CuuDuongThanCong.com PRACTICAL ALGORITHMS FOR 3D COMPUTER GRAPHICS SECOND EDITION K18939_FM.indd CuuDuongThanCong.com 11/15/13 1:51 PM This page intentionally left blank CuuDuongThanCong.com PRACTICAL ALGORITHMS FOR 3D COMPUTER GRAPHICS SECOND EDITION 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 CuuDuongThanCong.com 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 CuuDuongThanCong.com Contents Preface ix I Basic principles 1 Introduction 1.1 A note on mathematics for 3D computer 1.2 Getting up to speed and following up 1.3 Assumed knowledge 1.4 Computer graphics and computer games 1.5 The full spectrum graphics Basic theory and mathematical results 2.1 Coordinate systems 2.2 Vectors 2.3 Homogeneous coordinates 2.4 The line in vector form 2.5 The plane 2.6 Intersection of a line and a plane 2.7 Closest distance of a point from a line 2.8 Closest distance of approach between two lines 2.9 Reflection in a plane 2.10 Refraction at a plane 2.11 Intersection of a line with primitive shapes 2.12 Transformations 2.13 Parametric curves 2.14 Interpolation 2.15 B´ezier curves 2.16 Splines 2.17 Parametric surfaces 2.18 Angular interpolation (quaternions) v CuuDuongThanCong.com 11 11 14 14 16 17 18 19 20 21 22 23 26 41 42 46 50 56 58 vi CONTENTS Data structures for 3D graphics 3.1 Integer coordinates 3.2 Vertices and polygons 3.3 Algorithms for editing arrays of structures 3.4 Making an edge list from a list of polygonal faces 3.5 Finding adjacent polygons 3.6 Finding polygons adjacent to edges 69 69 70 76 79 81 84 Basic visualization 4.1 The rendering pipeline 4.2 Hidden surface drawing and 4.3 Anti-aliasing 4.4 Lighting and shading 4.5 Materials and shaders 4.6 Image and texture mapping 4.7 Perlin noise 4.8 Pseudo shadows 4.9 Line drawing 4.10 Tricks and tips rasterization 87 88 92 110 114 128 134 144 149 154 163 Realistic visualization 5.1 Radiometric lighting and shading 5.2 Ray tracing 5.3 Ray tracing optimization 5.4 Multi-threading and parallel processing 165 167 168 171 184 Computer animation 6.1 Keyframes (tweening) 6.2 Animating rigid motion 6.3 Character animation 6.4 Inverse kinematics 6.5 Physics 6.6 Animating cloth and hair 6.7 Particle modeling 187 187 189 202 215 237 246 252 II Practical 3D graphics 259 Real-time 3D: OpenGL 7.1 The basics 7.2 Native programming 7.3 The GL shading language 7.4 The P-buffer and framebuffer objects 7.5 Rendering a particle system using OpenGL 263 266 273 277 290 290 CuuDuongThanCong.com CONTENTS 7.6 III Summing up vii Mobile 3D: OpenGLES 8.1 OpenGLES 8.2 3D on iOS 8.3 3D on Android 8.4 Summing up The 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 292 293 294 296 304 313 complete package: OpenFX Using OpenFX The OpenFX files and folders structure Coordinate system and units User interface implementation The Animation module The Designer module The Renderer module Adding to the software Continuing to dissect OpenFX 319 321 324 327 328 331 341 350 362 369 Practical algorithms for modeling and procedural textures 371 10 Modeling with polygonal datasets 10.1 Triangulating polygons 10.2 Triangulating polygons with holes 10.3 Subdividing polygonal facets 10.4 Lofting 10.5 Surfaces of revolution 10.6 Beveling 10.7 Orienting surface normals 10.8 Delaunay triangulation 10.9 Boolean modeling 10.10Metaball modeling and marching cubes 10.11Texture coordinate generation 10.12Building polygonal primitives 375 376 385 391 394 400 401 405 407 413 429 442 451 11 Algorithms for procedural textures 11.1 A standard interface 11.2 CPU textures 11.3 GPU textures 11.4 Fur and short hair 453 454 463 492 497 Bibliography 499 Index 504 CuuDuongThanCong.com This page intentionally left blank CuuDuongThanCong.com Preface Taken as a whole, the topics covered in this book will enable you to create a complete suite of programs for three-dimensional computer animation, modeling and image synthesis It is about practical algorithms for each stage in the creative process The text takes you from the construction of polygonal models of objects (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 The content of the first edition of the book, published in 2001, arose from my experience of working on two comprehensive commercial 3D animation and modeling application programs (Envisage 3D and SoftFX) for the personal computer in the 1990s In that time the capabilities of both the hardware and software for creating computer graphics increased almost unimaginably Back in 2001 it was hard to envisage how radically the graphics scene would change again as the special purpose graphics processors (GPUs) rolled out, 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 part II of the first edition has been completely re-written with three new chapters covering the modern approach to real-time 3D programming and an introduction into 3D graphics for mobile devices I’ve chosen in part II to focus on OpenGL as the preferred API for gaining access to the high-speed hardware, primarily because of its simplicity, long pedigree and platform independence Many books cover OpenGL in great depth and ix CuuDuongThanCong.com 490 11 Algorithms for procedural textures Figure 11.34: Leather: (a) A derivative of Stucco textures and (b) Truncated Peas In practical terms this means using finite differences to determine the partial differentials Thus the change in surface normal ∆n = (∆nu , ∆nv , ∆nw ) is given by: ∆nu = ∆nv ∆nw = = − (f N oise(u + δ, v, w) − f N oise(u − δ, v, w)) − (f N oise(u, v + δ, w) − f N oise(u, v − δ, w)) − (f N oise(u, v, w + δ) − f N oise(u, v, w − δ)) δ is a small increment in the coordinates of magnitude about 0.01 This value is chosen because the texture cell is of unit size and we are only interested in features whose sizes are of the order of about 0.05 units on this scale The steepness of the slopes of the features are governed by a parameter α which scales ∆n before adding it to n: n = n + α∆n Applying a turbulent perturbation to (u, v, w) before proceeding with the stucco algorithm will give very ragged and irregular boundaries and mixing the stucco surface with other bumpy textures also makes interesting surfaces 11.2.29 Leather and cauliflower In this section we will look at a couple of interesting combination textures Leather is a natural material in common use and it would be nice to have a computer-generated texture that approximates to it There is no one correct leather pattern so we will look at two variations The first is produced by mixing stucco textures and the second is yet another variant of the ubiquitous Pea In some circumstances the cauliflower pattern, so called because it resembles the florets of that vegetable, could even be used as a leather texture The leather textures are illustrated in figure 11.34 and the cauliflower in figure 11.35 The stucco leather is composed with the sum of stucco (canyon) textures (using thresholds of 0.48, 0.54, 0.60 and 0.66) preprocessed by perturbing (u, v, w) with a little turbulence, viz.: CuuDuongThanCong.com 11.2 CPU textures 491 Figure 11.35: A Cauliflower vTurbulence (D, u , v , w ) ; u += D [ ] ∗ ; v += D [ ] ∗ ; w += D [ ] ∗ ; As each of the texture layers are added they are scaled and offset by: u += ; v += ; w += ; u ∗= ; v ∗= ; w ∗= ; // offset // scale The only slight complication in this texture compared with a basic stucco is that special consideration must be given to those points where the canyons overlap A canyon within a canyon must not be allowed to develop One way to achieve this is by using a flag to indicate if point (u, v, w) is on the floor or side of a canyon; the floor takes precedence After all patterns have been processed the flag is used to select the correct ∆n The second potential leather pattern follows the Pea algorithm until the slope α of the pea at (u, v, w) is determined Then α is modified as follows: Thresholded: (if α < 0.4 not bend n) Subjected to a functional transformation α = α0.25 Perturbed by an attenuated standard noise vector vN oise() at scale For the cauliflower surface the Crumpled algorithm is followed with the following exceptions: The point (u, v, w) (input coordinate) is not perturbed after the addition to ∆n The “bumps” are inverted so that they bump out The amplitude of ∆n is not attenuated during the phase in which the Pea procedure is repeated times The feature size is reduced much more rapidly; times on each loop CuuDuongThanCong.com 492 11 Algorithms for procedural textures Vertex position, normal and color (input) l a y o u t ( l o c a t i o n = ) i n vec3 V e r t e x P o s i t i o n ; l a y o u t ( l o c a t i o n = ) i n vec3 VertexNormal ; l a y o u t ( l o c a t i o n = ) i n vec4 V e r t e x C o l o u r ; Vertex position relative to the texture base axes l a y o u t ( l o c a t i o n = ) i n vec3 S h a d e r P o s i t i o n ; Material base axes for the texture unit cell u n i f o r m vec Uvector ; u n i f o r m vec Vvector ; u n i f o r m vec Wvector ; geometric uniform uniform uniform Transformations mat4 NormalMatrix ; mat4 ModelViewMatrix ; mat4 M o d e l V i e w P r o j e c t i o n M a t r i x ; Variables to pass to fragment shader out vec tnorm ; Vertex normal vector out vec PP ; Vertex position out vec t e x p o s ; Texture position out vec C o l o r ; Vertex color The three texture cell axes out vec uvec ; out vec vvec ; out vec wvec ; Listing 11.12: The global variables used for input and output to a GLSL vertex shader for all algorithmic and procedural textures 11.3 GPU textures The texture algorithms described in section 11.2 can, for the most part, be adapted to execute as part of a pair of GPU shader codes Like the CPU procedural textures the GPU textures make use of a repeating unit cell, and some other attributes that are passed to the GPU from the CPU application programs In this section, the GPU specific features of an algorithm that has already been discussed in section 11.2 will be elaborated Most of the work in surface texturing which has to be done on a per-pixel basis takes place in the GPU’s pixel (or fragment) program The vertex program has usually to little more than pass on the texture cell coordinates, vertex position, and surface normal vector Listings 11.12 and 11.13 gives a GLSL vertex shader that is sufficient to work for all the textures in this chapter Note: The code that follows from the fragment programs makes use of the output variables defined in listing 11.12 CuuDuongThanCong.com 11.3 GPU textures 493 void main ( void ) { Compute the transformed normal tnorm = NormalMatrix ∗ VertexNormal ; Set the built in variable with the vertex position g l P o s i t i o n = ModelViewProjectionMatrix ∗ VertexPosition ; Copy the vertex position PP= ModelViewMatrix ∗ V e r t e x P o s i t i o n ; Pass the vertex position in the texture cell and the texture cell edge vectors to fragment program texpos = ShaderPosition ; vvec = n o r m a l i z e ( NormalMatrix ∗ Vvector ) ; wvec = n o r m a l i z e ( NormalMatrix ∗ Wvector ) ; uvec = n o r m a l i z e ( NormalMatrix ∗ Uvector ) ; Set color ouptut variable Color = VertexColor ; } Listing 11.13: The code for the GLSL vertex shader that is used for all the algorithmic and procedural textures 11.3.1 Regular patterns Dots and checks and many other regular patterns are easily generated in a few lines of fragment shader code, for example, a 3D space filling checker texture that repeats once per unit cell can be coded as: //Assign interpolated input to a shader variable vec s u r f a c e c o l o u r=g l C o l o r ; //Offset into cell (from varying variable) vec p=f l o o r ( t e x p o s ) ; i n t i d = i n t ( p x)+ i n t ( p y)+ i n t ( p z ) ; which octet ? //Override shader color value i f ( ( ( i d / ) ∗ ) != i d ) { s u r f a c e c o l o u r=vec ( s e c o n d c o l o r , ) ; } //other code and pass on value of s u r f a c e c o l o u r The FCC dots pattern of section 11.2.2 requires a static array of six vectors vec3 ppp[6] During execution each vector is tested against the texture cell coordinate When the texture coordinate of the pixel lies within a spherical volume centered on one of the list of vectors (ppp[] ) it is colored according to the code in listing 11.14 11.3.2 Regular bumps Equivalent GPU textures for the bumps, pimples and dimples (section 11.2.4) require an array of 35 cell relative positions for the center of the bumps, and CuuDuongThanCong.com 494 11 Algorithms for procedural textures vec p = t e x p o s ; p=f r a c t ( p ) ; vec s u r f a c e c o l o u r=g l C o l o r ; f l o a t e= // the edge fraction f o r ( i =0; i zb ) { zb=( S i z e −a ) ; i f ( i == | | i == ) { s u r f a c e c o l o u r=vec4 ( Colour1 , ) ; // alternative colors } e l s e i f ( i == | | i == ) { s u r f a c e c o l o u r=vec4 ( Colour2 , ) ; // for the crystal layers } else { s u r f a c e c o l o u r=vec4 ( Colour3 , ) ; } i f ( a > e ) { // use mix function to blend sphere edges s u r f a c e c o l o u r=mix ( s u r f a c e c o l o u r , g l C o l o r , ( a−e ) / ( S i z e −e ) ) ; } } } } Listing 11.14: Generating a volume dot pattern a procedure for bending the surface normal The perturbation of the normal vector can follow closely the form of the code in the non-GPU C code whilst taking advantage of the GLSL’s vector operations and built in functions A few key lines in the shader are shown in listing 11.15 11.3.3 Textures involving noise Noise-based textures in a GPU require the use of a texture lookup A 3D texture can be thought of as a large table of random numbers that will give a reproducible and periodic random number when looked-up for a 3D texture coordinate Our noise texture array contains 64 × 64 × 64 entries; the OpenFX code in file “gldesign noise.c” that can be found in folder “render2” shows how to build a suitable noise texture for shaders using Perlin’s algorithm In the case of the contour texture of section 11.2.9 the noise value from the 3D noise texture lookup at the texture cell coordinate is passed through a cyclic smoothing function to amplify/attenuate the underlying surface color as given in listing 11.16 This texture lookup will naturally repeat for any input coordinate outside the range [0, 1] Other textures like clouds and caustics follow this approach closely in their GPU implementations CuuDuongThanCong.com 11.3 GPU textures 495 vec vnorm = n o r m a l i z e ( tnorm ) ; // use built in function p = t e x p o s ; // texture cell relative position f o r ( i i =0; i i 1.0 − T h i c k n e s s ) { s u r f a c e c o l o u r=vec ( n oiseH ∗C1 , ) ; } Listing 11.16: A contour texture shader 11.3.4 Perturbation of regular textures The leopard and reptile textures are obtained by perturbing a regular pattern, such as that set up in section 11.3.1 The GPU implementation can then follow the logic given in figures 11.9 and 11.10 CuuDuongThanCong.com 496 11 Algorithms for procedural textures #define PATTERN( x , y , z ) ( (( z )+127.0)∗511.0 + (( x )+127.0)∗255.0 + (( y )+127.0)) \ \ f l o a t zb = ; // maximum value of texture Z buffer vec p f = t e x p o s ; // texture position from varying variable vec p=f r a c t ( p f ) ; // fractional part p f=f l o o r ( p f ) ; // 27 cell patterns are needed to fill the texture cell i n t i i ; f o r ( i i =0; i i