MULTIMEDIA

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

2/19/2011 4:29:50 PM
It’s important to remember to disable depth testing when blending is enabled. If depth testing is turned on, triangles that lie beneath other triangles get completely rejected, so their color can’t contribute to the framebuffer.

An equally important caveat is that you should render your triangles in back-to-front order; the standard blending math simply doesn’t work if you try to draw the top layer before the layer beneath it. Let’s demonstrate why this is so. Suppose you’d like to depict a half-opaque red triangle on top of a half-opaque green triangle. Assuming the clear color is black, the history of a pixel in the framebuffer would look like this if you use back-to-front ordering:

  1. Clear to Black. Result: (0, 0, 0).

  2. Draw the half-opaque green triangle. Result: (0, 0.5, 0).

  3. Draw the half-opaque red triangle. Result: (0.5, 0.25, 0).

So, the resulting pixel is a yellowish red; this is what you’d expect. If you try to draw the red triangle first, the result is different:

  1. Clear to Black. Result: (0, 0, 0).

  2. Draw the half-opaque red triangle. Result: (0.5, 0, 0).

  3. Draw the half-opaque green triangle. Result: (0.25, 0.5, 0).

Now you have yellowish green. Order matters when you’re blending! Incidentally, there’s a way to adjust the blending equations so that you can draw in front-to-back order instead of back-to-front; we’ll show how in the next section.


Warning:

When blending is enabled, sort your draw calls from farthest to nearest, and disable depth testing.


Alpha Testing

OpenGL ES supports an alternative to blending called alpha testing. Rather than applying an equation at every pixel to determine the final color, alpha testing performs a simple comparison of the source alpha with a presupplied value. If the comparison fails, the framebuffer is left unchanged; otherwise, the source color is written out. Alpha testing is generally used less frequently than blending because it cannot be used for smooth anti-aliasing or fade-out effects. In some ways, alpha testing is similar to blending with one-bit alpha.

With alpha testing, there’s no back-to-front ordering requirement like there is with blending. It’s simple to enable and configure:

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_LESS, 1.0f);

The first parameter of glAlphaFunc is a comparison function, similar to the depth comparison function . The second parameter is the reference value used for comparison. Use alpha testing with caution: Apple’s documentation warns that it can adversely affect performance.

Other  
  •  Building LOB Applications : Using Visual Studio 2010 WCF RIA Data Services Tooling
  •  Building LOB Applications : Implementing CRUD Operations in WCF Data Services
  •  iPhone 3D Programming : Blending and Augmented Reality - Wrangle Premultiplied Alpha
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Recipe
  •  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
  •  
    Most View
    Windows Server 2003 : Building an Active Directory Structure (part 1) - The First Domain
    Table Dressing With iWork
    All the stuff you didn't know (Part 2)
    GIGABYTE GA-Z77X-UP4 TH - Thunderbolt Technology Bonus
    From Sakshat to Aakash - The Complete Story
    Getting the Most Out of the Microsoft Outlook Client : Security Enhancements in Outlook 2007
    HP ENVY 14 Spectre – The Spectral Beauty
    Samsung NX20 - Strut To Succeed
    Toshiba Satellite M840 – Colorful And Powerful
    Some Of The Biggest Brands In The World Had Their Products (Part 2) - Fujitsu Lifebook LH772, Acer Aspire V5- 571G
    Top 10
    NZXT Phantom 630 Modular Full Tower Case
    Rosewill Launches Armor Evolution Mid-Tower Case
    Windows 8 - An In-Depth Expert Review (Part 6)
    Windows 8 - An In-Depth Expert Review (Part 5)
    Windows 8 - An In-Depth Expert Review (Part 4)
    Windows 8 - An In-Depth Expert Review (Part 3)
    Windows 8 - An In-Depth Expert Review (Part 2)
    Windows 8 - An In-Depth Expert Review (Part 1)
    Kobo Mini: Does The World Need A Smaller E-Reader? (Part 2)
    Kobo Mini : Does The World Need A Smaller E-Reader? (Part 1)