You are previewing OpenGL® SuperBible, Third Edition.
O'Reilly logo
OpenGL® SuperBible, Third Edition

Book Description

In a world where everything from how we take pictures to how we get our food is designed to happen as quickly and easily as possible, it can take a lot longer to learn how to make it all work behind the scenes. Theories and concepts can weigh you down when all you really want to know is how to create, design and go. That's where OpenGL SuperBible comes in. Complete with supporting figures and a CD packed with free tools, demos and libraries, OpenGL SuperBible simplifies 3D programming concepts without wasting your time with programming theory. Mac and Windows users will both find this comprehensive hands-on guide helpful as you learn to program for games, visualization applications and multi-platforms. This is the ultimate resource for any new programmer.

Table of Contents

  1. Copyright
    1. Dedication
  2. About the Authors
  3. Acknowledgments
  4. We Want to Hear from You!
  5. Introduction
    1. What's in This Book
      1. Part I: Classic OpenGL
      2. Part II: OpenGL Everywhere
      3. Part III: OpenGL: The Next Generation
    2. Conventions Used in This Book
    3. About the Companion CD
      1. Building the Sample Programs
    4. Dare to Dream in 3D!
  6. I. Classic OpenGL
    1. 1. Introduction to 3D Graphics and OpenGL
      1. What's This All About?
      2. A Brief History of Computer Graphics
        1. Enter the CRT
        2. Going 3D
          1. 2D + Perspective = 3D
          2. 3D Artifacts
      3. A Survey of 3D Effects
        1. Perspective
        2. Color and Shading
        3. Light and Shadows
        4. Texture Mapping
        5. Fog
        6. Blending and Transparency
        7. Antialiasing
      4. Common Uses for 3D Graphics
        1. Real-Time 3D
        2. Non–Real-Time 3D
      5. Basic 3D Programming Principles
        1. Immediate Mode and Retained Mode (Scene Graphs)
        2. Coordinate Systems
          1. 2D Cartesian Coordinates
          2. Coordinate Clipping
          3. Viewports: Mapping Drawing Coordinates to Window Coordinates
          4. The Vertex–A Position in Space
          5. 3D Cartesian Coordinates
        3. Projections: Getting 3D to 2D
          1. Orthographic Projections
          2. Perspective Projections
      6. Summary
    2. 2. Using OpenGL
      1. What Is OpenGL?
        1. Evolution of a Standard
          1. The OpenGL ARB
          2. Licensing and Conformance
        2. The API Wars
          1. Enter DirectX
          2. OpenGL Comes to the PC
          3. OpenGL for Games?
          4. Direct3D's Head Start
          5. Dirty Pool
          6. The Future of OpenGL
      2. How Does OpenGL Work?
        1. Generic Implementations
        2. Hardware Implementations
        3. The Pipeline
      3. OpenGL: An API, Not a Language
        1. Libraries and Headers
      4. API Specifics
        1. Data Types
        2. Function-Naming Conventions
      5. Platform Independence
        1. Using GLUT
        2. Setting Up Your Programming Environment
        3. Your First Program
          1. The Header
          2. The Body
          3. Display Mode: Single Buffered
          4. Creating the OpenGL Window
          5. Displaying Callback
          6. Set Up the Context and Go!
          7. OpenGL Graphics Calls
          8. Clearing the Color Buffer
          9. Flushing That Queue
        4. Drawing Shapes with OpenGL
          1. Drawing a Rectangle
          2. Scaling to the Window
          3. Setting the Viewport and Clipping Volume
          4. Defining the Viewport
          5. Defining the Clipped Viewing Volume
          6. Keeping a Square Square
      6. Animation with OpenGL and GLUT
        1. Double-Buffering
      7. The OpenGL State Machine
        1. Saving and Restoring States
      8. OpenGL Errors
        1. When Bad Things Happen to Good Code
      9. Identifying the Version
      10. Getting a Clue with glHint
      11. Using Extensions
        1. Checking for an Extension
        2. Who's Extension Is This?
        3. Getting to OpenGL Beyond 1.1 on Windows
      12. Summary
      13. Reference
    3. 3. Drawing in Space: Geometric Primitives and Buffers
      1. Drawing Points in 3D
      2. Setting Up a 3D Canvas
      3. A 3D Point: The Vertex
      4. Draw Something!
        1. Drawing Points
          1. Our First Example
      5. Setting the Point Size
      6. Drawing Lines in 3D
        1. Line Strips and Loops
        2. Approximating Curves with Straight Lines
        3. Setting the Line Width
        4. Line Stippling
      7. Drawing Triangles in 3D
        1. Triangles: Your First Polygon
        2. Winding
        3. Triangle Strips
        4. Triangle Fans
      8. Building Solid Objects
        1. Setting Polygon Colors
        2. Hidden Surface Removal
        3. Culling: Hiding Surfaces for Performance
        4. Polygon Modes
      9. Other Primitives
        1. Four-Sided Polygons: Quads
          1. Quad Strips
        2. General Polygons
        3. Filling Polygons, or Stippling Revisited
        4. Polygon Construction Rules
        5. Subdivision and Edges
      10. Other Buffer Tricks
        1. Using Buffer Targets
        2. Manipulating the Depth Buffer
        3. Cutting It Out with Scissors
        4. Using the Stencil Buffer
          1. Creating the Stencil Pattern
      11. Summary
      12. Reference
    4. 4. Geometric Transformations: The Pipeline
      1. Is This the Dreaded Math Chapter?
      2. Understanding Transformations
        1. Eye Coordinates
        2. Viewing Transformations
        3. Modeling Transformations
        4. The Modelview Duality
        5. Projection Transformations
        6. Viewport Transformations
      3. The Matrix: Mathematical Currency for 3D Graphics
        1. What Is a Matrix?
        2. The Transformation Pipeline
        3. The Modelview Matrix
          1. Translation
          2. Rotation
          3. Scaling
        4. The Identity Matrix
        5. The Matrix Stacks
        6. A Nuclear Example
      4. Using Projections
        1. Orthographic Projections
        2. Perspective Projections
        3. A Far-Out Example
      5. Advanced Matrix Manipulation
        1. Loading a Matrix
        2. Performing Your Own Transformations
        3. Adding Transformations Together
      6. Moving Around in OpenGL Using Cameras and Actors
        1. An Actor Frame
        2. Euler Angles: “Use the Frame, Luke!”
        3. Camera Management
      7. Bringing It All Together
      8. Summary
      9. Reference
    5. 5. Color, Materials, and Lighting: The Basics
      1. What Is Color?
        1. Light as a Wave
        2. Light as a Particle
        3. Your Personal Photon Detector
        4. The Computer as a Photon Generator
      2. PC Color Hardware
      3. PC Display Modes
        1. Screen Resolution
        2. Color Depth
          1. The 4-Bit Color Mode
          2. The 8-Bit Color Mode
          3. The 24-Bit Color Mode
          4. The 16- and 32-Bit Color Modes
      4. Using Color in OpenGL
        1. The Color Cube
        2. Setting the Drawing Color
        3. Shading
        4. Setting the Shading Model
      5. Color in the Real World
        1. Ambient Light
        2. Diffuse Light
        3. Specular Light
        4. Putting It All Together
      6. Materials in the Real World
        1. Material Properties
        2. Adding Light to Materials
        3. Calculating Ambient Light Effects
        4. Diffuse and Specular Effects
      7. Adding Light to a Scene
        1. Enabling the Lighting
        2. Setting Up the Lighting Model
        3. Setting Material Properties
      8. Using a Light Source
        1. Which Way Is Up?
        2. Surface Normals
        3. Specifying a Normal
        4. Unit Normals
        5. Finding a Normal
        6. Setting Up a Source
        7. Setting the Material Properties
        8. Specifying the Polygons
      9. Lighting Effects
        1. Specular Highlights
        2. Specular Light
        3. Specular Reflectance
        4. Specular Exponent
        5. Normal Averaging
      10. Putting It All Together
        1. Creating a Spotlight
        2. Drawing a Spotlight
      11. Shadows
        1. What Is a Shadow?
        2. Squish Code
        3. A Shadow Example
        4. Sphere World Revisited
      12. Summary
      13. Reference
    6. 6. More on Colors and Materials
      1. Blending
        1. Combining Colors
        2. Changing the Blending Equation
        3. Antialiasing
          1. Multisample
      2. Fog
      3. Accumulation Buffer
      4. Other Color Operations
        1. Color Masking
        2. Color Logical Operations
        3. Alpha Testing
        4. Dithering
      5. Summary
      6. Reference
    7. 7. Imaging with OpenGL
      1. Bitmaps
        1. A Bitmapped Sample
          1. Setting the Raster Position
          2. Drawing the Bitmap
      2. Pixel Packing
      3. Pixmaps
        1. Packed Pixel Formats
        2. A More Colorful Example
        3. Moving Pixels Around
        4. Saving Pixels
      4. More Fun with Pixels
        1. Pixel Zoom
        2. Pixel Transfer
        3. Pixel Mapping
      5. The Imaging “Subset”
        1. Imaging Pipeline
          1. Color Matrix
          2. Color Lookup
            1. Proxies
            2. Other Operations
          3. Convolutions
            1. From the Color Buffer
            2. Separable Filters
            3. One-Dimensional Kernels
            4. Other Convolution Tweaks
          4. Histogram
            1. MinMax Operations
      6. Summary
      7. Reference
    8. 8. Texture Mapping: The Basics
      1. Loading Textures
        1. Using the Color Buffer
        2. Updating Textures
      2. Mapping Textures to Geometry
        1. Texture Matrix
      3. A Simple 2D Example
      4. Texture Environment
      5. Texture Parameters
        1. Basic Filtering
        2. Texture Wrap
        3. Cartoons with Texture
        4. Mipmapping
          1. Mipmap Filtering
          2. Generating Mip Levels
            1. Hardware Generation of Mipmaps
          3. LOD BIAS
      6. Texture Objects
        1. Managing Multiple Textures
          1. Resident Textures
          2. Texture Priorities
      7. Summary
      8. Reference
    9. 9. Texture Mapping: Beyond the Basics
      1. Secondary Color
      2. Anisotropic Filtering
      3. Texture Compression
        1. Compressing Textures
        2. Loading Compressed Textures
      4. Texture Coordinate Generation
        1. Object Linear Mapping
        2. Eye Linear Mapping
        3. Sphere Mapping
        4. Cube Mapping
          1. Loading Cube Maps
          2. Using Cube Maps
      5. Multitexture
        1. Multiple Texture Coordinates
        2. A Multitextured Example
      6. Texture Combiners
      7. Summary
      8. Reference
    10. 10. Curves and Surfaces
      1. Built-in Surfaces
        1. Setting Quadric States
        2. Drawing Quadrics
        3. Modeling with Quadrics
      2. Bézier Curves and Surfaces
        1. Parametric Representation
          1. Control Points
          2. Continuity
        2. Evaluators
          1. A 2D Curve
          2. Evaluating a Curve
          3. A 3D Surface
          4. Lighting and Normal Vectors
      3. NURBS
        1. From Bézier to B-Splines
        2. Knots
        3. Creating a NURBS Surface
        4. NURBS Properties
        5. Defining the Surface
        6. Trimming
        7. NURBS Curves
      4. Tessellation
        1. The Tessellator
        2. Tessellator Callbacks
        3. Specifying Vertex Data
        4. Putting It All Together
      5. Summary
      6. Reference
    11. 11. It's All About the Pipeline: Faster Geometry Throughput
      1. Model Assembly 101
        1. Pieces and Parts
          1. The Head
          2. The Shaft
          3. The Thread
          4. Putting It Together
      2. Display Lists
        1. Batch Processing
        2. Preprocessed Batches
        3. Display List Caveats
        4. Converting to Display Lists
      3. Measuring Performance
        1. A Better Example
      4. Vertex Arrays
        1. Loading the Geometry
        2. Enabling Arrays
        3. Where's the Data?
        4. Draw!
        5. Indexed Vertex Arrays
          1. A Simple Cube
          2. Getting Serious
            1. Measuring the Cost
            2. Creating a Suitable Indexed Array
            3. Comparing the Cost
      5. Summary
      6. Reference
    12. 12. Interactive Graphics
      1. Selection
        1. Naming Your Primitives
        2. Working with Selection Mode
        3. The Selection Buffer
        4. Picking
        5. Hierarchical Picking
        6. Feedback
        7. The Feedback Buffer
        8. Feedback Data
        9. Passthrough Markers
      2. A Feedback Example
        1. Label the Objects for Feedback
        2. Step 1: Select the Object
        3. Step 2: Get Feedback on the Object
      3. Summary
      4. Reference
  7. II. OpenGL Everywhere
    1. 13. Wiggle: OpenGL on Windows
      1. OpenGL Implementations on Windows
        1. Generic OpenGL
        2. Installable Client Driver
        3. Mini-Client Driver
        4. Mini-Driver
        5. Extended OpenGL
      2. Basic Windows Rendering
        1. GDI Device Contexts
        2. Pixel Formats
          1. Describing a Pixel Format
          2. Enumerating Pixel Formats
          3. Selecting and Setting a Pixel Format
        3. The OpenGL Rendering Context
      3. Putting It All Together
        1. Creating the Window
          1. First, You Need a Device Context
          2. Enter Win32
          3. Three Steps Forward, Two Steps Back
          4. All Is Not Lost
        2. Using the OpenGL Rendering Context
          1. Initializing the Rendering Context
          2. Shutting Down the Rendering Context
        3. Other Windows Messages
          1. The WM_PAINT Message
      4. Windows Palettes
        1. Color Matching
        2. Palette Arbitration
        3. Creating a Palette for OpenGL
          1. Do You Need a Palette?
          2. The Palette's Structure
          3. The 3-3-2 Palette
          4. Building the Palette
        4. Palette Creation and Disposal
          1. Some Restrictions Apply
      5. OpenGL and Windows Fonts
        1. 3D Fonts and Text
          1. Rendering 3D Text
        2. 2D Fonts and Text
      6. Full-Screen Rendering
        1. Creating a Frameless Window
        2. Creating a Full-Screen Window
      7. Multithreaded Rendering
      8. OpenGL and WGL Extensions
        1. Simple Extensions
        2. Using New Entrypoints
        3. WGL Extensions
          1. Extended Pixel Formats
          2. Win32 to the Max
      9. Summary
      10. Reference
    2. 14. OpenGL on MacOS X
      1. The Basics
        1. Frameworks
        2. Using the GLUT API
      2. Using the AGL and Carbon APIs
        1. Pixel Formats
        2. Managing Contexts
        3. Doing Double-Buffered Rendering
        4. Your First AGL Program
        5. Using Bitmap Fonts
      3. Using the Cocoa API
        1. The NSOpenGL Class
        2. Your First Cocoa Program
      4. Summary
      5. Reference
    3. 15. GLX: OpenGL on Linux
      1. The Basics
        1. Using the OpenGL and X11 Libraries
        2. Using the GLUT Library
        3. OpenGL on Linux
        4. OpenGL Emulation: Mesa
      2. The OpenGL Extension for the X Window System
        1. X Window System Basics
        2. Choosing a Visual
        3. Managing OpenGL Contexts
        4. Creating an OpenGL Window
        5. Double-Buffered Windows
        6. Putting It All Together
      3. Creating Bitmap Fonts for OpenGL
      4. Offscreen Rendering
        1. Using GLX Pixmaps
        2. Using Pbuffers
      5. Using the Motif Library
        1. GLwDrawingArea and GLwMDrawingArea: The OpenGL Widgets
        2. Callbacks
          1. The GLwNexposeCallback Callback
          2. The GLwNginitCallback Callback
          3. The GLwNinputCallback Callback
          4. The GLwNresizeCallback Callback
        3. Functions
        4. Putting It All Together
      6. Summary
      7. Reference
  8. III. OpenGL: The Next Generation
    1. 16. Buffer Objects: It's Your Video Memory; You Manage It!
      1. First, You Need Vertex Arrays
        1. Generating the Spherical Particle Clouds
        2. Enabling the Vertex Arrays
        3. More Spheres, Please!
      2. Migration to Buffer Objects
        1. Buffer Object Management
        2. Rendering with Buffer Objects
      3. Loading Data into Buffer Objects
        1. Copying Data into the Buffer Object
        2. Mapping the Buffer Object Directly
      4. A Few Loose Ends
      5. Summary
      6. Reference
    2. 17. Occlusion Queries: Why Do More Work Than You Need To?
      1. The World Before Occlusion Queries
      2. Bounding Boxes
      3. Querying the Query Object
      4. Summary
      5. Reference
    3. 18. Depth Textures and Shadows
      1. Be That Light
        1. Fit the Scene to the Window
        2. No Bells or Whistles, Please
      2. A New Kind of Texture
      3. Draw the Shadows First?!
      4. And Then There Was Light
        1. Projecting Your Shadow Map: The “Why”
        2. Projecting Your Shadow Map: The “How”
        3. The Shadow Comparison
      5. Two Out of Three Ain't Bad
      6. A Few Words About Polygon Offset
      7. Summary
      8. Reference
    4. 19. Programmable Pipeline: This Isn't Your Father's OpenGL
      1. Out with the Old
        1. Fixed Vertex Processing
          1. Vertex Transformation
          2. Lighting
          3. Texture Coordinate Generation and Transformation
          4. Clipping
        2. Fixed Fragment Processing
          1. Texture Application and Environment
          2. Color Sum
          3. Fog Application
          4. Antialiasing Application
      2. In with the New
        1. Programmable Vertex Shaders
          1. Replacing Vertex Transformation
          2. Replacing Lighting
          3. Replacing Texture Coordinate Processing
        2. Fixed Functionality Glue
        3. Programmable Fragment Shaders
          1. Replacing Texturing
          2. Replacing Color Sum
          3. Replacing Fog
      3. Introduction to Shader Extensions
        1. Low-Level Extensions
        2. High-Level Extensions
      4. Summary
    5. 20. Low-Level Shading: Coding to the Metal
      1. Managing Low-Level Shaders
        1. Creating and Binding Shaders
        2. Loading Shaders
        3. Deleting Shaders
        4. Setting Up the Extensions
      2. Instruction Sets
        1. Common Instructions
        2. Vertex-Specific Instructions
        3. Fragment-Specific Instructions
      3. Variable Types
        1. Temporaries
        2. Parameters
          1. Inline Constants
          2. State-Bound Parameters
          3. Program Parameters
          4. Parameter Arrays
        3. Attributes
          1. Vertex Attributes
          2. Fragment Attributes
        4. Outputs
          1. Vertex Outputs
          2. Fragment Outputs
        5. Aliases
        6. Addresses
      4. Input and Output Modifiers
        1. Input Negate
        2. Input Swizzle
        3. Output Writemask
        4. Output Clamp
      5. Resource Consumption and Queries
        1. Parser Limits
          1. Texture Indirections
        2. Native Limits
        3. Other Queries
      6. Shader Options
        1. Position-Invariant Vertex Option
        2. Fog Application Fragment Options
        3. Precision Hint Fragment Option
      7. Summary
      8. Reference
    6. 21. High-Level Shading: The Real Slim Shader
      1. Managing High-Level Shaders
        1. Shader Objects
          1. Creating and Deleting
          2. Specifying Shader Text
          3. Compiling Shaders
        2. Program Objects
          1. Creating and Deleting
          2. Attaching and Detaching
          3. Linking Programs
          4. Validating Programs
          5. Using Programs
        3. Setting Up the Extensions
      2. Variables
        1. Basic Types
        2. Structures
        3. Arrays
        4. Qualifiers
        5. Built-in Variables
      3. Expressions
        1. Operators
        2. Array Access
        3. Constructors
        4. Component Selectors
      4. Control Flow
        1. Loops
        2. if/else
        3. discard
        4. Functions
          1. Texture Lookup Functions
      5. Summary
      6. Reference
    7. 22. Vertex Shading: Do-It-Yourself Transform, Lighting, and Texgen
      1. Getting Your Feet Wet
      2. Diffuse Lighting
      3. Specular Lighting
      4. Improved Specular Lighting
      5. Per-Vertex Fog
      6. Per-Vertex Point Size
      7. Customized Vertex Transformation
      8. Vertex Blending
      9. Summary
    8. 23. Fragment Shading: Empower Your Pixel Processing
      1. Color Conversion
        1. Grayscale
        2. Sepia Tone
        3. Inversion
        4. Heat Signature
          1. Dependent Texture Lookups
        5. Per-Fragment Fog
      2. Image Processing
        1. Blur
        2. Sharpen
        3. Dilation and Erosion
        4. Edge Detection
      3. Lighting
        1. Diffuse Lighting
        2. Multiple Specular Lights
      4. Procedural Texture Mapping
        1. Checkerboard Texture
        2. Beach Ball Texture
        3. Toy Ball Texture
      5. Summary
    9. A. Further Reading
      1. Other Good OpenGL Books
      2. 3D Graphics Books
      3. Web Sites
    10. B. Glossary
    11. C. OpenGL ES
      1. Reduction of Data Types
      2. Totally Gone
      3. Greatly Reduced Functionality
        1. Texture Mapping
        2. Raster Operations
        3. Lighting
      4. Conclusion
  9. CD-ROM
    1. What's on the CD-ROM
    2. Windows Installation Instructions
    3. License Agreement