PC's JOGL Blog

Learning the Java JOGL OpenGL Bindings

Wednesday, January 24, 2007

Blending with glPolygonOffset

glPolygonOffset() is used to offset the polygon being rendered, and thus the pixels in the polygon, from the others currently in the displaybuffer. This does not actually effect the values written to the ZBuffer and is useful when blending two textures or polygons together.

The following images give an example of the problem and the effect of using glPolygonOffset. There are two polygons are rendered, the first being red, with the second being yellow blended over it in the same position in 3D space. The aim of the blending is to make a clean transition between red and yellow, or between 2 textures, in an animation. The second yellow polygon is a little bigger that the first red polygon be show the effect more clearly. In the first image, the "popping" of the red polygon through the yellow polygon occurs because the pixels values of the yellow polygon do not exactly match the Z buffer values of the first.

By enabling polygon offset the second polygon depth tests are performed using the following calculation...

"The value of the offset is factor * DZ + r * units, where DZ is a measurement of the change in depth relative to the screen area of the polygon, and r is the smallest value that is guaranteed to
produce a resolvable offset for a given implementation. The offset is added before the depth test is performed and before the value is written into the depth buffer *" OpenGL Reference Documentation

The result is that the second yellow polygon gives the desired result and blends nicely over the red polygon. This effect is very useful when blended between textures, for example, when changing a models level of detail. It is important when using this method that the polygons are drawn in a particular order. The offset yellow polygon must be draw original red polygon otherwise the effect does not work.

// Render first polygon
gl.glEnable(GL.GL_POLYGON_OFFSET_FILL) ;
gl.glPolygonOffset(-1.0f, 1.0f) ;
// Render second polygon
gl.glDisable(GL.GL_POLYGON_OFFSET_FILL) ;

Note that offsets can be used for the lines and the points in a polygon as well by enabling GL.GL_POLYGON_OFFSET_LINE and/or GL.GL_POLGON_OFFSET_POINT.

1 Comments:

At 2:21 PM, Anonymous Stephen said...

Nice to see you haven't fallen off the face of the Earth, and I look forward to many future postings.

 

Post a Comment

<< Home