Browse code

Change from `printError` to `debugMessageCallback`

Robert Cranston authored on 10/11/2024 21:12:43
Showing 1 changed files

... ...
@@ -1,4 +1,5 @@
1 1
 /// Inlcudes
2
+#include <iostream>
2 3
 #include "MicroGlut.h"
3 4
 #include "GL_utilities.h"
4 5
 
... ...
@@ -17,17 +18,37 @@ GLfloat positions[][3] =
17 18
 };
18 19
 
19 20
 
21
+/// Debug message callback
22
+void APIENTRY debugMessageCallback(
23
+    GLenum          /* source    */,
24
+    GLenum          /* type      */,
25
+    GLuint          /* id        */,
26
+    GLenum          /* severity  */,
27
+    GLsizei         /* length    */,
28
+    GLchar  const *    message,
29
+    void    const * /* userParam */
30
+)
31
+{
32
+    std::cerr << message << std::endl;
33
+}
34
+
35
+
20 36
 /// Init
21 37
 void init(void)
22 38
 {
39
+    // Debug message callback.
40
+    // Requires OpenGL 4.3 or the GL_KHR_debug extension (which is not hardware
41
+    // dependent and supported almost everywhere *except* MacOS).
42
+    glEnable(GL_DEBUG_OUTPUT);
43
+    glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
44
+    glDebugMessageCallback(debugMessageCallback, nullptr);
45
+
23 46
     // GL inits.
24 47
     glClearColor(0.2, 0.2, 0.5, 0.0);
25 48
     glDisable(GL_DEPTH_TEST);
26
-    printError("init GL");
27 49
 
28 50
     // Load and compile shader.
29 51
     GLuint program = loadShaders("tsbk07.vert", "tsbk07.frag");
30
-    printError("init shader");
31 52
 
32 53
     // Allocate and activate Vertex Array Object (VAO), used for uploading
33 54
     // the geometry.
... ...
@@ -42,7 +63,6 @@ void init(void)
42 63
     glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
43 64
     glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
44 65
     glEnableVertexAttribArray(positionLocation);
45
-    printError("init VBOs");
46 66
 }
47 67
 
48 68
 
... ...
@@ -56,7 +76,6 @@ void display(void)
56 76
     glBindVertexArray(vertexArray);
57 77
 
58 78
     // Draw.
59
-    printError("display draw");
60 79
     glDrawArrays(GL_TRIANGLES, 0, ARRAY_COUNT(positions));
61 80
 
62 81
     // Show on the screen.