MULTIMEDIA

iPhone 3D Programming : Adding Depth and Realism - Creating and Using the Depth Buffer

1/4/2011 4:13:26 PM
Let’s enhance the wireframe viewer by adding in a depth buffer; this paves the way for converting the wireframes into solid triangles. Before making any changes, use Finder to make a copy of the folder that contains the SimpleWireframe project. Rename the folder to ModelViewer, and then open the copy of the SimpleWireframe project inside that folder. Select Project→Rename, and rename the project to ModelViewer.

Open RenderingEngine.ES1.cpp, and add GLuint m_depthRenderbuffer; to the private: section of the class declaration. Next, find the Initialize method, and delete everything from the comment // Create the framebuffer object to the glBindRenderbufferOES call. Replace the code you deleted with the code in Example 2.

Example 2. Adding depth to ES1::RenderingEngine::Initialize
// Extract width and height from the color buffer.
int width, height;
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES,
GL_RENDERBUFFER_WIDTH_OES, &width);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES,
GL_RENDERBUFFER_HEIGHT_OES, &height);

// Create a depth buffer that has the same size as the color buffer.
glGenRenderbuffersOES(1, &m_depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, m_depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES,
width, height);

// Create the framebuffer object.
GLuint framebuffer;
glGenFramebuffersOES(1, &framebuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES,
GL_RENDERBUFFER_OES, m_colorRenderbuffer);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES,
GL_RENDERBUFFER_OES, m_depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, m_colorRenderbuffer);

// Enable depth testing.
glEnable(GL_DEPTH_TEST);



The ES 2.0 variant of Example 4-2 is almost exactly the same. Repeat the process in that file, but remove all _OES and OES suffixes.

Next, find the call to glClear (in both rendering engines), and add a flag for depth:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

At this point, you should be able to compile and run, although depth testing doesn’t buy you anything yet since the app is still rendering in wireframe.

By default, the depth buffer gets cleared to a value of 1.0; this makes sense since you want all your pixels to initially pass the depth test, and OpenGL clamps the maximum window-space Z coordinate to 1.0. Incidentally, if you want to clear the depth buffer to some other value, you can call glClearDepthf, similar to glClearColor. You can even configure the depth test itself using glDepthFunc. By default, pixels “win” if their Z is less than the value in the depth buffer, but you can change the test to any of these conditions:


GL_NEVER

Pixels never pass the depth test.


GL_ALWAYS

Pixels always pass the depth test.


GL_LESS

Pixels pass only if their Z value is less than the Z value in the depth buffer. This is the default.


GL_LEQUAL

Pixels pass only if their Z value is less than or equal to the Z value in the depth buffer.


GL_EQUAL

Pixels pass only if their Z value is equal to the Z value in the depth buffer. This could be used to create an infinitely thin slice of the scene.


GL_GREATER

Pixels pass only if their Z value is greater than the Z value in the depth buffer.


GL_GEQUAL

Pixels pass only if their Z value is greater than or equal to the Z value in the depth buffer.


GL_NOTEQUAL

Pixels pass only if their Z value is not equal to the Z value in the depth buffer.

The flexibility of glDepthFunc is a shining example of how OpenGL is often configurable to an extent more than you really need. I personally admire this type of design philosophy in an API; anything that is reasonably easy to implement in hardware is exposed to the developer at a low level. This makes the API forward-looking because it enables developers to dream up unusual effects that the API designers did not necessarily anticipate.

Other  
  •  iPhone 3D Programming : Adding Depth and Realism - Examining the Depth Buffer
  •  iPhone 3D Programming : HelloCone with Fixed Function
  •  iPhone 3D Programming : Vector Beautification with C++
  •  jQuery 1.3 : An image carousel
  •  jQuery 1.3 : Headline rotator
  •  Silverlight : Print a Document
  •  Silverlight : Capture a Webcam
  •  Silverlight : Make Your Application Run out of the Browser
  •  Silverlight : Put Content into a 3D Perspective
  •  Silverlight : Response to Timer Events on the UI Thread
  •  Silverlight : Build a Download and Playback Progress Bar
  •  Silverlight : Play a Video
  •  C# 4.0 : Add a Static Constructor and Initialization
  •  C# 4.0 : Add a Constructor
  •  .NET Compact Framework : Font Selection
  •  .NET Compact Framework : Drawing Text
  •  Programming the Service Bus
  •  WCF Services : Generics
  •  WCF Services : Delegates and Data Contracts
  •  WCF Services : Enumerations
  •  
    Top 10
    -  Most Favorite Business Softwares – Feb 2013
    -  Maingear Nomad 15 – Don’t Judge A Gaming Laptop By Its Cover
    -  MSI GX60 Gaming Notebook - Great Looks And A Fast GPU
    -  OCUK Limited Edition P170EM - A Great Screen And Balanced Setup
    -  Samsung Series 5 550P5C-S03 Review - A Perfect Farewell To Windows 7
    -  Kobo Mini eReader Review
    -  Keep Kids Online Safely (Part 3)
    -  Keep Kids Online Safely (Part 2)
    -  Keep Kids Online Safely (Part 1)
    -  Nikon 24-85MM F3.5-4.5G ED-IF VR With Amazing Optical Performance
    Most View
    -  Surface Won’t Sell Windows 8...
    -  Windows Server 2008: Domain Name System and IPv6 - Troubleshooting DNS
    -  Hot List: Photo printing – Snap Happy – Backup Storage – November 2012
    -  Samsung Galaxy Note II
    -  Update Your Android Tablet (Part 2)
    -  iMac - The Sleekest Desktop PC Ever
    -  Algorithms for Compiler Design: THE LR PARSER
    -  Apple TV Explained
    -  iTunes Entertainment Weekly - Movies
    -  Writing A Novel In Scrivener
    -  Windows Phone 7 Development : Wiring Up Events to an Application Bar ( part 2)
    -  Windows 7 : How to Use System Restore, How to Troubleshoot USB Problems
    -  Windows 7 : Programming Drivers for the User Mode Driver Framework - Required Driver Functionality, UMDF Sample Drivers
    -  Performing Granular Backup Using the SharePoint Central Administration
    -  Secure Your Smartphone (Part 1)
    -  Apple iPhone 5 - The World’s Best Smartphone? (Part 1)
    -  Sharepoint 2007: Use the My Links to Manage Your Links
    -  Windows 7: Customizing Menus and the Control Panel (part 2) - Navigating and Customizing the Control Panel
    -  Document Software: Qoppa Software PDF Studio 7.0 Pro
    -  Windows 7 : Recovering After a Crash or Other Problem (part 3)