computer graphics - gbv

14
Computer Graphics Using OpenGL Second Edition F. S. Hill Jr. Department of Electrical and Computer Engineering University of Massachusetts Prentice Hall Prentice Hall Upper Saddle River, NJ 07458

Upload: others

Post on 28-Nov-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Computer Graphics Using OpenGL Second Edition

F. S. Hill Jr. Department of Electrical and Computer Engineering

University of Massachusetts

Prentice Hall

Prentice Hall Upper Saddle River, NJ 07458

Contents

Preface vii

1 Introduction to Computer Graphics 1 1.1 What is Computer Graphics? 1 1.2 Where Computer Genera ted Pictures are Used 3

1.2.1. Art, Entertainment, and Publishing 3 1.2.2. Computer Graphics and Image Processing 3 1.2.3." Monitoring a Process 5 1.2.4. Displaying Simulations 6 1.2.5. Computer-aided Design 6 1.2.6. Scientific Analysis and Visualization 7

1.3 Elements of Pictures created in Computer Graphics 10 1.3.1. Polylines 10 1.3.2. Text 12 1.3.3. Filled Regions 13 1.3.4. Raster Images 15 1.3.5. Representation of Gray Shades and Color for Raster

Graphics 18 1.4 Graphics Display Devices 22

1.4.1. Line Drawing Displays 22 1.4.2. Raster Displays 23 1.4.3. Indexed Color and the LUT 26 1.4.4. Other Raster Display Devices 29 1.4.5. Hard Copy Raster Devices 30

1.5 Graphics Input Primitives and Devices 32 1.5.1. Types of Input Graphics Primitives 32 1.5.2. Types of Physical Input Devices 32

1.6. Summary 35 1.7. Fur ther Reading 36

2 Getting Started Drawing Figures 37 2.1 Getting Started Making Pictures 37

2.1.1. Device-independent Programming, and OpenGL 39 2.1.2. Windows-based Programming 40 2.1.3. Opening a Window for Drawing 41

2.2 Drawing Basic Graphics Primitives 42 2.2.1. Examples of Drawing Dot Constellations 47

2.3 Making Line Drawings 51 2.3.1. Drawing Polylines and Polygons 53 2.3.2. Line Drawing usingmovetoQ and l i n e t o Q 58 2.3.3. Drawing Aligned Rectangles 59 2.3.4. Aspect Ratio of an Aligned Rectangle 60 2.3.5. Filling Polygons 61 2.3.6. Other Graphics Primitives in OpenGL 62

2.4 Simple Interaction with the Mouse and Keyboard 63 2.4.1. Mouse Interaction 63 2.4.2. Keyboard Interaction 66

Contents XVÜ

2.5. Summary 67 2.6. Case Studies 68

Case Study 2.1 Pseudorandom Clouds of Dots 68 Case Study 2.2 Introduction to Iterated Function Systems 69 Case Study 2.3 The Golden Ratio and Other Jewels 73 Case Study 2.4 Building and Using Polyline Files 75 Case Study 2.5 Stippling of Lines and Polygons 75 Case Study 2.6 Polyline Editor 76 Case Study 2.7 Building and Running Mazes 77

2.7. Further Reading 79

More Drawing Tools 81 3.1. Introduction 3.2. World Windows and Viewports 82

3.2.1. The Mapping from the Window to the Viewport 83 3.2.2. Setting the Window and Viewport Automatically 92

3.3. Clipping Lines 95 3.3.1. Clipping a Line 96 3.3.2. The Cohen-Sutherland Clipping Algorithm 96

3.4. Developing the Canvas Class 100 3.4.1. Some useful Supporting Classes 100 3.4.2. Declaration of Class Canvas 102 3.4.3. Implementation of Class Canvas 103

3.5. Relative Drawing 105 3.5.1. Developing moveRel() and l i n e R e l Q 105 3.5.2. TurtleGraphics 106

3.6. Figures Based on Regulär Polygons 110 3.6.1. The Regulär Polygons 110 3.6.2. Variations on n-Gons 112

3.7. Drawing Circles and Ares 116 3.7.1. Drawing arcs 116

3.8. Using the Parametric Form of a Curve 119 3.8.1. Parametric Forms for Curves 120 3.8.2. Drawing Curves Represented Parametrically 123 3.8.3. Superellipses 124 3.8.4. Polar Coordinate Shapes 126 3.8.5. 3D Curves 127

3.9. Summary 129 3.10. Case Studies 130

Case Study 3.1 Studying the Logistic Map and Simulation of Chaos 130

Case Study 3.2 Implementation of the Cohen-Sutherland Clipper in C/C++ 131

Case Study 3.3 Implementing Canvas for Turbo C++ 133 Case Study 3.4 Drawing Arches 135 Case Study 3.5 Some Figures Used in Physics and Engineering 136 Case Study 3.6 Tilings 137 Case Study 3.7 Playful Variations on a Theme 140 Case Study 3.8 Circles Rolling around Circles 141 Case Study 3.9 Superellipses 142

3.11. Fur ther Reading 143

Contents

4 Vector Tools for Graphics 144

4.1. Introduction 145 4.2. Review ofVectors 147

4.2.1 Operations with Vectors 148 4.2.2. Linear Combinations ofVectors 149 4.2.3. The Magnitude of a Vector, and Unit Vectors 151

4.3. The Dot Product 152 4.3.1. Properties of the Dot Product 153 4.3.2. The Angle Between two Vectors 154 4.3.3.' The Sign ofb-c; Perpendicularity 154 4.3.4. The 2D "Perp" Vector 156 4.3.5. Orthogonal Projections, and the Distance from a Point

to a Line 157 4.3.6. Applications of Projection: Reflections 159

4.4. The Cross Product of Two Vectors 160 4.4.1. Geometrie Interpretation of the Cross Product 162 4.4.2. Finding the Normal to a Plane 163

4.5. Representations of Key Geometr ie Objects 164 4.5.1. Coordinate Systems and Coordinate Frames 165 4.5.2. Affine Combinations of Points 167 4.5.3. Linear Interpolation of Two Points 170 4.5.4 "Tweening" for Art and Animation 170 4.5.5 Preview: Quadratic and Cubic Tweening, and Bezier

Curves 172 4.5.6. Representing Lines and Planes 173

4.6. Finding the Intersection of Two Line Segments 181 4.6.1. Application of Line Intersections: the Circle Through

Three Points 184 4.7. Intersections of Lines with Planes, and Clipping 186 4.8. Polygon Intersection Problems 188

4.8.1. Working with Convex Polygons and Polyhedra 188 4.8.2. Ray Intersections and Clipping for Convex

Polygons 189 4.8.3. The Cyrus-Beck Clipping Algorithm 192 4.8.4. Clipping against Arbitrary Polygons 194 4.8.5. More Advanced Clipping 196

4.9. Summary 197 4.10. Case Studies 198

Case Study 4.1. Animation with Tweening 198 Case Study 4.2. Circles Galore 198 Case Study 4.3. Is point Q inside Polygon PI 200 Case Study 4.4. Reflections in a Chamber (2D Ray

Tracing) 200 Case Study 4.5. Cyrus-Beck Clipping 201 Case Study 4.6. Clipping a Polygon Against a Convex Polygon:

Sutherland-Hodgman Clipping 202 Case Study 4.7. Clipping a Polygon against another: Weiler

Atherton Clipping 204 Case Study 4.8. Boolean Operations on Polygons 207

4.11. Further Reading 208

XX Contents

5 Transformations of Objects 209 5.1 Introduction 210 5.2 Introduction to Transformations 211

5.2.1. Transforming Points and Objects 214 5.2.2. The Affine Transformations 216 5.2.3. Geometrie Effects of elementary 2D Affine

Transformations 217 5.2.4. The Inverse of an Affine Transformation 222 5.2.5. Composing Affine Transformations 223 5.2.6. Examples of Composing 2D Transformations 224 5.2.7. Some Useful Properties of Affine Transformations 228

5.3. 3D Affine Transformations 233 5.3.1. The Elementary 3D Transformations 234 5.3.2. Composing 3D Affine Transformations 238 5.3.3. Combining Rotations 238 5.3.4. Summary of Properties of 3D Affine Transformations 243

5.4. Changing Coordinate Systems 244 5.5. Using Affine Transformations in a Program 247

5.5.1. Saving the CT for Later Use 254 5.6. Drawing 3D Scenes with O p e n G L 258

5.6.1. An Overview of the Viewing Process and the Graphics Pipeline 259

5.6.2. Some OpenGL tools for Modeling and Viewing 262 5.6.3. Drawing Elementary Shapes Provided by OpenGL 265 5.6.4. Reading a Scene Description from a File 273

5.7. Summary 276 5.8. CaseStudies 278

Case Study 5.1. Doing Your Own Transforming by the CT in Canvas 278

Case Study 5.2. Draw the Star of Fig 5.39 Using Multiple Rotations 278

Case Study 5.3. Decomposing a 2D Affine Transformation 278 Case Study 5.4. Generalized 3D Shears 282 Case Study 5.5. Rotation About an Axis: the Constructive

Approach 283 Case Study 5.6. Decomposing 3D Affine Transformations 284 Case Study 5.7. Drawing 3D Scenes Described by SDL 286

5.9. Further Reading 286

6 Modeling Shapes with Polygonal Meshes 287 6.1. Introduction 288 6.2. Introduction to Solid Modeling with Polygonal Meshes 288

6.2.1. Defining a Polygonal Mesh 290 6.2.2. Finding the Normal Vectors 292 6.2.3. Properties of Meshes 294 6.2.4. Mesh Models for Nonsolid Objects 295 6.2.5. Working with Meshes in a Program 296

6.3. Polyhedra 299 6.3.1. Prisms and Antiprisms 300 6.3.2. The Piatonic Solids 302 6.3.3. Other Interesting Polyhedra ' 307

Contents XXI

6.4. Extruded Shapes 310 6.4.1. Creating Prisms 310 6.4.2. Arrays of Extruded Prisms: "Bricklaying" 311 6.4.3. Extrusions with a "Twist" 313 6.4.4. Building Segmented Extrusions: Tubes and Snakes 315 6.4.5. "Discretely" Swept Surfaces of Revolution 320

6.5. Mesh Approximations to Smooth Objects 321 6.5.1. Representations for Surfaces 322 6.5.2. The Normal Vector to a Surface 323 6.5.3. The Effect of an Affine Transformation 325 6.5.4., Three "Generic" Shapes: the Sphere, Cylinder, and

Cone 326 6.5.5. Forming a Polygonal Mesh for a Curved Surface 329 6.5.6. Ruled Surfaces 331 6.5.7. Surfaces of Revolution 337 6.5.8. The Quadric Surfaces 339 6.5.9. The Superquadrics 342 6.5.10. Tubes Based on 3D Curves 344 6.5.11. Surfaces Based on Explicit Functions of Two

Variables 345 6.6. Summary 346 6.7. CaseStudies 347

Case Study 6.1. Meshes Stored in Files 347 Case Study 6.2. Derivation of the Newell Method 347 Case Study 6.3. The Prism 350 Case Study 6.4. Prism Arrays and Extruded Quad-strips 351 Case Study 6.5. Tubes and Snakes Based on a Parametric

Curve 352 Case Study 6.6. Building Discrete-Stepped Surfaces of

Revolution 352 Case Study 6.7. On Edge lists and Wire-frame Models 352 Case Study 6.8. Vaulted Ceilings 353 Case Study 6.9. On Piatonic Solids 353 Case Study 6.10. On Archimedian Solids 353 Case Study 6.11. Algebraic Form for the quadric Surfaces 353 Case Study 6.12. Superquadric Scenes 354 Case Study 6.13. Drawing Smooth parametric Surfaces 354 Case Study 6.14. Taper, Twist, Bend, and Squash It 355

6.8. Further Reading 357

Three-Dimensional Viewing 358 7.1 Introduction 359 7.2. The Camera Revisited 359

7.2.1. Setting the View Volume 360 7.2.2. Positioning and Pointing the Camera 361

7.3. Building a Camera in a Program 366 7.3.1. "Flying" the Camera 368

7.4. Perspective Projections of 3D Objects 371 7.4.1 Perspective Projection of a Point 372 7.4.2 Perspective Projection of a Line 375 7.4.3. Incorporating Perspective in the Graphics Pipeline 379

7.5. Producing Stereo Views 392 7.6. Taxonomy of Projections 394

7.6.1. One-, Two-, and Three-Point Perspective 394 7.6.2. Types of Parallel Projections 398

7.7. Summary 404 7.8. CaseStudies 405

Case Study 7.1. Flying a Camera through a Scene 405 Case Study 7.2. Stereo Views 406 Case Study 7.3. Creating Parallel Projections 406 Case Study 7.4. Do-it-yourself Viewing (As if OpenGL were

NotAvailable) 406 Case Study 7.5. Removal of Back Face for Greater Efficiency 406

7.9. Further Reading 407

Rendering Facesfor Visual Realism 408 8.1. Introduction 409 8.2. Introduction to Shading Models 413

8.2.1. Geometrie Ingredients for Finding Reflected Light 414

8.2.2. Computing the Diffuse Component 415 8.2.3. Specular Reflection 416 8.2.4. TheRoleof Ambient Light 419 8.2.5. Combining Light Contributions 420 8.2.6. Adding Color 421 8.2.7. Shading and the Graphics Pipeline 422 8.2.8. Using Light Sources in OpenGL 425 8.2.9. Working with Material Properties in OpenGL 429 8.2.10. Shading of Scenes Specified by SDL 430

8.3. Fiat Shading and Smooth Shading 430 8.3.1. Fiat Shading 432 8.3.2. Smooth Shading 433

8.4. Removing Hidden Surfaces 436 8.4.1. The Depth Buffer Approach 437

8.5. Adding Texture to Faces 439 8.5.1. Pasting the Texture onto a Fiat Surface 442 8.5.2. Rendering the Texture 444 8.5.3. What Does a Texture Modulate? 451 8.5.4. A Texture Example Using OpenGL 453 8.5.5. Wrapping Texture on Curved Surfaces 457 8.5.6. Reflection Mapping 461

8.6. Adding Shadows of Objects 465 8.6.1. Shadows as Texture 465 8.6.2. Creating Shadows with the Use of a Shadow Buffer 467

8.7. Summary 469 8.8. CaseStudies 469

Case Study 8.1. Creating Shaded Objects using OpenGL 469 Case Study 8.2. The Do-it-Yourself Graphics Pipeline 470 Case Study 8.3. Add Polygon Fill and Depth-Buffer Removal

of Hidden Surfaces 420 Case Study 8.4. Rendering Texture 470 Case Study 8.5. Applying Procedural 3D Textures 470

Case Study 8.6. Drawing Shadows 471 Case Study 8.7. Extending SDL to Include Texturing 471

8.9. Further Reading 471

9 Approaches to Infinity 472 9.1. Introduction 473 9.2 Fractals and Self-Similarity 473

9.2.1. Successive Refinement of Curves 474 9.2.2. Drawing Koch Curves and Snowflakes 475 9.2.3*. Fractional Dimension 477

9.3. String Production and Peano Curves 478 9.3.2. Producing Recursively and Drawing in a Program 480 9.3.3. Allowing Branching 483

9.4. Tiling the Plane 485 9.4.1. Monohedral Tilings 486 9.4.2. Dihedral Tilings 488 9.4.3. Drawing Tilings 491 9.4.4. Reptiles 491

9.5. Creating an Image by Means of I terated Functions Systems 494 9.5.1. An Experimental Copier 494 9.5.2. Some Underlying theory of the Copying Process 496 9.5.3. Drawing the fc-th Iterate 497 9.5.4. The Chaos Game 498 9.5.5. Finding the IFS, Fractal Image Compression 501

9.6. The Mandelbrot Set 504 9.6.1. Mandelbrot Sets and Iterated Functions Systems 505 9.6.2. Defining the Mandelbrot Set 508 9.6.3. Computing whether the point c is in the Mandelbrot Set 9.6.4. Drawing the Mandelbrot Set 511 9.6.5. Some Notes on the Mandelbrot Set 512

9.7. Julia Sets 513 9.7.1. The Filled-in Julia Set Kc 513 9.7.2. Drawing Filled-in Julia Sets 513 9.7.3. Some Notes on Fixedpoints and Basins of Attraction 514 9.7.4. The Julia Set Jc 514

9.8. Random Fractals 519 9.8.1. Fractalizing a Segment 519 9.8.2. Controlling the Spectral Density of the Fractal Curve 521

9.9. Summary 523 9.10. Case Studies 524

Case Study 9.1. Drawing String Productions 524 Case Study 9.2. Drawing Snowflakes and Reptiles 524 Case Study 9.3. Playing the Chaos Game 526 Case Study 9.4. Drawing Orbits in the Mandelbrot Set 527 Case Study 9.5. Creating Pictures of the Mandelbrot Set 527 Case Study 9.6. Creating Pictures of Julia Sets 527 Case Study 9.7. Nonperiodic Tilings; Penrose Tiles 527 Case Study 9.8. Fractalizing Curves 530 Case Study 9.9. Modeling Fractalized Mountains 530

9.11. Further Reading 531

Tools for Raster Displays 532 10.1. Introduction 533 10.2. Manipulating Pixmaps 534

10.2.1. Operations of Interest for Pixmaps 534 10.2.2. Useful Data Types for Pixmaps 535 10.2.3. Scaling and Rotating Images 540

10.3. Combining Pixmaps 543 10.3.1. The Read-Modify-Write Cycle 544 10.3.2. The Alpha Channel and Image Blending 545 10.3.3. Logical Combinations of Pixmaps 549 10.3.4. The BitBlt Operation 553

10.4. Do-It-Yourself Line Drawing: Bresenham's Algorithm 554 10.4.1. Bresenham's Line-Drawing Algorithm 555

10.5 Defining and Filling Regions of Pixels 561 10.5.1. Defining Regions 561 10.5.2. Pixel-Defined Regions 561 10.5.3. A Recursive Flood-Fill Algorithm 562 10.5.4. Filling Regions with Patterns 563 10.5.5. Using Coherence: Region Filling Based on Runs of

Pixels 565 10.6. Manipulating Symbolically Defined Regions 566

10.6.1. Rectangle-defined Regions 567 10.6.2. Path-defined Regions 568

10.7. Filling Polygon-Defined Regions 570 10.7.1. Which Pixels on an Edge Belong to a

Polygon? 571 10.7.2 Improving the Algorithm's Performance 573

10.8. Aliasing; Antialiasing Techniques 577 10.8.1. Antialiasing Techniques 578 10.8.2. Antialiasing ofTexture 583 10.8.3. Antialiasing Using OpenGL 585

10.9. Creating More Shades and Colors 587 10.9.1. Ordered Dither 587 10.9.2. Error Diffusion 592 Summary 594 Case Studies 594 Case Study 10.1.

Case Study 10.2.

Case Study 10.3. Case Study 10.4.

Case Study 10.5. Case Study 10.6.

Case Study 10.7. Case Study 10.8.

Reading and Displaying BMP Image Files 594 Dissolving Between Two Pixmaps with OpenGL 595 Region Filling Based on Runs 595 Working with the "Shape" Data Structure 595 Chain Coding of Shapes 596 Filling "Horizontally Convex" Polygons 596 General Polygon Filling 596 Error Diffusion 596

10.12. Fur ther Reading 596

Curve and Surface Design 597 11.1. Introduction 598

11.1,1 Parametric Curves as Trajectories 598 11.1.2. Smoothness of Motion 598

11.2. Describing Curves by Means of Polynomials 603 11.3. On Interactive Curve Design 607 11.4. Bezier Curves for Curve Design 609

11.4.1. The de Casteljau Algorithm 610 11.5. Properties of Bezier Curves 614 11.6. Finding Better Blending Functions 618

11.6.1. The Problem of Local Control 619 11.6.2. Wish List for a Set of Blending Functions 620 11.6.3. Piecewise Polynomial Curves and Splines 621 11.6.4. Building a set of Blending Functions Out ofg(r) 623 11.6.5. Spline Curves and Basis Functions 624

11.7. T h e B-Spline Basis Functions 627 11.7.1. Definition of B-Spline Functions 628 11.7.2. Using Multiple Knots in the Knot Vector 632 11.7.3. Open B-Spline Curves: Standard Knot Vector 633

11.8. Useful Properties of B-Spline Curves for Design 637 11.8.1. Using Multiple Control Points 638

11.9. Rational Splines and N U R B S Curves 639 11.10. A Glimpse at Interpolation 643

11.10.1. Interpolation Using Piecewise Cubic Polynomials 643 11.10.2. Hermite Interpolation 644 11.10.3. The Natural Cubic Spline 648 11.10.4. Computing the Slopes in Cubic Interpolation 649 11.10.5. Specifying the Tangent Vectors Interactively 653

11.11. Modeling Curved Surfaces 654 11.11.1. Ruled Surfaces Based on B-Splines 654 11.11.2. Surfaces of Revolution Based on B-Splines 655 11.11.3. Bezier Surface Patches 656 11.11.4. Patching Together Bezier Patches 658 11.11.5. B-Spline Patches 659 11.11.6. NURBS Surfaces 660

11.12. Summary 662 11.13. Case Studies 663

Case Study 11.1. A Potpourri of Interesting Parametric Curves 663

Case Study 11.2. ElliptiPool 664 Case Study 11.3. Bezier Curves 666 Case Study 11.4. A Quadratic Spline-Curve Generator 666 Case Study 11.5. Building a Spline-Curve Editor 666 Case Study 11.6. Interpolation of Control Points with

B-Splines 667 Case Study 11.7. Interpolating with Cubic Polynomials 668 Case Study 11.8. The Venerable Teapot 669 Case Study 11.9. Invariance to Projective Transformations 670 Case Study 11.10. Drawing NURBS Patches 670

11.14. Further Reading 671

Color Theory 672 12.1. Introduction 672 12.2. Describing Colors 673

12.2.1. Dominant Wavelength 675 12.2.2. Color Matching 676

12.3. The International Commission on Illumination Standard 678 12.3.1. Constructing the CIE Chart 680 12.3.2. Using the CIE Chromaticity Diagram 681 12.3.3. Color Gamuts 682

12.4. Color Spaces 683 12.4.1. The RGB and CMY Color Spaces 683 12.4.2. Additive and Subtractive Color Systems 685 12.4.3. The HLS Color Model 686

12.5. Color Quantization 688 12.5.1. Uniform Quantization 690 12.5.2. The Popularity Algorithm 692 12.5.3. The Median-cut Algorithm 692 12.5.4. Octree Quantization 693

12.6. Summary 695 12.7. Chapter Exercises 696 12.8. Case Studies 696

Case Study 12.1. Drawing the CIE Chart 696 Case Study 12.2. Drawing RGB Space 696 Case Study 12.3. HSV to RGB 696 Case Study 12.4. Uniform Color Quantization 696 Case Study 12.5. Popularity Color Quantization 696 Case Study 12.6. Median Cut Color Quantization 697 Case Study 12.7. Octree Color Quantization 697

12.9. Fur ther Reading 698

Hidden Surface Removal 699 13.1 Introduction 700

13.1.2. Object Precision versus Image Precision Approaches 701

13.1.3. Description of the Polygon Mesh Data 702 13.2. The Dep th Buffer Algori thm Revisited 704 13.3. List Priority H S R Methods 705

13.3.1. The Heedless Painter's Algorithm 706 13.3.2. HSR Using Binary Space Partition Trees 707 13.3.3. The Depth Sort Algorithm 708

13.4. A Scan-Line H S R Method 713 13.5. Area Subdivision Approaches 716

13.5.1. Quadrant Subdivision 716 13.5.2. Other Definitions of a Simple Region 719

13.6. On Hidden Line Removal Methods 721 13.6.1. The GeometrieTesting in e d g e T e s t Q 724

13.7. H S R Methods for Curved Surfaces 725 13.8. Summary 727

Contents xxvii

13.9. CaseStudies 729 Case Study 13.1. Testing the Painter's Algorithm 729 Case Study 13.2. Test and Split 729 Case Study 13.3. Using BSP Trees for HSR 730 Case Study 13.4. Using Depth Sorting for HSR 730 Case Study 13.5. Working with a Scan-Line HSR Approach 730 Case Study 13.6. Drawing with the Warnock Algorithm 731 Case Study 13.7. The Edge Stack Algorithm for HLR 731

13.10. Further Reading 731

14 Ray Träcjng 732 14.1. Introduction 733 14.2. Setting Up the Geometry of Ray Tracing 734 14.3. Overview of the Ray-Tracing Process 736 14.4. Intersection of a Ray with an Object 737

14.4.1. Intersection of a Ray with a Generic Plane 738 14.4.2. Intersection with a Generic Sphere 739 14.4.3. Intersection of the Ray with Transformed Objects 740

14.5. Organizing a Ray Tracer Application 741 14.5.1. A Routine to Compute Ray-Sphere Intersections 747 14.5.2. A Complete Ray Tracer for Emissive-Sphere

Scenes 749 14.6. Intersecting Rays with Other Primitives 750

14.6.1 Intersecting with a Square 750 14.6.2. Intersecting with a Tapered Cylinder 750 14.6.3. Intersecting with a Cube (or any Convex

Polyhedron) 754 14.6.4. Adding More Primitives 759

14.7. Drawing Shaded Pictures of Scenes 760 14.7.1. Finding the Normal at the Hit Spot 761 14.7.2. Coloring Objects According to their Surface

Materials 762 14.7.3. Physically-based Shading Models: Cook-Torrance

Shading 764 14.8. Adding Surface Texture 770

14.8.1. Solid Texture 770 14.8.2. Pasting Images onto Surfaces 778 14.8.3. Antialiasing Ray Tracings 780

14.9. Using Extents 781 14.9.1. Box and Sphere Extents 783 14.9.2. Using Projection Extents 788

14.10. Adding Shadows for Grea ter Realism 790 14.11 Reflections and Transparency 793

14.11.1 The Refraction of Light 795 14.11.2. Dealing with Refraction in shade() 799

14.12. Compound Objects: Boolean Operations on Objects 802 14.12.1. Ray Tracing CSG Objects 803 14.12.2. Data Structure for Boolean Objects 805 14.12.3. Intersecting Rays with Boolean Objects 806 14.12.4. Building and Using Extents for CSG Objects 811

14.13. Summary 814

XXVlii Contents

14.14. CaseStudies 815 Case Study 14.1. An Emissive Ray Tracer 815 Case Study 14.2. A Renaissance Ray Tracer 815 Case Study 14.3. Implementing Shadows in a Ray Tracer 815 Case Study 14.4. Using Extents to Speed up Ray Tracing 816 Case Study 14.5. Ray Tracing with 3D Textures 816 Case Study 14.6. Antialiasing 816 Case Study 14.7. Ray Tracing Other Primitives 816 Case Study 14.8. A 2D Ray Tracer to Explore Refraction 816 Case Study 14.9. Reflected and Refracted Light 816 Case Study 14.10. Ray Tracing Boolean Combinations of

Objects 816 14.15. Further Reading 817

Appendixes

1 Graphics Tools - Obtaining OpenGL 818 Al . l Obtaining and Installing OpenGL 818

2 Some Mathematics for Computer Graphics 820 A2.1 Some Key Definitions for Matrices and their

Operations 820 A2.1.1 Manipulations with Matrices 821 A2.1.2 Multiplying Two Matrices 821 A2.1.3 Partitioning a Matrix 823 A2.1.4 The Determinant of a Matrix 824 A2.1.5 The Inverse of a Matrix 824

A2.2 Some Properties of Vectors and their Operations 826 A2.2.1 The Perp of a Vector; the Perp Dot Product 826 A2.2.2. The Triple Scalar Product 826 A2.2.3. The Triple Vector product and Products of Four

Vectors 828 A2.3. The Arithmetic of Complex Numbers 828 A2.4. Spherical Coordinates and Direction Cosines 830

3 Some Useful Classes and Utility Routines 833 Classes for 2D Graphis 833 RGBPixmap Class 837 The Scene and Supporting Classes 840 Noise Class 862 Some Classes that are Useful for Ray Tracing 863

4 An Introduction to POSTSCRIPT® 866 A4.1. About the POSTSCRIPT Language 867

A4.1.1. Some Preliminaries 867 A4.1.2. POSTSCRIPT is "Stackbased"

A4.1.3. Some Stack Operators: pop, dup, exch, and c l e a r 868

A4.1.4. More Advanced Stack Operators 869 A4.1.5. Some Arithmetic Operators 870

A4.2. Graphics Operators in POSTSCRIPT 872 A4.2.1. Coordinate Systems and Transformations 872 A4.2.2. Path Construction Verbs 873 A4.2.3. Ares of Circles 874 A4.2.4. Used for Painting Verbs A4.2.5. Coordinate Transformations 876 A4.2.6. Graphics State Operators 879

A4.3. Drawing Text in POSTSCRIPT 880 A4.4. Defining New Variables and Procedures 881

A4.4.1. Defining Variables 881 A4.4.2. Defining Procedures 882 A4.4.3. A Simple Form of Iteration, Using repea t 885

A4.5. Decisions and Iterations 886 A4.5.1. Verbs that Take Boolean Values for Arguments 887 A4.5.2. Making Decisions 887 A4.5.3. Iterating 889

A4.6. Printing Values 893 A4.7. Drawing Gray-scale Image 894

An Introduction to SDL 897 A5.1 Syntax of SDL 898 A5.2. Macros in SDL 901 A5.3. ExtendingSDL 902

References 904

Index 909