1
\$\begingroup\$

You can see most of my source code in this question: Instead of the specified Texture, black circles on a green background are getting rendered. Why?

Now I have the problem, that my texture has a white outline on its transparent parts. After googling and setting up glBlendFunc, the outline just got "softer".

This is how it looks like:

Teh bug

This is how I now setup OpenGL:

    public static void SetupGL()
    {
        GL.Enable(EnableCap.Blend);
        GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

        GL.Enable(EnableCap.Texture2D);
        GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);

    }
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Perhaps setting textures to be nearest instead of linear. Also, make sure your texture does not actually have a outline. \$\endgroup\$
    – House
    Commented Nov 20, 2012 at 19:53
  • \$\begingroup\$ setting the Filters to Nearest solved it, thanks :) \$\endgroup\$
    – vinzBad
    Commented Nov 20, 2012 at 20:07

2 Answers 2

6
\$\begingroup\$

You should use pre-multiplied alpha for transparent textures that you intend to do alpha blending on. That will solve the color bleeding you're seeing.

First, make sure to multiply the RGB values of your texture with the A.

Second, use glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA) as the blend mode.

\$\endgroup\$
2
  • \$\begingroup\$ how do I accomplish this? should I loop through all pixels of my image and multiply them? I use a .png, I thought it already has premultiplied alpha? \$\endgroup\$
    – vinzBad
    Commented Nov 21, 2012 at 15:46
  • 1
    \$\begingroup\$ PNG stores unassociated (non-premultiplied) alpha. You'll have to premultiply the pixels yourself after loading, before calling glTexImage2d. Loop through the pixels and multiply (color.rgb *= color.a). \$\endgroup\$
    – ccxvii
    Commented Nov 21, 2012 at 16:12
1
\$\begingroup\$

You should try setting your textures to GL_NEAREST. It looks like you're using GL_LINEAR which could make your textures not be "crisp" on the edges where you want them to be.

\$\endgroup\$
3
  • \$\begingroup\$ This isn't a long-term solution and will look ugly once the sprite starts moving, especially when it gets bigger. Premultiplying alpha or choosing an appropriate background color will work much better in this case. \$\endgroup\$
    – snake5
    Commented Nov 21, 2012 at 15:18
  • \$\begingroup\$ @snake5 Good plan. \$\endgroup\$
    – House
    Commented Nov 21, 2012 at 15:20
  • \$\begingroup\$ Just do little OpenGL I thought, 2D would be easy I thought :( now stuff gets strange... \$\endgroup\$
    – vinzBad
    Commented Nov 21, 2012 at 16:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .