OpenGL 应用程序控制台未关闭
OpenGL Application console not closing
我正在使用 Visual Studio 社区,我正在尝试创建 OpenGL 应用程序。
我正在使用 GLFW 打开这样的 window:
int main() {
//Init stuff
int width = 1920;
int height = 1080;
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(width, height, "LearnOpenGL", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create Window du schmok" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK) {
std::cout << "Glew was not initialized du schmok" << std::endl;
}
glViewport(0, 0, width, height);
VertexBuffer vbo(vertices, sizeof(vertices));
IndexBuffer ibo(indices, 6);
while (!glfwWindowShouldClose(window))
{
glClearColor(0.0f, 0.3f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
我已经将索引缓冲区和顶点缓冲区抽象为 类,如下所示:
顶点缓冲区:
VertexBuffer::VertexBuffer(float data[], unsigned int size)
{
GL_CALL(glGenBuffers(1, &m_ID));
GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, m_ID));
GL_CALL(glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW));
}
VertexBuffer::~VertexBuffer()
{
GL_CALL(glDeleteBuffers(1, &m_ID));
}
void VertexBuffer::Bind()
{
GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, m_ID));
}
void VertexBuffer::Unbind()
{
GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, 0));
}
和索引缓冲区:
IndexBuffer::IndexBuffer(unsigned int indices[], unsigned int count)
{
m_Count = count;
GL_CALL(glGenBuffers(1, &m_ID));
GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ID));
GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_Count * sizeof(unsigned int), indices, GL_STATIC_DRAW));
}
IndexBuffer::~IndexBuffer()
{
GL_CALL(glDeleteBuffers(1, &m_ID));
}
void IndexBuffer::Bind()
{
GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ID));
}
void IndexBuffer::Unbind()
{
GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
}
问题是关闭 window 后,控制台保持打开状态并闪烁等待。我只能用 Visual Studio 或手动关闭控制台来终止程序。
我已经对代码进行了试验,这是因为我为缓冲区创建对象的两行:
没有这两行它就可以工作。有谁知道这是为什么?
正如评论中提到的,问题来自于你的缓冲区的破坏:程序试图在 OpenGL 上下文被破坏后调用 glDestroyX
(在缓冲区析构函数中),这会抛出 GL_Call
尝试使用 GL 上下文进行处理,因此它本身也会抛出错误等等。
要解决这个问题,请在作用域内声明和使用缓冲区,并在作用域结束后销毁 GL 上下文,这样您的 GL 对象就会在之前销毁GL上下文被破坏。
我正在使用 Visual Studio 社区,我正在尝试创建 OpenGL 应用程序。 我正在使用 GLFW 打开这样的 window:
int main() {
//Init stuff
int width = 1920;
int height = 1080;
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(width, height, "LearnOpenGL", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create Window du schmok" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK) {
std::cout << "Glew was not initialized du schmok" << std::endl;
}
glViewport(0, 0, width, height);
VertexBuffer vbo(vertices, sizeof(vertices));
IndexBuffer ibo(indices, 6);
while (!glfwWindowShouldClose(window))
{
glClearColor(0.0f, 0.3f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
我已经将索引缓冲区和顶点缓冲区抽象为 类,如下所示: 顶点缓冲区:
VertexBuffer::VertexBuffer(float data[], unsigned int size)
{
GL_CALL(glGenBuffers(1, &m_ID));
GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, m_ID));
GL_CALL(glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW));
}
VertexBuffer::~VertexBuffer()
{
GL_CALL(glDeleteBuffers(1, &m_ID));
}
void VertexBuffer::Bind()
{
GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, m_ID));
}
void VertexBuffer::Unbind()
{
GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, 0));
}
和索引缓冲区:
IndexBuffer::IndexBuffer(unsigned int indices[], unsigned int count)
{
m_Count = count;
GL_CALL(glGenBuffers(1, &m_ID));
GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ID));
GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_Count * sizeof(unsigned int), indices, GL_STATIC_DRAW));
}
IndexBuffer::~IndexBuffer()
{
GL_CALL(glDeleteBuffers(1, &m_ID));
}
void IndexBuffer::Bind()
{
GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ID));
}
void IndexBuffer::Unbind()
{
GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
}
问题是关闭 window 后,控制台保持打开状态并闪烁等待。我只能用 Visual Studio 或手动关闭控制台来终止程序。 我已经对代码进行了试验,这是因为我为缓冲区创建对象的两行: 没有这两行它就可以工作。有谁知道这是为什么?
正如评论中提到的,问题来自于你的缓冲区的破坏:程序试图在 OpenGL 上下文被破坏后调用 glDestroyX
(在缓冲区析构函数中),这会抛出 GL_Call
尝试使用 GL 上下文进行处理,因此它本身也会抛出错误等等。
要解决这个问题,请在作用域内声明和使用缓冲区,并在作用域结束后销毁 GL 上下文,这样您的 GL 对象就会在之前销毁GL上下文被破坏。