hi,
it is a funny and strange effect.
I managed to get it working “correctly” by mainly mixing both images, foreground and background, in the shader rather than outside of it as it is in the current example.
I’m not sure why this is happening, but my guess is that is has to do with the blending options. I’ll figure it out and post it then.
but, for now, to fix it make the following changes.
gl3 frag shader
#version 150
// these are our textures
uniform sampler2DRect tex0;
uniform sampler2DRect maskTex;
uniform sampler2DRect backTex;
// this comes from the vertex shader
in vec2 texCoordVarying;
// this is the output of the fragment shader
out vec4 outputColor;
void main()
{
float mask = texture(maskTex, texCoordVarying).r;
outputColor = mix(texture(tex0, texCoordVarying), texture(backTex, texCoordVarying), mask);
}
or GL2
#version 120
uniform sampler2DRect tex0;
uniform sampler2DRect maskTex;
uniform sampler2DRect backTex;
varying vec2 texCoordVarying;
void main()
{
// Get alpha value
float mask = texture2DRect(maskTex, texCoordVarying).r;
gl_FragColor = mix(texture2DRec(tex0, texCoordVarying), texture2DRec(backTex, texCoordVarying), mask);
}
and in ofApp::draw()
void ofApp::draw(){
ofSetColor(255);
//----------------------------------------------------------
// this is our alpha mask which we draw into.
if(bBrushDown) {
maskFbo.begin();
int brushImageSize = 50;
int brushImageX = mouseX - brushImageSize * 0.5;
int brushImageY = mouseY - brushImageSize * 0.5;
brushImage.draw(brushImageX, brushImageY, brushImageSize, brushImageSize);
maskFbo.end();
}
//----------------------------------------------------------
// HERE the shader-masking happends
fbo.begin();
// Cleaning everthing with alpha mask on 0 in order to make it transparent by default
ofClear(0, 0, 0, 0);
shader.begin();
// here is where the fbo is passed to the shader
shader.setUniformTexture("maskTex", maskFbo.getTextureReference(), 1 );
shader.setUniformTexture("backTex", backgroundImage.getTexture(), 2);
foregroundImage.draw(0, 0);
shader.end();
fbo.end();
fbo.draw(0,0);
//----------------------------------------------------------
ofDrawBitmapString("Drag the Mouse to draw", 15,15);
ofDrawBitmapString("Press spacebar to clear", 15, 30);
}
hope it helps.
best