使用GLFW3创建OpenGL 4.3窗口失败

时间:2014-01-28 01:35:24

标签: c++ opengl glfw

我设置了一个最小的应用程序,用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错误65540:仅对OpenGL版本3.2及更高版本存在上下文配置文件   无法打开GLFW窗口...

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

1 个答案:

答案 0 :(得分:6)

这很简单:

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);代替第二个提示。