added bunch of error checks to shapes demo
[freeglut] / progs / demos / shapes / shapes.c
index e984d29..4f0e904 100644 (file)
 #include <crtdbg.h>
 #endif
 
+/* report GL errors, if any, to stderr */
+void checkError(const char *functionName)
+{
+    GLenum error;
+    while (( error = glGetError() ) != GL_NO_ERROR) {
+        fprintf (stderr, "GL error 0x%X detected in %s\n", error, functionName);
+    }
+}
+
 /*
  * OpenGL 2+ shader mode needs some function and macro definitions, 
  * avoiding a dependency on additional libraries like GLEW or the
@@ -282,6 +291,7 @@ GLint getAttribOrUniformLocation(const char* name, GLuint program, GLboolean isA
 
         return uniform;
     }
+    checkError ("getAttribOrUniformLocation");
 }
 
 GLuint program;
@@ -305,12 +315,16 @@ void compileAndCheck(GLuint shader)
         fprintf (stderr, "compile log: %s\n", infoLog);
         free (infoLog);
     }
+    checkError ("compileAndCheck");
 }
 
 GLuint compileShaderSource(GLenum type, GLsizei count, const ourGLchar **string)
 {
     GLuint shader = gl_CreateShader (type);
     gl_ShaderSource (shader, count, string, NULL);
+
+    checkError ("compileShaderSource");
+
     compileAndCheck (shader);
     return shader;
 }
@@ -329,6 +343,7 @@ void linkAndCheck(GLuint program)
         fprintf (stderr, "link log: %s\n", infoLog);
         free (infoLog);
     }
+    checkError ("linkAndCheck");
 }
 
 void createProgram(GLuint vertexShader, GLuint fragmentShader)
@@ -340,6 +355,9 @@ void createProgram(GLuint vertexShader, GLuint fragmentShader)
     if (fragmentShader != 0) {
         gl_AttachShader (program, fragmentShader);
     }
+
+    checkError ("createProgram");
+
     linkAndCheck (program);
 }
 
@@ -353,15 +371,17 @@ void initShader(void)
     GLuint fragmentShader =
         compileShaderSource (GL_FRAGMENT_SHADER, fragmentShaderLines, fragmentShaderSource);
 
+    checkError ("initShader - 1");
+
     createProgram (vertexShader, fragmentShader);
 
     gl_UseProgram (program);
 
-    attribute_fg_coord      = getAttribOrUniformLocation("fg_coord"         , program, TRUE);
-    attribute_fg_normal     = getAttribOrUniformLocation("fg_normal"        , program, TRUE);
-    uniform_m               = getAttribOrUniformLocation("m"                , program, FALSE);
-    uniform_p               = getAttribOrUniformLocation("p"                , program, FALSE);
-    uniform_m_3x3_inv_transp= getAttribOrUniformLocation("m_3x3_inv_transp" , program, FALSE);
+    attribute_fg_coord      = getAttribOrUniformLocation("fg_coord"         , program, GL_TRUE);
+    attribute_fg_normal     = getAttribOrUniformLocation("fg_normal"        , program, GL_TRUE);
+    uniform_m               = getAttribOrUniformLocation("m"                , program, GL_FALSE);
+    uniform_p               = getAttribOrUniformLocation("p"                , program, GL_FALSE);
+    uniform_m_3x3_inv_transp= getAttribOrUniformLocation("m_3x3_inv_transp" , program, GL_FALSE);
 
     gl_UseProgram (0);
 
@@ -370,6 +390,8 @@ void initShader(void)
         shaderReady = -1;
     else
         shaderReady = 1;
+
+    checkError ("initShader - 2");
 }
 
 /*
@@ -606,7 +628,7 @@ static void display(void)
         gl_load_identity();
 
         gl_push_matrix();
-            /* Not in reverse order like normal OpenGL, matrices are multiplied in in order specified in our util library */
+            /* Not in reverse order like normal OpenGL, our util library multiplies the matrices in the order they are specified in */
             gl_rotatef((float)a,0,0,1);
             gl_rotatef((float)b,1,0,0);
             gl_translatef(0,1.2f,-6);
@@ -627,6 +649,8 @@ static void display(void)
         gl_UseProgram (0);
         glutSetVertexAttribCoord3(-1);
         glutSetVertexAttribNormal(-1);
+
+        checkError ("display");
     }
     else
     {