I tried to get ofFbo work in the second GLFW window in the multiWindowOneAppExample , but failed. I know multiple ofFbo will work using the multi-windows & multi-apps, but I am not sure if it is possible to use one or multiple ofFbos in the multi-windows & single-app scenarios, especially in the second or third window. Please help me to work this out.
I understand ofFbo has related to the renderer, so I explicitly set the ofFbo’s renderer by passing the ofFbo::Settings sturcture. But it turns out wont work.
My working environment is Linux16.04_x64 + OF0.9.8 stable.
Here are my codes:
// main.cpp
#include "ofMain.h"
#include "ofApp.h"
#include "ofAppGLFWWindow.h"
//========================================================================
int main( ){
ofGLFWWindowSettings settings;
settings.width = 300;
settings.height = 300;
settings.setPosition(ofVec2f(0,0));
settings.resizable = true;
shared_ptr<ofAppBaseWindow> mainWindow = ofCreateWindow(settings);
settings.width = 300;
settings.height = 300;
settings.setPosition(ofVec2f(300,0));
settings.resizable = true;
settings.shareContextWith = mainWindow;
shared_ptr<ofAppBaseWindow> secondWindow = ofCreateWindow(settings);
shared_ptr<ofApp> mainApp(new ofApp);
mainApp->renderer1= dynamic_pointer_cast<ofBaseGLRenderer>(mainWindow->renderer());
mainApp->renderer2= dynamic_pointer_cast<ofBaseGLRenderer>(secondWindow->renderer());
ofAddListener(secondWindow->events().draw, mainApp.get(), &ofApp::drawSecondWindow, OF_EVENT_ORDER_APP);
ofAddListener(secondWindow->events().update, mainApp.get(), &ofApp::updateSecondWindow,OF_EVENT_ORDER_APP);
ofRunApp(mainWindow, mainApp);
ofRunMainLoop();
}
// ofApp.h
#pragma once
#include "ofMain.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y);
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
void setupSecondWindow();
void drawSecondWindow(ofEventArgs & args);
void updateSecondWindow(ofEventArgs & args);
ofFbo fbo1;
ofFbo fbo2;
std::shared_ptr<ofBaseGLRenderer> renderer1, renderer2;
};
// ofApp.cpp
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup()
{
ofFbo::Settings settings1(renderer1);
ofFbo::Settings settings2(renderer2);
settings1.width = 180;
settings1.height = 100;
settings1.internalformat = GL_RGBA;
settings1.numSamples = ofFbo::maxSamples();
settings2.width = 180;
settings2.height = 100;
settings2.internalformat = GL_RGBA;
settings2.numSamples = ofFbo::maxSamples();
fbo1.allocate(settings1);
fbo2.allocate(settings2);
}
//--------------------------------------------------------------
void ofApp::update()
{
fbo1.begin();
{
ofClear(0,0,0);
ofBackground(ofColor::yellow);
ofSetColor(ofColor::red);
ofDrawBitmapString("Frame buffer object1", 10, 20);
// animate the cube
ofPushMatrix();
ofTranslate(90, 60);
ofRotate(sin(ofGetElapsedTimef()) * 180);
ofDrawRectangle(-25, -25, 50, 50);
ofPopMatrix();
}
fbo1.end();
}
//--------------------------------------------------------------
void ofApp::updateSecondWindow(ofEventArgs & args)
{
fbo2.begin();
{
ofClear(0,0,0);
ofBackground(ofColor::white);
ofSetColor(ofColor::green);
ofDrawBitmapString("Frame buffer object2", 10, 20);
// animate the cube
ofPushMatrix();
ofTranslate(90, 60);
ofRotate(sin(ofGetElapsedTimef()) * 180);
ofDrawRectangle(-25, -25, 50, 50);
ofPopMatrix();
}
fbo2.end();
}
//--------------------------------------------------------------
void ofApp::drawSecondWindow(ofEventArgs & args)
{
ofBackground(ofColor::black);
ofSetColor(ofColor::white);
ofDrawBitmapString("Second Window", 20, 20);
// draw the fbo
ofSetColor(ofColor::white);
fbo2.draw(30,30);
}
//--------------------------------------------------------------
void ofApp::draw()
{
ofBackground(ofColor::black);
ofSetColor(ofColor::white);
ofDrawBitmapString("First Window", 20, 20);
// draw the fbo
ofSetColor(ofColor::white);
fbo1.draw(30,30);
}
And here is the snapshot of the running programming:
The fbo2 in the second window is totally black.