ofBackground - what does it do, when to use it?

I have been assuming it would essentially fill the visible screen with the specified color. It seemed to have the same effect as ofDrawRectangle for the screen, but seemed to be slightly faster.

But then my client tried my program that sometimes sets ofSetBackgroundAuto(false) and which was working fine on my Windows and Mac computers, on two of his Windows computers, and on those computers (which do fine when ofSetBackgroundAuto(true) is set), they either showed a black screen or weren’t wiping the background when I called ofBackground() in Draw().

Checking the documentation, I see:

Sets the background color.

It takes as input r,g,b (0-255). The background is cleared automatically, just before the draw() command, so if the background color is not changing, you could call this inside of setup() (once, at the start of the application). If the background color is changing, you can call this inside of update().

Which doesn’t clearly communicate to me what ofBackground() actually does. And I’m quite surprised that it suggests calling it in Update() rather than in Draw(). Is that a mistake, or is it like ofSetColor(), where you’re just telling OF what color to draw the background when IT does that itself? And then for whatever reason, on some Windows computers, it ends up happening at different times, either right before draw, or right after ofBackground is called?

I’m quite confused…

Hello Drazinut,

I was facing the same question. I have read the code and here’s my conclusion:

  • ofSetBackgroundColor() set the color that is used to automatically clear the background just before draw(), if the auto clear is on (ofSetBackgroundAuto(true), by default).
  • ofBackground() do the same, and also clear the output immediatly

Here’s the code, for these functions in ofGraphics.cpp:

void ofBackground(int r, int g, int b, int a){
void ofSetBackgroundColor(const ofColor & c){

ofGetCurrentRenderer() return a ofBaseRenderer. Comments in ofGraphicsBaseTypes.h:

class ofBaseRenderer{

    /// \brief Immediately paint a background color to the screen.
    /// If automatic background drawing is enabled (which it is by default) this
    /// method called from ofApp::setup() will also repaint the background with
    /// this color each frame.
    /// \param c The color to paint the background with.
    virtual void background(const ofColor & c)=0;

    /// \brief Set this renderer's background color.
    /// \param c The color to request this renderer to use.
    virtual void setBackgroundColor(const ofColor & c)=0;

The functions definition in ofGLRenderer.cpp confirm this (the comments are mine):

void ofGLRenderer::setBackgroundColor(const ofColor & color){
	currentStyle.bgColor = color;
	// Set the color used to clear by OpenGL
	glClearColor(currentStyle.bgColor[0]/255.,currentStyle.bgColor[1]/255.,currentStyle.bgColor[2]/255., currentStyle.bgColor[3]/255.);
void ofGLRenderer::background(const ofColor & c){
	// Clear now, using the previously defined glClearColor
void ofGLRenderer::startRender(){
	// ... in this function we find:
	if ( getBackgroundAuto() ){

Does this information help you to understand what happens in your case ?

1 Like

It is helpful, yes (and thanks very much!), but I still don’t understand why my client’s machines are messing up when I set ofSetFrameRate(60); ofSetBackgroundAuto(false) and then call ofBackground(0, 0, 0) in draw().

One of his machines would just show a black screen with nothing drawn.
The other would not clear the background.

I’ve sent him a new version that doesn’t call ofBackground but just uses ofDrawRectangle… I’m eagerly awaiting to hear if that fixes it.

If it doesn’t, then I may need a way to detect that it’s messing up, and if so, not set BackgroundAuto to false on those machines.