1

I have an Window identifier for X11 Window. I didn't setup this window, I just can get its id (and I suppose, visual id). How can I setup OpenGL context for this window?

In particular, I want to use glXMakeCurrent, but this function receives Display and GLXContext objects. I can create context using glXCreateContext(display, vi, 0, GL_TRUE); but again I need in Display and XVisualInfo objects.

genpfault
  • 51,148
  • 11
  • 85
  • 139
flamingo
  • 496
  • 5
  • 14

2 Answers2

3

This code fully creates a window, and initializes the opengl context.

Display *dpy( XOpenDisplay( NULL );
int screen = XDefaultScreen( dpy );
const int fbCfgAttribslist[] =
        {
            GLX_RENDER_TYPE, GLX_RGBA_BIT,
            GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
            None
        };
int nElements = 0;
GLXFBConfig * glxfbCfg = glXChooseFBConfig( dpy,
                                      screen,
                                      fbCfgAttribslist,
                                      & nElements );

const int pfbCfg[] =
        {
            GLX_PBUFFER_WIDTH, WINDOW_WIDTH,
            GLX_PBUFFER_HEIGHT, WINDOW_HEIGHT,
            GLX_PRESERVED_CONTENTS, True,
            GLX_LARGEST_PBUFFER, False,
            None
        };
GLXPbuffer pBufferId = glXCreatePbuffer( dpy, glxfbCfg[ 0 ], pfbCfg );


XVisualInfo * visInfo = glXGetVisualFromFBConfig( dpy, glxfbCfg[ 0 ] );

GLXContext  glCtx = glXCreateContext( dpy, visInfo, NULL, True );


glXMakeContextCurrent( dpy,
                       pBufferId,
                       pBufferId,
                       glCtx );

Actually this is setting up a pbuffer, not a window, but in your case, since you got window created, and visuals set, you can just skip the first part, and go on with the opengl context creation.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
2

Setting up a OpenGL context on a X11 window is covered by one of my code examples (based upon an example by FTB/fungus), with the notable difference that is uses FBConfig over a regular Visual.

https://github.com/datenwolf/codesamples/blob/master/samples/OpenGL/x11argb_opengl/x11argb_opengl.c

That you're given only a Window, but not a Display is a problem. You can't just go around assume the window being on the default display, so whereever you get that Window from, it also must give you the corresponding Display connection as well. Just passing around Window IDs is a failure in API design, when it comes to X11.

datenwolf
  • 159,371
  • 13
  • 185
  • 298