PDA

View Full Version : FBO's and alpha



gbiv
04-13-2008, 09:56 PM
Hello,
I'd like to create a RTT using an FBO which I then use as a texture on a billboard. My problem is that I'm not able to generate a texture that has "alpha" values. For instance if it were a tree that I rendered in the FBO, I'd try to set my clear color to (0,0,0,0) and then render my tree model in the FBO to create the texture. The output however has the tree surrounded by black rather than "clear". Is this the correct behavior or should I be able to setup the FBO properly to create a the texture properly?
thanks for any input.
biv

k_szczech
04-14-2008, 01:38 AM
Explain "tree surrounded by black". Do you mean that your entire texture is black, or do you see black edge aroud the tree?

If you're experiencing the 'black edge' problem, then it's because pixels of texture are filtered with black background pixels. The simplest solution would be to enable alpha testing when rendering tree from texture and set alpha test to (GL_GREATER, 0.95f).

The nicest solution would be to perform blur on the texture after you rendered tree into it. Texels with alpha > 0 should copy their color to neighbour texels that have alpha = 0, but alpha should not be copied.

babis
04-14-2008, 04:28 AM
For the 'black edge' problem :

It's even nicer solution,if you can afford it, to render first the blurred tree on the texture ( without writing to alpha as k_szczech said ) & then layer upon it the 'normal' rendered tree. This way you have no blurring on the inside (where alpha == 1)

Dark Photon
04-14-2008, 06:17 AM
I'm not able to generate a texture that has "alpha" values....The output however has the tree surrounded by black rather than "clear".

This will solve your problem:

Tom Forsyth's Blog (http://home.comcast.net/~tom_forsyth/blog.wiki.html) (See Pre-multiplied Alpha article)

Bottom line: stop using SRC_ALPHA / ONE_MINUS_SRC_ALPHA blending. Use ONE / ONE_MINUS_SRC_ALPHA blending, and pre-multiply the alpha into the color components.

gbiv
04-14-2008, 01:19 PM
Sorry for the late responses here. By that I mean, the tree renders as expected on the textured quad however the entire quad is visible. The portion of the quad that is not the tree (not just the border of the tree) is black (or whatever I set my clear color to) rather than completely transparent, so the "illusion" of the billboarded trees is lost because you can tell it's just a textured quad. Did that make sense or do I need to post some pics for clarity?

babis
04-14-2008, 02:02 PM
ok so you either got no alpha at all, or you have but you don't use it. How do you render to the texture? & how do you draw it afterwards?

zed
04-14-2008, 03:03 PM
were a tree that I rendered in the FBO, I'd try to set my clear color to (0,0,0,0) and then render my tree model in the FBO to create the texture. The output however has the tree surrounded by black rather than "clear".
yes thats the correct behaviour
glClearColor(...) clears the buffer to that color, it doesnt make that color invisible

to make the black invisible u have to set those pixels alpha values to say 0.0 (+ the trees pixels alpha values to 1.0) + then do alphatesting or blending etc

i suppose u could in a shader make black invisible by going

if ( dot( vec3(0.0), texture_value ) > 0.99 )
discard;

but #A its pretty messy + #B its gonna be slower

Dark Photon
04-15-2008, 05:46 AM
I'd try to set my clear color to (0,0,0,0) and then render my tree model in the FBO to create the texture. The output however has the tree surrounded by black rather than "clear".
yes thats the correct behaviour
glClearColor(...) clears the buffer to that color, it doesnt make that color invisible
Well, he said he cleared to 0,0,0,0, which has alpha = 0, which by convention is 100% transparent. Something else much be wrong. Such as rendering with BLEND disabled or some such.

babis, check the alpha values in your edge texels in your texture and make sure their alpha is 0. Also check that when you're blending this texture onto the frame buffer you have BLEND mode enabled:


glEnable ( GL_BLEND ) ;
glBlendFunc ( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
glBlendEquation ( GL_FUNC_ADD ) ;

gbiv
04-15-2008, 08:32 AM
Hello all, thanks for the replies. I've tried most of these but with no success. I think this has to do with my FBO setup. However, I'm not sure how to get around the problem if the clear color isn't setting alpha values in the FBO:
Here are some screen shots of my problem:
http://www.vrac.iastate.edu/~biv/fbo_clear_color_problem/clearcolor_black_alpha_0.tiff
http://www.vrac.iastate.edu/~biv/fbo_clear_color_problem/clearcolor_red_alpha_0.tiff

The flag is textured with the results of rendering the fire to my FBO. No matter what I set the clear color to, the texture doesn't seem to have any alpha information from the FBO outside of the object that I'm rendering (in this case the fire) so even if my texenv is set to REPLACE, the texture still retains the clear color as is shown on the flag. My texture is of format RGBA and is a color buffer attachment to my FBO so I think this is correct...

I'm not sure how to access those texels without adding some extra pass to post process the texture info as zed suggested, and I agree, this could get messy. Can anyone else reproduce this problem with a simple FBO rtt? This could help me narrow down if the problem is in my code or not.
Again thanks for the input here.
biv

zed
04-15-2008, 01:22 PM
Well, he said he cleared to 0,0,0,0, which has alpha = 0
i believe those alpha values are the destination alpha values eg GL_DST_ALPHA
i think what gbiv wants is SRC_ALPHA (ie from the texture)

ok looked at the screenshot i think he wants
A/ first to draw textureA(fire) into a FBO textureB
B/ draw this textureB onto a polygon onscreen

perhaps try
A/ set the textureB alphavalues from the textureA alphavalues (many ways of doing this)
B/ draw this texture onscreen with GL_DST_ALPHA, GL_ONE

zed
04-16-2008, 12:59 AM
actually DST_ALPHA doesnt make sense, as the texture is just a RGBA texture (it makes no diff that it was created by a FBO)

Dark Photon
04-16-2008, 04:36 AM
I'm not sure how to get around the problem if the clear color isn't setting alpha values in the FBO
The thing is, the clear color should be setting alpha value -- if you've got your GL state set up right when clearing and rendering into the texture. That is, assuming:
The texture you have bound to COLOR_ATTACHMENT0 has an alpha channel Your color write mask allows alpha writes (i.e. do a glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE )With these assumptions, glClearColor 0,0,0,0 should result in clearing your texture to transparent black.


Here are some screen shots of my problem:
http://www.vrac.iastate.edu/~biv/fbo_clear_color_problem/clearcolor_black_alpha_0.tiff
http://www.vrac.iastate.edu/~biv/fbo_clear_color_problem/clearcolor_red_alpha_0.tiff
0,0,0,0 is the only clear color that makes sense if you want a transparent background, and that pic looks good.


I'm not sure how to access those texels without adding some extra pass to post process the texture info as zed suggested
Yes you could do that, or just probe the alpha values indirectly via alpha test. That is, when blending the FBO texture into the main framebuffer, glEnable( GL_ALPHA_TEST ), glAlphaFunc ( GL_GREATER, 0.5 ), and see what you get. Then change the 0.5 to other values to quickly binary search what alpha value you've actually got back there.