/** * Sets the GL blending state according to the specified color mode. If <code>havePremultipliedColors</code> is * true, this applies a blending function appropriate for colors premultiplied by the alpha component. Otherwise, * this applies a blending function appropriate for non-premultiplied colors. * * @param gl the GL context. * @param havePremultipliedColors true to configure blending for colors premultiplied by the alpha components, and * false to configure blending for non-premultiplied colors. * * @throws ArgumentException if the GL is null. */ public static void applyBlending(GL2 gl, bool havePremultipliedColors) { if (gl == null) { String message = Logging.getMessage("nullValue.GLIsNull"); Logging.logger().severe(message); throw new ArgumentException(message); } gl.glEnable(GL2.GL_ALPHA_TEST); gl.glAlphaFunc(GL2.GL_GREATER, 0.0f); if (havePremultipliedColors) { gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE_MINUS_SRC_ALPHA); } else { // The separate blend function correctly handles regular (non-premultiplied) colors. We want // Cd = Cs*As + Cf*(1-As) // Ad = As + Af*(1-As) // So we use GL_EXT_blend_func_separate to specify different blending factors for source color and source // alpha. bool haveExtBlendFuncSeparate = gl.isExtensionAvailable(GL_EXT_BLEND_FUNC_SEPARATE); if (haveExtBlendFuncSeparate) { gl.glBlendFuncSeparate( GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA, // rgb blending factors GL.GL_ONE, GL.GL_ONE_MINUS_SRC_ALPHA); // alpha blending factors } else { gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); } } }