title: How to create an OpenGL 3.3 or 4.x context through EGL
body: <p>I am interested in making an OpenGL app which doesn't depend on X11. As I have seen, this should be possible through EGL. There are even examples on the internet. But how can I control the context version? The sample code below creates an OpenGL context(on wayland) with version 2.1, but on my computer it shows that the highest OpenGL version supported is 3.3(such context can be created with glXCreateContextAttribsARB using GLX and xlib in the X server). So my question is: Can I somehow create an OpenGL context with higher version through EGL and if yes, how?</p>
  
  <p>Example code:</p>
  
  <pre><code>#include &lt;wayland-client.h&gt;
  #include &lt;wayland-egl.h&gt;
  #include &lt;EGL/egl.h&gt;
  #include &lt;GL/gl.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  
  #define WIDTH 256
  #define HEIGHT 256
  
  static struct wl_display *display;
  static struct wl_compositor *compositor = NULL;
  static struct wl_shell *shell = NULL;
  static EGLDisplay egl_display;
  static char running = 1;
  
  struct window {
      EGLContext egl_context;
      struct wl_surface *surface;
      struct wl_shell_surface *shell_surface;
      struct wl_egl_window *egl_window;
      EGLSurface egl_surface;
  };
  
  // listeners
  static void registry_add_object (void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) {
      if (!strcmp(interface,"wl_compositor")) {
          compositor = wl_registry_bind (registry, name, &amp;wl_compositor_interface, 0);
      }
      else if (!strcmp(interface,"wl_shell")) {
          shell = wl_registry_bind (registry, name, &amp;wl_shell_interface, 0);
      }
  }
  static void registry_remove_object (void *data, struct wl_registry *registry, uint32_t name) {
  
  }
  static struct wl_registry_listener registry_listener = {&amp;registry_add_object, &amp;registry_remove_object};
  
  static void shell_surface_ping (void *data, struct wl_shell_surface *shell_surface, uint32_t serial) {
      wl_shell_surface_pong (shell_surface, serial);
  }
  static void shell_surface_configure (void *data, struct wl_shell_surface *shell_surface, uint32_t edges, int32_t width, int32_t height) {
      struct window *window = data;
      wl_egl_window_resize (window-&gt;egl_window, width, height, 0, 0);
  }
  static void shell_surface_popup_done (void *data, struct wl_shell_surface *shell_surface) {
  
  }
  static struct wl_shell_surface_listener shell_surface_listener = {&amp;shell_surface_ping, &amp;shell_surface_configure, &amp;shell_surface_popup_done};
  
  static void create_window (struct window *window, int32_t width, int32_t height) {
      eglBindAPI (EGL_OPENGL_API);
      EGLint attributes[] = {
          EGL_RED_SIZE, 8,
          EGL_GREEN_SIZE, 8,
          EGL_BLUE_SIZE, 8,
      EGL_NONE};
      EGLConfig config;
      EGLint num_config;
      eglChooseConfig (egl_display, attributes, &amp;config, 1, &amp;num_config);
      window-&gt;egl_context = eglCreateContext (egl_display, config, EGL_NO_CONTEXT, NULL);
  
      window-&gt;surface = wl_compositor_create_surface (compositor);
      window-&gt;shell_surface = wl_shell_get_shell_surface (shell, window-&gt;surface);
      wl_shell_surface_add_listener (window-&gt;shell_surface, &amp;shell_surface_listener, window);
      wl_shell_surface_set_toplevel (window-&gt;shell_surface);
      window-&gt;egl_window = wl_egl_window_create (window-&gt;surface, width, height);
      window-&gt;egl_surface = eglCreateWindowSurface (egl_display, config, window-&gt;egl_window, NULL);
      eglMakeCurrent (egl_display, window-&gt;egl_surface, window-&gt;egl_surface, window-&gt;egl_context);
  }
  static void delete_window (struct window *window) {
      eglDestroySurface (egl_display, window-&gt;egl_surface);
      wl_egl_window_destroy (window-&gt;egl_window);
      wl_shell_surface_destroy (window-&gt;shell_surface);
      wl_surface_destroy (window-&gt;surface);
      eglDestroyContext (egl_display, window-&gt;egl_context);
  }
  static void draw_window (struct window *window) {
      glClearColor (0.0, 1.0, 0.0, 1.0);
      glClear (GL_COLOR_BUFFER_BIT);
  
      printf("%s\n", glGetString(GL_VERSION));
  
      eglSwapBuffers (egl_display, window-&gt;egl_surface);
  }
  
  int main () {
      display = wl_display_connect (NULL);
      struct wl_registry *registry = wl_display_get_registry (display);
      wl_registry_add_listener (registry, &amp;registry_listener, NULL);
      wl_display_dispatch (display);
  
      egl_display = eglGetDisplay (display);
      eglInitialize (egl_display, NULL, NULL);
  
      struct window window;
      create_window (&amp;window, WIDTH, HEIGHT);
  
      while (running) {
          wl_display_dispatch_pending (display);
          draw_window (&amp;window);
      }
  
      delete_window (&amp;window);
      eglTerminate (egl_display);
      wl_display_disconnect (display);
      return 0;
  }
  </code></pre>
  
  <p>Source: <a href="https://github.com/eyelash/tutorials" rel="nofollow noreferrer">https://github.com/eyelash/tutorials</a></p>