Setup GL context with multi window

I’ve been getting crazy trying to make shaders work after I upgraded to OF 0.9

Everything was compiling fine and there was no warnings on the console ouput but whatever shader I tried had no efect.
Finally I realized that it had to do with the way multi windows is setup.

This is my main.cpp

#include "ofApp.h"
#include "ofAppWebUi.h"

int main( ){
    ofGLFWWindowSettings settings;

    settings.width = 600;
    settings.height = 400;
    settings.position.x = 300;
    settings.position.y = 0;
    settings.resizable = true;
    shared_ptr<ofAppBaseWindow> mainWindow = ofCreateWindow(settings);
    //ofSetupOpenGL(mainWindow, 1024,500, OF_WINDOW); //this fails. See screenshot.
    
    settings.width = 300;
    settings.height = 400;
    settings.position.x = 0;
    settings.position.y = 0;
    settings.resizable = true;
    shared_ptr<ofAppBaseWindow> guiWindow = ofCreateWindow(settings);
    
    shared_ptr<ofApp> mainApp(new ofApp);
    shared_ptr<ofAppWebUi> secondApp(new ofAppWebUi);
    
    ofSetupOpenGL(guiWindow, 1024,500, OF_WINDOW);

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

I’ve seen that is possible to set the GL context by passing a window pointer, but then it ofMainLoop.h fails to compile.

=

Am I doing it right? Thanks!

Anyone?

“I’ve been getting crazy trying to make shaders work after I upgraded to OF 0.9”

Not directly related to multi window but, this sounds familiar to me, i wanted to use ofSetCurrentRenderer(ofGLProgrammableRenderer::TYPE); to make my shaders run but not work.

I found in a example of that fork a way to set the OpenGL version of the render and my shaders worked again, sorry but i don’t remember where i saw this

EDIT: i found what im talking about

not sure what’s the problem but when you use ofCreateWindow you shouldn’t be using ofSetupOpenGL. both do more or less the same but ofSetupOpenGL is the old syntax and is only tested with 1 window. there should be some kind of check internally so it throws an error or just ignores this second call but meanwhile, can you check if removing that call fixes the problem?

i think this was related to: https://github.com/openframeworks/openFrameworks/issues/3636 should be fixed now

Thanks @pandereto & @arturo.

If its a bug, still not fixed, but by creating the second window after the main ofApp setup() does the trick (as explained on the github link).

So, main.cpp end up looking like this:

#include "ofMain.h"
#include "ofApp.h"
    
int main( )
{
    ofGLFWWindowSettings settings;
    settings.width = 800;
    settings.height = 400;
    settings.position.x = 0;
    settings.position.y = 0;
    settings.resizable = true;
    shared_ptr<ofAppBaseWindow> mainWindow = ofCreateWindow(settings);

    shared_ptr<ofApp> mainApp(new ofApp);
    ofRunApp(mainWindow, mainApp);
    ofRunMainLoop();
}

And ofApp.cpp:

#include "ofApp.h"

void ofApp::setup()
{
    //setup everything else first...

    ofGLFWWindowSettings settings;
    settings.width = 300;
    settings.height = 400;
    settings.position.x = 0;
    settings.position.y = 0;
    settings.resizable = true;
    shared_ptr<ofAppBaseWindow> guiWindow = ofCreateWindow(settings);    
    shared_ptr<ofAppWebUi> secondApp(new ofAppWebUi);
    ofRunApp(guiWindow, secondApp);
}
   
 ...

mmh can you post a minimal example that shows the error?

Sure! I’m using [gl/shaderExample][1] just changing main.cpp (based on the [events/MultiWindowExample][2])

This makes mainWindow ignore the shader:

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

int main( ){
    ofGLFWWindowSettings settings;
    
    settings.width = 600;
    settings.height = 600;
    settings.position.x = 300;
    settings.resizable = true;
    shared_ptr<ofAppBaseWindow> mainWindow = ofCreateWindow(settings);
    
    settings.width = 300;
    settings.height = 300;
    settings.position.x = 0;
    settings.resizable = false;
    shared_ptr<ofAppBaseWindow> guiWindow = ofCreateWindow(settings);
    
    shared_ptr<ofApp> mainApp(new ofApp);
    shared_ptr<ofApp> guiApp(new ofApp);
    
    ofRunApp(guiWindow, guiApp);
    ofRunApp(mainWindow, mainApp);
    ofRunMainLoop();  
}

This works just fine:

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

int main( ){
    ofGLFWWindowSettings settings;
    
    settings.width = 600;
    settings.height = 600;
    settings.position.x = 300;
    settings.resizable = true;
    shared_ptr<ofAppBaseWindow> mainWindow = ofCreateWindow(settings);
    shared_ptr<ofApp> mainApp(new ofApp);
    ofRunApp(mainWindow, mainApp);
    
    settings.width = 300;
    settings.height = 300;
    settings.position.x = 0;
    settings.resizable = false;
    shared_ptr<ofAppBaseWindow> guiWindow = ofCreateWindow(settings);
    shared_ptr<ofApp> guiApp(new ofApp);
    ofRunApp(guiWindow, guiApp);
    
    ofRunMainLoop();
}

Also, not sure if related but I’ve got inconsitencies when getCurrentWindow() is called. Specifically calling ofSetFullscreen(), the wrong window is set.
[1]: https://github.com/openframeworks/openFrameworks/tree/master/examples/gl/shaderExample
[2]: https://github.com/openframeworks/openFrameworks/blob/master/examples/events/multiWindowExample/src/main.cpp

mmh, it’s working for me without problem are you sure you have the latest changes from master?

You’re right. My sincere apologies :sweat:
Was pulling from the wrong branch… So, no new commits were shown.

Hello,

Can I ask how did you solve it please? I have the latest oF0.9.2 but my shader is not working as well…

You might find my main.cpp as followed:

#include "ofMain.h"
#include "ofApp.h"
#include "projection.h"   // second window
#include "ofAppGLFWWindow.h"

//========================================================================
int main( ){
    ofGLFWWindowSettings settings;
    
    settings.width = 1200;
    settings.height = 748;
    settings.setPosition(ofVec2f(300,0));
//    settings.setGLVersion(4,3);     // this creates error
    settings.resizable = true;
    shared_ptr<ofAppBaseWindow> mainWindow = ofCreateWindow(settings);
    
    settings.width = 1200;
    settings.height = 748;
    settings.setPosition(ofVec2f(0,0));
    settings.resizable = false;
    shared_ptr<ofAppBaseWindow> projectionWindow = ofCreateWindow(settings);

/*
    //  shader is working on projectionApp and the interface of projectionApp looks the same as mainApp
        shared_ptr<ofApp> mainApp(new ofApp);
        shared_ptr<ofApp> projectionApp(new ofApp);
    */
        
    //  windows look the same as I want but shader is not drawing on mainApp
        shared_ptr<ofApp> mainApp(new ofApp);
        shared_ptr<projection> projectionApp(new projection);

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

It will be lovely if you could tell me how did you solve it please~

Cheers,
Karen

Sorry but I have solved it with adding .shareContextWith, and also a change of sequence in my code as below:

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

//========================================================================
int main( ){
    ofGLFWWindowSettings settings;

    settings.width = 1200;
    settings.height = 748;
    settings.setPosition(ofVec2f(0,0));
    settings.resizable = false;
    shared_ptr<ofAppBaseWindow> projectionWindow = ofCreateWindow(settings);
    
    settings.width = 1200;
    settings.height = 748;
    settings.setPosition(ofVec2f(300,0));
    settings.resizable = true;
        settings.shareContextWith = projectionWindow;
    shared_ptr<ofAppBaseWindow> mainWindow = ofCreateWindow(settings);
    
    
//  windows look the same as I want but shader is not drawing on mainApp
    shared_ptr<ofApp> mainApp(new ofApp);
    shared_ptr<projection> projectionApp(new projection);

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