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.
Browse code

Change `positions` from `float[]` to `float[][3]`

Robert Cranston authored on 10/11/2024 21:11:20
Showing 1 changed files
... ...
@@ -9,11 +9,11 @@
9 9
 
10 10
 /// Globals
11 11
 GLuint  vertexArray;
12
-GLfloat positions[] =
12
+GLfloat positions[][3] =
13 13
 {
14
-    -0.5f, -0.5f, 0.0f,
15
-    -0.5f, +0.5f, 0.0f,
16
-    +0.5f, -0.5f, 0.0f,
14
+    {-0.5f, -0.5f, 0.0f},
15
+    {-0.5f, +0.5f, 0.0f},
16
+    {+0.5f, -0.5f, 0.0f},
17 17
 };
18 18
 
19 19
 
... ...
@@ -56,8 +56,8 @@ void display(void)
56 56
     glBindVertexArray(vertexArray);
57 57
 
58 58
     // Draw.
59
-    glDrawArrays(GL_TRIANGLES, 0, ARRAY_COUNT(positions)/3);
60 59
     printError("display draw");
60
+    glDrawArrays(GL_TRIANGLES, 0, ARRAY_COUNT(positions));
61 61
 
62 62
     // Show on the screen.
63 63
     glutSwapBuffers();
Browse code

Change indentation from tabs to (4) spaces

Robert Cranston authored on 28/02/2024 00:05:53
Showing 1 changed files
... ...
@@ -11,70 +11,70 @@
11 11
 GLuint  vertexArray;
12 12
 GLfloat positions[] =
13 13
 {
14
-	-0.5f, -0.5f, 0.0f,
15
-	-0.5f, +0.5f, 0.0f,
16
-	+0.5f, -0.5f, 0.0f,
14
+    -0.5f, -0.5f, 0.0f,
15
+    -0.5f, +0.5f, 0.0f,
16
+    +0.5f, -0.5f, 0.0f,
17 17
 };
18 18
 
19 19
 
20 20
 /// Init
21 21
 void init(void)
22 22
 {
23
-	// GL inits.
24
-	glClearColor(0.2, 0.2, 0.5, 0.0);
25
-	glDisable(GL_DEPTH_TEST);
26
-	printError("init GL");
27
-
28
-	// Load and compile shader.
29
-	GLuint program = loadShaders("tsbk07.vert", "tsbk07.frag");
30
-	printError("init shader");
31
-
32
-	// Allocate and activate Vertex Array Object (VAO), used for uploading
33
-	// the geometry.
34
-	glGenVertexArrays(1, &vertexArray);
35
-	glBindVertexArray(vertexArray);
36
-
37
-	// Allocate and activate Vertex Buffer Objects (VBO), for vertex data.
38
-	GLint  positionLocation = glGetAttribLocation(program, "inPosition");
39
-	GLuint positionBuffer;
40
-	glGenBuffers(1, &positionBuffer);
41
-	glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
42
-	glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
43
-	glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
44
-	glEnableVertexAttribArray(positionLocation);
45
-	printError("init VBOs");
23
+    // GL inits.
24
+    glClearColor(0.2, 0.2, 0.5, 0.0);
25
+    glDisable(GL_DEPTH_TEST);
26
+    printError("init GL");
27
+
28
+    // Load and compile shader.
29
+    GLuint program = loadShaders("tsbk07.vert", "tsbk07.frag");
30
+    printError("init shader");
31
+
32
+    // Allocate and activate Vertex Array Object (VAO), used for uploading
33
+    // the geometry.
34
+    glGenVertexArrays(1, &vertexArray);
35
+    glBindVertexArray(vertexArray);
36
+
37
+    // Allocate and activate Vertex Buffer Objects (VBO), for vertex data.
38
+    GLint  positionLocation = glGetAttribLocation(program, "inPosition");
39
+    GLuint positionBuffer;
40
+    glGenBuffers(1, &positionBuffer);
41
+    glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
42
+    glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
43
+    glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
44
+    glEnableVertexAttribArray(positionLocation);
45
+    printError("init VBOs");
46 46
 }
47 47
 
48 48
 
49 49
 /// Display
50 50
 void display(void)
51 51
 {
52
-	// Clear the screen.
53
-	glClear(GL_COLOR_BUFFER_BIT);
52
+    // Clear the screen.
53
+    glClear(GL_COLOR_BUFFER_BIT);
54 54
 
55
-	// Select VAO.
56
-	glBindVertexArray(vertexArray);
55
+    // Select VAO.
56
+    glBindVertexArray(vertexArray);
57 57
 
58
-	// Draw.
59
-	glDrawArrays(GL_TRIANGLES, 0, ARRAY_COUNT(positions)/3);
60
-	printError("display draw");
58
+    // Draw.
59
+    glDrawArrays(GL_TRIANGLES, 0, ARRAY_COUNT(positions)/3);
60
+    printError("display draw");
61 61
 
62
-	// Show on the screen.
63
-	glutSwapBuffers();
62
+    // Show on the screen.
63
+    glutSwapBuffers();
64 64
 }
65 65
 
66 66
 
67 67
 /// Main
68 68
 int main(int argc, char * argv[])
69 69
 {
70
-	glutInit(&argc, argv);
71
-	glutInitContextVersion(3, 2);
72
-	glutInitWindowSize(600, 600);
73
-	glutCreateWindow("TSBK07");
70
+    glutInit(&argc, argv);
71
+    glutInitContextVersion(3, 2);
72
+    glutInitWindowSize(600, 600);
73
+    glutCreateWindow("TSBK07");
74 74
 
75
-	glutDisplayFunc(display);
75
+    glutDisplayFunc(display);
76 76
 
77
-	dumpInfo();
78
-	init();
79
-	glutMainLoop();
77
+    dumpInfo();
78
+    init();
79
+    glutMainLoop();
80 80
 }
Browse code

Unify lab1 and lab4

Robert Cranston authored on 27/02/2024 02:27:37
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,80 @@
1
+/// Inlcudes
2
+#include "MicroGlut.h"
3
+#include "GL_utilities.h"
4
+
5
+
6
+/// Defines
7
+#define ARRAY_COUNT(ARRAY) (sizeof(ARRAY)/sizeof(*(ARRAY)))
8
+
9
+
10
+/// Globals
11
+GLuint  vertexArray;
12
+GLfloat positions[] =
13
+{
14
+	-0.5f, -0.5f, 0.0f,
15
+	-0.5f, +0.5f, 0.0f,
16
+	+0.5f, -0.5f, 0.0f,
17
+};
18
+
19
+
20
+/// Init
21
+void init(void)
22
+{
23
+	// GL inits.
24
+	glClearColor(0.2, 0.2, 0.5, 0.0);
25
+	glDisable(GL_DEPTH_TEST);
26
+	printError("init GL");
27
+
28
+	// Load and compile shader.
29
+	GLuint program = loadShaders("tsbk07.vert", "tsbk07.frag");
30
+	printError("init shader");
31
+
32
+	// Allocate and activate Vertex Array Object (VAO), used for uploading
33
+	// the geometry.
34
+	glGenVertexArrays(1, &vertexArray);
35
+	glBindVertexArray(vertexArray);
36
+
37
+	// Allocate and activate Vertex Buffer Objects (VBO), for vertex data.
38
+	GLint  positionLocation = glGetAttribLocation(program, "inPosition");
39
+	GLuint positionBuffer;
40
+	glGenBuffers(1, &positionBuffer);
41
+	glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
42
+	glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
43
+	glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
44
+	glEnableVertexAttribArray(positionLocation);
45
+	printError("init VBOs");
46
+}
47
+
48
+
49
+/// Display
50
+void display(void)
51
+{
52
+	// Clear the screen.
53
+	glClear(GL_COLOR_BUFFER_BIT);
54
+
55
+	// Select VAO.
56
+	glBindVertexArray(vertexArray);
57
+
58
+	// Draw.
59
+	glDrawArrays(GL_TRIANGLES, 0, ARRAY_COUNT(positions)/3);
60
+	printError("display draw");
61
+
62
+	// Show on the screen.
63
+	glutSwapBuffers();
64
+}
65
+
66
+
67
+/// Main
68
+int main(int argc, char * argv[])
69
+{
70
+	glutInit(&argc, argv);
71
+	glutInitContextVersion(3, 2);
72
+	glutInitWindowSize(600, 600);
73
+	glutCreateWindow("TSBK07");
74
+
75
+	glutDisplayFunc(display);
76
+
77
+	dumpInfo();
78
+	init();
79
+	glutMainLoop();
80
+}