MATHEMATICAL TOOLS IN COMPUTER GRAPHICS WITH C# IMPLEMENTATIONS
This page intentionally left blank
World Scientific N E W J E R S E Y L O N D O N S I N G A P O R E B E I J I N G S H A N G H A I H O N G K O N G TA I P E I C H E N N A I
Published by World Scientific Publishing Co. Pte. Ltd. 5 Toh Tuck Link, Singapore 596224 USA office: 27 Warren Street, Suite 401-402, Hackensack, NJ 07601 UK office: 57 Shelton Street, Covent Garden, London WC2H 9HE British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library. MATHEMATICAL TOOLS IN COMPUTER GRAPHICS WITH C# IMPLEMENTATIONS Copyright 2008 by World Scientific Publishing Co. Pte. Ltd. All rights reserved. This book, or parts thereof, may not be reproduced in any form or by any means, electronic or mechanical, including photocopying, recording or any information storage and retrieval system now known or to be invented, without written permission from the Publisher. For photocopying of material in this volume, please pay a copying fee through the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, USA. In this case permission to photocopy is not required from the publisher. ISBN-13 978-981-279-102-3 ISBN-10 981-279-102-7 ISBN-13 978-981-279-103-0 (pbk) ISBN-10 981-279-103-5 (pbk) Printed in Singapore.
Preface Computer graphics is applied in many different fields including the entertainment industry, medical and geographic visualization and industrial design. Successful development of computer graphics programs and algorithms requires knowledge from a diverse set of fields to be combined and applied effectively. The most important part of any computer graphics algorithm is the underlying mathematical tools that are used to analyze and develop the algorithm. This book introduces computer graphics from a mathematical perspective. The basic computer graphics principles are introduced as well as more advanced topics. The mathematical techniques are developed in detail. Selected topics include: basic transforms, curves, surfaces and subdivision surfaces. New techniques such as wavelets, fractals, parameterization and fluid simulation are also included. Throughout a large portion of the text a new curve and surface algorithm are developed to illustrate the use of mathematics to develop computer graphics algorithms. C# implementations for many of the algorithms are provided throughout the book. The C# implementation allows the reader to obtain practical knowledge along with the theoretical knowledge. The provided C# implementations can be downloaded from our websites for use in other software projects. Book websites http://eve.uj.ac.za/gfxbook http://issc.uj.ac.za Email addresses of the authors alexandre.hardy@gmail.com steebwilli@gmail.com whsteeb@uj.ac.za v
This page intentionally left blank
Contents List of Figures Notation xiii xvii 1 Vectors, Matrices and Transforms 1 1.1 Vector Spaces.......................... 1 1.2 Points and Vectors....................... 4 1.2.1 Homogeneous Coordinates............... 4 1.3 Representing Objects by Points................ 5 1.4 Affine Transformations..................... 5 1.4.1 Introduction and Definitions............. 5 1.4.2 Scaling......................... 6 1.4.3 Translation....................... 7 1.4.4 Rotation......................... 7 1.4.5 Concatenation of Transforms............. 10 1.4.6 Projection........................ 11 1.5 Quaternions........................... 13 1.6 C# Implementation...................... 15 2 Lighting 23 2.1 Shading............................. 23 2.1.1 Affine Transforms and Normal Vectors........ 24 2.2 Local Lighting Models..................... 25 2.3 The Phong Lighting Model.................. 25 2.3.1 Emissive Properties.................. 26 2.3.2 Ambient Reflection................... 26 2.3.3 Diffuse Reflection.................... 26 2.3.4 Specular Reflection................... 27 2.3.5 Multiple Colored Light Sources............ 28 2.3.6 Attenuation....................... 29 vii
viii Contents 2.4 Lights.............................. 30 2.4.1 Spot Lights....................... 30 2.5 Transparent Objects...................... 31 2.6 Cook-Torrance Model..................... 31 2.6.1 Bidirectional Reflectivity............... 31 2.6.2 Cook-Torrance Model................. 32 2.6.3 Microfacet Distribution Term............. 33 2.6.4 Geometric Surface Occlusion Term.......... 33 2.6.5 Fresnel Term...................... 37 2.6.6 Beer-Lambert Law................... 38 2.7 C# Implementation...................... 39 3 Rasterization 49 3.1 Pixels.............................. 49 3.2 Drawing Lines......................... 49 3.2.1 Bresenham s Algorithm for Lines........... 50 3.3 Drawing Circles......................... 51 3.3.1 Bresenham s Algorithm for Circles.......... 51 3.4 Filling.............................. 53 3.4.1 Gouraud Shading.................... 54 3.5 Rasterization in C#...................... 55 3.5.1 Drawing Pixels..................... 57 3.6 Bresenham s Algorithms in C#................ 57 3.7 Fractals............................. 59 3.7.1 Mandelbrot Set..................... 60 3.7.2 Julia Set......................... 62 3.8 Iterated Function Systems................... 64 3.9 L-Systems and Fractals.................... 65 3.10 Kronecker Product and Fractals................ 69 3.10.1 Definitions....................... 69 3.10.2 Kronecker Product Fractals.............. 71 4 Curves 75 4.1 Introduction........................... 75 4.2 Affine Invariance........................ 76 4.3 Convex Hull........................... 77 4.4 Lagrange Interpolation..................... 78 4.4.1 C# Implementation.................. 79 4.5 Bézier Curves.......................... 83 4.5.1 Affine Invariance.................... 83 4.5.2 Convex Hull....................... 84 4.5.3 Derivative at Edges.................. 84
Contents ix 4.5.4 Piecewise Continuous Bézier Curves......... 86 4.5.5 Rendering........................ 87 4.5.6 Rational Bézier Curves................ 91 4.5.7 Bézier Curves: Conic Sections............. 91 4.5.8 C# Implementation.................. 91 4.6 Catmull-Rom Splines...................... 92 4.7 Bessel-Overhauser Splines................... 93 4.8 Tension-Continuity-Bias Splines................ 93 4.9 Uniform B-Splines....................... 94 4.9.1 Affine Invariance.................... 98 4.9.2 Convex Hull....................... 98 4.9.3 Cox-de Boor Formula................. 98 4.9.4 C# Implementation.................. 99 4.10 Non-Uniform B-Splines..................... 99 4.11 Interpolating with B-Splines.................. 100 4.11.1 Periodic Interpolation................. 102 4.12 Non-Uniform Rational B-Splines............... 104 4.13 Trigonometric Interpolation.................. 104 4.14 METAPOST and Bézier Curves................ 108 4.14.1 METAPOST Example................. 110 4.15 Curvature and Torsion..................... 112 4.16 Harmonic Interpolation.................... 117 4.17 Interpolation.......................... 118 4.18 Odd Case............................ 120 4.19 Even Case............................ 122 4.20 Examples............................ 125 4.21 Curvature Plots......................... 132 4.22 Numerical Stability....................... 136 4.23 Affine Invariance........................ 138 4.24 Convex Hull Property..................... 141 4.25 C# Implementation of Harmonic Interpolation....... 141 4.26 Chebyshev Polynomials.................... 142 4.26.1 Odd Case........................ 143 4.26.2 Even Case........................ 144 4.27 Non-Uniform Harmonic Interpolation............. 144 5 Wavelets 153 5.1 Introduction........................... 153 5.2 One-Dimensional Wavelets................... 154 5.3 Two-Dimensional Wavelets.................. 158 5.4 Curves.............................. 162 5.5 C# Implementation...................... 164
x Contents 6 Surfaces 167 6.1 Parametric Surfaces...................... 167 6.2 Tensor Product Surfaces.................... 168 6.3 Bézier Surfaces......................... 169 6.3.1 Tensor Product Bézier Surfaces............ 170 6.3.2 Triangular Bézier Surfaces............... 171 6.3.3 Rational Bézier Surfaces................ 173 6.3.4 Bézier Surface Interpolation.............. 173 6.4 B-Spline Tensor Product Surfaces............... 173 6.4.1 B-Spline Surface Interpolation............ 174 6.5 Subdivision Surfaces...................... 177 6.5.1 Loop Subdivision.................... 180 6.5.2 Modified Butterfly Subdivision............ 183 6.5.3 3 Subdivision..................... 185 6.5.4 Interpolating 3 Subdivision............. 187 6.5.5 Catmull-Clark Subdivision.............. 191 6.5.6 Doo-Sabin Subdivision................. 192 6.5.7 Comparison....................... 195 6.5.8 Interpolation with Subdivision Surfaces....... 198 6.6 Curvature of Surfaces..................... 200 6.7 Harmonic Surfaces....................... 206 6.8 Tensor Product Surface.................... 206 6.9 Harmonic Subdivision..................... 207 6.10 Local Harmonic Subdivision.................. 208 6.10.1 Local Harmonic Interpolation for Curves....... 209 6.10.2 Parametric Distance.................. 210 6.10.3 Subdivision Rules................... 212 6.10.4 Irregular Vertices.................... 214 6.10.5 Boundaries....................... 217 6.11 Geometry Images and Parameterization........... 219 6.11.1 Cutting a Mesh into a Disk.............. 219 6.11.2 Parameterization.................... 222 6.11.3 Rasterization of the Geometry Image......... 228 6.12 Interpolation of Geometry Images............... 229 6.13 Geometry Images Approximation............. 237 6.14 Rendering............................ 238 6.15 Approximating Basis Functions................ 240 6.16 Combined Results....................... 243 6.17 Curvature............................ 245 6.18 C# Implementation...................... 257
Contents xi 7 Raytracing 333 7.1 Raytracing Process....................... 333 7.2 Representation of a Ray.................... 338 7.3 Reflection............................ 338 7.4 Refraction............................ 339 7.5 Intersections........................... 341 7.5.1 Sphere.......................... 342 7.5.2 Infinite Plane...................... 342 7.5.3 Triangles........................ 343 7.5.4 Effect of Transforms.................. 344 7.6 C# Implementation of a Raytracer.............. 344 7.7 Implicit Surfaces........................ 368 7.7.1 Sphere Tracing..................... 369 7.7.2 Distance Functions................... 371 7.7.3 C# Implementation.................. 374 7.8 CSG Objects.......................... 376 7.8.1 C# Implementation.................. 377 7.9 Parametric Surfaces...................... 379 7.9.1 Interval Arithmetic................... 380 7.9.2 Interval Root Finding Bisection.......... 381 7.9.3 Interval Root Finding Newton-Raphson..... 382 7.9.4 Ray Tracing Harmonic Surfaces............ 385 7.10 Lighting Models........................ 386 7.11 Supersampling......................... 386 7.11.1 Regular Supersampling................ 387 7.11.2 Stochastic Supersampling............... 387 7.11.3 Adaptive Supersampling................ 388 7.12 Ambient Occlusion....................... 389 7.13 Ray Marching.......................... 392 7.14 Photon Mapping........................ 393 7.14.1 Transport Notation................... 394 7.14.2 Path Tracing...................... 394 7.14.3 Creating the Photon Map............... 395 7.14.4 Photon Tracing..................... 396 7.14.5 Photon Map Data Structure.............. 397 7.14.6 Radiance Estimate................... 399 7.14.7 C# Implementation.................. 401 8 Radiosity 409 8.1 Light Transport Notation................... 409 8.2 Radiosity Matrix........................ 410 8.3 Solving for Radiosity Values.................. 411
xii Contents 8.3.1 Solving: Jacobi Method................ 411 8.3.2 Solving: Gauss-Seidel Iteration............ 411 8.3.3 Solving: Shooting Method............... 412 8.4 Form Factors.......................... 412 8.4.1 Numerical Solution................... 413 8.4.2 Raytracing Method................... 413 8.4.3 Hemicube Method................... 413 8.5 Rendering............................ 415 8.6 C# Implementation...................... 415 9 Animation 427 9.1 Traditional Animation Techniques.............. 427 9.1.1 Keyframing....................... 427 9.1.2 Motion Capture.................... 428 9.2 Physics Models......................... 428 9.3 Animation of Position..................... 429 9.3.1 Arc length parameterization.............. 430 9.3.2 Orientation....................... 430 9.4 Articulated Structures (Kinematics)............. 431 9.4.1 Forward Kinematics.................. 432 9.4.2 Vertex Blending.................... 433 9.4.3 Inverse Kinematics................... 433 9.5 Mass Spring Systems...................... 434 9.6 Particle Systems........................ 435 9.7 Free Form Deformations.................... 435 9.8 Fluids.............................. 436 9.8.1 Navier-Stokes Equations................ 438 9.8.2 Advection........................ 439 9.8.3 Diffusion........................ 440 9.8.4 Projection........................ 440 9.8.5 Boundary Conditions................. 441 9.8.6 C# Implementation.................. 441 9.8.7 Free Surface....................... 449 9.8.8 C# Implementation of Free Surfaces......... 450 Bibliography 459 Index 471
List of Figures 1.1 Rotation in the two-dimensional Cartesian plane...... 8 1.2 Projection onto a plane.................... 11 1.3 The view frustum and canonical view volume........ 13 2.1 Diffuse reflection........................ 26 2.2 Specular reflection and the reflection vector......... 27 2.3 Phong lighting model...................... 29 2.4 Microfacets........................... 31 2.5 V shaped grooves........................ 34 2.6 Shadowing and masking.................... 35 2.7 Blinn s geometric lemma.................... 35 2.8 Phong compared to Cook-Torrance lighting model..... 38 3.1 Mandelbrot Set......................... 61 3.2 Julia Set............................. 63 3.3 IFS Fern............................. 65 3.4 Tree generated with an L-System............... 66 3.5 First four steps in the construction of the Hilbert curve.. 67 3.6 First five steps in the construction of the Koch snowflake. 67 3.7 Sierpinski carpet after seven iterations............ 72 3.8 First six iterations of the Sierpinski triangle......... 73 3.9 Kronecker product fractal: Gray scale fractal........ 74 3.10 First four iterations of checkerboard............. 74 4.1 Basis functions for uniform Lagrange interpolation of degree 3 80 4.2 Uniform Lagrange interpolation of 4 points......... 80 4.3 Basis functions for Bézier curves of degree 3......... 83 4.4 A Bézier curve and its convex hull.............. 84 4.5 Piecewise continuous Bézier curves.............. 87 4.6 de Casteljau s method..................... 88 4.7 Subdivision of figure 4.4 into two Bézier curves....... 91 4.8 Section of a circle, drawn with a rational Bézier curve... 92 4.9 Basis functions of B-splines of degree 3............ 96 4.10 Blending functions of B-splines of degree 3.......... 96 xiii
xiv List of figures 4.11 Bézier curve of degree 8.................... 100 4.12 B-spline curve of degree 3................... 100 4.13 Interpolation using B-splines................. 102 4.14 Periodic interpolation using B-splines............. 103 4.15 Interpolation using METAPOST................ 111 4.16 Periodic interpolation using METAPOST........... 111 4.17 Catenary and its curvature.................. 115 4.18 Spiral curve........................... 117 4.19 σ(t) function for n = 3..................... 126 4.20 σ k (t) basis functions for n = 3................. 126 4.21 σ(t) function for n = 4..................... 127 4.22 σ k (t) basis functions for n = 4................. 128 4.23 σ(t) function for n = 7..................... 128 4.24 σ(t) function for n = 16.................... 129 4.25 Circle drawn by METAPOST and harmonic interpolation.. 129 4.26 Circle drawn by B-spline and harmonic interpolation.... 130 4.27 Seven sided polygon: METAPOST, harmonic interpolation. 130 4.28 Seven sided polygon: B-spline, harmonic interpolation... 131 4.29 Five sided polygon: METAPOST, harmonic interpolation.. 131 4.30 Five sided polygon: B-spline, harmonic interpolation.... 132 4.31 Four point polygon: Trigonometric interpolation...... 133 4.32 3D curves: Comparison.................... 133 4.33 Curvature plot of figure 4.26.................. 134 4.34 Curvature plot of figure 4.28.................. 134 4.35 Curvature plot of figure 4.30.................. 135 4.36 Curvature plot of an eight sided figure............ 135 4.37 Stability of harmonic interpolation.............. 137 4.38 Non-uniform harmonic interpolation of seven points.... 151 4.39 Seven sided polygon: Non-uniform harmonic interpolation. 152 4.40 Five sided polygon: Non-uniform harmonic interpolation.. 152 4.41 Eight sided polygon: Non-uniform harmonic interpolation. 152 5.1 The box functions for V 2................... 157 5.2 The Haar wavelets for W 2................... 158 6.1 Example parametric surfaces................. 168 6.2 Example Bézier surface patches................ 170 6.3 Surface constructed from piecewise continuous Bézier patches 171 6.4 A Bézier triangle control mesh................ 171 6.5 B-spline surface fitting: Curve network............ 175 6.6 Stages in subdivision using Chaikin s scheme........ 178 6.7 Stages in interpolatory subdivision using the 4-point scheme 178 6.8 Notation for Loop subdivision................. 180 6.9 Triangle subdivision for Loop subdivision.......... 181
List of figures xv 6.10 Masks for Loop subdivision.................. 181 6.11 Cube: Loop subdivision.................... 182 6.12 Star: Loop subdivision..................... 182 6.13 Limit surfaces (Loop subdivision)............... 183 6.14 Masks for modified butterfly subdivision........... 184 6.15 Cube: Modified butterfly subdivision............. 185 6.16 Star: Modified butterfly subdivision............. 186 6.17 Limit surfaces (modified butterfly subdivision)....... 186 6.18 Masks for 3 subdivision................... 188 6.19 Triangle subdivision for 3 subdivision........... 188 6.20 Cube: 3 subdivision..................... 189 6.21 Star: 3 subdivision...................... 189 6.22 Limit surfaces ( 3 subdivision)................ 190 6.23 Masks for interpolating 3 subdivision............ 190 6.24 Subdivision for the Catmull-Clark scheme.......... 192 6.25 Cube (triangulated): Catmull-Clark subdivision....... 193 6.26 Cube (quadrilateral): Catmull-Clark subdivision...... 193 6.27 Star (triangulated): Catmull-Clark subdivision....... 194 6.28 Star (quadrilateral): Catmull-Clark subdivision....... 194 6.29 Limit surfaces (Catmull-Clark subdivision)......... 195 6.30 Cube (triangulated): Doo-Sabin subdivision......... 196 6.31 Cube (quadrilaterals): Doo-Sabin subdivision........ 196 6.32 Star (triangulated): Doo-Sabin subdivision......... 197 6.33 Star (quadrilaterals): Doo-Sabin subdivision......... 197 6.34 Limit surfaces (Doo-Sabin subdivision)............ 198 6.35 Subdivision of the mannequin head.............. 199 6.36 Tensor product harmonic surfaces............... 207 6.37 Stages in harmonic subdivision................ 208 6.38 Stages in subdivision to produce a torus........... 209 6.39 Stages in subdivision to produce a deformed torus..... 210 6.40 Basis functions for the 2D and 3D harmonic interpolation. 211 6.41 Harmonic interpolation of nearest 4 points.......... 211 6.42 Harmonic subdivision with local support........... 211 6.43 Stencil for regular vertices................... 213 6.44 Stencil for irregular vertices.................. 215 6.45 Subdivision of a simple chess piece.............. 217 6.46 Subdivision of a simple diamond model with a valence 12 vertex.............................. 218 6.47 Subdivision for refining an icosahedron............ 218 6.48 Subdivision for refining the mannequin head......... 218 6.49 Parameterization of Mannequin................ 220 6.50 Shape-preserving parameterization: Computation of weights 225
xvi List of figures 6.51 Mean value coordinates: Computation of weights...... 228 6.52 Application of theorems 6.1 and 6.3 (with m = 1)...... 237 6.53 Scanline-rendered harmonic surface (torus)......... 238 6.54 Approximation of harmonic tensor product surface..... 244 6.55 Bézier patches used to approximate figure 6.53....... 244 6.56 Final rendering of mannequin head Bézier patches..... 245 6.57 Final rendering of Spock formed from Bézier patches.... 246 6.58 Control points of various parametric surfaces........ 247 6.59 Comparison of plane and harmonic surface.......... 250 6.60 Comparison of partial sphere and harmonic surface..... 250 6.61 Comparison of torus and harmonic surface.......... 251 6.62 Comparison of sphere and harmonic surface......... 251 6.63 Comparison of Möbius strip and harmonic surface..... 252 6.64 Comparison of surface of revolution and harmonic surface. 253 6.65 Comparison of shell and harmonic surface.......... 254 6.66 Comparison of another shell and harmonic surface..... 254 6.67 Comparison of partial Klein bottle and harmonic surface.. 255 6.68 Comparison of overhand knot and harmonic surface.... 255 6.69 Comparison of Solomon s Seal knot and harmonic surface. 256 7.1 A scene and viewpoint..................... 334 7.2 Raytracing by casting rays from the viewer into the scene. 335 7.3 View of the scene from the camera.............. 336 7.4 Computation of the reflected vector r............ 339 7.5 Computation of the refracted vector t............ 340 7.6 Sphere tracing: Hit....................... 370 7.7 Sphere tracing: Miss...................... 371 7.8 Distance to a cone....................... 372 7.9 A superquadric......................... 373 7.10 Ray traced harmonic surface (torus)............. 386 7.11 Supersampling......................... 387 7.12 Adaptive supersampling.................... 388 7.13 Ambient occlusion....................... 390 7.14 Global illumination with photon mapping.......... 400 7.15 Caustics............................. 400 8.1 The Hemicube method..................... 414 8.2 Determining radiosity values.................. 414 8.3 Examples of radiosity solutions................ 415 9.1 An articulated structure.................... 432 9.2 A fluid cell in the MAC-grid.................. 437
Notation N the set of natural numbers Z the set of integer numbers Q the set of rational numbers R the set of real numbers C the set of complex numbers (of the form a + bi, a, b R) R n the Cartesian product space, i.e. R n = { (x 0, x 1,..., x n 1 ) x j R } z complex number z complex conjugate of z C i i = 1 I n n n unit matrix M T transpose of the matrix M M, det(m) determinant of the matrix M x = (x 0 x 1... x n 1 ) T column vector x R n x T row vector x R n 0 zero column vector n normal vector t tangent vector x Euclidean norm of x, x = x T x x, y the inner product (scalar product) of x and y x y dot product, an inner product defined by x T y = n 1 k=0 x ky k, where x, y R n x y the vector product (cross product) of x R 3 and y R 3, xvii
xviii Notation ang(a, b, c) the angle between the vectors a b and c b [0, 1] unit interval δ jk Kronecker delta, 1 if j = k, 0 otherwise I n n n identity matrix t curve parameter u, v surface parameters C k continuity of a curve or surface G k geometric continuity of a curve or surface x floor of x R, the largest y such that y Z and y x x ceiling of x R, the smallest y such that y Z and y x Kronecker product