White edges on alpha texture

Hello guys,

I am trying to render a tree with textures having alpha (tga format), but they keep showing white edges. Following is the image of how it looks like:

I am using a GLSL shader, this shader was already tested with non-alpha textured models and worked perfectly. Here is the fragment shader:


        varying vec4 diffuse,ambientGlobal, ambient;
        varying vec3 normal,lightDir,halfVector;
        varying float dist;

        uniform sampler2D tex;

        varying float fog_factor;

        void main()
        {
                                        
                vec3 ct,cf;
                vec4 texel;
                float intensity,at,af;

                intensity = max(dot(lightDir,normalize(normal)),0.0);

                 cf = intensity * (gl_FrontMaterial.diffuse).rgb + gl_FrontMaterial.ambient.rgb;
                af = gl_FrontMaterial.diffuse.a;

                texel = texture2D(tex,gl_TexCoord[0].st);
                ct = texel.rgb;
                at = texel.a;

  
                vec4 final_color = vec4(ct * cf, at * af);

                gl_FragColor= vec4(vec3(mix(gl_Fog.color, final_color, fog_factor)),final_color.w);


        }



Do I need to sort by depth to get rid of the white edges?

Try enabling SAMPLE_ALPHA_TO_COVERAGE - works wonders for alpha masked objects (e.g. billboards) in areas of high depth complexity (without any sorting).

Let’s see a pic of the color channel of your texture. My guess is that you’re using white for the “fill” color. Solution: premultiplied alpha.

Tried enabling SAMPLE_ALPHA_TO_COVERAGE, nothing changed.

Let’s see a pic of the color channel of your texture. My guess is that you’re using white for the “fill” color. Solution: premultiplied alpha. [/QUOTE]

I dot not know what you mean by fill color, but here is the tga:

http://www.4shared.com/file/188152650/f3dbfd28/trees3.html

the answer is premultiplied alpha, as dark photon points out.
i think tom forsyth explains it best, as he does most things:-
[]"]http://home.comcast.net/~tom_forsyth/blog.wiki.html#[[Premultiplied alpha]]]](http://home.comcast.net/~tom_forsyth/blog.wiki.html#[[Premultiplied alpha)
No really, you should read all of it. An eye opener.

So I need to use a pre-multiplied texture with a glBlendFunc(GL_SRC_ALPHA,GL_ONE) and glEnable(GL_BLEND) to draw those trees correctly. right?

Do I still need to worry about sorting like any other blending?

I believe it would be glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA ).

Yes.

Thanks!

I was trying to emulate the pre-multiplied alpha in a fragment shader doing:

final_color= vec4(vec3(mix(gl_Fog.color, final_color, fog_factor)),final_color.w);

gl_FragColor= vec4(vec3(final_color.xyz*final_color.w),final_color.w);

But my results were not that good. I will try exporting the tga in a pre-multiplied form, but why is that my little modification in the fragment shader shouldnt do the trick?

read the forsyth article someone’s already linked to - it will answer your questions. You have so obviously not read it. God only helps those who help themselves.

the answer is premultiplied alpha, as dark photon points out.
i think tom forsyth explains it best, as he does most things:-
http://home.comcast.net/~tom_forsyth/blog.wiki.html#[[Premultiplied alpha]]]
No really, you should read all of it. An eye opener.

O.M.G !

Time to re-write alot of my own rendering code!

well not really, just multiply your rgb channels by the alpha channel in your image loading code, then change one side of the blend equation with glBlendFunc. 20 minutes, unless you have to type using a stick grasped between your teeth.
Maybe you mean your particle systems?

It is mad though…I certainly didn’t realise the mistake until a couple of years ago when I read Forsyths blog. Is it me though, or does he come across as a real smart arse in that bit? Nobody likes a smart arse. Clever guy though.

Well, here are some photos of the result. This time with a texture changed instead of shader (although I still do not see why it did not work with the shader).

The problem:

I guess that cannot be helped. The only solution I see is by using a order independent transparency shader or by setting alpha to 0 or 255 and not using blending at all, only alpha test.

Do anyone see other possible solutions?

A simple tweak is to increase a bit the alpha test threshold so that the effect will be less visible, something around 96 or 128.

Other solution, higher quality : multisample with alpha to coverage if you can afford it.

Because you need to filter the texture in premultiplied space. Your shader gets the texture sample after filtering.

In which case you should disable blending, and un-premultiply the color value in the shader (i.e. divide rgb by alpha).

In which case you should disable blending, and un-premultiply the color value in the shader (i.e. divide rgb by alpha).

The blending could also be disabled when doing:

increase a bit the alpha test threshold so that the effect will be less visible
.

Isnt it?