OpenGL EGL eglGetDisplay 保持 return EGL 错误 0x3008(EGL_BAD_DISPLAY)

OpenGL EGL eglGetDisplay keeps return EGL error 0x3008(EGL_BAD_DISPLAY )

我的ubuntu版本是16.04,我先安装了mesa-common-dev, libgl1-mesa-dev, libglm-dev, libegl1-mesa-dev.然后安装了NVIDIA-Linux-x86_64-440.64.run支持opengl。

但是当我尝试 运行 玩具示例时,我不断收到此错误 main: Assertion display != EGL_NO_DISPLAY failed

/* Compile with gcc -g3 -o example example.c -lX11 -lEGL */

#include <assert.h>
#include <stdio.h>
#include <EGL/egl.h>
#include <EGL/eglplatform.h>

void printEGLError();

int main(void) {
  Display* x_display = XOpenDisplay(NULL);
  EGLDisplay display = eglGetDisplay(x_display);
  // EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  assert(display != EGL_NO_DISPLAY);

  EGLint major, minor;
  eglInitialize(display, &major, &minor);
  char *string = eglQueryString(display, EGL_CLIENT_APIS);
  assert(string);

  printf("%s\n", string);

  return 0;
}

/* Use printEGLError to show a description of the last EGL Error.
  The descriptions are taken from the eglGetError manual */
#define ERROR_DESC(...) fprintf(stderr, "%s\n", __VA_ARGS__); break

void printEGLError() {

  switch(eglGetError()) {

  case(EGL_SUCCESS):
    ERROR_DESC("The last function succeeded without error.");
  case(EGL_NOT_INITIALIZED):
    ERROR_DESC("EGL is not initialized, or could not be initialized, for the specified EGL display connection.");
  case(EGL_BAD_ACCESS):
    ERROR_DESC("EGL cannot access a requested resource (for example a context is bound in another thread).");
  case(EGL_BAD_ALLOC):
    ERROR_DESC("EGL failed to allocate resources for the requested operation.");
  case(EGL_BAD_ATTRIBUTE):
    ERROR_DESC("An unrecognized attribute or attribute value was passed in the attribute list.");
  case(EGL_BAD_CONTEXT):
    ERROR_DESC("An EGLContext argument does not name a valid EGL rendering context.");
  case(EGL_BAD_CONFIG):
    ERROR_DESC("An EGLConfig argument does not name a valid EGL frame buffer configuration.");
  case(EGL_BAD_CURRENT_SURFACE):
    ERROR_DESC("The current surface of the calling thread is a window, pixel buffer or pixmap that is no longer valid.");
  case(EGL_BAD_DISPLAY):
    ERROR_DESC("An EGLDisplay argument does not name a valid EGL display connection.");
  case(EGL_BAD_SURFACE):
    ERROR_DESC("An EGLSurface argument does not name a valid surface (window, pixel buffer or pixmap) configured for GL rendering.");
  case(EGL_BAD_MATCH):
    ERROR_DESC("Arguments are inconsistent (for example, a valid context requires buffers not supplied by a valid surface).");
  case(EGL_BAD_PARAMETER):
    ERROR_DESC("One or more argument values are invalid.");
  case(EGL_BAD_NATIVE_PIXMAP):
    ERROR_DESC("A NativePixmapType argument does not refer to a valid native pixmap.");
  case(EGL_BAD_NATIVE_WINDOW):
    ERROR_DESC("A NativeWindowType argument does not refer to a valid native window.");
  case(EGL_CONTEXT_LOST):
    ERROR_DESC("A power management event has occurred. The application must destroy all contexts and reinitialise OpenGL ES state and objects to continue rendering. ");
  }
}

更多信息:我的显卡是 Titan Xp,我尝试 运行 sudo servide lightdm stop 并删除了所有可能的远程桌面软件。但问题仍然存在。有人可以帮忙吗?

对于可能对这个问题感到困惑的人,只需unset DISPLAY。这可能会节省您的时间。