Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);

    }
share|improve this question
1  
Perhaps setting textures to be nearest instead of linear. Also, make sure your texture does not actually have a outline. –  Byte56 Nov 20 '12 at 19:53
    
setting the Filters to Nearest solved it, thanks :) –  vinzBad Nov 20 '12 at 20:07
add comment

2 Answers

up vote 6 down vote accepted

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.

share|improve this answer
    
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? –  vinzBad Nov 21 '12 at 15:46
1  
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). –  ccxvii Nov 21 '12 at 16:12
add comment

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.

share|improve this answer
    
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. –  snake5 Nov 21 '12 at 15:18
    
@snake5 Good plan. –  Byte56 Nov 21 '12 at 15:20
    
Just do little OpenGL I thought, 2D would be easy I thought :( now stuff gets strange... –  vinzBad Nov 21 '12 at 16:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.