PDA

View Full Version : White edges on alpha texture



zweifel
01-02-2010, 03:49 AM
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:

http://img412.imageshack.us/img412/9871/veg.jpg

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?

Brolingstanz
01-03-2010, 04:53 PM
Try enabling SAMPLE_ALPHA_TO_COVERAGE - works wonders for alpha masked objects (e.g. billboards) in areas of high depth complexity (without any sorting).

Dark Photon
01-03-2010, 06:49 PM
I am trying to render a tree with textures having alpha (tga format), but they keep showing white edges.
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.

zweifel
01-04-2010, 09:21 AM
Tried enabling SAMPLE_ALPHA_TO_COVERAGE, nothing changed.



I am trying to render a tree with textures having alpha (tga format), but they keep showing white edges.
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.

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

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

peterfilm
01-04-2010, 10:26 AM
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.

zweifel
01-04-2010, 12:44 PM
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?

James W. Walker
01-04-2010, 04:22 PM
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?

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


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

Yes.

zweifel
01-04-2010, 05:41 PM
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_col or.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?

knackered
01-05-2010, 05:08 AM
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.

BionicBytes
01-05-2010, 08:41 AM
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!

peterfilm
01-05-2010, 03:29 PM
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.

zweifel
01-06-2010, 07:22 AM
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).

http://img710.imageshack.us/img710/8374/treexr.jpg

The problem:

http://img24.imageshack.us/img24/7493/treedr.jpg

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?

ZbuffeR
01-06-2010, 07:31 AM
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.

Xmas
01-07-2010, 08:22 AM
(although I still do not see why it did not work with the shader).
Because you need to filter the texture in premultiplied space. Your shader gets the texture sample after filtering.


Other solution, higher quality : multisample with alpha to coverage if you can afford it.
In which case you should disable blending, and un-premultiply the color value in the shader (i.e. divide rgb by alpha).

zweifel
01-08-2010, 05:51 AM
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?