Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glDebugMessageCallback doesn't get called despite of error

I am using GLFW and GLEW in a C++ program to work with OpenGL. I want to be able to output OpenGL errors to the console. To test this, I made a little program:

#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

// Test error function
void GLAPIENTRY MessageCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) {
    std::cout << "ERROR";
}

// Main function
int main(void)
{
    // Just some initialization
    GLFWwindow* window;

    if (!glfwInit()) {
        return -1;
    }

    window = glfwCreateWindow(640, 480, "Debugtest", NULL, NULL);
    if (!window) {
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);
    glewInit();

    // Output the opengl version
    std::cout << glGetString(GL_VERSION) << std::endl;

    // Enable debug output
    glEnable(GL_DEBUG_OUTPUT);
    glDebugMessageCallback(MessageCallback, 0);

    unsigned int buffer;
    glGenBuffers(-1, &buffer); // -1 throws an error according to http://docs.gl/gl4/glGenBuffers

    // Loop until the user closes the window
    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();
    };

    glfwTerminate();
    return 0;
}

The code for the callback is from the khronos documentation. glGenBuffers should throw an error acccording to the docs. However, the OpenGL window stays white, the terminal just displays the OpenGL version (4.5.13474 Copatibility Profile Context 22.19.162.4).

What is the best way to handle errors? How do I fix my code?

like image 915
Jan Berndt Avatar asked Oct 17 '25 12:10

Jan Berndt


1 Answers

Thanks to @OutOfBound I found the answer. Before glfwCreateWindow, you need to call glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE).

Similar to his answer, you can do something like this to break when an error occurs (At least using the MSVC compiler):

#define call(x) x;\
    if (error) __debugbreak();

bool error = false;

void GLAPIENTRY MessageCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) {
    error = true;
    std::cout << "[OpenGL Error](" << type << ") " << message << std::endl;
}
like image 161
Jan Berndt Avatar answered Oct 19 '25 02:10

Jan Berndt