I am using Visual Studio Community and I am trying to create OpenGL application. I am using GLFW to open a window like this:
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;
}
I have abstracted the index buffer and the vertex buffer into classes that look like this: Vertex Buffer:
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));
}
and the Index Buffer:
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));
}
The problem is that after closing the window the console stays open and blinks waiting. I can only terminate the program with Visual Studio or by closing the console manually. I have experimented with the code and it is because of the two lines where I create the objects for my buffers: Without these two lines it works. Does anyone have an idea why that is?
As mentionned in the comments, the problem comes from the destruction of your buffers : the program tries to call glDestroyX
(in buffers destructors) after the OpenGL context was destroyed, which throws errors that GL_Call
tries to handle using GL context so it also throws error itself and so on.
To solve that, declare and use your buffers inside of a scope, and destroy GL the context after the end of the scope, so that your GL objects are destroyed before the GL context is destroyed.