HE COMPLETE OPENGL PROGI FOR WINDOW WIND

Similar documents
CHAPTER 1 Graphics Systems and Models 3

Computer Graphics: Programming, Problem Solving, and Visual Communication

OpenGL SUPERBIBLE. Fifth Edition. Comprehensive Tutorial and Reference. Richard S. Wright, Jr. Nicholas Haemel Graham Sellers Benjamin Lipchak

Interactive Computer Graphics A TOP-DOWN APPROACH WITH SHADER-BASED OPENGL

Module 13C: Using The 3D Graphics APIs OpenGL ES

Graphics and Interaction Rendering pipeline & object modelling

Advanced 3D Game Programming with DirectX* 10.0

GLOBAL EDITION. Interactive Computer Graphics. A Top-Down Approach with WebGL SEVENTH EDITION. Edward Angel Dave Shreiner

Rasterization Overview

Models and Architectures

Programming Guide. Aaftab Munshi Dan Ginsburg Dave Shreiner. TT r^addison-wesley

COMP30019 Graphics and Interaction Rendering pipeline & object modelling

Lecture outline. COMP30019 Graphics and Interaction Rendering pipeline & object modelling. Introduction to modelling

Introduction. What s New in This Edition

E.Order of Operations

Computer Graphics I Lecture 11

Graphics Shaders. Theory and Practice. Second Edition. Mike Bailey. Steve Cunningham. CRC Press. Taylor&FnincIs Croup tootutor London New York

Foundations of 3D Graphics Programming

Real-Time Rendering. Tomas Möller Eric Haines. A K Peters Natick, Massachusetts

Introduction to Computer Graphics with WebGL

2: Introducing image synthesis. Some orientation how did we get here? Graphics system architecture Overview of OpenGL / GLU / GLUT

Points and lines, Line drawing algorithms. Circle generating algorithms, Midpoint circle Parallel version of these algorithms

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

Models and Architectures. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

Buffers, Textures, Compositing, and Blending. Overview. Buffers. David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E.

World Coordinate System

Normalized Device Coordinate System (NDC) World Coordinate System. Example Coordinate Systems. Device Coordinate System

Computer Graphics Fundamentals. Jon Macey

Building scalable 3D applications. Ville Miettinen Hybrid Graphics

CS 465 Program 4: Modeller

CS 381 Computer Graphics, Fall 2008 Midterm Exam Solutions. The Midterm Exam was given in class on Thursday, October 23, 2008.

XGL Device Pipeline Porting Guide

Course Title: Computer Graphics Course no: CSC209

Sculpting 3D Models. Glossary

Topics and things to know about them:

CONTENTS IN DETAIL. What s in This Book?... xx Who Is This Book For?... xx

CS 130 Final. Fall 2015

3D Graphics and OpenGl. First Steps

Computer Graphics. Shadows

Deferred Rendering Due: Wednesday November 15 at 10pm

CS GAME PROGRAMMING Question bank

Introduction to Computer Graphics. Knowledge basic concepts 2D and 3D computer graphics

Computer Graphics: Programming, Problem Solving, and Visual Communication

Rendering. Converting a 3D scene to a 2D image. Camera. Light. Rendering. View Plane

The University of Calgary

Module Contact: Dr Stephen Laycock, CMP Copyright of the University of East Anglia Version 1

The Rendering Pipeline (1)

OpenGL. Toolkits.

Rendering Objects. Need to transform all geometry then

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

CS 4620 Program 3: Pipeline

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

Texture Mapping and Special Effects

Adaptive Point Cloud Rendering

1 INTRoduCTIon to BLENDER 1. 3 PREPARATIon 19

Computer Graphics 10 - Shadows

OpenGL: Open Graphics Library. Introduction to OpenGL Part II. How do I render a geometric primitive? What is OpenGL

3D Programming. 3D Programming Concepts. Outline. 3D Concepts. 3D Concepts -- Coordinate Systems. 3D Concepts Displaying 3D Models

Pipeline Operations. CS 4620 Lecture 14

Animation & Rendering

CS230 : Computer Graphics Lecture 4. Tamar Shinar Computer Science & Engineering UC Riverside

Review. Stephen J. Guy

Pipeline Operations. CS 4620 Lecture 10

A simple OpenGL animation Due: Wednesday, January 27 at 4pm

Texture. Texture Mapping. Texture Mapping. CS 475 / CS 675 Computer Graphics. Lecture 11 : Texture

CS 475 / CS 675 Computer Graphics. Lecture 11 : Texture

Lecturer Athanasios Nikolaidis

End-Term Examination

3D Rendering Pipeline

CSE4030 Introduction to Computer Graphics

Lecture 4 Advanced Computer Graphics (CS & SE )

OXFORD ENGINEERING COLLEGE (NAAC Accredited with B Grade) DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING LIST OF QUESTIONS

Academic Plan GAME & SIMULATION FOUNDATIONS

To Do. Computer Graphics (Fall 2008) Course Outline. Course Outline. Methodology for Lecture. Demo: Surreal (HW 3)

Ciril Bohak. - INTRODUCTION TO WEBGL

CS 464 Review. Review of Computer Graphics for Final Exam

Course Information Package

SHADERX 7 : ADVANCED RENDERING TECHNIQUES

Rendering Grass with Instancing in DirectX* 10

GEOMETRIC TOOLS FOR COMPUTER GRAPHICS

OpenGL refresher. Advanced Computer Graphics 2012

Geometry Primitives. Computer Science Department University of Malta. Sandro Spina Computer Graphics and Simulation Group. CGSG Geometry Primitives

Beginning Direct3D Game Programming: 1. The History of Direct3D Graphics

Topic 12: Texture Mapping. Motivation Sources of texture Texture coordinates Bump mapping, mip-mapping & env mapping

Computer Graphics. Prof. Feng Liu. Fall /21/2016

Computer Graphics Lecture 2

Orthogonal Projection Matrices. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

5 Linear Algebra Determinants Matrices...121

Overview: Ray Tracing & The Perspective Projection Pipeline

Geometric Programming for Computer-Aided Design

Graphics Processing Unit Architecture (GPU Arch)

Computer Graphics Introduction. Taku Komura

Clipping and Scan Conversion

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

CSE 167: Introduction to Computer Graphics Lecture #4: Vertex Transformation

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY CS2401 COMPUTER GRAPHICS QUESTION BANK

Graphics and Visualization (GV)

Blue colour text questions Black colour text sample answers Red colour text further explanation or references for the sample answers

Topic 11: Texture Mapping 11/13/2017. Texture sources: Solid textures. Texture sources: Synthesized

CS451Real-time Rendering Pipeline

Transcription:

HE COMPLETE OPENGL PROGI FOR WINDOW WIND WAITC GROUP PRESS. A Division of Sams Publishing Corte Madera CA

Table of Contents FOREWORD xxii INTRODUCTION xxiii PART I: INTRODUCTION TO OPENGL 1 CHAPTER 1: What Is OpenGL? 5 CHAPTER 2: 3D Graphics Fundamentals 13 CHAPTER 3: Learning OpenGL with the AUX Library 27 CHAPTER 4: OpenGL for Windows: OpenGL + Win32 = Wiggle 75 CHAPTER 5: Errors and Other Messages from OpenGL 109 PART II: USING OPENGL 121 CHAPTER 6: Drawing in 3D: Lines, Points, and Polygons 125 CHAPTER 7: Manipulating 3D Space: Coordinate Transformations 183 CHAPTER 8: Color and Shading 225 CHAPTER 9: Lighting and Lamps 261 CHAPTER 10: 3D Modeling and Object Composition 313 CHAPTER 11: Raster Graphics in OpenGL 345 CHAPTER 12: Texture Mapping 381 CHAPTER 13: Quadrics: Spheres, Cylinders, and Disks 431 PART IN: ADVANCED TOPICS AND SPECIAL EFFECTS 455 CHAPTER 14: The OpenGL State Machine 459 CHAPTER 15: Buffers: Not Just for Animation 471 CHAPTER 16: Visual Effects: Blending and Fog 503 CHAPTER 17: Curves and Surfaces: What the #%@!&* Are NURBS? 533 CHAPTER 18: Polygon Tessellation 575 CHAPTER 19: Interactive Graphics 589 CHAPTER 20: OpenGL on the 'Net: VRML 619 PART IV: OPENGL WITH 629 CHAPTER 21: MFC-Based OpenGL Programming 633 CHAPTER 22: OWL-Based OpenGL Programming 647 CHAPTER 23: Visual Basic and 4GL-Based OpenGL Programming 665 CHAPTER 24: The Future of OpenGL and Windows 681 APPENDIX A: Performance-Tuning OpenGL for Windows 687 APPENDIX B: Further Reading 691 APPENDIX C: Version 1.1 695 APPENDIX D: Glossary 697 INDEX 701 vii

V Contents Foreword..xxii Introduction xxiii PART I: INTRODUCTION TO OPENGL l CHAPTER 1: WHAT IS OPENGL? 5 About OpenGL 8 A History of OpenGL 8 Further Developments in OpenGL 9 How OpenGL Works 9 OpenGL under Windows 9 Graphics Architecture: Software versus Hardware 9 Limitations of the Generic Implementation 11 Future Prospects for OpenGL in Windows 11 CHAPTER 2:3D GRAPHICS FUNDAMENTALS 13 3D Perception 15 2D + Perspective = 3D 17 Hidden Line Removal 17 Colors and Shading 18 Lights and Shadows 18 Coordinate Systems 19 2D Cartesian Coordinates 19 Coordinate Clipping 20 Viewports, Your Window to 3D 21 Drawing Primitives 23 3D Cartesian Coordinates 23 Projections, The Essence of 3D 24 Orthographie Projections 25 Perspective Projections 25 Summary 26 CHAPTER 3: LEARNING OPENGL WITH THE AUX LIBRARY 27 OpenGL: An API, Not a Language 30 The OpenGL Division of Labor 30 viii

Contents OpenGL Data Types 31 Function Naming Conventions 32 The AUX Library 34 Platform Independence 34 AUX = Platform I/O, the Easy Way 35 Dissecting a Short OpenGL Program 35 The Includes 37 The Body 37 Display Mode: Single-Buffered 37 Position the Window 38 Create the OpenGL Window 38 Clear a Window (Erase with a Color) 40 Actually Clear 41 Flush That Queue 41 Drawing Shapes with OpenGL 42 The Rendering Function 43 Drawing a Rectangle 43 Initialization 44 Scaling to the Window 45 Setting the Viewport and Clipping Volume 45 Defining the Viewport 49 Defining the Clipping Volume 49 Keeping a Square Square 51 Animation with AUX 52 Double Buffering 54 Finally, Some 3D! 56 Summary 56 Reference Section 57 CHAPTER 4: OPENGL FOR WINDOWS: OPENGL + WIN32 = WIGGLE 75 Drawing in Windows Windows 78 GDI Device Contexts 78 OpenGL Rendering Contexts 81 Using the Wiggle Functions 81 Creating and Selecting a Rendering Context 82 Painting with OpenGL 83 Preparing the Window for OpenGL 84 Window Styles 84 ix

OPENGL SUPERBIBLE Pixel Formats 84 Retum of the Bouncing Square 86 Scaling to the Window 90 Ticktock, the Idle Clock 90 Lights, Camera, Action! 91 Summary 92 Reference Section 92 CHAPTER 5: ERRORS AND OTHER MESSAGES FROM OPENGL 109 When Bad Things Happen to Good Code 112 Who Am I and What Can I Do? 114 Extensions to OpenGL 115 Get a Clue with glhint 115 Summary 116 Reference Section 116 PART II: USING OPENGL 121 CHAPTER 6: DRAWING IN 3D: LINES, POINTS, AND POLYGONS 125 Drawing Points in 3D 128 Setting Up a 3D Canvas 128 A 3D Point: The Vertex 130 Draw Something! 130 Drawing Points 131 Our First Example 132 Setting the Point Size 135 Drawing Lines in 3D 137 Line Strips and Loops 139 Approximating Curves with Straight Lines 141 Setting the Line Width 141 Line Stippling 143 Drawing Triangles in 3D 146 Triangles: Your First Polygon 146 Winding 146 Triangle Strips 148 Triangle Fans 149 Building Solid Objects 150 Setting Polygon Colors 153 Hidden Surface Removal 153 X

Contents Culling: Hiding Surfaces for Performance 154 Polygon Modes 158 Other Primitives 159 Four-Sided Polygons: Quads 159 Quad Strips 159 General Polygons 160 Filling Polygons, or Stippling Revisited 161 Polygon Construction Rules 166 Subdivision and Edges 166 Summary 169 Reference Section 171 CHAPTER 7: MANIPULATING 3D SPACE: COORDINATE TRANSFORMATIONS 183 Is This the Dreaded Math Chapter? 186 Understanding Transformations 186 Eye Coordinates 187 Viewing Transformations 188 Modeling Transformations 189 The Modelview Duality 191 Projection Transformations 192 Viewport Transformations 193 Matrix Munching 193 What Is a Matrix? 193 The Transformation Pipeline 193 The Modelview Matrix 195 Translation 196 Rotation 196 Scaling 198 The Identity Matrix 198 The Matrix Stacks 201 A Nuclear Example 202 Using Projections 205 Orthographie Projections 205 Perspective Projections 207 A Far-Out Example 211 Advanced Matrix Manipulation 214 Loading a Matrix 214 Performing Your Own Transfprmations 215 xi

OPENGL SUPERBIBLE Other Transformations 215 Summary 215 Reference Section 216 CHAPTER 8: COLOR AND SHADING 225 What Is a Color? 228 Light as a Wave 228 Light as a Particle 229 Your Personal Photon Detector 230 The Computer as a Photon Generator 231 PC Color Hardware 232 PC Display Modes 233 Screen Resolution 234 Color Depth 234 4-Bit Color 234 8-Bit Color 234 24-Bit Color 235 Other Color Depths 235 Selecting a Color 236 The Color Cube 236 Setting the Drawing Color 237 Shading 238 Setting the Shading Model 241 Windows Palettes 242 Color Matching 242 Dithering 243 Advantages of a Palette in 8-Bit Mode 244 Palette Arbitration 244 Creating a Palette 246 Do You Need a Palette? 246 The Palette's Structure 247 The 3-3-2 Palette 248 Building the Palette 249 Palette Creation and Disposal 250 Some Restrictions Apply 251 Color Index Mode 251 Why Use Color Index Mode? 252 Using Color Index Mode 252 xii

Contents Show the Triangle 254 Summary 254 Reference Section 255 CHAPTER 9: LIGHTING AND LAMPS 261 Light in the Real World 264 Ambient Light 265 Diffuse Light 265 Specular Light 265 Put It All Together 267 Materials in the Real World 267 Material Properties 268 Adding Light to Materials 268 Calculating Ambient Light Effects 268 Diffuse and Specular Effects 269 Adding Light to a Scene 270 Enable the Lighting 270 Set Up the Lighting Model 271 Set Material Properties 271 Using a Light Source 274 Which Way Is Up? 274 Surface Normals 275 Specifying a Normal 276 Unit Normals 278 Finding a Normal 279 Setting Up a Source 281 Setting the Material Properties 282 Specifying the Polygons 283 Lighting Effects 284 Specular Highlights 284 Specular Light 285 Specular Reflectance 285 Specular Exponent 286 Normal Averaging 288 Spotlights 292 Creating a Spotlight 293 Drawing a Spotlight 294 Shadows 296 xiii

OPENGL SUPERBIBLE What Is a Shadow? 296 Squish Code 298 A Shadow Example 300 Lighting and Color Index Mode 302 Summary 303 Reference Section 303 CHAPTER 10:3D MODELING AND 0BJECT COMPOSITION 313 Defining the Task 316 Choosing a Projection 316 Choosing the Lighting and Material Properties 317 Displaying the Results 318 Constructing a Model, One Piece at a Time 319 The Head 319 The Shaft 323 The Thread 326 Putting the Model Together 330 A Makeshift Benchmark 331 Improving Performance 334 Creating a Display List 335 Summary 338 Reference Section 338 CHAPTER 11: RASTER GRAPHICS IN OPENGL 345 Drawing Bitmaps 347 Bitmap Fonts 350 Building a Simple Font Library 351 Pixmaps: Bitmaps with Color 354 Drawing Pixmaps 354 Remapping Colors 355 Color Mapping Tables 356 Scaling a Pixmap 357 Panning a Pixmap 358 Reading Pixmaps 358 Copying Pixmaps 361 A Bitmap File Viewer 361 About Windows Bitmap Files 362 xiv

Contents Reading the.bmp File 363 Writing the.bmp File 365 Printing the Bitmap 367 Displaying the Bitmap 370 Summary 372 Reference Section 372 CHAPTER 12: TEXTURE MAPPING 381 The Basics of Texture Mapping 384 Defining Texture Images 385 Defining 1D Textures 385 Defining 2D Textures 387 Drawing Textured Polygons 388 Mipmapped Textures 390 A Terrain Viewing Program 392 Defining the Terrain 393 Drawing Terrain 393 Drawing the Scene 395 Automatically Generating Texture Coordinates 396 Flying Through the Terrain 398 Summary 398 Reference Section 424 CHAPTER 13: QUADRICS: SPHERES, CYLINDERS, AND DISKS 431 Creaüng a Quadric 434 Changing the Way Quadrics Are Drawn 434 Drawing Cylinders 435 Drawing Cones 436 Texturing and Cylinders 436 Drawing Disks 436 Disks and Textures 437 Drawing Partial Disks 437 Drawing Spheres 437 Spheres and Textures 438 Drawing a Pencil 438 Summary 440 Reference Section 449

OPENGL SUPERBIBLE I PART IM: ADVANCED TOPICS AND SPECIAL EFFECTS 455 CHAPTER 14: THE OPENGL STATE MACHINE 459 Basic OpenGL State Functions 461 ' Saving and Restoring States 462 Drawing States 464 Depth Buffer States 465 Stencil Buffer States 465 Lighting States 465 Texturing States 466 Pixel States 467 Reference Section 467 CHAPTER 15: BUFFERS: NOT JUST FOR ANIMATION 471 What Are Buffers? 474 Configuring Buffers 474 The Color Buffer 477 Double Buffering 477 Stereo Buffering 478 Swapping Buffers 478 The Depth Buffer 479 Depth Comparisons 480 Depth Values 480 Applications of the Depth Buffer 481 Another Application of the Depth Buffer 484 Cutting Away Parts of a Scene 484 The Stencil Buffer 488 Using the Stencil Buffer 488 Stencil Buffer Functions 489 Drawing into the Stencil Buffer 489 The Accumulation Buffer 493 Using the Accumulation Buffer for Motion Blur 494 Using the Accumulation Buffer for Anti-Aliasing 497 Reference Section 498 CHAPTER 16: VISUAL EFFECTS: BLENDING AND FOG 503 Blending 505 Using Blending for Transparency 506 xvi j

Contents Using Blending with Anti-Aliasing 510 Using Blending for a Paint Program 511 Fog 519 Drawing Depth-Cued Teapots 520 Other Types of Fog 523 Fog Distance 524 Revisiting the Terrain Viewing Program 524 Summary 530 Reference Section 530 CHAPTER 17: CURVES AND SURFACES: WHAT THE #%@!&* ARE NURBS? 533 Curves and Surfaces 536 Parametric Representation 536 Control Points 537 Continuity 538 Evaluators 538 A 2D Curve 539 Evaluating a Curve 543 A 3D Surface 543 Lighting and Normal Vectors 545 NURBS 546 From Bezier to B-Splines 547 Knots 548 Creating a NURBS Surface 549 NURBS Properties 549 Define the Surface 549 Trimming 550 Summary l 553 Reference Section 553 CHAPTER 18: POLYGON TESSELLATION 575 Complex Polygons 577 Drawing Concave Polygons 578 Drawing Complex Polygons 579 Callback Functions 583 Summary 584 Reference Section 585 xvij

OPENGL SUPERBIBLE CHAPTER 19: INTERACTIVE GRAPHICS 589 Selection 592 Naming Your Primitives 592 Working with Selection Mode 594 The Selection Buffer 594 Picking 596 Hierarchical Picking 598 Feedback 602 The Feedback Buffer 602 Feedback Data 602 PassThrough Markers 604 An Example 604 Label the Objects for Feedback 604 Step 1: Select the Object 605 Step 2: Get Feedback on the Object 607 Summary 608 Reference Section 609 CHAPTER 20: OPENGL 0N THE 'NET: VRML 619 When Worlds Collide 621 Two-Dimensional Navigation 622 Enter VRML 622 WebSpace 624 Installation 624 The Walk Viewer 624 The Examiner Viewer 626 Open Inventor and VRML 626 Summary 628 PART IV: OPENGL WITH 629 CHAPTER 21: MFC-BASED OPENGL PROGRAMMING 633 Isolate Your OpenGL Code 636 Starting with AppWizard 637 Build the Shell 637 Add the Libraries 637 Get CView Ready for OpenGL 639 Pixel Format and Rendering Context 639 xviii

Contents I Clean Up the Rendering Context 641 I Handling Window Resizing 641 I Rendering the Scene 642 1 Don't Erase First 642 I CPalette Handling 643 Summary 646 CHAPTER 22: OWL-BASED OPENGL PROGRAMMING 647 ; Isolate Your OpenGL Code 650 ' Starting with AppExpert 651 Build the Shell 651 f Add the Headers 651 Add the Message Handlers 653 > Fleshing Out the Shell 654 Get TWindowView Ready for OpenGL 654 L Pixel Format and Rendering Context 655 f Clean Up the Rendering Context 656 s Handling Window Resizing 657 Rendering the Scene 657 No Flickering Allowed 658 I Keep It Moving 658 j TPalette Handling 660 Summary 663 CHAPTER 23: VISUAL BASIC AND 4GL-BASED OPENGL PROGRAMMING 665 t Low-Level Access Required 667 f The Magic of Objects 668 Plug and Play 668 I Wrap It Up 669 I Use and Operation of WaiteGL.OCX 669 OpenGL Flags 670 { Installing and Using WaiteGL from VB 4.0 670 s- Installing the Control 671 A Visual Basic Example 671 E-' ; Painting the OpenGL Window 672 f Now for Some Action 673 l Installing the OCX in Delphi 2.0 674 i m

OPENGL SUPERBIBLE Installing the Control 674 A Delphi Example 675 Painting the OpenGL Window 677 Now for Some Action 677 Some Notes About the Source 679 Summary 679 r CHAPTER 24: THE FUTURE OF OPENGL AND WINDOWS.681 1 Conclusion 686 APPENDIX A: PERFORMANCE-TUNING OPENGL FOR WINDOWS 687 Display Lists 688 Matrix Operations 688 Lighting Operations 688 Object Construction 688 Miscellaneous Tips 689 I APPENDIX B: FURTHER READING 691* Books on Windows Programming 691 Books and References on OpenGL 692 Books and References on Graphics Programming (3D in Particular) 692 OpenGL-Related Web and FTP Sites 692 VRML Repositories 693 j I I APPENDIX C: VERSION 1.1 695 APPENDIX D: GLOSSARY 697 INDEX 70il V XX