MULTIMEDIA

iPhone 3D Programming : Blending and Augmented Reality - Stencil Alternatives for Older iPhones

2/23/2011 7:56:58 PM
If your app needs to accommodate first- and second-generation iPhones, in many cases you can use a trick that acts like stenciling without actually requiring a stencil buffer. These various tricks include the following:
  • Using the framebuffer’s alpha component to store the “stencil” values and setting up a blending equation that tests against those values.

  • Turning off color writes and writing to the depth buffer to mask out certain regions. (The easiest way to uniformly offset generated depth values is with the glDepthRange function.)

  • Cropping simple rectangular regions can be achieved with OpenGL’s glScissor function.

  • Some of the bitwise operations available with stencil buffers are actually possible with colors as well. In fact, there are additional operations possible with colors, such as XOR. To see how to do this, check out the glLogicOp function.

Let’s demonstrate the first trick in the previous list: using framebuffer alpha as a fake stencil buffer. With this technique, it’s possible to achieve the result shown in Figure 1 on older iPhones. The sequence of operations becomes the following:

  1. Clear the depth buffer.

  2. Render the background image with α = 0.

  3. Render the textured disk normally with α = 1.

  4. Enable blending, and set the blending equation to S*Dα+D*(1 – Dα).

  5. Render the reflection of the floating object.

  6. Set the blending equation to S*Sα+D*(1 – Sα).

  7. Turn off depth testing, and render the textured disk again with α = 0.5; this fades out the reflection a bit.

  8. Clear the depth buffer, and re-enable depth testing.

  9. Render the actual floating object.

Example 1 shows the rendering code for these nine steps.

Example 1. Faking the stencil buffer
glClear(GL_DEPTH_BUFFER_BIT);

// Set up the transforms for the background.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustumf(-0.5, 0.5, -0.5, 0.5, NearPlane, FarPlane);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -NearPlane * 2);

// Render the dark background with alpha = 0.
glDisable(GL_DEPTH_TEST);
glColor4f(0.5, 0.5, 0.5, 0);
glBindTexture(GL_TEXTURE_2D, m_textures.Tiger);
RenderDrawable(m_drawables.Quad);

// Set up the transforms for the 3D scene.
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(m_projection.Pointer());
glMatrixMode(GL_MODELVIEW);
glRotatef(20, 1, 0, 0);
glBindTexture(GL_TEXTURE_2D, m_textures.Marble);

// Render the disk normally.
glColor4f(1, 1, 1, 1);
glTranslatef(0, DiskY, 0);
RenderDrawable(m_drawables.Disk);
glTranslatef(0, -DiskY, 0);
glEnable(GL_DEPTH_TEST);

// Render the reflection.
glPushMatrix();
glRotatef(theta, 0, 1, 0);
glTranslatef(0, KnotY, 0);
glEnable(GL_LIGHTING);
glBindTexture(GL_TEXTURE_2D, m_textures.Grille);
glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
glEnable(GL_BLEND);
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(m_mirror.Pointer());
RenderDrawable(m_drawables.Knot);
glLoadMatrixf(m_projection.Pointer());
glMatrixMode(GL_MODELVIEW);
glDisable(GL_LIGHTING);
glPopMatrix();

// Render the disk again to make the reflection fade out.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, m_textures.Marble);
glColor4f(1, 1, 1, 0.5);
glDisable(GL_DEPTH_TEST);
glTranslatef(0, DiskY, 0);
RenderDrawable(m_drawables.Disk);
glTranslatef(0, -DiskY, 0);
glEnable(GL_DEPTH_TEST);
glColor4f(1, 1, 1, 1);
glDisable(GL_BLEND);

// Clear the depth buffer.
glClear(GL_DEPTH_BUFFER_BIT);

// Render the floating object.
glEnable(GL_LIGHTING);
glBindTexture(GL_TEXTURE_2D, m_textures.Grille);
glPushMatrix();
glTranslatef(0, KnotY, 0);
glRotatef(theta, 0, 1, 0);
RenderDrawable(m_drawables.Knot);
glPopMatrix();
glDisable(GL_LIGHTING);

Other  
  •  iPhone 3D Programming : Blending and Augmented Reality - Poor Man’s Reflection with the Stencil Buffer
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Reading a Gamepad
  •  iPhone 3D Programming : Blending and Augmented Reality - Shifting Texture Color with Per-Vertex Color
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Extensions and Their Uses
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Caveats
  •  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
  •  
    Video
    Top 10
    Windows Server 2003 : Domain Name System - Command-Line Utilities
    Microsoft .NET : Design Principles and Patterns - From Principles to Patterns (part 2)
    Microsoft .NET : Design Principles and Patterns - From Principles to Patterns (part 1)
    Brother MFC-J4510DW - An Innovative All-In-One A3 Printer
    Computer Planet I7 Extreme Gaming PC
    All We Need To Know About Green Computing (Part 4)
    All We Need To Know About Green Computing (Part 3)
    All We Need To Know About Green Computing (Part 2)
    All We Need To Know About Green Computing (Part 1)
    Master Black-White Copying
    Most View
    Epic Moments in Sports (Part 1)
    iPhone 3D Programming : Blending and Augmented Reality - Blending Recipe
    HP X2301 : Micro Thin, Macro Sights
    Advanced ASP.NET : Understanding Caching
    Amateur Astronomy Applications (Part 1) - WorldWide Telescope, Stellarium
    How To Buy…SSD Drives (Part 2)
    Macro Marvel by Peiling Lee
    Under The Surface (Part 1)
    Microsoft .NET : Design Principles and Patterns - Object-Oriented Design (part 1) - Basic OOD Principles
    Hosting a Multi-Tenant Application on Windows Azure : Selecting a Single-Tenant or Multi-Tenant Architecture
    Google Nexus 4 - Features Of A High-End Smartphone For Half The Price
    Developing an SEO-Friendly Website: Content Delivery and Search Spider Control (part 2)
    SQL Server 2008 : Explaining Advanced Query Techniques - Managing Internationalization Considerations
    Frequently Asked Questions About UAC
    Windows Server 2008 : Harnessing the Power and Potential of FIM
    Parallel Programming with Microsoft .Net : Dynamic Task Parallelism - An Example
    Microsoft XNA Game Studio 3.0 : Displaying Images - Using Resources in a Game (part 3) - Sprite Drawing with SpriteBatch
    Windows Server 2003 : Active Directory - Understanding Operations Master Roles
    Windows 7 : Using Windows Defender (part 3) - Using Windows Defender Tools & Troubleshooting Windows Defender
    iOS 6 Beta Review (Part 1)