I’d like to do arbitrary blending of two textures on the GPU. The two textures come from ofxFBOTextures (the version on Damian’s github), and the blending is done with an ofxShader (the version on my Google Code).
It looks like my attempt at setting the sampler2DRect is totally ignored, though.
In the attached example, I draw into two FBOs: a red (.5, 0, 0) circle on the left, a green (0, .5, 0) circle on the right. They are additively blended with glBlend. When you click, it switches to using the shader for blending – except instead you just get a green (0, 1, 0) circle on the right.
I’m going to read through this FBO-tutorial and try it without ofxFBOTexture, but it’d be super helpful if someone knows how to do it with ofxFBOTexture!
uniform sampler2DRect base, top;
void main(void){
vec2 uv = gl_TexCoord[0].xy;
gl_FragColor = texture2DRect(top, uv) + texture2DRect(base, uv);
}
void testApp::draw() {
int r = 200;
leftFbo.begin();
ofBackground(0, 0, 0);
glColor4f(.5, 0, 0, 1);
ofCircle(w / 3, h / 2, r);
leftFbo.end();
rightFbo.begin();
ofBackground(0, 0, 0);
glColor4f(0, .5, 0, 1);
ofCircle(2 * w / 3, h / 2, r);
rightFbo.end();
glColor4f(1, 1, 1, 1);
if(isMousePressed) {
blendShader.begin();
leftFbo.bindAsTexture();
blendShader.setUniform("left", (int) leftFbo.getTextureData().textureID);
rightFbo.bindAsTexture();
blendShader.setUniform("right", (int) rightFbo.getTextureData().textureID);
texRect(w, h);
blendShader.end();
} else {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
leftFbo.draw(0, 0);
rightFbo.draw(0, 0);
glDisable(GL_BLEND);
}
}
(texRect() just draws a rectangle from the top left corner to the bottom right corner, with unnormalized texCoords.)