OpenGL GLFW 窗口一打开就关闭
Posted
技术标签:
【中文标题】OpenGL GLFW 窗口一打开就关闭【英文标题】:OpenGL GLFW window closes as soon as it opens 【发布时间】:2014-10-19 09:24:19 【问题描述】:我无法在黑色窗口中看到我的三角形。窗口虽然只是在打开时关闭,并且不允许我看到里面发生了什么。我在网上某处看到我与次要版本有关,我不知道如何检查我的 VGA 卡。
这是我的完整代码:
#define GLEW_STATIC
#include <stdio.h>
#include <GL\glew.h>
#include <GL\GLU.h>
#include <GL\glut.h>
#include <glm.hpp>
#include <GL\gl.h>
#include <GLFW\glfw3.h>
using namespace glm;
using namespace std;
int main()
glfwWindowHint(GLFW_SAMPLES, 4); // anti aliasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // openGL major version to be 3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); // minor set to 3, which makes the version 3.3
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // for MAC OS only
glfwWindowHint(GLFW_OPENGL_COMPAT_PROFILE, GLFW_OPENGL_CORE_PROFILE); //avoid using old openGL
GLFWwindow* window;
window = glfwCreateWindow(1024, 768, "First Window in OpenGL", NULL, NULL);
if (window == NULL)
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
glfwTerminate();
return -1;
glfwMakeContextCurrent(window);
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
static const GLfloat g_vertex_buffer_data[] =
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0,
0.0f, 1.0f, 0.0f
;
// identifying our vertex buffer
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
glewExperimental = true; // Needed in core profile
if (glewInit() != GLEW_OK)
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
do
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
return 0;
任何人都可以帮助我保持窗口打开并按代码预期的那样使用 ESC 键关闭它吗?
【问题讨论】:
【参考方案1】:窗口立即关闭的原因是您有分段错误。 这很可能是由于未能按正确顺序初始化事物。
在运行任何 glfw 函数调用之前初始化 GLFW
,如下所示:
// Initialise GLFW
if( !glfwInit() )
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
glfwWindowHint(GLFW_SAMPLES, 4); // anti aliasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // openGL major version to be 3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); // minor set to 3, which makes the version 3.3
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // for MAC OS only
glfwWindowHint(GLFW_OPENGL_COMPAT_PROFILE, GLFW_OPENGL_CORE_PROFILE); //avoid using old openGL
GLFWwindow* window;
window = gl
然后,在创建和设置 GL 上下文后将 glew
的初始化移动到右侧:
if (window == NULL)
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
glfwTerminate();
return -1;
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE; // Needed in core profile
const GLenum err = glewInit();
if (err != GLEW_OK)
fprintf(stderr, "Failed to initialize GLEW\n");
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
return -1;
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
// ... rest of code follows
我已经测试过了,你的代码现在应该可以正常工作了,并且在你点击ESC
之前窗口不会关闭。
【讨论】:
谢谢,当我这样做时,我的输出中会得到一堆报告,其中包含几个文件。错误是这样的:'openGL.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
似乎与您的原始问题无关,很可能您应该创建一个新问题来解决它。你检查过这个吗? ***.com/questions/15937707/…
不,它是相关的。我已经使用您的解决方案解决了它。我在实施您的解决方案时犯了一个错误。非常感谢斯瓦洛格以上是关于OpenGL GLFW 窗口一打开就关闭的主要内容,如果未能解决你的问题,请参考以下文章