Hi all,
So I’m trying to understand how FBOs with transparency work, maybe someone can help.
Basically I want to have a transparent FBO that I can draw stuff too, but the background is completely transparent.
My first question, how can I fade out the contents over time, without drawing a large transparent white box in the fbo each frame, as this makes there be a transparent box but I need it to be transparent, so basically i’m only fading out the pixels that have any pixels drawn into them.
If I can solve that problem first, I might be able to figure out the rest or come back for more help
Many thanks!
borg
January 20, 2013, 10:56pm
#2
Not quite what you are looking for but masking in FBOs with this shader supports transparency.
Modified from https://github.com/patriciogonzalezvivo/ofxSurface
//ofxAlphaMask header
ofImage source;
ofFbo result;
ofPath mask;
ofShader maskShader;
ofFbo maskFbo;
//ofxAlphaMask body
string shaderProgram;
bool disable_texture = config.getValue("config:disable_texture",1);//depends on graphics card support
if(disable_texture){
shaderProgram = "#version 120\n\
#extension GL_ARB_texture_rectangle : enable\n\
\n\
uniform sampler2D tex0;\n\
uniform sampler2D maskTex;\n\
uniform float texOpacity;\n\
uniform float maskOpacity;\n\
\n\
void main (void){\n\
vec2 pos = gl_TexCoord[0].st;\n\
\n\
vec4 src = texture2D(tex0, pos);\n\
float mask = texture2D(maskTex, pos).r;\n\
\n\
gl_FragColor = vec4( src.rgb * texOpacity , clamp( min(src.a,mask) , maskOpacity, 1.0));\n\
}\n";
ofDisableArbTex();
}else{
shaderProgram = "#version 120\n\
#extension GL_ARB_texture_rectangle : enable\n\
\n\
uniform sampler2DRect tex0;\n\
uniform sampler2DRect maskTex;\n\
uniform float texOpacity;\n\
uniform float maskOpacity;\n\
\n\
void main (void){\n\
vec2 pos = gl_TexCoord[0].st;\n\
\n\
vec4 src = sampler2DRect(tex0, pos);\n\
float mask = sampler2DRect(maskTex, pos).r;\n\
\n\
gl_FragColor = vec4( src.rgb * texOpacity , clamp( min(src.a,mask) , maskOpacity, 1.0));\n\
}\n";
}
maskShader.setupShaderFromSource(GL_FRAGMENT_SHADER, shaderProgram);
maskShader.linkProgram();
void ofxAlphaMask::update() {
if(!source.isAllocated()){
return;
}
if(updateMask){
maskFbo.allocate(w,h, GL_RGBA, ofFbo::maxSamples());
// Generate masking contour
maskFbo.begin();
ofClear(0,0,0,255);
ofBeginShape();
ofSetColor(255, 255, 255);
mask.setFilled(true);
mask.setFillHexColor(0xFFFFFF);
ofEnableSmoothing();
ofPushMatrix();
ofTranslate(offsetX,offsetY);
ofScale(maskScale,maskScale);
mask.draw(0,0);
ofPopMatrix();
ofEndShape(true);
maskFbo.end();
//draw new fbo masked
result.allocate(w,h, GL_RGBA, ofFbo::maxSamples());
result.begin();
maskShader.begin();
maskShader.setUniformTexture("maskTex", maskFbo.getTextureReference(), 1 );
maskShader.setUniform1f("texOpacity", texOpacity);
maskShader.setUniform1f("maskOpacity", maskOpacity);
ofClear(0, 0, 0, 0);
ofEnableAlphaBlending();
source.draw(0,0);
maskShader.end();
result.end();
updateMask = false;
}
}