Search code examples
opengl-esopengl-es-2.0x11xlibegl

How to create a window and fill it with color using OpenES 2.0 + X11?


I googled as hard as I can, but I found nothing.

What I want to do:

  • create a window with X11 (Xlib) and show it
  • fill the window with color using OpenGL ES 2.0

For OpenGL ES 2.0 support on my ArchLinux, I use MESA. I know how to create a simple X window using Xlib, I have a basic knowledge of EGL and OpenGL ES, but I can't understand how to use all them (X11 + EGL + OpenGL ES 2.0) in conjuction.

I would be very thakful if someone wrote at least a short code example on how to prepare a X window and connect it with OpenGL ES 2.0 correctly and start rendering.


Solution

  • Create Window:

    Window root;
    XSetWindowAttributes swa;
    XSetWindowAttributes  xattr;
    Atom wm_state;
    XWMHints hints;
    XEvent xev;
    EGLConfig ecfg;
    EGLint num_config;
    Window win;
    
    /*
     * X11 native display initialization
     */
    
    x_display = XOpenDisplay(NULL);
    if ( x_display == NULL )
    {
        return EGL_FALSE;
    }
    
    root = DefaultRootWindow(x_display);
    
    swa.event_mask  =  ExposureMask | PointerMotionMask | KeyPressMask;
    win = XCreateWindow(
               x_display, root,
               0, 0, esContext->width, esContext->height, 0,
               CopyFromParent, InputOutput,
               CopyFromParent, CWEventMask,
               &swa );
    
    xattr.override_redirect = FALSE;
    XChangeWindowAttributes ( x_display, win, CWOverrideRedirect, &xattr );
    
    hints.input = TRUE;
    hints.flags = InputHint;
    XSetWMHints(x_display, win, &hints);
    
    // make the window visible on the screen
    XMapWindow (x_display, win);
    XStoreName (x_display, win, title);
    
    // get identifiers for the provided atom name strings
    wm_state = XInternAtom (x_display, "_NET_WM_STATE", FALSE);
    
    memset ( &xev, 0, sizeof(xev) );
    xev.type                 = ClientMessage;
    xev.xclient.window       = win;
    xev.xclient.message_type = wm_state;
    xev.xclient.format       = 32;
    xev.xclient.data.l[0]    = 1;
    xev.xclient.data.l[1]    = FALSE;
    XSendEvent (
       x_display,
       DefaultRootWindow ( x_display ),
       FALSE,
       SubstructureNotifyMask,
       &xev );
    

    Set color:

       glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
       // Set the viewport
       glViewport ( 0, 0, esContext->width, esContext->height );
    
       // Clear the color buffer
       glClear ( GL_COLOR_BUFFER_BIT );
    

    Sources: