MULTIMEDIA

iPhone 3D Programming : Blending and Augmented Reality - Blending Recipe

2/16/2011 2:19:40 PM
Some of my favorite YouTube videos belong to the Will It Blend? series. The episode featuring the pulverization of an iPhone is a perennial favorite, seconded only by the Chuck Norris episode. Alas, this article deals with blending of a different sort. OpenGL blending requires five ingredients:
  1. Ensure your color contains alpha. If it comes from a texture, make sure the texture format contains alpha; if it comes from a vertex attribute, make sure it has all four color components.

  2. Disable depth testing.

    glDisable(GL_DEPTH_TEST);

  3. Pay attention to the ordering of your draw calls.

  4. Enable blending.

    glEnable(GL_BLENDING);

  5. Set your blending function.

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

For step 5, I’m giving a rather classic blending equation as an example, but that’s not always what you’ll want! (More on this later.) Specifically, the previous function call sets up the following equation:


S is the source color, D is the starting destination color, and F is the final destination color. By default, OpenGL’s blending equation is this:


Since the default blending function ignores alpha, blending is effectively turned off even when you’ve enabled it with glEnable. So, always remember to set your blending function—this is a common pitfall in OpenGL programming.

Here’s the formal declaration of glBlendFunc:

void glBlendFunc (GLenum sfactor, GLenum dfactor);

The blending equation is always an operation on two scaled operands: the source color and the destination color. The template to the equation is this:


The sfactor and dfactor arguments can be any of the following:


GL_ZERO

Multiplies the operand with zero.


GL_ONE

Multiplies the operand with one.


GL_SRC_ALPHA

Multiplies the operand by the alpha component of the source color.


GL_ONE_MINUS_SRC_ALPHA

Multiplies the operand by the inverted alpha component of the source color.


GL_DEST_ALPHA

Multiplies the operand by the alpha component of the destination color.


GL_ONE_MINUS_DEST_ALPHA

Multiplies the operand by the inverted alpha component of the destination color.

Additionally, the sfactor parameter supports the following:


GL_DST_COLOR

Component-wise multiplication of the operand with the destination color.


GL_ONE_MINUS_DST_COLOR

Component-wise multiplication of the operand with the inverted destination color.


GL_SRC_ALPHA_SATURATE

Returns the minimum of source alpha and inverted destination alpha. This exists mostly for historical reasons, because it was required for an outmoded anti-aliasing technique.

And the dfactor parameter also supports the following:


GL_SRC_COLOR

Component-wise multiplication of the operand with the source color.


GL_ONE_MINUS_SRC_COLOR

Component-wise multiplication of the operand with the inverted source color.

OpenGL ES 2.0 relaxes the blending constraints by unifying the set of choices for sfactor and dfactor, with the exception of GL_SRC_ALPHA_SATURATE.


Note:

ES 2.0 also adds the concept of “constant color,” specified via glBlendColor. For more information, look up glBlendColor and glBlendFunc at the Khronos website:

http://www.khronos.org/opengles/sdk/docs/man/

Other  
  •  Microsoft XNA Game Studio 3.0 : Controlling Color (part 3)
  •  Microsoft XNA Game Studio 3.0 : Controlling Color (part 2)
  •  Microsoft XNA Game Studio 3.0 : Controlling Color (part 1) - Games and Classes & Classes as Offices
  •  Microsoft XNA Game Studio 3.0 : Working with Colors
  •  iPhone 3D Programming : Textures and Image Capture - Creating Textures with the Camera
  •  iPhone 3D Programming : Textures and Image Capture - Dealing with Size Constraints
  •  Programming with DirectX : Game Math - Vectors
  •  iPhone 3D Programming : Textures and Image Capture - Generating and Transforming OpenGL Textures with Quartz
  •  iPhone 3D Programming : Textures and Image Capture - The PowerVR SDK and Low-Precision Textures
  •  Building LOB Applications : Using Visual Studio 2010 WCF Data Services Tooling
  •  Building LOB Applications : Accessing RESTful Data using OData
  •  Programming with DirectX : Additional Texture Mapping - Image Filters
  •  Microsoft XNA Game Studio 3.0 : Making a Game Program
  •  iPhone 3D Programming : Textures and Image Capture - Texture Compression with PVRTC
  •  iPhone 3D Programming : Textures and Image Capture - Texture Formats and Types
  •  iPhone 3D Programming : Textures and Image Capture - Fight Aliasing with Filtering
  •  iPhone 3D Programming : Textures and Image Capture - Texture Coordinates Revisited
  •  Programming with DirectX : Additional Texture Mapping - Sprites
  •  Programming with DirectX : Additional Texture Mapping - Alpha Mapping
  •  Microsoft XNA Game Studio 3.0 : Writing Your First Program (part 2) - Running the Same XNA Game on Different Devices
  •  
    Most View
    The Cat You Have To Have (Part 3)
    Windows Server 2003 : Moving from Workgroups to Domain Environments (part 1) - Using dcpromo, Confirming DNS Registration of DC Information
    HTC Desire SV Review – A Shining Mid-Range Dual-SIM Smartphone
    Long Live The Socket
    The Ultimate PC Security Toolbox (Part 2)
    Corsair H90 140mm Liquid CPU Cooler
    The Phantom 820 – One Of NZXT’s Largest Ever Cases
    .NET Debugging : Introduction to the Tools - Reflector for .NET, PowerDbg, Managed Debugging Assistants
    Top 5 Good-value Android Smartphones
    Windows Server 2003 : Clustering Technologies - Command-Line Utilities
    Top 10
    Thermalright Archon SB-E Cooler Review (Part 3)
    Thermalright Archon SB-E Cooler Review (Part 2)
    Thermalright Archon SB-E Cooler Review (Part 1)
    Acer CloudMobile - Ambitious Android Phone (Part 3)
    Acer CloudMobile - Ambitious Android Phone (Part 2)
    Acer CloudMobile - Ambitious Android Phone (Part 1)
    Huawei MediaPad 10 Tablet Review (Part 2)
    Huawei MediaPad 10 Tablet Review (Part 1)
    Mymemory.com - Calendars And Picture Books Review (Part 2)
    Mymemory.com - Calendars And Picture Books Review (Part 1)