11
Computer Graphics Using OpenGL Third Edition ^r.i F. S. Hill, Jr. and Stephen M. Kelley, Jr. Department of Electrical and Computer Engineering University of Massachusetts PEARSON Prentice Hall Upper Saddle River, NJ 07458

Computer Graphics by FS Hill

Embed Size (px)

DESCRIPTION

Complete book for computer graphics.

Citation preview

  • Computer Graphics Using OpenGL

    Third Edition

    ^ r . i

    F. S. Hill, Jr. and Stephen M. Kelley, Jr. Department of Electrical and Computer Engineering

    University of Massachusetts

    PEARSON Prentice

    Hall

    Upper Saddle River, NJ 07458

  • Contents Preface

    Introduction to Computer Graphics 1 1.1. What Is Computer Graphics? 1 1.2. Where Computer-Generated Pictures Are Used 3

    1.2.1. Art, Entertainment, and Publishing 4 1.2.2. Computer Graphics, Perception, and Image Processing 7 1.2.3. Monitoring a Process 8 1.2.4. Displaying Simulations 8 1.2.5. Computer-Aided Design: CAD 9 1.2.6. Scientific Analysis and Volume Visualization 10

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

    for Raster Images 21 1.4. Graphics Display Devices 25

    1.4.1. Line Drawing Displays 25 1.4.2. Raster Displays 26 1.4.3. Video Cards/3D Accelerators 29 1.4.4. Other Raster Display Devices 31 1.4.5. Hard Copy Raster Devices 32

    1.5. Graphics Input Primitives and Devices 34 1.5.1. Types of Logical Input Graphics Primitives 34 1.5.2. Types of Physical Input Devices 35

    1.6. Chapter Summary 37 1.7. Chapter Exercises 38 1.8. For Further Reading 38

    2 Initial Steps in Drawing Figures 39 2.1. To Get Started Making Pictures 39

    2.1.1. Device-Independent Programming and OpenGL 42 2.1.2. Window-Based Programming 43 2.1.3. How to Open a Window for Drawing 46

    2.2. The O p e n G L Basic Graphics Primitives 47 2.2.1. Examples of Drawing Dot Constellations 52

    2.3. Line Drawings in O p e n G L 59 2.3.1. Drawing Polylines and Polygons 60 2.3.3. Line Drawing Using moveTo O a n d l i n e T o () 65 2.3.4. Drawing Aligned Rectangles 66 2.3.5. On The Aspect Ratio of an Aligned Rectangle 67 2.3.6. Filling Polygons 69 2.3.7. Other Graphics Primitives in OpenGL 70

    xiii

  • xiv Contents

    2.4. Simple Interaction with the Mouse and Keyboard 71 2.4.1. Mouse Interaction 71 2.4.2. Keyboard Interaction 75

    2.5. Introduction to the Design and Use of Mens in an Application 76

    2.6. Summary 80 2.7. Case Studies 80

    Case Study 2.1 Pseudorandom Clouds of Dots 80 Case Study 2.2 Introduction to Iterated Function

    Systems 82 Case Study 2.3 The Golden Ratio and Other Jewels 86 Case Study 2.4 How to Build and Use Polyline Files 87 Case Study 2.5 How to Build and Run Mazes 88

    2.8. For Further Reading 89

    3 Additional Drawing Tools 90 3.1. Introduction 91 3.2. World Windows and Viewports 92

    3.2.1. The Mapping from the Window to the Viewport 95 3.3. Clipping Lines 105

    3.3.1. How to Clip a Line 105 3.3.2. The Cohen-Sutherland Clipping Algorithm 106

    3.4. Regulr Polygons, Circles, and Ares 109 3.4.1. The Regulr Polygons 109 3.4.2. Variations on n-gons 110 3.4.3. Drawing Ares and Circles 114 3.4.4. Successive Refinement of Curves 115

    3.5. The Parametric Form for a Curve 116 3.5.1. Parametric Forms for Curves 117 3.5.2. Drawing Curves Represented Parametrically 120 3.5.3. Polar Coordinate Shapes 121

    .6.

    .7. Summary of the Chapter 124 Case Studies Case Study 3.1

    Case Study 3.2

    Case Study 3.3 Case Study 3.4 Case Study 3.5

    Case Study 3.6

    124 Studying the Logistic Map and the Simulation of Chaos 124 Implementation of the Cohen-Sutherland Clipper in C/C++ 126 Animate Dino with the Keyboard 127 Drawing Arches 127 Some Figures Used in Physics and Engineering 128 Tilings 130

    3.8. For Further Reading 131

    Vector Tools for Graphics 132 4.1. Introduction 133 4.2. Review of Vectors 135

    4.2.1. Operations with Vectors 137 4.2.2. Linear Combinations of Vectors 138 4.2.3. The Magnitude of a Vector and Unit Vectors 139

  • Contents XV

    4.3. The Do t Product 140 4.3.1. Properties of the Dot Product 141 4.3.2. The Angle Between Two Vectors 142 4.3.3. The Sign of b.c and Perpendicularity 143 4.3.4. The 2D Perp Vector 144 4.3.5. Orthogonal Projections and the Distance from a Point

    to a Line 146 4.3.6. Applications of Projection: Reflections 148

    4.4. The Cross Product of Two Vectors 149 4.4.1. Geometrie Interpretation of the Cross Product 151 4.4.2. To Find the Normal Vector to a Plane 152 4.4.3. To Test the Convexity of a Planar Polygon 153

    4.5. Representat ions of Key Geometr ie Objects 154 4.5.1. Coordinate Systems and Coordinate Frames 155 4.5.2. Affine Combinations of Points 158 4.5.3. Linear Interpolation of Two Points 160 4.5.4. Preview: Bezier Curves Built from Quadratic and Cubic

    Tweening 163 4.5.5. Representing Lines and Planes 164

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

    Three Points 172 4.7. Intersections of Lines with Planes, and Clipping 174 4.8. Polygon Intersection Problems 177

    4.8.1. Working with Convex Polygons and Polyhedra 177 4.8.2. Ray Intersections and Clipping for Convex Polygons 178 4.8.3. The Cyrus-Beck Clipping Algorithm 181 4.8.4. More Advanced Clipping 183

    4.9. Summary of the Chapter 184 4.10. CaseStudies 185

    Case Study 4.1 Animation with Tweening 185 Case Study 4.2 Circles Galore 186 Case Study 4.3 Is Point Q Inside Convex Polygon PI 188 Case Study 4.4 Reflections in a Chamber (2D Ray Tracing) 188 Case Study 4.5 Cyrus-Beck Clipping 189

    4.11. For Further Reading 189

    5 Transformations of Objects 190 5.1. Introduction 191 5.2. Introduction to Transformations 192

    5.2.1. Points and Objects Transformed 195 5.2.2. The Affine Transformations 197 5.2.3. Geometrie Effects of Elementary 2D Affine

    Transformations 199 5.2.4. The Inverse of an Affine Transformation 204 5.2.5. To Compose Affine Transformations 205 5.2.6. Examples of Composing 2D Transformations 206 5.2.7. Some Useful Properties of Affine Transformations 209

    5.3. 3D Affine Transformations 214 5.3.1. The Elementary 3D Transformations 215 5.3.2. To Compose 3D Affine Transformations 219

  • xvi Contents

    5.3.3. A Combination of Rotations 220 5.3.4. Summary of Properties of 3D Affine Transformations 224

    5.4. How To Change Coordinate Systems 225 5.5. Affine Transformations Used in a Program 228

    5.5.1. The CT Saved for Later Use 235 5.6. To Draw 3D Scenes Interactively with O p e n G L 238

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

    5.6.2. Some OpenGL Tools for Modeling and Viewing 242 5.6.3. To Draw Elementary Shapes Provided by OpenGL 247 5.6.4. Reading a Scene Description from a File using SDL 256

    5.7. Summary of the Chapter 259 5.8. Case Studies 260

    Case Study 5.1 Draw the Star of Figure 5.36. Using Multiple Rotations 260

    Case Study 5.2 Decomposing a 3D Affine Transformation 260 Case Study 5.3 Interaction and Creation of Objects within a

    Scene 262 Case Study 5.4 Flying through Primitive Shapes 262 Case Study 5.5 Draw 3D scenes described by SDL 263

    5.9. For Further Reading 263

    6 Modeling Shapes with Polygonal Meshes 264 6.1. Introduction 265 6.2. Introduction to Solid Modeling with Polygonal Meshes 266

    6.2.1. To Define a Polygonal Mesh 268 6.2.1. Sample 3D File Formats 270 6.2.2. To Find the Normal Vectors 270 6.2.3. Properties of Meshes 272 6.2.4. Mesh Models for Nonsolid Objects 274 6.2.5. Working with Meshes in a Program 275

    6.3. Polyhedra 277 6.3.1. Prisms 278 6.3.2. The Piatonic Solids 279 6.3.3. Other Interesting Polyhedra 284

    6.4 Extruded Shapes 286 6.4.1. Creating Prisms 286 6.4.2. Arrays of Extruded Prisms"Brick Laying" 288 6.4.3. Extrusions with a "Twist" 289 6.4.4. To Build Segmented ExtrusionsTubes and Snakes 291 6.4.5. Discretely Swept Surfaces of Revolution 297

    6.5. Mesh Approximations to Smooth Objects 299 6.5.1. Representations for Surfaces 299 6.5.2. The Normal Vector to a Surface 300 6.5.3. The Effect of an Affine Transformation 302 6.5.4. Three "Generic" Shapes: the Sphere, Cylinder, and Cone 303 6.5.5. To Form a Polygonal Mesh for a Curved Surface 307 6.5.6. Ruled Surfaces 309 6.5.7. Surfaces of Revolution 312 6.5.8. Tubes Based on 3D Curves 314 6.5.9. Surfaces Based on Explicit Functions ofTwo Variables 314

  • Contents XV

    6.6. Particle Systems and Physically Based Systems 316 6.6.1. Particle Systems 316 6.6.2. Physically Based Systems 317

    6.7. Summary 318 6.8. Case Studies 319

    Case Study 6.1 Meshes Stored in Files 319 Case Study 6.2 Derivation of the Newell Metho 320 Case Study 6.3 The Prism 323 Case Study 6.4 Prism Arrays and Extruded Quad-Strips 324 Case Study 6.5 Tubes and Snakes Based on a Parametric

    Curve 325 Case Study 6.6 Building Discrete-Stepped Surfaces of

    Revolution 325 Case Study 6.7 On Edge Lists and Wireframe Models 325 Case Study 6.8 Vaulted Ceilings 326 Case Study 6.9 On Piatonic Solids 326

    6.9. For Further Reading 326

    Three-Dimensional Viewing 327 7.1. Introduction 328 7.2. The Camera Revisited 328

    7.2.1. To Set the View Volume 329 7.2.2. To Position and Point the Camera 330

    7.3. To Specify a Camera in a Program 335 7.3.1. To Fly the Camera Interactively 337

    7.4. Perspective Projections of 3D Objects 341 7.4.1. Perspective Projection of a Point 342 7.4.2. Perspective Projection of a Line 345 7.4.3. To Incorporate Perspective in the Graphics Pipeline 349

    7.5. To Produce Stereo Views 363 7.6. Taxonomy of Projections 365

    7.6.1. One-,Two-, and Three-Point Perspective 365 7.6.2. Types of Parallel Projections 369

    7.7. Summary 373 7.8. Case Studies 374

    Case Study 7.1 Flying a Camera Through a Scene 374 Case Study 7.2 Stereo Views 374 Case Study 7.3 Back Face Removal for Greater Efficiency 375

    7.9. For Further Reading 375

    Rendering Facesfor Visual Realism 376 8.1. Introduction 377 8.2. Introduction to Shading Models 381

    8.2.1. Geometrie Ingredients for Finding Reflected Light 382 8.2.2. How to Compute the Diffuse Component 383 8.2.3. Specular Reflection 385 8.2.4. The Role of Ambient Light and Exploiting Human

    Perception 388 8.2.5. How to Combine Light Contributions 389 8.2.6. To Add Color 390

  • xviii Contents

    8.2.7. Shading and the Graphics Pipeline 391 8.2.8. To Use Light Sources in OpenGL 394 8.2.9. To Work with Material Properties in OpenGL 398 8.2.10. Shading of Scenes Specified by SDL 399

    8.3. Fiat Shading and Smooth Shading 400 8.3.1. Fiat Shading and Mach Banding 401 8.3.2. Smooth Shading 402

    8.4. Adding Hidden Surface Removal 406 8.4.1. The Depth-Buffer ApproachThe Method OpenGL

    Uses 406 8.5. To Add Texture to Faces 409

    8.5.1. Paste the Texture onto a Fiat Surface 411 8.5.2. To Render the Texture 414 8.5.3. What Does the Texture Modulate? 421 8.5.4. A Texture Example Using OpenGL 422 8.5.5. Wrap Texture on Curved Surfaces 426 8.5.6. Reflection Mapping 431

    8.6. To Add Shadows of Objects 434 8.6.1. Introduction to Shadows 434 8.6.2. Shadows Using a Shadow Buffer 436 8.6.3. A Brief Look at Radiosity 438

    8.7. O p e n G L 2.0 & The Shading Language (GLSL) 439 8.7.1. Bump Mapping 440 8.7.2. Nonphotorealistic Rendering 441

    8.8. Summary 443 8.9. Case Studies 443

    Case Study 8.1 Creating Shaded Objects Using OpenGL 443 Case Study 8.2 Texture Rendering 444 Case Study 8.3 Extending SDL to Include Texturing 444

    8.10. For Further Reading 444

    9 Tools for Raster Displays 445 9.1. Introduction 446 9.2. Manipulating Pixmaps 447

    9.2.1. Operations of Interest for Pixmaps 447 9.2.2. Useful Data Types for Pixmaps 448 9.2.3. To Scale and Rotate Images 453

    9.3. Combining Pixmaps 456 9.3.1. The Read-Modify-Write Cycle 457 9.3.2. The Alpha Channel and Image Blending 457 9.3.3. Logical Combinations of Pixmaps 462 9.3.4. The BitBLT Operation 466

    9.4. D o It Yourself Line Drawing: Bresenham's Algorithm 467 9.4.1. Bresenham's Line-Drawing Algorithm 468

    9.5. To Define and Fill Regions of Pixels 476 9.5.1. To Define Regions 477

    9.6. Manipulating Symbolically Defined Regions 477 9.6.1. Rectangle-Defined Regions 477 9.6.2. Path-Defined Regions 478

  • 9.7. Filling Polygon-Defined Regions 480 9.7.1. Which Pixels on an Edge Belong to a Polygon? 481 9.7.2. Improving the Algorithm's Performance 483

    9.8. Aliasing and Antialiasing Techniques 487 9.8.1. Antialiasing Techniques 489 9.8.2. Antialiasing of Texture 492 9.8.3. Antialiasing Using OpenGL 494

    9.9. Creating More Shades and Colors 496 9.9.1. Error Diffusion 497

    9.10. Summary 499 9.11. CaseStudies 500

    Case Study 9.1 Reading and Displaying BMP Image Files 500 Case Study 9.2 Dissolving Between Two Pixmaps with OpenGL 500 Case Study 9.3 Working with the Shape Data Structure 500 Case Study 9.4 General Polygon Filling 501 Case Study 9.5 Error Diffusion 501

    9.12. Fur ther Reading 501

    Curve and Surface Design 502 10.1. Introduction 503

    10.1.1. Parametric Curves as Trajectories 503 10.1.2. Smoothness of Motion 504

    10.2. Describing Curves Using Polynomials 508 10.3. On Interactive Curve Design 512 10.4. Bezier Curves for Curve Design 514

    10.4.1. ThedeCasteljauAlgorithm 514 10.5. Properties of Bezier Curves 519 10.6. Finding Better Blending Functions 523

    10.6.1. The Problem of Local Control 523 10.6.2. Wish List for a Set of Blending Functions 525 10.6.3. Piecewise Polynomial Curves and Splines 527 10.6.4. To Build a Set of Blending Functions Out of g(f) 528 10.6.5. Spline Curves and Basis Functions 531

    10.7. The B-Spline Basis Functions 532 10.7.1. Definition of B-Spline Functions 533 10.7.2. How to Use Multiple Knots in the Knot Vector 537 10.7.3. Open B-Spline Curves: Standard Knot Vector 538

    10.8. Useful Properties of B-Spline Curves for Design 542 10.8.1. Using Multiple Control Points 543

    10.9. Rational Splines and N U R B S Curves 544 10.10. A Glimpse at Interpolation 548

    10.10.1. Interpolation Using Piecewise Cubic Polynomials 549 10.10.2. Hermite Interpolation 550 10.10.3. The Natural Cubic Spline 553 10.10.4. How to Compute the Slopes in Cubic Interpolation 555 10.10.5. How to Specify the Tangent Vectors Interactively 559

    10.11. Modeling Curved Surfaces 560 10.11.1. Ruled Surfaces Based on B-splines 560 10.11.2. Surfaces of Revolution Based on B-splines 561 10.11.3. Bezier Surface Patches 562

  • 10.11.4. To Patch Together Bezier Patches 563 10.11.5. B-Spline Patches 565 10.11.6. NURBS Surfaces 566 10.11.7. Subdivision of Surfaces 567

    10.12. Summary 569 10.13. Case Studies

    Case Study 10.1 A Potpourri of Interesting Parametric Curves 571 Case Study 10.2 Elliptipool 572 Case Study 10.3 Bezier Curves 574 Case Study 10.4 A Quadratic Spline Curve Generator 574 Case Study 10.5 Building a Spline Curve Editor 574 Case Study 10.6 Interpolation of Control Points with

    B-Splines 575 Case Study 10.7 Interpolating with Cubic Polynomials 576 Case Study 10.8 The Venerable Teapot 576 Case Study 10.9 NURBS: Invariance to Projective

    Transformations 577 Case Study 10.10 Drawing NURBS Patches 578

    10.14. For Further Reading 579

    Color Theory 580 11.1. Introduction 580

    11.1.1. An Aside on the Eye: Physiologie Basis for Human Color Perception 581

    11.1.2. RGB Color Blindness 582 11.2. Color Description 583

    11.2.1. Dominant Wavelength 584 11.2.2. Color Perception and Color Matching 585

    11.3. The CIE Standard 588 11.3.1. Constructing the CIE Chart 589 11.3.2. Using the CIE Chromaticity Diagram 591 11.3.3. Color Gamuts 592

    11.4. Color Spaces 593 11.4.1. The RGB Color Spaces 593 11.4.2. The Additive and Subtractive Color Systems 594 11.4.3. The HLS Color Model 596

    11.5. Indexed Color and the L U T 598 11.6. Color Quantization 599

    11.6.1. Uniform Quantization 601 11.6.2. The Popularity Algorithm 603 11.6.3. The Median Cut Algorithm 604 11.6.4. Octree Quantization 605

    11.7. Summary 607 11.8. Case Studies 608

    Case Study 11.1 Drawing RGB Space 608 Case Study 11.2 HSV to RGB 608 Case Study 11.3 Uniform Color Quantization 608 Case Study 11.4 Popularity Color Quantization 608 Case Study 11.5 Median Cut Color Quantization 608 Case Study 11.6 Octree Color Quantization 609

    11.9. For Further Reading 610

  • Contents xxi

    12 Introduction to Ray Tracing 611 12.1. Introduction 612 12.2. Setting U p the Geometry of Ray Tracing 613 12.3. Overview of the Ray Tracing Process 616 12.4. Intersection of a Ray with an Object 617

    12.4.1. Intersection of a Ray with the Generic Plane 619 12.4.2. Intersection with a Generic Sphere 619 12.4.3. Intersection of the Ray with Transformed Objects 620

    12.5. Organizing a Ray Tracer Application 622 12.5.1. A Routine to Compute Ray-Sphere Intersections 627 12.5.2. A Complete Ray Tracer for Emissive Sphere

    Scenes 629 12.6. Intersecting Rays with Other Primitives 629

    12.6.1. Intersecting with a Square 630 12.6.2. Intersecting with a Tapered Cylinder 630 12.6.3. Intersecting with a Cube (or Any Convex Polyhedron) 633 12.6.4. Adding More Primitives 639

    12.7. To Draw Shaded Pictures of Scenes 640 12.7.1. To Find the Normal at the Hit Spot 641 12.7.2. Color Objects According to Their Surface Materials 641 12.7.3. Physically Based Shading ModelsCook Torrance

    Shading 643 12.8. Adding Surface Texture 649

    12.8.1. Solid Texture 649 12.8.2. To Paste Images onto Surfaces 657

    12.9. Antialiasing Ray Tracings 659 12.10. Us ingExten t s 661

    12.10.1. Box and Sphere Extents 662 12.10.2. Using Projection Extents 667 12.10.3. Alternative Method for Accelerating Ray Tracing:

    BSPTrees 669 12.11. Adding Shadows for Greater Realism 671 12.12. Reflections and Transparency 674

    12.12.1. The Refraction of Light 677 12.12.2. Dealing with Refraction in shade O 681

    12.13. Compound Objects: Boolean Operat ions on Objects 683 12.13.1. Ray Tracing CSG Objects 685 12.13.2. Data Structure for Boolean Objects 687 12.13.3. Intersecting Rays with Boolean Objects 690 12.13.4. Building and Using Extents for CSG Objects 693

    12.14. Ray Tracing vs. Ray Casting 694 12.15. Summary 695 12.16. CaseStudies 696

    Case Study 12.1 An Emissive Ray Tracer 696 Case Study 12.2 A Renaissance Ray Tracer 696 Case Study 12.3 Implementing Shadows in a Ray Tracer 697 Case Study 12.4 Using Extents to Speed Up Ray Tracing 697 Case Study 12.5 Ray Tracing with 3D Textures 697 Case Study 12.6 Antialiasing 697 Case Study 12.7 Ray Tracing Other Primitives 697

  • XX Contents

    Case Study 12.8 A 2D Ray Tracer to Explore Refraction 697 Case Study 12.9 Reflected and Refracted Light 697 Case Study 12.10 RayTracing Boolean Combinations of

    Objects 698 12.17. For Further Reading 698

    APPENDIX

    1 Graphics Tools: How to Obtain and Install OpenGL 699 2 Some Mathematics for Computer Graphics 701

    A2.1. Some Key Definitions Pertaining to Matrices and Their Operat ions 701 A2.1.1. Manipulation^ with Matrices 702 A2.1.2. Multiplying Two Matrices 702 A2.1.3. Partitioning a Matrix 704 A2.1.4 The Determinant of a Matrix 705 A2.1.5. The Inverse of a Matrix 706

    A2.2. Some Properties of Vectors and Their Operat ions 707 A2.2.1. The Perp of a Vector; the Perp Dot Product 707

    A2.3. Spherical Coordinates and Direction Cosines 707

    3 SDL: Scene Description Language 710 A3.1. Syntax of SDL 742 A3.2. Macros in SDL 746 A3.3. Ex t end ingSDL 747

    4 Fractals and the Mandelbrot Set 749 A4.1. Introduction 749 A4.2. Fractals and Self-Similarity 749 A4.3. The Mandelbrot Set 750

    A4.3.1. Mandelbrot Sets and Iterated Function Systems 751 A4.3.2. How to Compute Whether Point c Is in the

    Mandelbrot Set 754 A4.3.3. Draw the Mandelbrot Set 754 A4.3.4. Some Notes on the Mandelbrot Set 757

    5 Relative and Turtle Drawing 759 A5.1. To Develop moveRel ( ) and 1 i neRel () 759 A5.2 Turtle Graphics 761 A5.3. Figures Based on Regulr Polygons 765

    A5.3.1. The Regulr Polygons 766 A5.3.2. Variations on -gons 767

    INDEX 771