对代码运行几次测试后,我确定GLFW和GLEW都成功初始化,但当我尝试创建要用于GLFW函数的GLFWwindow*
对象时,glfwCreateWindow()
函数返回nullptr
。为什么是这个,我该如何解决它?这里是我的代码:无法在C++中创建GLFWwindow - glfwCreateWindow返回nullptr?
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
const GLuint windowWidth = 500, windowHeight = 500;
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, "Learn OpenGL", nullptr, nullptr);
if (window == nullptr) {
std::cout << "Failed to create GLFW window!" << std::endl;
char myvar1; std::cin >> myvar1;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GL_TRUE) {
std::cout << "Failed to initialize GLEW" << std::endl;
char myvar2; std::cin >> myvar2;
return -1;
}
glViewport(0, 0, windowWidth, windowHeight);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
文档说“Windows:如果Microsoft GDI软件OpenGL实现是唯一可用,则窗口创建将失败。”你使用什么视频卡,你有最新的驱动程序,并且你正在运行远程桌面还是在控制台会话中? –
使用1.9GHz赛扬的板载显卡。我会尝试更新驱动程序。 –
使用OpenGL Extensions Viewer来确认您的GPU实际支持3.3。我遇到了同样的问题,事实证明,Parallels的GPU仿真只能达到3.2,但这正是我尝试在虚拟机上做图形所得到的结果。 –