From d42c97685284df35a34d419d258ae15fc02ae96b Mon Sep 17 00:00:00 2001
From: AnyOldName3 <krizdjali+github@gmail.com>
Date: Fri, 26 Oct 2018 15:11:50 +0100
Subject: [PATCH] Add detailed OpenGL debug messages

---
 components/debug/gldebug.cpp | 63 +++++++++++++++++++++++++++++++++---
 1 file changed, 59 insertions(+), 4 deletions(-)

diff --git a/components/debug/gldebug.cpp b/components/debug/gldebug.cpp
index 71d75fa737..52a1bb5a69 100644
--- a/components/debug/gldebug.cpp
+++ b/components/debug/gldebug.cpp
@@ -3,13 +3,66 @@
 #include <components/debug/debuglog.hpp>
 
 // OpenGL constants not provided by OSG:
-#define GL_DEBUG_OUTPUT 0x92E0
-#define GL_DEBUG_SEVERITY_MEDIUM 0x9147
-#define GL_DEBUG_TYPE_ERROR 0x824C
+#include <SDL_opengl_glext.h>
 
 void debugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam)
 {
-    Log(Debug::Error) << message;
+    std::string srcStr;
+    switch (source)
+    {
+    case GL_DEBUG_SOURCE_API:
+        srcStr = "API";
+        break;
+    case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
+        srcStr = "WINDOW_SYSTEM";
+        break;
+    case GL_DEBUG_SOURCE_SHADER_COMPILER:
+        srcStr = "SHADER_COMPILER";
+        break;
+    case GL_DEBUG_SOURCE_THIRD_PARTY:
+        srcStr = "THIRD_PARTY";
+        break;
+    case GL_DEBUG_SOURCE_APPLICATION:
+        srcStr = "APPLICATION";
+        break;
+    case GL_DEBUG_SOURCE_OTHER:
+        srcStr = "OTHER";
+        break;
+    default:
+        srcStr = "UNDEFINED";
+        break;
+    }
+
+    std::string typeStr;
+
+    Debug::Level logSeverity = Debug::Warning;
+    switch (type)
+    {
+    case GL_DEBUG_TYPE_ERROR:
+        typeStr = "ERROR";
+        logSeverity = Debug::Error;
+        break;
+    case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
+        typeStr = "DEPRECATED_BEHAVIOR";
+        break;
+    case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
+        typeStr = "UNDEFINED_BEHAVIOR";
+        break;
+    case GL_DEBUG_TYPE_PORTABILITY:
+        typeStr = "PORTABILITY";
+        break;
+    case GL_DEBUG_TYPE_PERFORMANCE:
+        typeStr = "PERFORMANCE";
+        break;
+    case GL_DEBUG_TYPE_OTHER:
+        typeStr = "OTHER";
+        break;
+    default:
+        typeStr = "UNDEFINED";
+        break;
+    }
+
+    Log(logSeverity) << "OpenGL " << typeStr << " [" << srcStr << "]: " << message;
 }
 
 void enableGLDebugExtension(unsigned int contextID)
@@ -45,6 +98,8 @@ void enableGLDebugExtension(unsigned int contextID)
 
         Log(Debug::Info) << "OpenGL debug callback attached.";
     }
+    else
+        Log(Debug::Error) << "Unable to attach OpenGL debug callback.";
 }
 
 EnableGLDebugOperation::EnableGLDebugOperation() : osg::GraphicsOperation("EnableGLDebugOperation", false)