C 如何创建用于EGL的本机X11窗口

C 如何创建用于EGL的本机X11窗口,c,opengl,x11,egl,C,Opengl,X11,Egl,如何创建在EGL中工作的本机X11窗口?在阅读eglIntro()时,几乎没有关于这个问题的文档。或者,有没有办法通过EGL本身创建本机窗口?我假设可以使用一个EGLNativeWindowType来代替X11的原生窗口类型。否,EGL本身不提供Xlib包装。你必须自己创建窗口 这里有一个简单的例子,让你开始。它指的是GLES2,但也应该与GLES1一起使用 首先,声明Xlib对象(显示和窗口) 最后,在main()例程中创建并打开display/Xwindow int main() {

如何创建在EGL中工作的本机X11窗口?在阅读eglIntro()时,几乎没有关于这个问题的文档。或者,有没有办法通过EGL本身创建本机窗口?我假设可以使用一个
EGLNativeWindowType
来代替X11的原生窗口类型。

否,EGL本身不提供Xlib包装。你必须自己创建窗口

这里有一个简单的例子,让你开始。它指的是GLES2,但也应该与GLES1一起使用

首先,声明Xlib对象(显示和窗口)

最后,在main()例程中创建并打开display/Xwindow

int main()
{
    int egl_error;

    Window root;
    XSetWindowAttributes  swa;

    /* open standard display (primary screen) */
    xdisplay = XOpenDisplay ( NULL );   
    if ( xdisplay == NULL ) {
        printf("Error opening X display\n");
        return 0;
    }
    // get the root window (usually the whole screen)
    root  =  DefaultRootWindow( shell->xdisplay );

    // list all events this window accepts
    swa.event_mask =
    StructureNotifyMask |
    ExposureMask        |
    PointerMotionMask   |
    KeyPressMask        |
    KeyReleaseMask      |
    ButtonPressMask     |
    ButtonReleaseMask;

    // Xlib's window creation
    win  =  XCreateWindow (
        xdisplay, root, 0, 0, 640, 480,   0,
        CopyFromParent, InputOutput, CopyFromParent, CWEventMask,
       &swa );

    XMapWindow ( xdisplay , win );         // make window visible
    XStoreName ( xdisplay , win , "EGL" );
打开显示器后,将创建并显示该窗口

最后,在main()例程中创建并打开display/Xwindow

int main()
{
    int egl_error;

    Window root;
    XSetWindowAttributes  swa;

    /* open standard display (primary screen) */
    xdisplay = XOpenDisplay ( NULL );   
    if ( xdisplay == NULL ) {
        printf("Error opening X display\n");
        return 0;
    }
    // get the root window (usually the whole screen)
    root  =  DefaultRootWindow( shell->xdisplay );

    // list all events this window accepts
    swa.event_mask =
    StructureNotifyMask |
    ExposureMask        |
    PointerMotionMask   |
    KeyPressMask        |
    KeyReleaseMask      |
    ButtonPressMask     |
    ButtonReleaseMask;

    // Xlib's window creation
    win  =  XCreateWindow (
        xdisplay, root, 0, 0, 640, 480,   0,
        CopyFromParent, InputOutput, CopyFromParent, CWEventMask,
       &swa );

    XMapWindow ( xdisplay , win );         // make window visible
    XStoreName ( xdisplay , win , "EGL" );
当您打开窗口时,初始化EGL

    egl_error = init_egl();
    if (!egl_error) {
        return 1;
    }
一旦拥有EGL&Xlib对象,就可以启动事件处理循环

while (1) {
        int keycode;
        XEvent  xev;

        if ( XPending ( xdisplay ) )
            if (XCheckWindowEvent(shell->xdisplay, shell->win, global_event_mask, &xev))
                process_xevent(shell, xev);

       /* if (should_exit) { break; }   // set some global flag if you want to exit */

       eglMakeCurrent( egl_display, egl_surface, egl_surface, egl_context );

       /* Call OpenGL as you see fit */

       /* get rendered buffer to the screen */
       eglSwapBuffers ( egl_display, egl_surface );
   }


   // deinitialize
}
这应该让你开始。代码是从一个更大的项目中提取出来的,因此在删除不相关的内容时可能会引入拼写错误

为了总结答案,更准确地说,这里的
EGLNativeWindowType
专用于
X11/Xlib.h中的
Window
标题和
EGLNativeDisplayType
Display*


更简单的方法可能是使用
libxcb
,但我没有任何经过测试的示例代码。可以作为依赖操作系统的OpenGL上下文创建例程的有用来源。

谢谢。稍加调整,这为我提供了一个标准的黑色窗口。再稍微调整一下,我就可以将它与我的项目集成。glClearColor+glClear组合提供一个标准的非黑色窗口作为下一个测试)