From d20f5688362e0b8e342e6866d8ed83189b468883 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Fri, 13 Apr 2018 13:21:30 +0200 Subject: [PATCH] Use GLUT for detecting errors. Rather than polling for errors, let GLUT do it. Can be enabled with the "-gldebug" CLI flag. --- src/fmri/ActivityAnimation.cpp | 1 - src/fmri/RenderingState.cpp | 2 -- src/fmri/glutils.cpp | 41 ++++++++++++++++++++-------------- src/fmri/glutils.hpp | 10 ++++----- src/fmri/main.cpp | 3 +++ 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/fmri/ActivityAnimation.cpp b/src/fmri/ActivityAnimation.cpp index 22380e7..8b58b0f 100644 --- a/src/fmri/ActivityAnimation.cpp +++ b/src/fmri/ActivityAnimation.cpp @@ -78,7 +78,6 @@ void ActivityAnimation::draw(float timeScale) glColor4fv(RenderingState::instance().pathColor().data()); glVertexPointer(3, GL_FLOAT, 0, startingPos.data()); glDrawElements(GL_LINES, lineIndices.size(), GL_UNSIGNED_INT, lineIndices.data()); - checkGLErrors(); } glDisableClientState(GL_VERTEX_ARRAY); } diff --git a/src/fmri/RenderingState.cpp b/src/fmri/RenderingState.cpp index a065989..5e3f6b3 100644 --- a/src/fmri/RenderingState.cpp +++ b/src/fmri/RenderingState.cpp @@ -481,8 +481,6 @@ void RenderingState::idleFunc() if (options.mouse_2_pressed) { move('s', false); } - - checkGLErrors(); throttleIdleFunc(); } glutPostRedisplay(); diff --git a/src/fmri/glutils.cpp b/src/fmri/glutils.cpp index 34a23ee..9d30cd4 100644 --- a/src/fmri/glutils.cpp +++ b/src/fmri/glutils.cpp @@ -7,19 +7,13 @@ #include #include "glutils.hpp" +#ifdef FREEGLUT +#include +#endif + using namespace fmri; using namespace std; -static void handleGLError(GLenum error) { - switch (error) { - case GL_NO_ERROR: - return; - - default: - cerr << "OpenGL error: " << (const char*) gluGetString(error) << endl; - } -} - void fmri::changeWindowSize(int w, int h) { // Prevent a divide by zero, when window is too short @@ -59,13 +53,6 @@ void fmri::renderText(std::string_view text, int x, int y) } } -void fmri::checkGLErrors() -{ - while (auto error = glGetError()) { - handleGLError(error); - } -} - void fmri::throttleIdleFunc() { using namespace std::chrono; @@ -129,3 +116,23 @@ void fmri::drawImageTiles(int n, const float *vertexBuffer, const float *texture glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } + + +void fmri::registerErrorCallbacks() +{ +#ifdef FREEGLUT + glutInitErrorFunc([](const char* format, va_list args) { + char buffer[1024]; + std::vsnprintf(buffer, sizeof(buffer), format, args); + LOG(ERROR) << "freeglut: " << buffer; + }); + glutInitWarningFunc([](const char* format, va_list args) { + char buffer[1024]; + std::vsnprintf(buffer, sizeof(buffer), format, args); + LOG(WARNING) << "freeglut: " << buffer; + }); + LOG(INFO) << "Freeglut error handlers installed successfully"; +#else + LOG(INFO) << "Compiled without freeglut, error handlers not available."; +#endif +} diff --git a/src/fmri/glutils.hpp b/src/fmri/glutils.hpp index cc53f9d..8286d2e 100644 --- a/src/fmri/glutils.hpp +++ b/src/fmri/glutils.hpp @@ -29,11 +29,6 @@ namespace fmri { */ void renderText(std::string_view text, int x = 0, int y = 0); - /** - * Check if there are OpenGL errors and report them. - */ - void checkGLErrors(); - /** * Slow down until the idle func is being called a reasonable amount of times. */ @@ -55,4 +50,9 @@ namespace fmri { */ void drawImageTiles(int n, const float *vertexBuffer, const float *textureCoords, const Texture &texture, float alpha); + + /** + * Attempmt to register + */ + void registerErrorCallbacks(); } diff --git a/src/fmri/main.cpp b/src/fmri/main.cpp index f810f11..97e0c8d 100644 --- a/src/fmri/main.cpp +++ b/src/fmri/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "LayerData.hpp" #include "Options.hpp" @@ -54,6 +55,8 @@ int main(int argc, char *argv[]) google::InitGoogleLogging(argv[0]); google::InstallFailureSignalHandler(); + registerErrorCallbacks(); + glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA | GLUT_ALPHA); glutCreateWindow(argv[0]);