MULTIMEDIA

iPhone 3D Programming : Adding Depth and Realism - Better Wireframes Using Polygon Offset

1/17/2011 2:44:04 PM
The toon shading example belongs to a class of effects called nonphotorealistic effects, often known as NPR effects. Having dangled the carrot of shaders in front of you, I’d now like to show that ES 1.1 can also render some cool effects.

For example, you might want to produce a intentionally faceted look to better illustrate the geometry; this is useful in applications such as CAD visualization or technical illustration. Figure 1 shows a two-pass technique whereby the model is first rendered with triangles and then with lines. The result is less messy than the wireframe viewer because hidden lines have been eliminated.

Figure 1. Two-pass wireframe with the Trefoil knot and Möbius strip


An issue with this two-pass technique is Z-fighting. An obvious workaround is translating the first pass backward ever so slightly, or translating the second pass forward. Unfortunately, that approach causes issues because of the nonlinearity of depth precision; some portions of your model would look fine, but other parts may have lines on the opposite side that poke through.

It turns out that both versions of OpenGL ES offer a solution to this specific issue, and it’s called polygon offset. Polygon offset tweaks the Z value of each pixel according to the depth slope of the triangle that it’s in. You can enable and set it up like so:

glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(factor, units);

factor scales the depth slope, and units gets added to the result. When polygon offset is enabled, the Z values in each triangle get tweaked as follows:


You can find the code to implement this effect in ModelViewer in the downloadable examples. Note that your RenderingEngine class will need to store two VBO handles for index buffers: one for the line indices, the other for the triangle indices. In practice, finding the right values for factor and units almost always requires experimentation.


Warning:

Because of a hardware limitation, first- and second-generation iPhones ignore the factor argument in glPolygonOffset.


Flat Shading

One rather obscure feature found only in OpenGL ES 1.1 is flat shading, whereby OpenGL forces the color of each triangle to be uniform. OpenGL performs flat shading by choosing a specific vertex in each triangle and then spreading its associated color throughout the triangle. You can turn it on and off as follows:

glShadeModel(GL_FLAT);   // Turn on flat shading
glShadeModel(GL_SMOOTH); // Turn off flat shading (the default)

I suspect this feature is a bit of a leftover from olden times, when flat shading was a desirable option to increase performance. Nowadays it’s probably useful only for nonphotorealistic rendering. Another way to achieve something similar is simply duplicating the same normal vector at each of the three corners in each triangle; this is one approach you could take for ES 2.0.


Other  
  •  Programming with DirectX : Textures in Direct3D 10 (part 2)
  •  Programming with DirectX : Textures in Direct3D 10 (part 1) - Textures Coordinates
  •  Programming with DirectX : Shading and Surfaces - Types of Textures
  •  iPhone 3D Programming : Adding Shaders to ModelViewer (part 2)
  •  iPhone 3D Programming : Adding Shaders to ModelViewer (part 1) - New Rendering Engine
  •  iPhone 3D Programming : Adding Depth and Realism - Shaders Demystified
  •  Programming with DirectX : Transformation Demo
  •  Programming with DirectX : View Transformations
  •  Programming with DirectX : World Transformations
  •  Programming with DirectX : Projection Transformations
  •  iPhone 3D Programming : Adding Depth and Realism - Lighting Up (part 2)
  •  iPhone 3D Programming : Adding Depth and Realism - Lighting Up (part 1)
  •  iPhone 3D Programming : Adding Depth and Realism - Surface Normals (part 2)
  •  iPhone 3D Programming : Adding Depth and Realism - Surface Normals (part 1)
  •  iPhone 3D Programming : Adding Depth and Realism - Filling the Wireframe with Triangles
  •  iPhone 3D Programming : Adding Depth and Realism - Creating and Using the Depth Buffer
  •  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
  •  
    Most View
    HTC One X+ Review - Highlighting Almost All Power Of HTC One X (Part 2)
    Skype For Windows 8 - A New Way Of Using Skype
    ASP.NET 4 : Data Source Controls (part 2) - Parameterized Commands
    Ten Stellar Keyboard Shortcuts
    Windows Phone 8 Group Test – June 2013 (Part 1) : HTC Windows Phone 8S, HTC Windows Phone 8X
    Microsoft Surface With Windows RT Review (Part 1)
    Improve Your Mac (Part 1) - Import Pictures into iPhoto
    Apple iPod Nano – The Trendy Compactness
    Sony Vaio Tap 20 Mobile Desktop - Meet The Laptablet
    Affordable Strong-Bass Denon Headphones
    Top 10
    The NZXT Kraken X40 Compact Liquid Cooler Review (Part 3)
    The NZXT Kraken X40 Compact Liquid Cooler Review (Part 2)
    T-Mobile’s Samsung Galaxy Note II Review (Part 6)
    T-Mobile’s Samsung Galaxy Note II Review (Part 5)
    T-Mobile’s Samsung Galaxy Note II Review (Part 4)
    T-Mobile’s Samsung Galaxy Note II Review (Part 3)
    T-Mobile’s Samsung Galaxy Note II Review (Part 2)
    T-Mobile’s Samsung Galaxy Note II Review (Part 1)
    Sony Cybershot DSC-TF1 - Affordable Water-Resistant Camera
    Buffalo MiniStation Slim 500GB External Hard Drive