Multiple-Windows-Single-OpenGL-Context

Hello,
I am a beginner with OpenGl Context stuff. Working on a project where there is multiple opengl context. I started with a sample code to handle the contexts.There are two different windows First drawing a GREEN quad and the second drawing a RED Quad, I am able to switch,make cureent the context but i am only getting the GREEN Quad.
Also can someone tell basically how to handle the opengl contexts,using multiple context in same Application,multiple context in same window…


#include <GL/glut.h>
#include <GL/glx.h>
#include <unistd.h>
#include <iostream>


#define PRINT(x) std::cout << x << std::endl;

void keyPressed1(unsigned char key, int x, int y) 
{
    PRINT("Key pressed in window 1")
}

void keyPressed2(unsigned char key, int x, int y)
 {
    PRINT("Key pressed in window 2")
}

unsigned int NewWindow(const char* name,int X,int Y,int W,int H)
{
    glutInitWindowPosition(X, Y);
    glutInitWindowSize(W, H);

    glutCreateWindow(name);
    
    GLXDrawable window = glXGetCurrentDrawable();
    std::cout<<"---------------------------------"<<std::endl;
    PRINT("window created has ID: " << window)
    PRINT("current glX drawable is " << glXGetCurrentDrawable());
    PRINT("current glX context is " << glXGetCurrentContext());
    std::cout<<"---------------------------------"<<std::endl;
    return window;

}

int main(int argc, char ** argv) {

    // Create GL context
    int sngBuf[] = { GLX_RGBA,
                     GLX_RED_SIZE, 1,
                     GLX_GREEN_SIZE, 1,
                     GLX_BLUE_SIZE, 1,
                     GLX_DEPTH_SIZE, 12,
                     None
    };
    Display * display = XOpenDisplay(0);
    XVisualInfo* vi = glXChooseVisual(display, DefaultScreen(display), sngBuf);
    GLXContext glContext = glXCreateContext(display, vi, 0, GL_TRUE);
    PRINT("mannual context created glX is " << glContext);

    // Create two windows
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    
    GLXDrawable window1 = NewWindow("First",100,100,400,400);
    PRINT("window 1 has ID: " << window1)
    glutKeyboardFunc(keyPressed1);

    GLXDrawable window2 = NewWindow("Second",800,100,400,400);
    PRINT("window 2 has ID: " << window2)
    glutKeyboardFunc(keyPressed2);

   
    // Render to window 1
    bool success = glXMakeCurrent(display,window1,glContext);
    if(!success)
        PRINT("failed to switch to window 1");
    PRINT("current glX drawable is " << glXGetCurrentDrawable());
    PRINT("current glX context is " << glXGetCurrentContext());

    // Draw green quad
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_QUADS);
    glColor3f(0.0f,1.0f,0.0f);
    glVertex3f(-0.5f, 0.5f, 0.0f);
    glVertex3f( 0.5f, 0.5f, 0.0f);
    glVertex3f( 0.5f,-0.5f, 0.0f);
    glVertex3f(-0.5f,-0.5f, 0.0f);
    glEnd();
    glFlush();
    glFinish(); // necessary


    // Render to window 2
    success = glXMakeCurrent(display,window2,glContext);
    if(!success)
        PRINT("failed to switch to window 2");
    PRINT("current glX drawable is " << glXGetCurrentDrawable());
    PRINT("current glX context is " << glXGetCurrentContext());
    // Draw red quad
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_QUADS);
    glColor3f(1.0f,0.0f,0.0f);
    glVertex3f(-0.5f, 0.5f, 0.0f);
    glVertex3f( 0.5f, 0.5f, 0.0f);
    glVertex3f( 0.5f,-0.5f, 0.0f);
    glVertex3f(-0.5f,-0.5f, 0.0f);
    glEnd();
    glFlush();
    glFinish(); // necessary


    glutMainLoop();
}

Why do you want to use multiple OpenGL contexts?

(Before we dive in, it’s probably worth verifying that you’re not laboring under a false expectation.)

Actually i am working on a project where i am using Qt for windowing management( using QOpenGLWidget ),(Just like we use GLUT/GLFW/etc for window creation…) Now the point is, there is multiple instances of the window creating object, which is in turn creating multiple opengl contexts, so i need to handle them…(basically i have you implement that in Qt opengl, but i started with GLUT and Glx, because The Qt OpenGL module is implemented as a platform-independent Qt/C++ wrapper around the platform-dependent GLX)

You might check out:

It doesn’t sound like you have to use multiple contexts. Just set Qt::AA_ShareOpenGLContexts.