我曾尝试在一个小项目中使用glfw和glew,但我在使用glewInit()初始化glew时遇到了麻烦:
这是代码:
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;
}
问题是
glewInit()
返回 Unknown error
,这很奇怪。我试图找到问题的根源,它发生在 glxewInit
中,更具体地说,它从 GLEW_ERROR_NO_GLX_DISPLAY
行返回
if (display == NULL) return GLEW_ERROR_NO_GLX_DISPLAY;
另一个奇怪的事情是,当我停用 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();
}
你知道它来自哪里吗?我目前在Ubuntu上,我在自己的项目目录中构建了glew和glfw
我尝试了一切,但无法解决问题
我已经解决了这个问题,看来 Wayland Wyndow 系统尚未与 GLEW API 完全兼容,因此我已在 GLFW CMakeLists.txt 中停用 Wayland 支持。所以现在 GLFW 使用 x11 窗口系统并且它运行得很好。