Sunteți pe pagina 1din 11

Computer Graphics

U s i n g O p e n G L
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 Comput er Graphi cs? 1
1.2. Wher e Comput er - Gener at ed Pictures Ar e 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 El ement s of Pictures Cr eat ed in Comput er Graphi cs 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. Graphi cs Display Devi ces 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. Graphi cs 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. Chapt er Summary 37
1.7. Chapt er Exercises 38
1.8. For Fur t her Readi ng 38
2 Initial Steps in Drawing Figures 39
2.1. To Get St art ed Maki ng 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 Ope nGL Basic Graphi cs Primitives 47
2.2.1. Examples of Drawing Dot Constellations 52
2.3. Li ne Drawi ngs in Ope nGL 59
2.3.1. Drawing Polylines and Polygons 60
2.3.3. Line Drawing Using moveTo Oa n d l i n e To () 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
xi v 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.
Summar y of t he Chapt er 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 Dot 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. Repr esent at i ons of Key Geomet r i e 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 t he Int ersect i on of Two Line Segment s 170
4.6.1. Application of Line Intersections: The Circle Through
Three Points 172
4.7. Int ersect i ons of Lines with Planes, and Clipping 174
4.8. Polygon Int ersect i on Probl ems 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. Summar y of t he Chapt er 184
4.10. CaseSt udi es 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 Fur t her Readi ng 189
5 Transformations of Objects 190
5.1. Int roduct i on 191
5.2. Int roduct i on 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 Transformat i ons 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 Coordi nat e Systems 225
5.5. Affine Transformations Used in a Program 228
5.5.1. The CT Saved for Later Use 235
5.6. To Dr aw 3D Scenes Interactively with Ope nGL 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 t he Chapt er 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 Fur t her Readi ng 263
6 Modeling Shapes with Polygonal Meshes 264
6.1. Int roduct i on 265
6.2. Int roduct i on to Solid Model i ng 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. Pol yhedra 277
6.3.1. Prisms 278
6.3.2. The Piatonic Solids 279
6.3.3. Other Interesting Polyhedra 284
6.4 Ext r uded 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 Approxi mat i ons t o Smoot h 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 Fur t her Readi ng 326
Three-Dimensional Viewing 327
7.1. Int roduct i on 328
7.2. The Camer a 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 Camer a in a Program 335
7.3.1. To Fly the Camera Interactively 337
7.4. Perspect i ve 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 Pr oduce St ereo 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 Fur t her Readi ng 375
Rendering Facesfor Visual Realism 376
8.1. Int roduct i on 377
8.2. Int roduct i on t o Shading Model s 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 Smoot h Shading 400
8.3.1. Fiat Shading and Mach Banding 401
8.3.2. Smooth Shading 402
8.4. Addi ng Hi dden Surface Removal 406
8.4.1. The Depth-Buffer ApproachThe Method OpenGL
Uses 406
8.5. To Add Text ure t o 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. Ope nGL 2.0 & The Shading Language ( GLSL) 439
8.7.1. Bump Mapping 440
8.7.2. Nonphotorealistic Rendering 441
8.8. Summar y 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 Fur t her Readi ng 444
9 Tools for Raster Displays 445
9.1. Int roduct i on 446
9.2. Mani pul at i ng 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. Combi ni ng 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. Do It Yourself Line Drawi ng: Br esenham' s
Al gori t hm 467
9.4.1. Bresenham's Line-Drawing Algorithm 468
9.5. To Define and Fill Regi ons of Pixels 476
9.5.1. To Define Regions 477
9.6. Mani pul at i ng Symbolically Defined Regi ons 477
9.6.1. Rectangle-Defined Regions 477
9.6.2. Path-Defined Regions 478
9.7. Filling Polygon-Defined Regi ons 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. Creat i ng Mor e Shades and Colors 496
9.9.1. Error Diffusion 497
9.10. Summar y 499
9.11. CaseSt udi es 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 t her Readi ng 501
Curve and Surface Design 502
10.1. Int roduct i on 503
10.1.1. Parametric Curves as Trajectories 503
10.1.2. Smoothness of Motion 504
10.2. Descri bi ng Curves Using Polynomials 508
10.3. On Int eract i ve Curve Desi gn 512
10.4. Bezi er Curves for Curve Desi gn 514
10.4.1. ThedeCasteljauAlgorithm 514
10.5. Propert i es of Bezi er Curves 519
10.6. Finding Bet t er Bl endi ng Funct i ons 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 Funct i ons 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 Propert i es of B-Spline Curves for Desi gn 542
10.8.1. Using Multiple Control Points 543
10.9. Rat i onal Splines and NURBS Curves 544
10.10. A Gl i mpse at Int erpol at i on 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. Model i ng 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 Fur t her Readi ng 579
Color Theory 580
11.1. Int roduct i on 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. Col or Descri pt i on 583
11.2.1. Dominant Wavelength 584
11.2.2. Color Perception and Color Matching 585
11.3. The CI E St andard 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. Col or 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 t he LUT 598
11.6. Col or Quant i zat i on 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 Fur t her Readi ng 610
Contents xxi
12 Introduction to Ray Tracing 611
12.1. Int roduct i on 612
12.2. Setting Up t he Geomet r y of Ray Tracing 613
12.3. Overvi ew of t he Ray Tracing Process 616
12.4. Int ersect i on of a Ray with an Obj ect 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. Organi zi ng a Ray Tracer Appl i cat i on 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 Ot her 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 Dr aw 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. Addi ng 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 i ngExt ent 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. Addi ng Shadows for Gr eat er Real i sm 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: Bool ean Operat i ons 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. CaseSt udi es 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 Fur t her Readi ng 698
APPENDIX
1 Graphics Tools: How to Obtain and Install OpenGL 699
2 Some Mathematics for Computer Graphics 701
A2. 1. Some Key Definitions Pert ai ni ng t o Mat ri ces and Thei r
Operat i ons 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 Propert i es of Vectors and Thei r Oper at i ons 707
A2.2.1. The Perp of a Vector; the Perp Dot Product 707
A2. 3. Spherical Coordi nat es and Di rect i on Cosines 707
3 SDL: Scene Description Language 710
A3. 1. Syntax of SDL 742
A3.2. Macros in SDL 746
A3. 3. Ext endi ngSDL 747
4 Fractals and the Mandelbrot Set 749
A4. 1. Int roduct i on 749
A4.2. Fractals and Self-Similarity 749
A4. 3. The Mandel br ot 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 Devel op moveRel ( ) and 1 i neRel () 759
A5. 2 Turtle Graphi cs 761
A5. 3. Figures Based on Regul r Polygons 765
A5.3.1. The Regulr Polygons 766
A5.3.2. Variations on -gons 767
INDEX 771

S-ar putea să vă placă și