cmakemakefileglfwglew

How to set up glew with glfw : trouble with GLEW_ERROR_NO_GLX_DISPLAY?


I have tried to use glfw and glew for a small project but I have trouble to initialize glew using glewInit():

Here is the Code:

bool WindowGLFW::initialize() {
    // Initialize GLFW
    if (!glfwInit()) {
      std::string msg = std::string("GLFW library could not be initialized \n");
      LOG_ON_MAIN_LOG(MESSAGE_STATUS_BAD(msg));
      return false;
    }

    _width = _glfwParameters.width;
    _height = _glfwParameters.height;
    _ratioNumer = _glfwParameters.width;
    _ratioDenom = _glfwParameters.width;
    _nameWindow = _glfwParameters.width;

    /*set the Windows hint informations*/
    glfwWindowHint(GLFW_RESIZABLE, _glfwParameters.resizable);
    glfwWindowHint(GLFW_VISIBLE, _glfwParameters.visible);
    glfwWindowHint(GLFW_DECORATED, _glfwParameters.decorated);
    glfwWindowHint(GLFW_FOCUSED, _glfwParameters.focused);
    glfwWindowHint(GLFW_AUTO_ICONIFY, _glfwParameters.auto_iconify);
    glfwWindowHint(GLFW_FLOATING, _glfwParameters.floating);
    glfwWindowHint(GLFW_MAXIMIZED, _glfwParameters.maximized);
    glfwWindowHint(GLFW_CENTER_CURSOR, _glfwParameters.center_cursor);
    glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, _glfwParameters.transparent_framebuffer);
    glfwWindowHint(GLFW_FOCUS_ON_SHOW, _glfwParameters.focus_on_show);
    glfwWindowHint(GLFW_SCALE_TO_MONITOR, _glfwParameters.scale_to_monitor);

    /*set Framebuffer hint information*/
    glfwWindowHint(GLFW_RED_BITS, _glfwParameters.red_bits);
    glfwWindowHint(GLFW_GREEN_BITS, _glfwParameters.green_bits);
    glfwWindowHint(GLFW_BLUE_BITS, _glfwParameters.blue_bits);
    glfwWindowHint(GLFW_ALPHA_BITS, _glfwParameters.alpha_bits);
    glfwWindowHint(GLFW_DEPTH_BITS, _glfwParameters.depth_bits);
    glfwWindowHint(GLFW_ACCUM_RED_BITS, _glfwParameters.accum_red_bits);
    glfwWindowHint(GLFW_ACCUM_GREEN_BITS, _glfwParameters.accum_green_bits);
    glfwWindowHint(GLFW_ACCUM_BLUE_BITS, _glfwParameters.accum_blue_bits);
    glfwWindowHint(GLFW_ACCUM_ALPHA_BITS, _glfwParameters.accum_alpha_bits);
    glfwWindowHint(GLFW_AUX_BUFFERS, _glfwParameters.aux_buffers);
    glfwWindowHint(GLFW_SAMPLES, _glfwParameters.samples);
    glfwWindowHint(GLFW_SRGB_CAPABLE, _glfwParameters.srgb_capable);
    glfwWindowHint(GLFW_STENCIL_BITS, _glfwParameters.stencil_bits);

    /*set Monitor hint information*/
    glfwWindowHint(GLFW_REFRESH_RATE, _glfwParameters.refresh_rate);

    /*set Context hint information*/
    glfwWindowHint(GLFW_CLIENT_API, _glfwParameters.client_api);
    glfwWindowHint(GLFW_CONTEXT_CREATION_API, _glfwParameters.context_creation_api);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, _glfwParameters.context_version_major);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, _glfwParameters.context_version_minor);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, _glfwParameters.context_forward_compat);
    glfwWindowHint(GLFW_OPENGL_PROFILE, _glfwParameters.opengl_profile);
    glfwWindowHint(GLFW_CONTEXT_RELEASE_BEHAVIOR, _glfwParameters.context_release_behaviour);
    glfwWindowHint(GLFW_CONTEXT_NO_ERROR, _glfwParameters.context_no_error);
    glfwWindowHint(GLFW_CONTEXT_ROBUSTNESS, _glfwParameters.opengl_robustness);
    glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, _glfwParameters.opengl_debug_context);

    //probably should switch to c++14 to use make_unique...whatever
    _window = std::unique_ptr<GLFWwindow, customGLFWWindowDeletor>( glfwCreateWindow(_width, _height, _nameWindow.c_str(),  glfwGetPrimaryMonitor(), NULL));
    if(!_window.get()) {
      /* think about maybe changing this and the other
       * occurrence of a manager. To have dissociation between
       * managers and actual code (separate manager and current code)
       */
      std::string msg = std::string("window can not be initialized \n");
      LOG_ON_MAIN_LOG(MESSAGE_STATUS_BAD(msg));
      glfwTerminate();
      return false;
    }
    /* Make the window's context current */
    glfwMakeContextCurrent(_window.get());
    std::string msg = std::string("GLFW window was created/initialized ! ");
    LOG_ON_MAIN_LOG(MESSAGE_STATUS_GOOD(msg));

    //Initialize GLEW
    if(_glfwParameters.client_api == GLFW_OPENGL_API) {
      glewExperimental=GL_TRUE;
      GLenum err = glewInit();
      if (err != GLEW_OK) {
        /* Need to convert uc_msg to i32 so uc_msg will be output as ainteger
         * and not a character then string for the MACRO
         */
         std::cout << glewGetErrorString(err) << std::endl;
        std::cout << glewGetString(GLEW_VERSION) << std::endl;
        const unsigned char* uc_msg = glewGetErrorString(err);
        std::string msg = std::string(reinterpret_cast<const char*>(uc_msg));
        LOG_ON_MAIN_LOG(MESSAGE_STATUS_BAD(msg));
        glfwTerminate();
        return -1;
      }
    }


    setCallbacks();

    return true;
  }

the problem is that glewInit() returns Unknown error which is weird. I have tried to find where the problem comes from and it happens in the glxewInit and more particularly it returns GLEW_ERROR_NO_GLX_DISPLAY from the line if (display == NULL) return GLEW_ERROR_NO_GLX_DISPLAY;

Another weird thing is that the windows works perfectly when I desactivate glewInit():

while (!glfwWindowShouldClose( _window->getWindow().get() ) ) {
        /* Render here */
        /* Set the clear color to black (0, 0, 0, 1) using OpenGL */
        glClearColor(0.4f, 0.4f, 0.4f, 0.5f);
        /* Clear the color buffer */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        _window->swapBuffer();

        /* Poll for and process events */
        _window->poolEvents();
    }

Do you have some idea where it came from? I am currently on Ubuntu, and I built glew and glfw on my own project directory

I tried everything I could, but I could not fix the problem


Solution

  • I have resolve the problem, it seems that Wayland Wyndow system is not yet fully compatible with GLEW API so I have deactivate Wayland support in the GLFW CMakeLists.txt. So now GLFW uses the x11 Window System and it works perfectly.