You are previewing OpenGL® ES 2.0 Programming Guide.
O'Reilly logo
OpenGL® ES 2.0 Programming Guide

Book Description

OpenGL ES 2.0 is the industry’s leading software interface and graphics library for rendering sophisticated 3D graphics on handheld and embedded devices. With OpenGL ES 2.0, the full programmability of shaders is now available on small and portable devices—including cell phones, PDAs, consoles, appliances, and vehicles. However, OpenGL ES differs significantly from OpenGL. Graphics programmers and mobile developers have had very little information about it—until now.

In the OpenGL® ES 2.0 Programming Guide, three leading authorities on the Open GL ES 2.0 interface—including the specification’s editor—provide start-to-finish guidance for maximizing the interface’s value in a wide range of high-performance applications. The authors cover the entire API, including Khronos-ratified extensions. Using detailed C-based code examples, they demonstrate how to set up and program every aspect of the graphics pipeline. You’ll move from introductory techniques all the way to advanced per-pixel lighting, particle systems, and performance optimization. 

Coverage includes:

  • Shaders in depth: creating shader objects, compiling shaders, checking for compile errors, attaching shader objects to program objects, and linking final program objects

  • The OpenGL ES Shading Language: variables, types, constructors, structures, arrays, attributes, uniforms, varyings, precision qualifiers, and invariance

  • Inputting geometry into the graphics pipeline, and assembling geometry into primitives

  • Vertex shaders, their special variables, and their use in per-vertex lighting, skinning, and other applications

  • Using fragment shaders—including examples of multitexturing, fog, alpha test, and user clip planes

  • Fragment operations: scissor test, stencil test, depth test, multisampling, blending, and dithering

  • Advanced rendering: per-pixel lighting with normal maps, environment mapping, particle systems, image post-processing, and projective texturing

  • Real-world programming challenges: platform diversity, C++ portability, OpenKODE, and platform-specific shader binaries

  • Table of Contents

    1. Copyright
    2. List of Figures
    3. List of Examples
    4. List of Tables
    5. Foreword
    6. Preface
      1. Intended Audience
      2. Organization of the Book
        1. Chapter 1—Introduction to OpenGL ES 2.0
        2. Chapter 2—Hello Triangle: An OpenGL ES 2.0 Example
        3. Chapter 3—An Introduction to EGL
        4. Chapter 4—Shaders and Programs
        5. Chapter 5—OpenGL ES Shading Language
        6. Chapter 6—Vertex Attributes, Vertex Arrays, and Buffer Objects
        7. Chapter 7—Primitive Assembly and Rasterization
        8. Chapter 8—Vertex Shaders
        9. Chapter 9—Texturing
        10. Chapter 10—Fragment Shaders
        11. Chapter 11—Fragment Operations
        12. Chapter 12—Framebuffer Objects
        13. Chapter 13—Advanced Programming with OpenGL ES 2.0
        14. Chapter 14—State Queries
        15. Chapter 15—OpenGL ES and EGL on Handheld Platforms
        16. Appendix A—GL_HALF_FLOAT_OES
        17. Appendix B—Built-In Functions
        18. Appendix C—Shading Language Grammar
        19. Appendix D—ES Framework API
        20. Appendix E—OpenGL ES 2.0 on the iPhone 3GS
      3. Examples Code and Shaders
      4. Errata
    7. Acknowledgements
    8. About the Authors
      1. Aaftab Munshi
      2. Dan Ginsburg
      3. Dave Shreiner
    9. 1. Introduction to OpenGL ES 2.0
      1. What Is OpenGL ES?
      2. OpenGL ES 2.0
        1. Vertex Shader
        2. Primitive Assembly
        3. Rasterization
        4. Fragment Shader
        5. Per-Fragment Operations
      3. OpenGL ES 2.0 and OpenGL ES 1.x Backward Compatibility
      4. EGL
      5. Programming with OpenGL ES 2.0
        1. Libraries and Include Files
        2. EGL Command Syntax
        3. OpenGL ES Command Syntax
        4. Error Handling
        5. Flush and Finish
        6. Basic State Management
      6. Further Reading
    10. 2. Hello Triangle: An OpenGL ES 2.0 Example
      1. Code Framework
      2. Where to Download the Examples
      3. Hello Triangle Example
      4. Building and Running the Examples
      5. Using the OpenGL ES 2.0 Framework
      6. Creating a Simple Vertex and Fragment Shader
      7. Compiling and Loading the Shaders
      8. Creating a Program Object and Linking the Shaders
      9. Setting the Viewport and Clearing the Color Buffer
      10. Loading the Geometry and Drawing a Primitive
      11. Displaying the Back Buffer
    11. 3. An Introduction to EGL
      1. Communicating with the Windowing System
      2. Checking for Errors
      3. Initializing EGL
      4. Determining the Available Surface Configurations
      5. Querying EGLConfig Attributes
      6. Letting EGL Choose the Config
      7. Creating an On-Screen Rendering Area: The EGL Window
      8. Creating an Off-Screen Rendering Area: EGL Pbuffers
      9. Creating a Rendering Context
      10. Making an EGLContext Current
      11. Putting All Our EGL Knowledge Together
      12. Synchronizing Rendering
    12. 4. Shaders and Programs
      1. Shaders and Programs
        1. Creating and Compiling a Shader
        2. Creating and Linking a Program
      2. Uniforms and Attributes
        1. Getting and Setting Uniforms
        2. Getting and Setting Attributes
      3. Shader Compiler and Shader Binaries
    13. 5. OpenGL ES Shading Language
      1. OpenGL ES Shading Language Basics
      2. Variables and Variable Types
      3. Variable Constructors
      4. Vector and Matrix Components
      5. Constants
      6. Structures
      7. Arrays
      8. Operators
      9. Functions
      10. Built-In Functions
      11. Control Flow Statements
      12. Uniforms
      13. Attributes
      14. Varyings
      15. Preprocessor and Directives
      16. Uniform and Varying Packing
      17. Precision Qualifiers
      18. Invariance
    14. 6. Vertex Attributes, Vertex Arrays, and Buffer Objects
      1. Specifying Vertex Attribute Data
        1. Constant Vertex Attribute
        2. Vertex Arrays
        3. Performance Hints
          1. How to store different attributes of a vertex
          2. Which data format to use for vertex attributes
        4. How the Normalized Flag in glVertexAttribPointer Works
        5. Selecting Between a Constant Vertex Attribute or a Vertex Array
      2. Declaring Vertex Attribute Variables in a Vertex Shader
        1. Binding Vertex Attributes to Attribute Variables in a Vertex Shader
      3. Vertex Buffer Objects
        1. Missing OpenGL ES Buffer Usage Enums Supported by OpenGL
      4. Mapping Buffer Objects
        1. Performance Tip
    15. 7. Primitive Assembly and Rasterization
      1. Primitives
        1. Triangles
        2. Lines
        3. Point Sprites
      2. Drawing Primitives
        1. Performance Tips
      3. Primitive Assembly
        1. Coordinate Systems
        2. Clipping
        3. Perspective Division
        4. Viewport Transformation
      4. Rasterization
        1. Culling
        2. Polygon Offset
    16. 8. Vertex Shaders
      1. Vertex Shader Overview
        1. Vertex Shader Built-In Variables
          1. Built-In Special Variables
          2. Built-In Uniform State
          3. Built-In Constants
        2. Precision Qualifiers
        3. ES 2.0 Vertex Shader Limitations
          1. Length of Vertex Shader
          2. Temporary Variables
          3. Flow Control
          4. Conditional Statements
          5. Array Indexing
          6. Counting Number of Uniforms Used in a Vertex Shader
      2. Vertex Shader Examples
        1. A Simple Vertex Shader
        2. Lighting in a Vertex Shader
      3. Generating Texture Coordinates
      4. Vertex Skinning
      5. OpenGL ES 1.1 Vertex Pipeline as an ES 2.0 Vertex Shader
    17. 9. Texturing
      1. Texturing Basics
        1. 2D Textures
        2. Cubemap Textures
        3. Texture Objects and Loading Textures
        4. Texture Filtering and Mipmapping
        5. Automatic Mipmap Generation
        6. Texture Coordinate Wrapping
        7. Using Textures in the Fragment Shader
        8. Example of Using a Cubemap Texture
      2. Compressed Textures
      3. Texture Subimage Specification
      4. Copying Texture Data from the Color Buffer
      5. Optional Extensions
        1. 3D Textures
        2. Ericsson Texture Compression (ETC)
        3. Floating-Point Textures
        4. Non-Power-of-Two Textures
    18. 10. Fragment Shaders
      1. Fixed Function Fragment Shaders
      2. Fragment Shader Overview
        1. Built-In Special Variables
        2. Built-In Constants
        3. Precision Qualifiers
        4. ES 2.0 Fragment Shader Limitations
      3. Implementing Fixed Function Techniques Using Shaders
        1. Multitexturing
        2. Fog
        3. Alpha Test (Using Discard)
        4. User Clip Planes
    19. 11. Fragment Operations
      1. Buffers
        1. Requesting Additional Buffers
        2. Clearing Buffers
        3. Using Masks to Control Writing to Framebuffers
      2. Fragment Tests and Operations
        1. Using the Scissor Test
        2. Stencil Buffer Testing
        3. Depth Buffer Testing
      3. Blending
      4. Dithering
      5. Multisampled Antialiasing
      6. Reading and Writing Pixels to the Framebuffer
    20. 12. Framebuffer Objects
      1. Why Framebuffer Objects?
      2. Framebuffer and Renderbuffer Objects
        1. Choosing a Renderbuffer Versus a Texture as a Framebuffer Attachment
        2. Framebuffer Objects Versus EGL Surfaces
      3. Creating Framebuffer and Renderbuffer Objects
      4. Using Renderbuffer Objects
      5. Using Framebuffer Objects
        1. Attaching a Renderbuffer as a Framebuffer Attachment
        2. Attaching a 2D Texture as a Framebuffer Attachment
        3. Attaching an Image of a 3D Texture as a Framebuffer Attachment
        4. Checking for Framebuffer Completeness
      6. Deleting Framebuffer and Renderbuffer Objects
        1. Deleting Renderbuffer Objects That Are Used as Framebuffer Attachments
        2. Reading Pixels and Framebuffer Objects
      7. Examples
      8. Performance Tips and Tricks
    21. 13. Advanced Programming with OpenGL ES 2.0
      1. Per-Fragment Lighting
        1. Lighting with a Normal Map
        2. Lighting Shaders
        3. Lighting Equations
      2. Environment Mapping
      3. Particle System with Point Sprites
        1. Particle System Setup
        2. Particle System Vertex Shader
        3. Particle System Fragment Shader
      4. Image Postprocessing
        1. Render-to-Texture Setup
        2. Blur Fragment Shader
        3. Light Bloom
      5. Projective Texturing
        1. Projective Texturing Basics
        2. Matrices for Projective Texturing
        3. Projective Spotlight Shaders
      6. Noise Using a 3D Texture
        1. Generating Noise
        2. Using Noise
      7. Procedural Texturing
        1. A Procedural Texture Example
        2. Antialiasing of Procedural Textures
        3. Further Reading on Procedural Textures
    22. 14. State Queries
      1. OpenGL ES 2.0 Implementation String Queries
      2. Querying Implementation-Dependent Limits
      3. Querying OpenGL ES State
      4. Hints
      5. Entity Name Queries
      6. Nonprogrammable Operations Control and Queries
      7. Shader and Program State Queries
      8. Vertex Attribute Queries
      9. Texture State Queries
      10. Vertex Buffer Queries
      11. Renderbuffer and Framebuffer State Queries
    23. 15. OpenGL ES and EGL on Handheld Platforms
      1. Handheld Platforms Overview
        1. Online Resources
      2. C++ Portability
      3. OpenKODE
      4. Platform-Specific Shader Binaries
      5. Targeting Extensions
    24. A. GL_HALF_FLOAT_OES
      1. 16-Bit Floating-Point Number
      2. Converting Float to Half-Float
    25. B. Built-In Functions
      1. Angle and Trigonometry Functions
      2. Exponential Functions
      3. Common Functions
      4. Geometric Functions
      5. Matrix Functions
      6. Vector Relational Functions
      7. Texture Lookup Functions
      8. Derivative Functions
    26. C. Shading Language Grammar
    27. D. ES Framework API
      1. Framework Core Functions
      2. Transformation Functions
    28. E. OpenGL ES 2.0 on the iPhone 3GS
      1. Getting Started with the iPhone SDK 3.0
        1. Getting the Sample Code for the iPhone
        2. Building the Sample Code Using Xcode
      2. Porting the Sample Code to the iPhone
        1. Objective C
        2. Creating an OpenGL ES 2.0 Rendering Surface
        3. Using a Framebuffer Object for Rendering
      3. Discussion of a Complete Example
        1. EAGLView.h Header File
        2. EAGLView.m Source File
      4. Transitioning from OpenGL ES 1.1 to OpenGL ES 2.0
      5. Conclusion