When drawing a series of wide smoothed lines that overlap, such as an outline composed of a GL_LINE_LOOP, more than one fragment may be produced for a given pixel. Since smooth lines require enabling GL_BLEND, this may cause the pixel to appear brighter or darker than expected, as the fragments add more color to that pixel than in other locations.
An application may use a combination of the stencil test and alpha test to pass only the fragments that have the highest alpha, and therefore contribute the most color to a pixel. This technique uses repeated application of the alpha test to pass fragments with decreasing alpha, and uses the stencil test and buffer to mark where fragments previously passed. This has the effect of sorting fragments by alpha value.
glClear(GL_STENCIL_BUFFER_BIT); glEnable(GL_STENCIL_TEST); glEnable(GL_ALPHA_TEST); glEnable(GL_LINE_SMOOTH); glEnable(GL_BLEND); glStencilFunc(GL_NOTEQUAL, 1, 0xff); glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); for(a = .98f; a >= 0.0f; a -= .02f) { glAlphaFunc(GL_GREATER, a); /* draw lines here */ }
Because this draws the line set repeatedly (50 times in this example), you should consider the alpha values likely to be used by your application and alter the loop appropriately.
For example, to improve performance by reducing the number of iterations, your application may favor higher alpha values by increasing the step size as the value in the loop decreases, or simply end the loop early.
On the other hand, if your application requires more accuracy, it is possible to iterate through every possible alpha value and pass only the fragments in each iteration that match each specific alpha value.