c – 使用GLFW3创建OpenGL 4.3窗口失败

我设置了一个最小的应用程序,用GLFW3打开一个空白窗口:

#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

void glfwErrorCallback(int error, const char *description)
{
   std::cerr << "GLFW error " << error << ": " << description << std::endl;
}

int main(int argc, char **argv)
{
   GLFWwindow* window;
   glfwSetErrorCallback(glfwErrorCallback);

   if(!glfwInit())
   {
      std::cerr << "Failed to initialize GLFW...\n";
      return -1;
   }

   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
   glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

   window = glfwCreateWindow(1024, 768, "GLFW window", NULL, NULL);
   if(!window)
   {
      std::cerr << "Failed to open GLFW window...\n";
      glfwTerminate();
      return -1;
   }

   glewExperimental = GL_TRUE;
   if (glewInit())
   {
      std::cerr << "Failed to initialize GLEW...\n";
      glfwTerminate();
      return -1;
   }

   glfwMakeContextCurrent(window);

   while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && !glfwWindowShouldClose(window))
   {
      glfwSwapBuffers(window);
      glfwPollEvents();
   }

   glfwTerminate();
   return 0;
}

它会导致以下错误:

GLFW error 65540: Context profiles only exist for OpenGL version 3.2 and above
Failed to open GLFW window…

该应用程序在Linux上运行,使用Bumblebee的optirun.使用freeglut而不是GLFW时,代码可以正常工作.导致错误的代码有什么问题?

解决方法:

这很简单:

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); // Major = 4
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // Major was 4, now it is 3.

// Minor = ???   [Something < 2]

你需要使用glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);而是第二个提示.

上一篇:openGL起飞篇


下一篇:c – 重新调整GLFW窗口时,渲染区域不会更改