Search code examples
javaopenglantialiasingslick2dartifacts

Slick2d using anti-aliasing with Animations/Images


I'm using slick2d to render an Animation, but using anti-aliasing

Without AA, the movement is choppy, as one would expect for something without AA. I turn on AA in my game's preRenderState with:

g.setAntiAlias(true);

This leads to:

Image with graphical glitch

Note the diagonal line in the center, presumably caused by the two triangles that render the rectangle not meeting precicely. How can I remove that while still using AA to smooth my movement? I found http://www.java-gaming.org/index.php?topic=27313.0 but the solution was "remove AA" which I am reluctant to do.


Solution

  • This looks suspiciously like artifacts due to GL_POLYGON_SMOOTH.

    When you use that (deprecated) functionality, you are expected to draw all opaque geometry with blending enabled and the blend function: GL_SRC_ALPHA_SATURATE, GL_ONE. Failure to do so produces white outlines on most contours (aliased edges, basically).

    Chapter 6 of the OpenGL Redbook states:

    Now you need to blend overlapping edges appropriately. First, turn off the depth buffer so that you have control over how overlapping pixels are drawn. Then set the blending factors to GL_SRC_ALPHA_SATURATE (source) and GL_ONE (destination). With this specialized blending function, the final color is the sum of the destination color and the scaled source color; the scale factor is the smaller of either the incoming source alpha value or one minus the destination alpha value.

    This means that for a pixel with a large alpha value, successive incoming pixels have little effect on the final color because one minus the destination alpha is almost zero. With this method, a pixel on the edge of a polygon might be blended eventually with the colors from another polygon that's drawn later. Finally, you need to sort all the polygons in your scene so that they're ordered from front to back before drawing them.

    That is a lot of work that almost nobody does correctly when they try to enable GL_POLYGON_SMOOTH.