Skip to content

Commit 0c0a45d

Browse files
authored
Merge pull request #5462 from roymacdonald/fix-fboMaskShaderExample
fboAlphaMask example fix.
2 parents 7a3c551 + 7a26a99 commit 0c0a45d

File tree

5 files changed

+43
-44
lines changed

5 files changed

+43
-44
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
21
precision highp float;
32

4-
uniform sampler2D tex0;
3+
// these are our textures
4+
uniform sampler2D tex0;//this is the background texture
55
uniform sampler2D maskTex;
6+
uniform sampler2D foregroundTex;
67

8+
// this comes from the vertex shader
79
varying vec2 texCoordVarying;
810

911
void main()
1012
{
11-
vec3 src = texture2D(tex0, texCoordVarying).rgb;
12-
float mask = texture2D(maskTex, texCoordVarying).r;
13-
gl_FragColor = vec4(src , mask);
13+
// Get color values from the background and foreground
14+
vec4 back = texture2D(tex0, texCoordVarying);
15+
vec4 fore = texture2D(foregroundTex, texCoordVarying);
16+
17+
// get alpha from mask
18+
float mask = texture2D(maskTex, texCoordVarying).r;
19+
20+
//mix colors from background and foreground based on the mask value
21+
gl_FragColor = mix(fore , back, mask);
1422
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
#version 120
22

3-
uniform sampler2DRect tex0;
3+
// these are our textures
4+
uniform sampler2DRect tex0;//this is the background texture
45
uniform sampler2DRect maskTex;
6+
uniform sampler2DRect foregroundTex;
57

8+
// this comes from the vertex shader
69
varying vec2 texCoordVarying;
710

811
void main()
912
{
10-
// Get color value from
11-
vec3 src = texture2DRect(tex0, texCoordVarying).rgb;
13+
// Get color values from the background and foreground
14+
vec4 back = texture2DRect(tex0, texCoordVarying);
15+
vec4 fore = texture2DRect(foregroundTex, texCoordVarying);
1216

13-
// Get alpha value
17+
// get alpha from mask
1418
float mask = texture2DRect(maskTex, texCoordVarying).r;
1519

16-
// Set
17-
gl_FragColor = vec4(src , mask);
20+
//mix colors from background and foreground based on the mask value
21+
gl_FragColor = mix(fore , back, mask);
1822
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#version 150
22

33
// these are our textures
4-
uniform sampler2DRect tex0;
4+
uniform sampler2DRect tex0;//this is the background texture
55
uniform sampler2DRect maskTex;
6+
uniform sampler2DRect foregroundTex;
67

78
// this comes from the vertex shader
89
in vec2 texCoordVarying;
@@ -12,12 +13,13 @@ out vec4 outputColor;
1213

1314
void main()
1415
{
15-
// get rgb from tex0
16-
vec3 src = texture(tex0, texCoordVarying).rgb;
16+
// Get color values from the background and foreground
17+
vec4 back = texture(tex0, texCoordVarying);
18+
vec4 fore = texture(foregroundTex, texCoordVarying);
1719

1820
// get alpha from mask
1921
float mask = texture(maskTex, texCoordVarying).r;
20-
21-
//mix the rgb from tex0 with the alpha of the mask
22-
outputColor = vec4(src , mask);
23-
}
22+
23+
//mix colors from background and foreground based on the mask value
24+
outputColor = mix(fore , back, mask);
25+
}

examples/shader/07_fboAlphaMask/src/ofApp.cpp

+11-25
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,21 @@ void ofApp::setup(){
1313
}
1414
#endif
1515

16-
backgroundImage.loadImage("A.jpg");
17-
foregroundImage.loadImage("B.jpg");
18-
brushImage.loadImage("brush.png");
16+
backgroundImage.load("A.jpg");
17+
foregroundImage.load("B.jpg");
18+
brushImage.load("brush.png");
1919

2020
int width = backgroundImage.getWidth();
2121
int height = backgroundImage.getHeight();
2222

2323
maskFbo.allocate(width, height);
24-
fbo.allocate(width, height);
2524

2625
// Clear the FBO's
2726
// otherwise it will bring some junk with it from the memory
2827
maskFbo.begin();
2928
ofClear(0,0,0,255);
3029
maskFbo.end();
31-
32-
fbo.begin();
33-
ofClear(0,0,0,255);
34-
fbo.end();
35-
30+
3631
bBrushDown = false;
3732
}
3833

@@ -61,26 +56,17 @@ void ofApp::draw(){
6156

6257
//----------------------------------------------------------
6358
// HERE the shader-masking happends
64-
fbo.begin();
65-
// Cleaning everthing with alpha mask on 0 in order to make it transparent by default
66-
ofClear(0, 0, 0, 0);
67-
59+
6860
shader.begin();
6961
// here is where the fbo is passed to the shader
70-
shader.setUniformTexture("maskTex", maskFbo.getTextureReference(), 1 );
71-
72-
backgroundImage.draw(0, 0);
62+
shader.setUniformTexture("maskTex", maskFbo.getTexture(), 1 );
63+
shader.setUniformTexture("foregroundTex", foregroundImage.getTexture(), 2 );
64+
65+
backgroundImage.draw(0, 0);
7366

7467
shader.end();
75-
fbo.end();
76-
77-
//----------------------------------------------------------
78-
// FIRST draw the background image
79-
foregroundImage.draw(0,0);
80-
81-
// THEN draw the masked fbo on top
82-
fbo.draw(0,0);
83-
68+
69+
8470
//----------------------------------------------------------
8571
ofDrawBitmapString("Drag the Mouse to draw", 15,15);
8672
ofDrawBitmapString("Press spacebar to clear", 15, 30);

examples/shader/07_fboAlphaMask/src/ofApp.h

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class ofApp : public ofBaseApp{
2626
ofImage brushImage;
2727

2828
ofFbo maskFbo;
29-
ofFbo fbo;
3029

3130
bool bBrushDown;
3231
};

0 commit comments

Comments
 (0)