Setup OpenGL on X11

a 夏天 提交于 2019-12-07 09:52:35

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!