The Cg Tutorial: The Definitive Guide to Programmable Real-Time Graphics

Book description

"Cg is the key to unlocking the power of a new generation of programmable graphics hardware. This book is the definitive introduction to Cg, and will be essential for anyone programming high-quality real-time graphics. The Cg Tutorial will teach you to use Cg to create effects never before available for real-time applications."
--Larry Gritz, Author of Advanced RenderMan (Morgan Kaufmann, 2000)

"An important and timely book: Pixel-level procedural textures--animated clouds, fire, water, the whole bag of procedural tricks--finally go from the movie screen to the desktop. Access to computation of this power through a C-like language will usher in an exciting new era for the graphics community."
--Ken Perlin, Professor, New York University

Cg (C for graphics) is a complete programming environment for the fast creation of special effects and real-time cinematic quality experiences on multiple platforms. By providing a new level of abstraction, Cg lets developers more directly target OpenGL®, DirectX®, Windows®, Linux, Mac OS X®, and console platforms, such as the Xbox™, without having to program directly to the graphics hardware assembly language. Cg was developed by NVIDIA® Corporation in close collaboration with Microsoft® Corporation, and is compatible with both the OpenGL API and Microsoft's HLSL for DirectX 9.0.

The Cg Tutorial explains how to implement both basic and advanced techniques for today's programmable GPU architectures.

Major topics covered include:

  • 3D transformations

  • Per-vertex and per-pixel lighting

  • Skinning and key-frame interpolation

  • Environment mapping

  • Bump mapping

  • Fog

  • Performance optimization

  • Projective texturing

  • Cartoon shading

  • Compositing

  • The accompanying CD-ROM includes the tools needed to run the sample programs in the book.

    Table of contents

    1. Copyright
      1. Dedication
    2. Cg Language Concepts
    3. Figures and Plates
      1. Figures
      2. Plates
    4. Examples
    5. Tables
    6. Equations
    7. Foreword
    8. Preface
      1. Our Intended Audience
      2. The Book’s Structure
      3. Formatting Conventions
      4. Trying the Examples
      5. Acknowledgments
    9. 1. Introduction
      1. 1.1. What Is Cg?
        1. 1.1.1. A Language for Programming Graphics Hardware
        2. 1.1.2. Cg’s Data-Flow Model
        3. 1.1.3. GPU Specialization and CPU Generalization
        4. 1.1.4. The Performance Rationale for Cg
        5. 1.1.5. Coexistence with Conventional Languages
        6. 1.1.6. Other Aspects of Cg
        7. 1.1.7. The Limited Execution Environment of Cg Programs
      2. 1.2. Vertices, Fragments, and the Graphics Pipeline
        1. 1.2.1. The Evolution of Computer Graphics Hardware
        2. 1.2.2. Four Generations of Computer Graphics Hardware
          1. Pre-GPU Graphics Acceleration
          2. First-Generation GPUs
          3. Second-Generation GPUs
          4. Third-Generation GPUs
          5. Fourth-Generation GPUs
        3. 1.2.3. The Graphics Hardware Pipeline
          1. Vertex Transformation
          2. Primitive Assembly and Rasterization
          3. Interpolation, Texturing, and Coloring
          4. Raster Operations
          5. Visualizing the Graphics Pipeline
        4. 1.2.4. The Programmable Graphics Pipeline
          1. The Programmable Vertex Processor
          2. The Programmable Fragment Processor
        5. 1.2.5. Cg Provides Vertex and Fragment Programmability
      3. 1.3. Cg’s Historical Development
        1. 1.3.1. Microsoft and NVIDIA’s Collaboration to Develop Cg and HLSL
        2. 1.3.2. Noninteractive Shading Languages
          1. Shade Trees
          2. The RenderMan Shading Language
          3. Hardware-Amenable Shading Languages
        3. 1.3.3. Programming Interfaces for 3D Graphics
      4. 1.4. The Cg Environment
        1. 1.4.1. Standard 3D Programming Interfaces: OpenGL and Direct3D
          1. OpenGL
          2. Direct3D
          3. 3D Programming Interface Détente
        2. 1.4.2. The Cg Compiler and Runtime
          1. Support for Dynamic Compilation
          2. CgGL and CgD3D, the 3D-API-Specific Cg Libraries
          3. How the Cg Runtime Fits into Your Application
        3. 1.4.3. The CgFX Toolkit and File Format
          1. What CgFX Provides
          2. Multiple Shader Instancing
          3. CgFX and Digital Content Creation
          4. How CgFX Fits into Your Application
          5. Onward to the Tutorial
      5. 1.5. Exercises
      6. 1.6. Further Reading
    10. 2. The Simplest Programs
      1. 2.1. A Simple Vertex Program
        1. The Naming Convention for Examples
        2. 2.1.1. Output Structures
        3. 2.1.2. Identifiers
          1. Keywords in Cg
        4. 2.1.3. Structure Members
        5. 2.1.4. Vectors
        6. 2.1.5. Matrices
        7. 2.1.6. Semantics
        8. 2.1.7. Functions
          1. Entry Functions
          2. Internal Functions
        9. 2.1.8. Input and Output Semantics Are Different
        10. 2.1.9. The Function Body
      2. 2.2. Compiling Your Example
        1. 2.2.1. Vertex Program Profiles
        2. 2.2.2. Classes of Cg Compilation Errors
        3. 2.2.3. Profile-Dependent Errors
          1. Capability
          2. Context
          3. Capacity
          4. Preventing Errors
        4. 2.2.4. The Norm: Multiple Entry Functions
        5. 2.2.5. Downloading and Configuring Vertex and Fragment Programs
      3. 2.3. A Simple Fragment Program
        1. 2.3.1. Fragment Program Profiles
      4. 2.4. Rendering with Your Vertex and Fragment Program Examples
        1. 2.4.1. Rendering a Triangle with OpenGL
        2. 2.4.2. Rendering a Triangle with Direct3D
        3. 2.4.3. Getting the Same Results
      5. 2.5. Exercises
      6. 2.6. Further Reading
    11. 3. Parameters, Textures, and Expressions
      1. 3.1. Parameters
        1. 3.1.1. Uniform Parameters
          1. The uniform Type Qualifier
          2. When There Is No uniform Qualifier
          3. What uniform Means in RenderMan vs. Cg
        2. 3.1.2. The const Type Qualifier
        3. 3.1.3. Varying Parameters
      2. 3.2. Texture Samplers
        1. 3.2.1. Sampler Objects
        2. 3.2.2. Sampling Textures
        3. 3.2.3. Sending Texture Coordinates While Sampling a Texture
      3. 3.3. Math Expressions
        1. 3.3.1. Operators
        2. 3.3.2. Profile-Dependent Numeric Data Types
          1. Representing Continuous Data Types
        3. 3.3.3. Standard Library Built-In Functions
          1. Function Overloading
          2. The Cg Standard Library’s Efficiency and Precision
        4. 3.3.4. 2D Twisting
          1. The length and sincos Standard Library Routines
          2. Call-by-Result Parameter Passing
          3. Rotating Vertices
          4. The Importance of Tessellation for Vertex Programs
        5. 3.3.5. Double Vision
          1. The Double Vision Vertex Program
          2. Out Parameters vs. Output Structures
          3. The Double Vision Fragment Program for Advanced Fragment Profiles
          4. The Double Vision Fragment Program for Basic Fragment Profiles
      4. 3.4. Exercises
      5. 3.5. Further Reading
    12. 4. Transformations
      1. 4.1. Coordinate Systems
        1. 4.1.1. Object Space
        2. 4.1.2. Homogeneous Coordinates
        3. 4.1.3. World Space
        4. 4.1.4. The Modeling Transform
        5. 4.1.5. Eye Space
        6. 4.1.6. The View Transform
          1. The Modelview Matrix
        7. 4.1.7. Clip Space
        8. 4.1.8. The Projection Transform
          1. The Projection Matrix
        9. 4.1.9. Normalized Device Coordinates
          1. Perspective Division
        10. 4.1.10. Window Coordinates
      2. 4.2. Applying the Theory
      3. 4.3. Exercises
      4. 4.4. Further Reading
    13. 5. Lighting
      1. 5.1. Lighting and Lighting Models
      2. 5.2. Implementing the Basic Per-Vertex Lighting Model
        1. 5.2.1. The Basic Lighting Model
          1. The Emissive Term
          2. The Ambient Term
          3. The Diffuse Term
          4. The Specular Term
          5. Adding the Terms Together
          6. Simplifications
        2. 5.2.2. A Vertex Program for Basic Per-Vertex Lighting
          1. Application-Specified Data
          2. A Debugging Tip
          3. The Vertex Program Body
            1. Calculating the Clip-Space Position
            2. Swizzling
            3. Write Masking
            4. The Emissive Light Contribution
            5. The Ambient Light Contribution
            6. The Diffuse Light Contribution
            7. The Specular Light Contribution
            8. Conditional Expressions
            9. Putting It All Together
        3. 5.2.3. The Fragment Program for Per-Vertex Lighting
        4. 5.2.4. Per-Vertex Lighting Results
      3. 5.3. Per-Fragment Lighting
        1. 5.3.1. Implementing Per-Fragment Lighting
        2. 5.3.2. The Vertex Program for Per-Fragment Lighting
        3. 5.3.3. The Fragment Program for Per-Fragment Lighting
      4. 5.4. Creating a Lighting Function
        1. 5.4.1. Declaring a Function
        2. 5.4.2. A Lighting Function
        3. 5.4.3. Structures
        4. 5.4.4. Arrays
        5. 5.4.5. Flow Control
        6. 5.4.6. Computing the Diffuse and Specular Lighting
      5. 5.5. Extending the Basic Model
        1. 5.5.1. Distance Attenuation
        2. 5.5.2. Adding a Spotlight Effect
          1. Intensity Variation
        3. 5.5.3. Directional Lights
      6. 5.6. Exercises
      7. 5.7. Further Reading
    14. 6. Animation
      1. 6.1. Movement in Time
      2. 6.2. A Pulsating Object
        1. 6.2.1. The Vertex Program
        2. 6.2.2. Displacement Calculation
          1. Creating a Time-Based Function
          2. Adding Controls to the Program
          3. Varying the Magnitude of Bulging
          4. Updating the Vertex Position
          5. Precompute Uniform Parameters When Possible
      3. 6.3. Particle Systems
        1. 6.3.1. Initial Conditions
        2. 6.3.2. Vectorized Computations
        3. 6.3.3. The Particle System Parameters
        4. 6.3.4. The Vertex Program
          1. Computing the Particle Positions
          2. Computing the Particle Color
          3. Computing the Particle Size
        5. 6.3.5. Dressing Up Your Particle System
      4. 6.4. Key-Frame Interpolation
        1. 6.4.1. Key-Framing Background
        2. 6.4.2. Interpolation Approaches
          1. Linear Interpolation
          2. Quadratic Interpolation
        3. 6.4.3. Basic Key-Frame Interpolation
        4. 6.4.4. Key-Frame Interpolation with Lighting
      5. 6.5. Vertex Skinning
        1. 6.5.1. The Theory of Vertex Skinning
          1. Constructing Poses from Matrices
          2. Lighting
          3. Storage Requirements Compared with Key Frames
        2. 6.5.2. Vertex Skinning in a Vertex Program
      6. 6.6. Exercises
      7. 6.7. Further Reading
    15. 7. Environment Mapping Techniques
      1. 7.1. Environment Mapping
        1. 7.1.1. Cube Map Textures
        2. 7.1.2. Generating Cube Maps
        3. 7.1.3. The Environment Mapping Concept
        4. 7.1.4. Computing Reflection Vectors
        5. 7.1.5. Assumptions for Environment Mapping
      2. 7.2. Reflective Environment Mapping
        1. 7.2.1. Application-Specified Parameters
        2. 7.2.2. The Vertex Program
          1. Basic Operations
          2. Transforming the Vectors into World Space
          3. Normalizing the Normal
          4. Calculating the Incident Vector
          5. Calculating the Reflection Vector
          6. Normalizing Vectors
        3. 7.2.3. The Fragment Program
        4. 7.2.4. Control Maps
        5. 7.2.5. Vertex Program vs. Fragment Program
      3. 7.3. Refractive Environment Mapping
        1. 7.3.1. The Physics of Refraction
          1. Snell’s Law
          2. The Ratio of Indices of Refraction
        2. 7.3.2. The Vertex Program
        3. 7.3.3. The Fragment Program
      4. 7.4. The Fresnel Effect and Chromatic Dispersion
        1. 7.4.1. The Fresnel Effect
        2. 7.4.2. Chromatic Dispersion
        3. 7.4.3. Application-Specified Parameters
        4. 7.4.4. The Vertex Program
          1. Calculating the Reflected Vector
          2. Calculating the Refracted Vectors
          3. Calculating the Reflection Coefficient
        5. 7.4.5. The Fragment Program
          1. Performing the Texture Lookups
          2. Computing the Final Result
      5. 7.5. Exercises
      6. 7.6. Further Reading
    16. 8. Bump Mapping
      1. 8.1. Bump Mapping a Brick Wall
        1. 8.1.1. The Brick Wall Normal Map
        2. 8.1.2. Storing Bump Maps As Normal Map Textures
          1. Conventional Color Textures
          2. Storing Normals in Conventional Color Textures
          3. Generating Normal Maps from Height Fields
        3. 8.1.3. Simple Bump Mapping for a Brick Wall
          1. The Vertex Program
          2. The Fragment Program
          3. Constructing a Normalization Cube Map
        4. 8.1.4. Specular Bump Mapping
          1. The Vertex Program
          2. The Fragment Program
          3. Further Improvements
        5. 8.1.5. Bump Mapping Other Geometry
          1. Object-Space Bump Mapping
          2. Texture-Space Bump Mapping
      2. 8.2. Bump Mapping a Brick Floor
        1. About Rotation Matrices
        2. 8.2.1. The Vertex Program for Rendering a Brick Floor
      3. 8.3. Bump Mapping a Torus
        1. 8.3.1. The Mathematics of the Torus
        2. 8.3.2. The Bump-Mapped Torus Vertex Program
      4. 8.4. Bump Mapping Textured Polygonal Meshes
        1. 8.4.1. Examining a Single Triangle
        2. 8.4.2. Caveats
          1. The Orthogonality of Texture Space with Object Space
          2. Beware of Zero-Area Triangles in Texture Space
          3. Negative-Area Triangles in Texture Space
          4. Nonuniform Stretching of Bump Map Texture Coordinates
        3. 8.4.3. Generalizing to a Polygonal Mesh
          1. Blending Bases at Shared Vertices
      5. 8.5. Combining Bump Mapping with Other Effects
        1. 8.5.1. Decal Maps
        2. 8.5.2. Gloss Maps
        3. 8.5.3. Geometric Self-Shadowing
      6. 8.6. Exercises
      7. 8.7. Further Reading
    17. 9. Advanced Topics
      1. 9.1. Fog
        1. 9.1.1. Uniform Fog
        2. 9.1.2. The Attributes of Fog
        3. 9.1.3. The Mathematics of Fog
          1. Absorption, Scattering, and Redirection over a Unit Distance
          2. Expressing Fog As a Linear Blend
          3. Applying Fog over Multiple Units of Distance
        4. 9.1.4. Intuiting the Equations
        5. 9.1.5. Creating Uniform Fog with Cg
          1. Planar Fog Distance
          2. Per-Fragment Euclidean Fog Distance
          3. Encoding the Fog Factor Conversion Function in a Texture
      2. 9.2. Nonphotorealistic Rendering
        1. 9.2.1. Toon Shading
        2. 9.2.2. Implementing Toon Shading
          1. Diffuse Shading
          2. Specular Highlights
          3. Silhouette Outlining
          4. Addressing Aliasing
        3. 9.2.3. Putting It All Together
        4. 9.2.4. Problems with This Technique
      3. 9.3. Projective Texturing
        1. 9.3.1. How Projective Texturing Works
          1. Comparing Transformations
          2. Homogeneous Texture Coordinates
        2. 9.3.2. Implementing Projective Texturing
          1. Calculating Texture Coordinates in the Vertex Program
        3. 9.3.3. The Code for Projective Texturing
          1. The Vertex Program
          2. The Fragment Program
          3. Notable Issues
      4. 9.4. Shadow Mapping
        1. Extensions
      5. 9.5. Compositing
        1. 9.5.1. Mapping Input to Output Pixels
        2. 9.5.2. Basic Compositing Operations
      6. 9.6. Exercises
      7. 9.7. Further Reading
    18. 10. Profiles and Performance
      1. 10.1. Profile Descriptions
        1. 10.1.1. The Vertex Shader Profile for DirectX 8
        2. 10.1.2. The Basic NVIDIA Vertex Program Profile for OpenGL
        3. 10.1.3. The ARB Vertex Program Profile for OpenGL
        4. 10.1.4. The Vertex Shader Profiles for DirectX 9
        5. 10.1.5. The Advanced NVIDIA Vertex Program Profile for OpenGL
        6. 10.1.6. The Pixel Shader Profiles for DirectX 8
        7. 10.1.7. The Basic NVIDIA Fragment Program Profile for OpenGL
        8. 10.1.8. The DirectX 9 Pixel Shader Profiles
        9. 10.1.9. The ARB Fragment Program Profile for OpenGL
        10. 10.1.10. The Advanced NVIDIA Fragment Program Profile for OpenGL
      2. 10.2. Performance
        1. 10.2.1. Use the Cg Standard Library
        2. 10.2.2. Take Advantage of Uniform Parameters
        3. 10.2.3. Using Vertex Programs vs. Fragment Programs
        4. 10.2.4. Data Types and Their Impact on Performance
        5. 10.2.5. Take Advantage of Vectorization
        6. 10.2.6. Use Textures to Encode Functions
        7. 10.2.7. Use Swizzling and Negation Freely
        8. 10.2.8. Shade Only the Pixels That You Must
        9. 10.2.9. Shorter Assembly Is Not Necessarily Faster
      3. 10.3. Exercises
      4. 10.4. Further Reading
    19. A. Getting Started with Cg
      1. A.1. Getting This Book’s Accompanying Software
      2. A.2. Getting the Cg Toolkit
    20. B. The Cg Runtime
      1. B.1. What Is the Cg Runtime?
      2. B.2. Why Use the Cg Runtime?
        1. B.2.1. Future-Proofing
        2. B.2.2. No Dependency Issues
        3. B.2.3. Input Parameter Management
      3. B.3. How Does the Cg Runtime Work?
        1. B.3.1. Header Files
        2. B.3.2. Creating a Context
        3. B.3.3. Compiling a Program
        4. B.3.4. Loading a Program
        5. B.3.5. Modifying the Program Parameters
        6. B.3.6. Executing a Program
        7. B.3.7. Releasing Resources
        8. B.3.8. Handling Errors
      4. B.4. More Details
    21. C. The CgFX File Format
      1. C.1. What Is CgFX?
      2. C.2. Format Overview
        1. C.2.1. Techniques
        2. C.2.2. Passes
        3. C.2.3. Render States
        4. C.2.4. Variables and Semantics
        5. C.2.5. Annotations
        6. C.2.6. A Sample CgFX File
      3. C.3. Cg Plug-Ins Supporting the CgFX Format
      4. C.4. Learning More About CgFX
    22. D. Cg Keywords
      1. D.1. Cg Keyword List
    23. E. Cg Standard Library Functions
      1. E.1. Mathematical Functions
      2. E.2. Geometric Functions
      3. E.3. Texture Map Functions
      4. E.4. Derivative Functions
      5. E.5. Debugging Function
    24. Color Inserts

    Product information

    • Title: The Cg Tutorial: The Definitive Guide to Programmable Real-Time Graphics
    • Author(s): Randima Fernando, Mark J. Kilgard
    • Release date: February 2003
    • Publisher(s): Addison-Wesley Professional
    • ISBN: 9780321545398