Possible example error regarding ofFbo

Edit: I changed the title as this could be an unnoticed behaviour in one of the OF examples. In the second post on this thread it’s described how to check this in the alphaMaskingShaderExample.

Following on this-topic.

As I see back when it was discussed it wasn’t clear how to use FBOs and transparency. The solution proposed meant calling ofBackground within the fbo.

The alpha value I render in a FBO is blended with that FBO’s background color, so if I ofClear(0.0), my alpha values will blend with black instead of blending with whatever is behind.

  
  
void testApp::setup(){  
	img.loadImage("gradient.png");    
    ofSetVerticalSync(true);    
    fbo.allocate( ofGetWidth(), ofGetHeight() );    
	ofBackground(255,255,255);  
    ofEnableAlphaBlending();   
}  
  
void testApp::draw(){  
	fbo.begin();  
	ofClear(0.0);  
	img.draw(mouse.x, mouse.y);  
	fbo.end();  
	fbo.draw(0,0);  
}  
  

See the attached files.

How can I clear the transparency of an fbo so it blends with whatever is rendered behind, instead of blending with the currento fbo’s color? Is it even possible?

The solution from the previous topic isn’t good for me as I need to render arbitrarily behind my fbos. That solution only works for an specific background color.

This problem can be seen in the alphaMaskingShaderExample, it’s difficult to notice though. In the update function, try changing

  
    ofClear(0 ,0 ,0 , 0);   

by

  
    ofClear(255 ,0 ,0 , 0);   

As the alpha value is always 0, we would expect the color used to be irrelevant. However, this color is used for blending when applying the mask. When clearing with black we still can see black shading, but it’s barely noticeable.

How can we achieve true alpha masking, just taking into account the two images being used?

Can anyone confirm if this is an error or just an unexpected behaviour? Maybe I’m confused on what to use FBOs for. My workaround was leaving all the alpha blending until the end, when I draw to the screen buffer.
However it would be nice to know wether it’s possible to set alpha blending for fbos to be fully transparent.

The alpha value I render in a FBO is blended with that FBO’s background color, so if I ofClear(0.0), my alpha values will blend with black instead of blending with whatever is behind.

I’m not sure that’s true, the alpha value just combines the colors in the front based on alpha values. If you clear the main renderer and then clear the FBO you won’t have anything in the background. Since you’re making a white background it’s blending with the white in the background where the alpha is < 1.f.

That’s what I thought, but in practice it does blend with the color of the ofFbo instead of the color of what’s behind. You can check it in the alphaMaskingShaderExample. Without modifying it you can actually tell the brush is being blended with black.

This image is from the example:

In there, yellow and white should blend and give a brighter yellow fading to white. But we get a darker yellow before it blends to transparent. This is because it’s being blended with the ofFbo’s black background. (Try changing ofClear(0,0,0,0) to ofClear(255,0,0,0) and it’ll be more obvious.

To fix it we need to apply the mask not on the fbo, but when rendering to the screen buffer. Then it would be actual alpha masking.

I’ve researched a bit and glBlendFunc and glBlendFuncSeparate apparently can solve this problem. I just found it easier to leave any blending for the final screen rendering.

I guess this is just relevant if ofFbos are also thought to be use for this purpose (blending alpha with background or elements behind).

Ah, ok, I see what you’re talking about now. I don’t think that’s the sole purpose of the ofFbo, but yes, it’s definitley one use. I have an example of using the old glBlendFunc laying around, but I’d like to update it to glBlendFuncSeparate to show how that works because it’s pretty handy.

1 Like