Draw one instance of an object to multiple different windows?

Hey guys, i’m trying to enable proper multi window support for my software now that i’ve updated it to the latest nightly build of oF. I need to use something similar to the multiWindowOneAppExample as there are too many classes being shared between my GUI and Output windows to not have them in a single ofApp class.

My question is, how can I have a single ofFbo instance that can then be drawn into the various windows that i’ve created in my main.cpp. I’ve in included some code below that demonstrates what i’m trying to do. The below code draws the fbo into the draw() but not the drawSecondScreen() function… why is this? and how can I make it draw to both?

Thanks.

//--------------- main.cpp

#include "ofMain.h"
#include "ofApp.h"
#include "ofAppGLFWWindow.h"

//========================================================================
int main( ){
	ofGLFWWindowSettings settings;
	settings.width = 600;
	settings.height = 600;
	settings.setPosition(ofVec2f(600,0));
	settings.resizable = true;
	shared_ptr<ofAppBaseWindow> mainWindow = ofCreateWindow(settings);

	settings.width = 600;
	settings.height = 600;
	settings.setPosition(ofVec2f(0,0));
	settings.resizable = false;
	shared_ptr<ofAppBaseWindow> secondWindow = ofCreateWindow(settings);
	secondWindow->setVerticalSync(false);

	shared_ptr<ofApp> mainApp(new ofApp);
	mainApp->setupSecondScreen();
	ofAddListener(secondWindow->events().draw,mainApp.get(),&ofApp::drawSecondScreen);

	ofRunApp(mainWindow, mainApp);
	ofRunMainLoop();
}

//--------------- ofApp.h

#pragma once
#include "ofMain.h"

class ofApp : public ofBaseApp{
	public:
		void setup();
		void setupSecondScreen();
		void update();
		void draw();
		void drawSecondScreen(ofEventArgs & args);
    
        ofImage img;
        ofFbo fbo;
};

//--------------- ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
	ofBackground(255);
    img.load("myImage.jpg");
    fbo.allocate(img.getWidth(), img.getHeight());
}
//--------------------------------------------------------------
void ofApp::setupSecondScreen(){
	ofSetBackgroundColor(0);
}
//--------------------------------------------------------------
void ofApp::update(){
    fbo.begin();
    ofClear(0,0,0,0);
    img.draw(0,0);
    fbo.end();
}
//--------------------------------------------------------------
void ofApp::draw(){
    ofSetColor(255);
    fbo.draw(0, 0);
}
//--------------------------------------------------------------
void ofApp::drawSecondScreen(ofEventArgs & args){
    ofSetColor(255);
    fbo.draw(0, 0);
}

Not all resources can be shared among screens, usually resources that represent memory storage can be shared among contexts. that means for example that you can’t share an fbo, in the sense of having both contexts draw to it but you can share the texture the fbo draws to.

When creating the windows you have to specify that they share a context like:

ofGLFWWindowSettings settings;
	settings.width = 600;
	settings.height = 600;
	settings.setPosition(ofVec2f(600,0));
	settings.resizable = true;
	shared_ptr<ofAppBaseWindow> mainWindow = ofCreateWindow(settings);

	settings.width = 600;
	settings.height = 600;
	settings.setPosition(ofVec2f(0,0));
	settings.resizable = false;
        settings.shareContextWith = mainWindow;
	shared_ptr<ofAppBaseWindow> secondWindow = ofCreateWindow(settings);
	secondWindow->setVerticalSync(false);

	shared_ptr<ofApp> mainApp(new ofApp);
	mainApp->setupSecondScreen();
	ofAddListener(secondWindow->events().draw,mainApp.get(),&ofApp::drawSecondScreen);

	ofRunApp(mainWindow, mainApp);
	ofRunMainLoop();

Note settings.shareContextWith = mainWindow; before creating the second window

Amazing! was hoping there was a straight forward solution like that. Thanks Arturo!