我在C ++程序中使用GLFW和GLEW与OpenGL一起使用。我希望能够将OpenGL错误输出到控制台。为了测试这一点,我做了一个小程序:
#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;
}
回调的代码来自khronos文档。glGenBuffers
应该根据文档抛出错误。但是,OpenGL窗口保持白色,终端仅显示OpenGL版本(4.5.13474 Copatibility Profile Context 22.19.162.4
)。
处理错误的最佳方法是什么?如何修复我的代码?
感谢@OutOfBound,我找到了答案。之前glfwCreateWindow
,您需要致电glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE)
。
与他的答案类似,您可以执行以下操作以在发生错误时中断(至少使用MSVC编译器):
#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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句