Why does this code not draw a circle?


#1

Hello. I’m wondering why this code doesn’t display a circle. I believe I’m disabling the auto background. However, something is overwriting the circle initially written in setup().

#include “ofApp.h”

//--------------------------------------------------------------
void ofApp::setup(){
ofSetColor(255);
ofDrawCircle(100, 100, 100, 100);
ofSetBackgroundAuto(false);
}

//--------------------------------------------------------------
void ofApp::update(){

}

//--------------------------------------------------------------
void ofApp::draw(){

}


#2

you have to put this in draw()


#3

It’s true that in this particular case, there would be no difference if I put it in draw. However, in the current project I’m generating a complex visualization. That should only be done once, and ideally in setup. Perhaps a more precise question would be “what is erasing the circle after setup()” completes? Processing and other languages are behaving differently. For example, this Processing sketch draws a circle:

void setup() {
size(1024, 768);
ellipse(100, 100, 100, 100);
}

void draw() {
}


#4

More curiously, if I move the logic to the first draw() call, I still don’t see the circle:

void ofApp::setup() {
ofSetColor(255);
ofDrawCircle(100, 100, 100, 100);
ofSetBackgroundAuto(false);
}

//--------------------------------------------------------------
void ofApp::update() {

}

//--------------------------------------------------------------
void ofApp::draw() {
if (ofGetFrameNum() == 1) {
ofSetColor(255);
ofDrawCircle(100, 100, 100, 100);
}
}


#5

Yeah - I haven’t used ofSetBackgroundAuto much but that is weird that it doesn’t work on the first frame. If you change ofGetFrameNum() == 3 it does however it flickers which I wouldn’t think you would want


#6

Here’s how to do it with an ofFbo which seems to do what I think you want

ofFbo fbo;
//--------------------------------------------------------------
void ofApp::setup(){

    fbo.allocate(ofGetWidth(), ofGetHeight());
    fbo.begin();
    ofClear(0,0,0,0);
    ofSetColor(255);
    ofDrawCircle(100, 100, 100, 100);
    fbo.end();
}

//--------------------------------------------------------------
void ofApp::update(){

}

//--------------------------------------------------------------
void ofApp::draw(){
    
    
    fbo.draw(0, 0);
}

#7

Thanks. Yeah, that does do what I want. I am still very curious why an FBO is required. It means there’s something fundamental about oF I’m not understanding.

I was actually working with FBOs and getting blending behavior I didn’t understand. So I stripped the example down to a simplified case when I stumbled across that odd case.

Also, it would be nice to not be required to use an FBO. There are some things about the way they blend that I don’t understand (see, for example Memo’s thread). It would be nice to just sidestep that issue. (To which you could legitimately call me lazy, but less work is less work.)


#8

What if you change the order of the functions in the first snippet that you have posted, and you set a color for the circle?

void ofApp::setup(){
    ofSetColor(255);
    ofSetBackgroundAuto(false);

    ofSetColor(255,0,0);
    ofDrawCircle(100, 100, 100, 100);
}

#9

The problem is that OF only sets the perspective settings before calling draw so if you draw in setup the perspective matrix is still the opengl default. When binding an ofFbo, it sets the correct perspective to draw into it, that’s why using an fbo works

You can try using ofSetupPerspective() before doing any draw call but using ofSetBackgroundAuto or trying to draw outside of draw in the screen is problematic and the recommended way to do it is using an fbo


#10

Thanks, Arturo! That makes perfect sense.

I do have one related question. Why would the circle in the sketch below only persist on frame 3 or above?

void ofApp::setup() {
	ofSetBackgroundAuto(false);
}

void ofApp::draw() {
    //circle only persists on frame 3 or above
	if (ofGetFrameNum() == 1) {
		ofSetColor(255);
		ofDrawCircle(100, 100, 100, 100);
	}
}

#11

As I said, ofSetBackgroundAuto is kind of problematic, it uses a draw mode that it’s not very standard and sometimes, depending on the platform, the graphics card driver… it might behave erratically

If you want to have that kind of behaviour the safest is to use an fbo


#12

Ah, there’s a delay in the graphics driver engaging. Hence the variability in timing. That makes sense. Much obliged for the detailed answers.