Problem with FBO and ofSetColor

Hello,

I have a problem understanding why this doesn’t work correctly:

void ofApp::setup(){
    fbo = new ofFbo(); fbo->allocate(ofGetWidth(), ofGetHeight());
}

void ofApp::draw(){
    ofClear(127, 127, 127);

    ofSetColor(0, 255, 0);
    ofCircle(100, 100, 40);

    fbo->begin();
    ofClear(0, 0, 0);
    ofSetColor(255, 0, 0);
    ofCircle(200, 200, 40);
    fbo->end();
    fbo->draw(0, 0);
}

This is what it draws (clearly the second circle should be red):

I found a fix by wrapping the 2nd and 3rd line of draw() within a ofPushStyle() and ofPopStyle(). Now that I managed to fix it, you might ask why I’m making this thread… Well, to be completely honest I don’t KNOW why it fixed it. I would like to have to some clarification.

Thanks.

After troubleshooting some more I think I’ve found the problem.

It seems that the rgb color of each pixels in the FBO is limited to the values set on the last call of ofSetColor(r,g,b).

So the fix is to call ofSetColor(255, 255, 255) right before calling draw(x,y) on the fbo. This way it opens up all the 3 color channels.

Very strange, is this a bug? (is it better to avoid ofSetColor entirely?)

the fbo is a texture and when you draw it, the draw color “tints” what you draw, just like it does an image or any other pixel based thing you could draw. When you set the draw color to be white, it draws it as is, but if the draw color is black, the result is black. If you set the draw color to be red (ofSetColor(255,0,0)) this tints the output red. In your case, you’ve tinted the output to ofSetColor(0, 255, 0) – setting ofSetColor(255,255,255) fixes this.

The way to think about the tint is that it’s darkening the pixels in each channel by (val / 255), for example, ofSetColor(0,255,0) means you are saying

pixelRed = imagePixelRed * 0/255
pixelGreen = imagePixelGreen * 255/255
pixeBlue = imagePixelBlue * 0/255

255/255 = 1 so you are drawing the pixels as is if the channel is set to 255. white is 255 on each channel.

ofPushStyle / ofPopStyle store and restore drawing styles, which can be useful since setting the color is a global change. By default, the drawing color is white in OF (because the tinting trips up most beginners) so push and pop style also work here.

hope that helps!

1 Like