简体   繁体   中英

Setup OpenGL on X11

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM