Setup OpenGL on X11

独自空忆成欢 提交于 2019-12-23 01:37:24

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/12856820/setup-opengl-on-x11

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