How to initialize OpenGL with glXChooseFBConfig and ctypes module?
<p>I want to make some simple OpenGL animation on my tkinter window. I dont want to include any needless dependencies so I'm writing everything from scratch with ctypes. So far I have this:</p>

<pre><code>#!/usr/bin/env python3

import tkinter
import ctypes
from ctypes import cdll

GLX_PIXMAP_BIT = 0x00000002
GLX_WINDOW_BIT = 0x00000001
GLX_PBUFFER_BIT = 0x00000004
GLX_RGBA_BIT = 0x00000001
GLX_COLOR_INDEX_BIT = 0x00000002
GLX_PBUFFER_CLOBBER_MASK = 0x08000000
GLX_FRONT_LEFT_BUFFER_BIT = 0x00000001
GLX_FRONT_RIGHT_BUFFER_BIT = 0x00000002
GLX_BACK_LEFT_BUFFER_BIT = 0x00000004
GLX_BACK_RIGHT_BUFFER_BIT = 0x00000008
GLX_AUX_BUFFERS_BIT = 0x00000010
GLX_DEPTH_BUFFER_BIT = 0x00000020
GLX_STENCIL_BUFFER_BIT = 0x00000040
GLX_ACCUM_BUFFER_BIT = 0x00000080
GLX_CONFIG_CAVEAT = 0x20
GLX_X_VISUAL_TYPE = 0x22
GLX_TRANSPARENT_TYPE = 0x23
GLX_TRANSPARENT_INDEX_VALUE = 0x24
GLX_TRANSPARENT_RED_VALUE = 0x25
GLX_TRANSPARENT_GREEN_VALUE = 0x26
GLX_TRANSPARENT_BLUE_VALUE = 0x27
GLX_TRANSPARENT_ALPHA_VALUE = 0x28
GLX_DONT_CARE = 0xFFFFFFFF
GLX_NONE = 0x8000
GLX_SLOW_CONFIG = 0x8001
GLX_TRUE_COLOR = 0x8002
GLX_DIRECT_COLOR = 0x8003
GLX_PSEUDO_COLOR = 0x8004
GLX_STATIC_COLOR = 0x8005
GLX_GRAY_SCALE = 0x8006
GLX_STATIC_GRAY = 0x8007
GLX_TRANSPARENT_RGB = 0x8008
GLX_TRANSPARENT_INDEX = 0x8009
GLX_VISUAL_ID = 0x800B
GLX_SCREEN = 0x800C
GLX_NON_CONFORMANT_CONFIG = 0x800D
GLX_DRAWABLE_TYPE = 0x8010
GLX_RENDER_TYPE = 0x8011
GLX_X_RENDERABLE = 0x8012
GLX_FBCONFIG_ID = 0x8013
GLX_RGBA_TYPE = 0x8014
GLX_COLOR_INDEX_TYPE = 0x8015
GLX_MAX_PBUFFER_WIDTH = 0x8016
GLX_MAX_PBUFFER_HEIGHT = 0x8017
GLX_MAX_PBUFFER_PIXELS = 0x8018
GLX_PRESERVED_CONTENTS = 0x801B
GLX_LARGEST_PBUFFER = 0x801C
GLX_WIDTH = 0x801D
GLX_HEIGHT = 0x801E
GLX_EVENT_MASK = 0x801F
GLX_DAMAGED = 0x8020
GLX_SAVED = 0x8021
GLX_WINDOW = 0x8022
GLX_PBUFFER = 0x8023
GLX_PBUFFER_HEIGHT = 0x8040
GLX_PBUFFER_WIDTH = 0x8041
GLX_ACCUM_ALPHA_SIZE = 17
GLX_ACCUM_BLUE_SIZE = 16
GLX_ACCUM_GREEN_SIZE = 15
GLX_ACCUM_RED_SIZE = 14
GLX_ALPHA_SIZE = 11
GLX_AUX_BUFFERS = 7
GLX_BAD_ATTRIBUTE = 2
GLX_BAD_CONTEXT = 5
GLX_BAD_ENUM = 7
GLX_BAD_SCREEN = 1
GLX_BAD_VALUE = 6
GLX_BAD_VISUAL = 4
GLX_BLUE_SIZE = 10
GLX_BUFFER_SIZE = 2
GLX_BufferSwapComplete = 1
GLX_DEPTH_SIZE = 12
GLX_DOUBLEBUFFER = 5
GLX_GREEN_SIZE = 9
GLX_LEVEL = 3
GLX_NO_EXTENSION = 3
GLX_PbufferClobber = 0
GLX_RED_SIZE = 8
GLX_RGBA = 4
GLX_STENCIL_SIZE = 13
GLX_STEREO = 6
GLX_USE_GL = 1


class OpenGLView(tkinter.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._x_window_id = self.winfo_id()
        self._init_gl()

    def _init_gl(self):
        self._xlib = cdll.LoadLibrary('libX11.so')
        self._gl = cdll.LoadLibrary('libGL.so')
        self._glx = cdll.LoadLibrary('libGLU.so')
        self._x_display = self._xlib.XOpenDisplay()
        elements = [
            GLX_X_RENDERABLE, 1,
            GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
            GLX_RENDER_TYPE, GLX_RGBA_BIT,
            GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
            GLX_DOUBLEBUFFER, 1,
            GLX_RED_SIZE, 8,
            GLX_GREEN_SIZE, 8,
            GLX_BLUE_SIZE, 8,
            GLX_ALPHA_SIZE, 8,
            GLX_DEPTH_SIZE, 24,
            GLX_STENCIL_SIZE, 8,
            0
        ]
        elements = (ctypes.c_int * len(elements))(*elements)
        gl_configs = self._glx.glXChooseFBConfig(self._x_display, 0, ctypes.byref(elements), ctypes.sizeof(elements))
        context = self._glx.glXCreateNewContext(self._x_display, gl_configs[0], self._glx.GLX_RGBA_TYPE, None, True)
        self._glx.glXMakeContextCurrent(self._x_display, self._x_window_id, self._x_window_id, context)

tk = tkinter.Tk()
v = OpenGLView(tk)
v.pack(fill=tkinter.BOTH, expand=True)
tk.mainloop()
</code></pre>

<p>Why this piece of code generates this error?</p>

<pre><code>libGL error: No matching fbConfigs or visuals found
libGL error: failed to load driver: swrast
Segmentation fault (core dumped)
</code></pre>

<p>After selecting a FBConfig you also have to select a window visual that matches that FBConfig to make sure that this FBConfig can in fact be used for the window you're trying to attach do. Also the Window must have set its visual/FBConfig to that format so that you can make current the OpenGL context on it.</p>

<p>I have an <a href="https://github.com/datenwolf/codesamples/blob/master/samples/OpenGL/x11argb_opengl/x11argb_opengl.c" rel="nofollow">example on how to do this in C</a> in my <code>codesamples</code> repository over at GitHub. Feel free to use that as a template. There's also an alongside example that shows how to use GLX alongside Xcb (the example you can find over at the Xcb homepage is broken).</p>

python opengl ctypes glx