From 0559ea55d04160bedc221a147146b801ad0ff9a2 Mon Sep 17 00:00:00 2001 From: capnramses Date: Sat, 10 Dec 2016 19:33:53 +0000 Subject: [PATCH] reformatted code in demos 00 to 02 with clang-format, tidied the pure C bits --- .clang-format | 47 ++++++ 00_hello_triangle/main.c | 159 +++++++++-------- 00_hello_triangle_gl2.1/main.c | 157 +++++++++-------- 01_extended_init/main.c | 296 +++++++++++++++----------------- 02_shaders/gl_utils.c | 148 ++++++++-------- 02_shaders/gl_utils.h | 23 +-- 02_shaders/main.c | 300 +++++++++++++++++---------------- 7 files changed, 572 insertions(+), 558 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..e7c5b12 --- /dev/null +++ b/.clang-format @@ -0,0 +1,47 @@ +--- +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +ConstructorInitializerIndentWidth: 2 +AlignEscapedNewlinesLeft: false +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakTemplateDeclarations: false +AlwaysBreakBeforeMultilineStrings: false +BreakBeforeBinaryOperators: false +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BinPackParameters: true +ColumnLimit: 84 +ConstructorInitializerAllOnOneLineOrOnePerLine: false +DerivePointerBinding: false +ExperimentalAutoDetectBinPacking: false +IndentCaseLabels: false +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCSpaceBeforeProtocolList: true +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 60 +PenaltyBreakString: 1000 +PenaltyBreakFirstLessLess: 120 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerBindsToType: false +SpacesBeforeTrailingComments: 1 +Cpp11BracedListStyle: false +Standard: Cpp03 +IndentWidth: 2 +TabWidth: 2 +UseTab: Always +BreakBeforeBraces: Attach +IndentFunctionDeclarationAfterType: false +SpacesInParentheses: true +SpacesInAngles: false +SpaceInEmptyParentheses: false +SpacesInCStyleCastParentheses: false +SpaceAfterControlStatementKeyword: true +SpaceBeforeAssignmentOperators: true +ContinuationIndentWidth: 2 +... + diff --git a/00_hello_triangle/main.c b/00_hello_triangle/main.c index 3b07e6a..231b52a 100644 --- a/00_hello_triangle/main.c +++ b/00_hello_triangle/main.c @@ -12,134 +12,131 @@ | This uses the libraries GLEW and GLFW3 to start GL. Download and compile | | these first. Linking them might be a pain, but you'll need to master this. | \******************************************************************************/ -#include /* include GLEW and new version of GL on Windows */ +#include /* include GLEW and new version of GL on Windows */ #include /* GLFW helper library */ #include -int main () { - GLFWwindow* window = NULL; - const GLubyte* renderer; - const GLubyte* version; +int main() { + GLFWwindow *window = NULL; + const GLubyte *renderer; + const GLubyte *version; GLuint vao; GLuint vbo; /* geometry to use. these are 3 xyz points (9 floats total) to make a triangle */ - GLfloat points[] = { - 0.0f, 0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - -0.5f, -0.5f, 0.0f - }; + GLfloat points[] = { 0.0f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f }; /* these are the strings of code for the shaders the vertex shader positions each vertex point */ - const char* vertex_shader = - "#version 410\n" - "in vec3 vp;" - "void main () {" - " gl_Position = vec4 (vp, 1.0);" - "}"; + const char *vertex_shader = "#version 410\n" + "in vec3 vp;" + "void main () {" + " gl_Position = vec4 (vp, 1.0);" + "}"; /* the fragment shader colours each fragment (pixel-sized area of the triangle) */ - const char* fragment_shader = - "#version 410\n" - "out vec4 frag_colour;" - "void main () {" - " frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);" - "}"; + const char *fragment_shader = "#version 410\n" + "out vec4 frag_colour;" + "void main () {" + " frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);" + "}"; /* GL shader objects for vertex and fragment shader [components] */ GLuint vert_shader, frag_shader; /* GL shader programme object [combined, to link] */ GLuint shader_programme; /* start GL context and O/S window using the GLFW helper library */ - if (!glfwInit ()) { - fprintf (stderr, "ERROR: could not start GLFW3\n"); + if ( !glfwInit() ) { + fprintf( stderr, "ERROR: could not start GLFW3\n" ); return 1; - } + } - /* We must specify 3.2 core if on Apple OS X -- other O/S can specify - anything here. I defined 'APPLE' in the makefile for OS X */ +/* We must specify 3.2 core if on Apple OS X -- other O/S can specify +anything here. I defined 'APPLE' in the makefile for OS X */ #ifdef APPLE - glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2); - glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); - glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 ); + glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 2 ); + glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE ); + glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE ); #endif - window = glfwCreateWindow (640, 480, "Hello Triangle", NULL, NULL); - if (!window) { - fprintf (stderr, "ERROR: could not open window with GLFW3\n"); + window = glfwCreateWindow( 640, 480, "Hello Triangle", NULL, NULL ); + if ( !window ) { + fprintf( stderr, "ERROR: could not open window with GLFW3\n" ); glfwTerminate(); return 1; } - glfwMakeContextCurrent (window); + glfwMakeContextCurrent( window ); /* start GLEW extension handler */ glewExperimental = GL_TRUE; - glewInit (); + glewInit(); /* get version info */ - renderer = glGetString (GL_RENDERER); /* get renderer string */ - version = glGetString (GL_VERSION); /* version as a string */ - printf ("Renderer: %s\n", renderer); - printf ("OpenGL version supported %s\n", version); + renderer = glGetString( GL_RENDERER ); /* get renderer string */ + version = glGetString( GL_VERSION ); /* version as a string */ + printf( "Renderer: %s\n", renderer ); + printf( "OpenGL version supported %s\n", version ); /* tell GL to only draw onto a pixel if the shape is closer to the viewer than anything already drawn at that pixel */ - glEnable (GL_DEPTH_TEST); /* enable depth-testing */ - /* with LESS depth-testing interprets a smaller depth value as meaning "closer" */ - glDepthFunc (GL_LESS); + glEnable( GL_DEPTH_TEST ); /* enable depth-testing */ + /* with LESS depth-testing interprets a smaller depth value as meaning "closer" */ + glDepthFunc( GL_LESS ); /* a vertex buffer object (VBO) is created here. this stores an array of data on the graphics adapter's memory. in our case - the vertex points */ - glGenBuffers (1, &vbo); - glBindBuffer (GL_ARRAY_BUFFER, vbo); - glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (GLfloat), points, GL_STATIC_DRAW); - + glGenBuffers( 1, &vbo ); + glBindBuffer( GL_ARRAY_BUFFER, vbo ); + glBufferData( GL_ARRAY_BUFFER, 9 * sizeof( GLfloat ), points, GL_STATIC_DRAW ); + /* the vertex array object (VAO) is a little descriptor that defines which data from vertex buffer objects should be used as input variables to vertex - shaders. in our case - use our only VBO, and say 'every three floats is a + shaders. in our case - use our only VBO, and say 'every three floats is a variable' */ - glGenVertexArrays (1, &vao); - glBindVertexArray (vao); - glEnableVertexAttribArray (0); // "attribute #0 should be enabled when this vao is bound" - // this VBO is already bound, but it's a good habit to explicitly specify which VBO's data the following + glGenVertexArrays( 1, &vao ); + glBindVertexArray( vao ); + // "attribute #0 should be enabled when this vao is bound" + glEnableVertexAttribArray( 0 ); + // this VBO is already bound, but it's a good habit to explicitly specify which + // VBO's data the following // vertex attribute pointer refers to - glBindBuffer (GL_ARRAY_BUFFER, vbo); - // "attribute #0 is created from every 3 variables in the above buffer, of type float (i.e. make me vec3s)" - glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL); - + glBindBuffer( GL_ARRAY_BUFFER, vbo ); + // "attribute #0 is created from every 3 variables in the above buffer, of type + // float (i.e. make me vec3s)" + glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, NULL ); + /* here we copy the shader strings into GL shaders, and compile them. we then create an executable shader 'program' and attach both of the compiled - shaders. we link this, which matches the outputs of the vertex shader to + shaders. we link this, which matches the outputs of the vertex shader to the inputs of the fragment shader, etc. and it is then ready to use */ - vert_shader = glCreateShader (GL_VERTEX_SHADER); - glShaderSource (vert_shader, 1, &vertex_shader, NULL); - glCompileShader (vert_shader); - frag_shader = glCreateShader (GL_FRAGMENT_SHADER); - glShaderSource (frag_shader, 1, &fragment_shader, NULL); - glCompileShader (frag_shader); - shader_programme = glCreateProgram (); - glAttachShader (shader_programme, frag_shader); - glAttachShader (shader_programme, vert_shader); - glLinkProgram (shader_programme); - + vert_shader = glCreateShader( GL_VERTEX_SHADER ); + glShaderSource( vert_shader, 1, &vertex_shader, NULL ); + glCompileShader( vert_shader ); + frag_shader = glCreateShader( GL_FRAGMENT_SHADER ); + glShaderSource( frag_shader, 1, &fragment_shader, NULL ); + glCompileShader( frag_shader ); + shader_programme = glCreateProgram(); + glAttachShader( shader_programme, frag_shader ); + glAttachShader( shader_programme, vert_shader ); + glLinkProgram( shader_programme ); + /* this loop clears the drawing surface, then draws the geometry described - by the VAO onto the drawing surface. we 'poll events' to see if the window + by the VAO onto the drawing surface. we 'poll events' to see if the window was closed, etc. finally, we 'swap the buffers' which displays our drawing - surface onto the view area. we use a double-buffering system which means - that we have a 'currently displayed' surface, and 'currently being drawn' - surface. hence the 'swap' idea. in a single-buffering system we would see - stuff being drawn one-after-the-other */ - while (!glfwWindowShouldClose (window)) { + surface onto the view area. we use a double-buffering system which means + that we have a 'currently displayed' surface, and 'currently being drawn' + surface. hence the 'swap' idea. in a single-buffering system we would see + stuff being drawn one-after-the-other */ + while ( !glfwWindowShouldClose( window ) ) { /* wipe the drawing surface clear */ - glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glUseProgram (shader_programme); - glBindVertexArray (vao); + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + glUseProgram( shader_programme ); + glBindVertexArray( vao ); /* draw points 0-3 from the currently bound VAO with current in-use shader */ - glDrawArrays (GL_TRIANGLES, 0, 3); + glDrawArrays( GL_TRIANGLES, 0, 3 ); /* update other events like input handling */ - glfwPollEvents (); + glfwPollEvents(); /* put the stuff we've been drawing onto the display */ - glfwSwapBuffers (window); + glfwSwapBuffers( window ); } - + /* close GL context and any other GLFW resources */ glfwTerminate(); return 0; diff --git a/00_hello_triangle_gl2.1/main.c b/00_hello_triangle_gl2.1/main.c index c6e0018..3b4d785 100644 --- a/00_hello_triangle_gl2.1/main.c +++ b/00_hello_triangle_gl2.1/main.c @@ -12,128 +12,121 @@ | This uses the libraries GLEW and GLFW3 to start GL. Download and compile | | these first. Linking them might be a pain, but you'll need to master this. | \******************************************************************************/ -#include /* include GLEW and new version of GL on Windows */ +#include /* include GLEW and new version of GL on Windows */ #include /* GLFW helper library */ #include -int main () { - GLFWwindow* window = NULL; - const GLubyte* renderer; - const GLubyte* version; +int main() { + GLFWwindow *window = NULL; + const GLubyte *renderer; + const GLubyte *version; GLuint vao; GLuint vbo; /* geometry to use. these are 3 xyz points (9 floats total) to make a - triangle */ - GLfloat points[] = { - 0.0f, 0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - -0.5f, -0.5f, 0.0f - }; + triangle */ + GLfloat points[] = { 0.0f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f }; /* these are the strings of code for the shaders the vertex shader positions each vertex point */ - const char* vertex_shader = - "#version 120\n" - "attribute vec3 vp;" - "void main () {" - " gl_Position = vec4 (vp, 1.0);" - "}"; + const char *vertex_shader = "#version 120\n" + "attribute vec3 vp;" + "void main () {" + " gl_Position = vec4 (vp, 1.0);" + "}"; /* the fragment shader colours each fragment (pixel-sized area of the triangle) */ - const char* fragment_shader = - "#version 120\n" - "void main () {" - " gl_FragColor = vec4 (0.5, 0.0, 0.5, 1.0);" - "}"; + const char *fragment_shader = "#version 120\n" + "void main () {" + " gl_FragColor = vec4 (0.5, 0.0, 0.5, 1.0);" + "}"; /* GL shader objects for vertex and fragment shader [components] */ GLuint vs, fs; /* GL shader programme object [combined, to link] */ GLuint shader_programme; /* start GL context and O/S window using the GLFW helper library */ - if (!glfwInit ()) { - fprintf (stderr, "ERROR: could not start GLFW3\n"); + if ( !glfwInit() ) { + fprintf( stderr, "ERROR: could not start GLFW3\n" ); return 1; - } + } - glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 2); - glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 1); + glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 2 ); + glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 1 ); - window = glfwCreateWindow (640, 480, "Hello Triangle", NULL, NULL); - if (!window) { - fprintf (stderr, "ERROR: could not open window with GLFW3\n"); + window = glfwCreateWindow( 640, 480, "Hello Triangle", NULL, NULL ); + if ( !window ) { + fprintf( stderr, "ERROR: could not open window with GLFW3\n" ); glfwTerminate(); return 1; } - glfwMakeContextCurrent (window); + glfwMakeContextCurrent( window ); // start GLEW extension handler glewExperimental = GL_TRUE; - glewInit (); + glewInit(); /* get version info */ - renderer = glGetString (GL_RENDERER); /* get renderer string */ - version = glGetString (GL_VERSION); /* version as a string */ - printf ("Renderer: %s\n", renderer); - printf ("OpenGL version supported %s\n", version); + renderer = glGetString( GL_RENDERER ); /* get renderer string */ + version = glGetString( GL_VERSION ); /* version as a string */ + printf( "Renderer: %s\n", renderer ); + printf( "OpenGL version supported %s\n", version ); // tell GL to only draw onto a pixel if the shape is closer to the viewer - glEnable (GL_DEPTH_TEST); // enable depth-testing - // depth-testing interprets a smaller value as "closer" - glDepthFunc (GL_LESS); - + glEnable( GL_DEPTH_TEST ); // enable depth-testing + // depth-testing interprets a smaller value as "closer" + glDepthFunc( GL_LESS ); + /* a vertex buffer object (VBO) is created here. this stores an array of - data on the graphics adapter's memory. in our case - the vertex points */ - glGenBuffers (1, &vbo); - glBindBuffer (GL_ARRAY_BUFFER, vbo); - glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (GLfloat), points, - GL_STATIC_DRAW); - + data on the graphics adapter's memory. in our case - the vertex points */ + glGenBuffers( 1, &vbo ); + glBindBuffer( GL_ARRAY_BUFFER, vbo ); + glBufferData( GL_ARRAY_BUFFER, 9 * sizeof( GLfloat ), points, GL_STATIC_DRAW ); + /* the vertex array object (VAO) is a little descriptor that defines which data from vertex buffer objects should be used as input variables to vertex - shaders. in our case - use our only VBO, and say 'every three floats is a + shaders. in our case - use our only VBO, and say 'every three floats is a variable' */ - glGenVertexArrays (1, &vao); - glBindVertexArray (vao); - glEnableVertexAttribArray (0); - glBindBuffer (GL_ARRAY_BUFFER, vbo); - glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL); - + glGenVertexArrays( 1, &vao ); + glBindVertexArray( vao ); + glEnableVertexAttribArray( 0 ); + glBindBuffer( GL_ARRAY_BUFFER, vbo ); + glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, NULL ); + /* here we copy the shader strings into GL shaders, and compile them. we - then create an executable shader 'program' and attach both of the compiled - shaders. we link this, which matches the outputs of the vertex shader to - the inputs of the fragment shader, etc. and it is then ready to use */ - vs = glCreateShader (GL_VERTEX_SHADER); - glShaderSource (vs, 1, &vertex_shader, NULL); - glCompileShader (vs); - fs = glCreateShader (GL_FRAGMENT_SHADER); - glShaderSource (fs, 1, &fragment_shader, NULL); - glCompileShader (fs); - shader_programme = glCreateProgram (); - glAttachShader (shader_programme, fs); - glAttachShader (shader_programme, vs); - glLinkProgram (shader_programme); - + then create an executable shader 'program' and attach both of the compiled + shaders. we link this, which matches the outputs of the vertex shader to + the inputs of the fragment shader, etc. and it is then ready to use */ + vs = glCreateShader( GL_VERTEX_SHADER ); + glShaderSource( vs, 1, &vertex_shader, NULL ); + glCompileShader( vs ); + fs = glCreateShader( GL_FRAGMENT_SHADER ); + glShaderSource( fs, 1, &fragment_shader, NULL ); + glCompileShader( fs ); + shader_programme = glCreateProgram(); + glAttachShader( shader_programme, fs ); + glAttachShader( shader_programme, vs ); + glLinkProgram( shader_programme ); + /* this loop clears the drawing surface, then draws the geometry described - by the VAO onto the drawing surface. we 'poll events' to see if the window - was closed, etc. finally, we 'swap the buffers' which displays our drawing - surface onto the view area. we use a double-buffering system which means - that we have a 'currently displayed' surface, and 'currently being drawn' - surface. hence the 'swap' idea. in a single-buffering system we would see - stuff being drawn + by the VAO onto the drawing surface. we 'poll events' to see if the window + was closed, etc. finally, we 'swap the buffers' which displays our drawing + surface onto the view area. we use a double-buffering system which means + that we have a 'currently displayed' surface, and 'currently being drawn' + surface. hence the 'swap' idea. in a single-buffering system we would see + stuff being drawn one-after-the-other */ - while (!glfwWindowShouldClose (window)) { + while ( !glfwWindowShouldClose( window ) ) { // wipe the drawing surface clear - glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glUseProgram (shader_programme); - glBindVertexArray (vao); + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + glUseProgram( shader_programme ); + glBindVertexArray( vao ); // draw points 0-3 from the currently bound VAO with current shader - glDrawArrays (GL_TRIANGLES, 0, 3); - // update other events like input handling - glfwPollEvents (); + glDrawArrays( GL_TRIANGLES, 0, 3 ); + // update other events like input handling + glfwPollEvents(); // put the stuff we've been drawing onto the display - glfwSwapBuffers (window); + glfwSwapBuffers( window ); } - + // close GL context and any other GLFW resources glfwTerminate(); return 0; diff --git a/01_extended_init/main.c b/01_extended_init/main.c index f880c7f..ff10939 100644 --- a/01_extended_init/main.c +++ b/01_extended_init/main.c @@ -8,84 +8,76 @@ |******************************************************************************| | Extended Initialisation. Some extra detail. | \******************************************************************************/ -#include // include GLEW and new version of GL on Windows +#include // include GLEW and new version of GL on Windows #include // GLFW helper library #include #include #include #include // for doing gl_log() functions that work like printf() -//#include /* for visual studio i had to comment this out and define pure-C bool :( */ +//#include /* for visual studio i had to comment this out and define +//pure-C bool :( */ #define bool int #define true 1 #define false 0 #define GL_LOG_FILE "gl.log" /* start a new log file. put the time and date at the top */ -bool restart_gl_log () { +bool restart_gl_log() { time_t now; - char* date; - FILE* file = fopen (GL_LOG_FILE, "w"); + char *date; + FILE *file = fopen( GL_LOG_FILE, "w" ); - if (!file) { - fprintf ( - stderr, - "ERROR: could not open GL_LOG_FILE log file %s for writing\n", - GL_LOG_FILE - ); + if ( !file ) { + fprintf( stderr, "ERROR: could not open GL_LOG_FILE log file %s for writing\n", + GL_LOG_FILE ); return false; } - now = time (NULL); - date = ctime (&now); - fprintf (file, "GL_LOG_FILE log. local time %s", date); - fprintf (file, "build version: %s %s\n\n", __DATE__, __TIME__); - fclose (file); + now = time( NULL ); + date = ctime( &now ); + fprintf( file, "GL_LOG_FILE log. local time %s", date ); + fprintf( file, "build version: %s %s\n\n", __DATE__, __TIME__ ); + fclose( file ); return true; } /* add a message to the log file. arguments work the same way as printf() */ -bool gl_log (const char* message, ...) { +bool gl_log( const char *message, ... ) { va_list argptr; - FILE* file = fopen (GL_LOG_FILE, "a"); - if (!file) { - fprintf ( - stderr, - "ERROR: could not open GL_LOG_FILE %s file for appending\n", - GL_LOG_FILE - ); + FILE *file = fopen( GL_LOG_FILE, "a" ); + if ( !file ) { + fprintf( stderr, "ERROR: could not open GL_LOG_FILE %s file for appending\n", + GL_LOG_FILE ); return false; } - va_start (argptr, message); - vfprintf (file, message, argptr); - va_end (argptr); - fclose (file); + va_start( argptr, message ); + vfprintf( file, message, argptr ); + va_end( argptr ); + fclose( file ); return true; } /* same as gl_log except also prints to stderr */ -bool gl_log_err (const char* message, ...) { +bool gl_log_err( const char *message, ... ) { va_list argptr; - FILE* file = fopen (GL_LOG_FILE, "a"); - if (!file) { - fprintf ( - stderr, - "ERROR: could not open GL_LOG_FILE %s file for appending\n", - GL_LOG_FILE - ); + FILE *file = fopen( GL_LOG_FILE, "a" ); + if ( !file ) { + fprintf( stderr, "ERROR: could not open GL_LOG_FILE %s file for appending\n", + GL_LOG_FILE ); return false; } - va_start (argptr, message); - vfprintf (file, message, argptr); - va_end (argptr); - va_start (argptr, message); - vfprintf (stderr, message, argptr); - va_end (argptr); - fclose (file); + va_start( argptr, message ); + vfprintf( file, message, argptr ); + va_end( argptr ); + va_start( argptr, message ); + vfprintf( stderr, message, argptr ); + va_end( argptr ); + fclose( file ); return true; } /* we will tell GLFW to run this function whenever it finds an error */ -void glfw_error_callback (int error, const char* description) { - gl_log_err ("GLFW ERROR: code %i msg: %s\n", error, description); +void glfw_error_callback( int error, const char *description ) { + gl_log_err( "GLFW ERROR: code %i msg: %s\n", error, description ); } // keep track of window size for things like the viewport and the mouse cursor @@ -93,20 +85,17 @@ int g_gl_width = 640; int g_gl_height = 480; /* we will tell GLFW to run this function whenever the window is resized */ -void glfw_window_size_callback (GLFWwindow* window, int width, int height) { +void glfw_window_size_callback( GLFWwindow *window, int width, int height ) { g_gl_width = width; g_gl_height = height; - printf ("width %i height %i\n", width, height); + printf( "width %i height %i\n", width, height ); /* update any perspective matrices used here */ } /* we can use a function like this to print some GL capabilities of our adapter to the log file. handy if we want to debug problems on other people's computers */ -void log_gl_params () { - int i; - int v[2]; - unsigned char s = 0; +void log_gl_params() { GLenum params[] = { GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, GL_MAX_CUBE_MAP_TEXTURE_SIZE, @@ -121,7 +110,7 @@ void log_gl_params () { GL_MAX_VIEWPORT_DIMS, GL_STEREO, }; - const char* names[] = { + const char *names[] = { "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS", "GL_MAX_CUBE_MAP_TEXTURE_SIZE", "GL_MAX_DRAW_BUFFERS", @@ -135,163 +124,158 @@ void log_gl_params () { "GL_MAX_VIEWPORT_DIMS", "GL_STEREO", }; - gl_log ("GL Context Params:\n"); + gl_log( "GL Context Params:\n" ); // integers - only works if the order is 0-10 integer return types - for (i = 0; i < 10; i++) { + for ( int i = 0; i < 10; i++ ) { int v = 0; - glGetIntegerv (params[i], &v); - gl_log ("%s %i\n", names[i], v); + glGetIntegerv( params[i], &v ); + gl_log( "%s %i\n", names[i], v ); } // others + int v[2]; v[0] = v[1] = 0; - glGetIntegerv (params[10], v); - gl_log ("%s %i %i\n", names[10], v[0], v[1]); - glGetBooleanv (params[11], &s); - gl_log ("%s %i\n", names[11], (unsigned int)s); - gl_log ("-----------------------------\n"); + glGetIntegerv( params[10], v ); + gl_log( "%s %i %i\n", names[10], v[0], v[1] ); + unsigned char s = 0; + glGetBooleanv( params[11], &s ); + gl_log( "%s %i\n", names[11], (unsigned int)s ); + gl_log( "-----------------------------\n" ); } double previous_seconds; int frame_count; /* we will use this function to update the window title with a frame rate */ -void _update_fps_counter (GLFWwindow* window) { +void _update_fps_counter( GLFWwindow *window ) { double current_seconds; double elapsed_seconds; char tmp[128]; - - current_seconds = glfwGetTime (); + + current_seconds = glfwGetTime(); elapsed_seconds = current_seconds - previous_seconds; - if (elapsed_seconds > 0.25) { + if ( elapsed_seconds > 0.25 ) { previous_seconds = current_seconds; - + double fps = (double)frame_count / elapsed_seconds; - sprintf (tmp, "opengl @ fps: %.2f", fps); - glfwSetWindowTitle (window, tmp); + sprintf( tmp, "opengl @ fps: %.2f", fps ); + glfwSetWindowTitle( window, tmp ); frame_count = 0; } frame_count++; } -int main () { - GLFWwindow* window; - const GLubyte* renderer; - const GLubyte* version; - GLfloat points[] = { - 0.0f, 0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - -0.5f, -0.5f, 0.0f - }; +int main() { + GLFWwindow *window; + const GLubyte *renderer; + const GLubyte *version; + GLfloat points[] = { 0.0f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f }; GLuint vbo; GLuint vao; - const char* vertex_shader = - "#version 410\n" - "in vec3 vp;" - "void main () {" - " gl_Position = vec4 (vp, 1.0);" - "}"; - - const char* fragment_shader = - "#version 410\n" - "out vec4 frag_colour;" - "void main () {" - " frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);" - "}"; + const char *vertex_shader = "#version 410\n" + "in vec3 vp;" + "void main () {" + " gl_Position = vec4 (vp, 1.0);" + "}"; + + const char *fragment_shader = "#version 410\n" + "out vec4 frag_colour;" + "void main () {" + " frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);" + "}"; GLuint shader_programme, vs, fs; - restart_gl_log (); + restart_gl_log(); // start GL context and O/S window using the GLFW helper library - gl_log ("starting GLFW\n%s\n", glfwGetVersionString ()); + gl_log( "starting GLFW\n%s\n", glfwGetVersionString() ); // register the error call-back function that we wrote, above - glfwSetErrorCallback (glfw_error_callback); - if (!glfwInit ()) { - fprintf (stderr, "ERROR: could not start GLFW3\n"); + glfwSetErrorCallback( glfw_error_callback ); + if ( !glfwInit() ) { + fprintf( stderr, "ERROR: could not start GLFW3\n" ); return 1; } - /* We must specify 3.2 core if on Apple OS X -- other O/S can specify - anything here. I defined 'APPLE' in the makefile for OS X */ +/* We must specify 3.2 core if on Apple OS X -- other O/S can specify + anything here. I defined 'APPLE' in the makefile for OS X */ #ifdef APPLE - glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2); - glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); - glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 ); + glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 2 ); + glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE ); + glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE ); #endif /* we can run a full-screen window here */ - + /*GLFWmonitor* mon = glfwGetPrimaryMonitor (); const GLFWvidmode* vmode = glfwGetVideoMode (mon); GLFWwindow* window = glfwCreateWindow ( vmode->width, vmode->height, "Extended GL Init", mon, NULL );*/ - window = glfwCreateWindow ( - g_gl_width, g_gl_height, "Extended Init.", NULL, NULL - ); - if (!window) { - fprintf (stderr, "ERROR: could not open window with GLFW3\n"); + window = + glfwCreateWindow( g_gl_width, g_gl_height, "Extended Init.", NULL, NULL ); + if ( !window ) { + fprintf( stderr, "ERROR: could not open window with GLFW3\n" ); glfwTerminate(); return 1; } - glfwSetWindowSizeCallback (window, glfw_window_size_callback); - glfwMakeContextCurrent (window); - - glfwWindowHint (GLFW_SAMPLES, 4); + glfwSetWindowSizeCallback( window, glfw_window_size_callback ); + glfwMakeContextCurrent( window ); + + glfwWindowHint( GLFW_SAMPLES, 4 ); // start GLEW extension handler glewExperimental = GL_TRUE; - glewInit (); + glewInit(); // get version info - renderer = glGetString (GL_RENDERER); // get renderer string - version = glGetString (GL_VERSION); // version as a string - printf ("Renderer: %s\n", renderer); - printf ("OpenGL version supported %s\n", version); - gl_log ("renderer: %s\nversion: %s\n", renderer, version); - log_gl_params (); + renderer = glGetString( GL_RENDERER ); // get renderer string + version = glGetString( GL_VERSION ); // version as a string + printf( "Renderer: %s\n", renderer ); + printf( "OpenGL version supported %s\n", version ); + gl_log( "renderer: %s\nversion: %s\n", renderer, version ); + log_gl_params(); // tell GL to only draw onto a pixel if the shape is closer to the viewer - glEnable (GL_DEPTH_TEST); // enable depth-testing - glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer" - - glGenBuffers (1, &vbo); - glBindBuffer (GL_ARRAY_BUFFER, vbo); - glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (GLfloat), points, GL_STATIC_DRAW); - - glGenVertexArrays (1, &vao); - glBindVertexArray (vao); - glEnableVertexAttribArray (0); - glBindBuffer (GL_ARRAY_BUFFER, vbo); - glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL); - - vs = glCreateShader (GL_VERTEX_SHADER); - glShaderSource (vs, 1, &vertex_shader, NULL); - glCompileShader (vs); - fs = glCreateShader (GL_FRAGMENT_SHADER); - glShaderSource (fs, 1, &fragment_shader, NULL); - glCompileShader (fs); - shader_programme = glCreateProgram (); - glAttachShader (shader_programme, fs); - glAttachShader (shader_programme, vs); - glLinkProgram (shader_programme); - - while (!glfwWindowShouldClose (window)) { - _update_fps_counter (window); + glEnable( GL_DEPTH_TEST ); // enable depth-testing + glDepthFunc( GL_LESS ); // depth-testing interprets a smaller value as "closer" + + glGenBuffers( 1, &vbo ); + glBindBuffer( GL_ARRAY_BUFFER, vbo ); + glBufferData( GL_ARRAY_BUFFER, 9 * sizeof( GLfloat ), points, GL_STATIC_DRAW ); + + glGenVertexArrays( 1, &vao ); + glBindVertexArray( vao ); + glEnableVertexAttribArray( 0 ); + glBindBuffer( GL_ARRAY_BUFFER, vbo ); + glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, NULL ); + + vs = glCreateShader( GL_VERTEX_SHADER ); + glShaderSource( vs, 1, &vertex_shader, NULL ); + glCompileShader( vs ); + fs = glCreateShader( GL_FRAGMENT_SHADER ); + glShaderSource( fs, 1, &fragment_shader, NULL ); + glCompileShader( fs ); + shader_programme = glCreateProgram(); + glAttachShader( shader_programme, fs ); + glAttachShader( shader_programme, vs ); + glLinkProgram( shader_programme ); + + while ( !glfwWindowShouldClose( window ) ) { + _update_fps_counter( window ); // wipe the drawing surface clear - glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glViewport (0, 0, g_gl_width, g_gl_height); - - glUseProgram (shader_programme); - glBindVertexArray (vao); + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + glViewport( 0, 0, g_gl_width, g_gl_height ); + + glUseProgram( shader_programme ); + glBindVertexArray( vao ); // draw points 0-3 from the currently bound VAO with current in-use shader - glDrawArrays (GL_TRIANGLES, 0, 3); - // update other events like input handling - glfwPollEvents (); - if (GLFW_PRESS == glfwGetKey (window, GLFW_KEY_ESCAPE)) { - glfwSetWindowShouldClose (window, 1); + glDrawArrays( GL_TRIANGLES, 0, 3 ); + // update other events like input handling + glfwPollEvents(); + if ( GLFW_PRESS == glfwGetKey( window, GLFW_KEY_ESCAPE ) ) { + glfwSetWindowShouldClose( window, 1 ); } // put the stuff we've been drawing onto the display - glfwSwapBuffers (window); + glfwSwapBuffers( window ); } - + // close GL context and any other GLFW resources glfwTerminate(); return 0; diff --git a/02_shaders/gl_utils.c b/02_shaders/gl_utils.c index d5ba153..8e04c90 100644 --- a/02_shaders/gl_utils.c +++ b/02_shaders/gl_utils.c @@ -21,86 +21,77 @@ #define MAX_SHADER_LENGTH 262144 /*--------------------------------LOG FUNCTIONS------------------------------*/ -bool restart_gl_log () { +bool restart_gl_log() { time_t now; - char* date; - FILE* file = fopen (GL_LOG_FILE, "w"); - - if (!file) { - fprintf ( - stderr, - "ERROR: could not open GL_LOG_FILE log file %s for writing\n", - GL_LOG_FILE - ); + char *date; + FILE *file = fopen( GL_LOG_FILE, "w" ); + + if ( !file ) { + fprintf( stderr, "ERROR: could not open GL_LOG_FILE log file %s for writing\n", + GL_LOG_FILE ); return false; } - now = time (NULL); - date = ctime (&now); - fprintf (file, "GL_LOG_FILE log. local time %s\n", date); - fclose (file); + now = time( NULL ); + date = ctime( &now ); + fprintf( file, "GL_LOG_FILE log. local time %s\n", date ); + fclose( file ); return true; } -bool gl_log (const char* message, ...) { +bool gl_log( const char *message, ... ) { va_list argptr; - FILE* file = fopen (GL_LOG_FILE, "a"); - if (!file) { - fprintf ( - stderr, - "ERROR: could not open GL_LOG_FILE %s file for appending\n", - GL_LOG_FILE - ); + FILE *file = fopen( GL_LOG_FILE, "a" ); + if ( !file ) { + fprintf( stderr, "ERROR: could not open GL_LOG_FILE %s file for appending\n", + GL_LOG_FILE ); return false; } - va_start (argptr, message); - vfprintf (file, message, argptr); - va_end (argptr); - fclose (file); + va_start( argptr, message ); + vfprintf( file, message, argptr ); + va_end( argptr ); + fclose( file ); return true; } /* same as gl_log except also prints to stderr */ -bool gl_log_err (const char* message, ...) { +bool gl_log_err( const char *message, ... ) { va_list argptr; - FILE* file = fopen (GL_LOG_FILE, "a"); - if (!file) { - fprintf ( - stderr, - "ERROR: could not open GL_LOG_FILE %s file for appending\n", - GL_LOG_FILE - ); + FILE *file = fopen( GL_LOG_FILE, "a" ); + if ( !file ) { + fprintf( stderr, "ERROR: could not open GL_LOG_FILE %s file for appending\n", + GL_LOG_FILE ); return false; } - va_start (argptr, message); - vfprintf (file, message, argptr); - va_end (argptr); - va_start (argptr, message); - vfprintf (stderr, message, argptr); - va_end (argptr); - fclose (file); + va_start( argptr, message ); + vfprintf( file, message, argptr ); + va_end( argptr ); + va_start( argptr, message ); + vfprintf( stderr, message, argptr ); + va_end( argptr ); + fclose( file ); return true; } /*--------------------------------GLFW3 and GLEW-----------------------------*/ -bool start_gl () { - const GLubyte* renderer; - const GLubyte* version; +bool start_gl() { + const GLubyte *renderer; + const GLubyte *version; - gl_log ("starting GLFW %s", glfwGetVersionString ()); + gl_log( "starting GLFW %s", glfwGetVersionString() ); - glfwSetErrorCallback (glfw_error_callback); - if (!glfwInit ()) { - fprintf (stderr, "ERROR: could not start GLFW3\n"); + glfwSetErrorCallback( glfw_error_callback ); + if ( !glfwInit() ) { + fprintf( stderr, "ERROR: could not start GLFW3\n" ); return false; } - /* We must specify 3.2 core if on Apple OS X -- other O/S can specify - anything here. I defined 'APPLE' in the makefile for OS X */ +/* We must specify 3.2 core if on Apple OS X -- other O/S can specify + anything here. I defined 'APPLE' in the makefile for OS X */ #ifdef APPLE - glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2); - glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); - glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 ); + glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 2 ); + glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE ); + glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE ); #endif /*GLFWmonitor* mon = glfwGetPrimaryMonitor (); const GLFWvidmode* vmode = glfwGetVideoMode (mon); @@ -108,64 +99,63 @@ bool start_gl () { vmode->width, vmode->height, "Extended GL Init", mon, NULL );*/ - g_window = glfwCreateWindow ( - g_gl_width, g_gl_height, "Extended Init.", NULL, NULL - ); - if (!g_window) { - fprintf (stderr, "ERROR: could not open window with GLFW3\n"); + g_window = + glfwCreateWindow( g_gl_width, g_gl_height, "Shaders", NULL, NULL ); + if ( !g_window ) { + fprintf( stderr, "ERROR: could not open window with GLFW3\n" ); glfwTerminate(); return false; } - glfwSetWindowSizeCallback (g_window, glfw_window_size_callback); - glfwMakeContextCurrent (g_window); + glfwSetWindowSizeCallback( g_window, glfw_window_size_callback ); + glfwMakeContextCurrent( g_window ); - glfwWindowHint (GLFW_SAMPLES, 4); + glfwWindowHint( GLFW_SAMPLES, 4 ); /* start GLEW extension handler */ glewExperimental = GL_TRUE; - glewInit (); + glewInit(); /* get version info */ - renderer = glGetString (GL_RENDERER); /* get renderer string */ - version = glGetString (GL_VERSION); /* version as a string */ - printf ("Renderer: %s\n", renderer); - printf ("OpenGL version supported %s\n", version); - gl_log ("renderer: %s\nversion: %s\n", renderer, version); + renderer = glGetString( GL_RENDERER ); /* get renderer string */ + version = glGetString( GL_VERSION ); /* version as a string */ + printf( "Renderer: %s\n", renderer ); + printf( "OpenGL version supported %s\n", version ); + gl_log( "renderer: %s\nversion: %s\n", renderer, version ); return true; } -void glfw_error_callback (int error, const char* description) { - fputs (description, stderr); - gl_log_err ("%s\n", description); +void glfw_error_callback( int error, const char *description ) { + fputs( description, stderr ); + gl_log_err( "%s\n", description ); } /* a call-back function */ -void glfw_window_size_callback (GLFWwindow* window, int width, int height) { +void glfw_window_size_callback( GLFWwindow *window, int width, int height ) { g_gl_width = width; g_gl_height = height; - printf ("width %i height %i\n", width, height); + printf( "width %i height %i\n", width, height ); /* update any perspective matrices used here */ } double previous_seconds; int frame_count; -void _update_fps_counter (GLFWwindow* window) { +void _update_fps_counter( GLFWwindow *window ) { double current_seconds; double elapsed_seconds; - previous_seconds = glfwGetTime (); - current_seconds = glfwGetTime (); + previous_seconds = glfwGetTime(); + current_seconds = glfwGetTime(); elapsed_seconds = current_seconds - previous_seconds; - if (elapsed_seconds > 0.25) { + if ( elapsed_seconds > 0.25 ) { double fps; char tmp[128]; previous_seconds = current_seconds; fps = (double)frame_count / elapsed_seconds; - sprintf (tmp, "opengl @ fps: %.2f", fps); - glfwSetWindowTitle (window, tmp); + sprintf( tmp, "opengl @ fps: %.2f", fps ); + glfwSetWindowTitle( window, tmp ); frame_count = 0; } frame_count++; diff --git a/02_shaders/gl_utils.h b/02_shaders/gl_utils.h index 0b7afd0..90d165a 100644 --- a/02_shaders/gl_utils.h +++ b/02_shaders/gl_utils.h @@ -9,10 +9,11 @@ #ifndef _GL_UTILS_H_ #define _GL_UTILS_H_ -#include /* include GLEW and new version of GL on Windows */ +#include /* include GLEW and new version of GL on Windows */ #include /* GLFW helper library */ #include -/* #include // for visual studio i had to comment this out and define pure-C bool :( */ +/* #include // for visual studio i had to comment this out and define + * pure-C bool :( */ #define bool int #define true 1 #define false 0 @@ -21,23 +22,23 @@ extern int g_gl_width; extern int g_gl_height; -extern GLFWwindow* g_window; +extern GLFWwindow *g_window; -bool start_gl (); +bool start_gl(); -bool restart_gl_log (); +bool restart_gl_log(); -bool gl_log (const char* message, ...); +bool gl_log( const char *message, ... ); /* same as gl_log except also prints to stderr */ -bool gl_log_err (const char* message, ...); +bool gl_log_err( const char *message, ... ); -void glfw_error_callback (int error, const char* description); +void glfw_error_callback( int error, const char *description ); -void log_gl_params (); +void log_gl_params(); -void _update_fps_counter (GLFWwindow* window); +void _update_fps_counter( GLFWwindow *window ); -void glfw_window_size_callback (GLFWwindow* window, int width, int height); +void glfw_window_size_callback( GLFWwindow *window, int width, int height ); #endif diff --git a/02_shaders/main.c b/02_shaders/main.c index a6f5757..67e546a 100644 --- a/02_shaders/main.c +++ b/02_shaders/main.c @@ -13,7 +13,7 @@ | uncomment the version number code if on an Apple machine | \******************************************************************************/ #include "gl_utils.h" -#include /* include GLEW and new version of GL on Windows */ +#include /* include GLEW and new version of GL on Windows */ #include /* GLFW helper library */ #include #include @@ -29,55 +29,69 @@ cursor */ int g_gl_width = 640; int g_gl_height = 480; -GLFWwindow* g_window = NULL; - -const char* GL_type_to_string (GLenum type) { - switch (type) { - case GL_BOOL: return "bool"; - case GL_INT: return "int"; - case GL_FLOAT: return "float"; - case GL_FLOAT_VEC2: return "vec2"; - case GL_FLOAT_VEC3: return "vec3"; - case GL_FLOAT_VEC4: return "vec4"; - case GL_FLOAT_MAT2: return "mat2"; - case GL_FLOAT_MAT3: return "mat3"; - case GL_FLOAT_MAT4: return "mat4"; - case GL_SAMPLER_2D: return "sampler2D"; - case GL_SAMPLER_3D: return "sampler3D"; - case GL_SAMPLER_CUBE: return "samplerCube"; - case GL_SAMPLER_2D_SHADOW: return "sampler2DShadow"; - default: break; +GLFWwindow *g_window = NULL; + +const char *GL_type_to_string( GLenum type ) { + switch ( type ) { + case GL_BOOL: + return "bool"; + case GL_INT: + return "int"; + case GL_FLOAT: + return "float"; + case GL_FLOAT_VEC2: + return "vec2"; + case GL_FLOAT_VEC3: + return "vec3"; + case GL_FLOAT_VEC4: + return "vec4"; + case GL_FLOAT_MAT2: + return "mat2"; + case GL_FLOAT_MAT3: + return "mat3"; + case GL_FLOAT_MAT4: + return "mat4"; + case GL_SAMPLER_2D: + return "sampler2D"; + case GL_SAMPLER_3D: + return "sampler3D"; + case GL_SAMPLER_CUBE: + return "samplerCube"; + case GL_SAMPLER_2D_SHADOW: + return "sampler2DShadow"; + default: + break; } return "other"; } /* print errors in shader compilation */ -void _print_shader_info_log (GLuint shader_index) { +void _print_shader_info_log( GLuint shader_index ) { int max_length = 2048; int actual_length = 0; char log[2048]; - glGetShaderInfoLog (shader_index, max_length, &actual_length, log); - printf ("shader info log for GL index %i:\n%s\n", shader_index, log); + glGetShaderInfoLog( shader_index, max_length, &actual_length, log ); + printf( "shader info log for GL index %i:\n%s\n", shader_index, log ); } /* print errors in shader linking */ -void _print_programme_info_log (GLuint sp) { +void _print_programme_info_log( GLuint sp ) { int max_length = 2048; int actual_length = 0; char log[2048]; - glGetProgramInfoLog (sp, max_length, &actual_length, log); - printf ("program info log for GL index %i:\n%s", sp, log); + glGetProgramInfoLog( sp, max_length, &actual_length, log ); + printf( "program info log for GL index %i:\n%s", sp, log ); } /* validate shader */ -bool is_valid (GLuint sp) { +bool is_valid( GLuint sp ) { int params = -1; - glValidateProgram (sp); - glGetProgramiv (sp, GL_VALIDATE_STATUS, ¶ms); - printf ("program %i GL_VALIDATE_STATUS = %i\n", sp, params); - if (GL_TRUE != params) { - _print_programme_info_log (sp); + glValidateProgram( sp ); + glGetProgramiv( sp, GL_VALIDATE_STATUS, ¶ms ); + printf( "program %i GL_VALIDATE_STATUS = %i\n", sp, params ); + if ( GL_TRUE != params ) { + _print_programme_info_log( sp ); return false; } return true; @@ -85,209 +99,197 @@ bool is_valid (GLuint sp) { /* print absolutely everything about a shader - only useful if you get really stuck wondering why a shader isn't working properly */ -void print_all (GLuint sp) { +void print_all( GLuint sp ) { int params = -1; - int i; - printf ("--------------------\nshader programme %i info:\n", sp); - glGetProgramiv (sp, GL_LINK_STATUS, ¶ms); - printf ("GL_LINK_STATUS = %i\n", params); + printf( "--------------------\nshader programme %i info:\n", sp ); + glGetProgramiv( sp, GL_LINK_STATUS, ¶ms ); + printf( "GL_LINK_STATUS = %i\n", params ); - glGetProgramiv (sp, GL_ATTACHED_SHADERS, ¶ms); - printf ("GL_ATTACHED_SHADERS = %i\n", params); + glGetProgramiv( sp, GL_ATTACHED_SHADERS, ¶ms ); + printf( "GL_ATTACHED_SHADERS = %i\n", params ); - glGetProgramiv (sp, GL_ACTIVE_ATTRIBUTES, ¶ms); - printf ("GL_ACTIVE_ATTRIBUTES = %i\n", params); + glGetProgramiv( sp, GL_ACTIVE_ATTRIBUTES, ¶ms ); + printf( "GL_ACTIVE_ATTRIBUTES = %i\n", params ); - for (i = 0; i < params; i++) { + for ( int i = 0; i < params; i++ ) { char name[64]; int max_length = 64; int actual_length = 0; int size = 0; GLenum type; - glGetActiveAttrib (sp, i, max_length, &actual_length, &size, &type, - name); - if (size > 1) { + glGetActiveAttrib( sp, i, max_length, &actual_length, &size, &type, name ); + if ( size > 1 ) { int j; - for (j = 0; j < size; j++) { + for ( j = 0; j < size; j++ ) { char long_name[64]; int location; - sprintf (long_name, "%s[%i]", name, j); - location = glGetAttribLocation (sp, long_name); - printf (" %i) type:%s name:%s location:%i\n", - i, GL_type_to_string (type), long_name, location); + sprintf( long_name, "%s[%i]", name, j ); + location = glGetAttribLocation( sp, long_name ); + printf( " %i) type:%s name:%s location:%i\n", i, GL_type_to_string( type ), + long_name, location ); } } else { - int location = glGetAttribLocation (sp, name); - printf (" %i) type:%s name:%s location:%i\n", - i, GL_type_to_string (type), name, location); + int location = glGetAttribLocation( sp, name ); + printf( " %i) type:%s name:%s location:%i\n", i, GL_type_to_string( type ), + name, location ); } } - glGetProgramiv (sp, GL_ACTIVE_UNIFORMS, ¶ms); - printf ("GL_ACTIVE_UNIFORMS = %i\n", params); - for (i = 0; i < params; i++) { + glGetProgramiv( sp, GL_ACTIVE_UNIFORMS, ¶ms ); + printf( "GL_ACTIVE_UNIFORMS = %i\n", params ); + for ( int i = 0; i < params; i++ ) { char name[64]; int max_length = 64; int actual_length = 0; int size = 0; GLenum type; - glGetActiveUniform (sp, i, max_length, &actual_length, &size, &type, - name); - if (size > 1) { + glGetActiveUniform( sp, i, max_length, &actual_length, &size, &type, name ); + if ( size > 1 ) { int j; - for (j = 0; j < size; j++) { + for ( j = 0; j < size; j++ ) { char long_name[64]; int location; - sprintf (long_name, "%s[%i]", name, j); - location = glGetUniformLocation (sp, long_name); - printf (" %i) type:%s name:%s location:%i\n", - i, GL_type_to_string (type), long_name, location); + sprintf( long_name, "%s[%i]", name, j ); + location = glGetUniformLocation( sp, long_name ); + printf( " %i) type:%s name:%s location:%i\n", i, GL_type_to_string( type ), + long_name, location ); } } else { - int location = glGetUniformLocation (sp, name); - printf (" %i) type:%s name:%s location:%i\n", - i, GL_type_to_string (type), name, location); + int location = glGetUniformLocation( sp, name ); + printf( " %i) type:%s name:%s location:%i\n", i, GL_type_to_string( type ), + name, location ); } } - _print_programme_info_log (sp); + _print_programme_info_log( sp ); } /* copy a shader from a plain text file into a character array */ -bool parse_file_into_str (const char* file_name, char* shader_str, int max_len) { - FILE* file = fopen (file_name , "r"); - if (!file) { - gl_log_err ("ERROR: opening file for reading: %s\n", file_name); +bool parse_file_into_str( const char *file_name, char *shader_str, int max_len ) { + FILE *file = fopen( file_name, "r" ); + if ( !file ) { + gl_log_err( "ERROR: opening file for reading: %s\n", file_name ); return false; } - size_t cnt = fread (shader_str, 1, max_len - 1, file); - if ((int)cnt >= max_len - 1) { - gl_log_err ("WARNING: file %s too big - truncated.\n", file_name); + size_t cnt = fread( shader_str, 1, max_len - 1, file ); + if ( (int)cnt >= max_len - 1 ) { + gl_log_err( "WARNING: file %s too big - truncated.\n", file_name ); } - if (ferror (file)) { - gl_log_err ("ERROR: reading shader file %s\n", file_name); - fclose (file); + if ( ferror( file ) ) { + gl_log_err( "ERROR: reading shader file %s\n", file_name ); + fclose( file ); return false; } // append \0 to end of file string shader_str[cnt] = 0; - fclose (file); + fclose( file ); return true; } -int main () { - GLfloat points[] = { - 0.0f, 0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - -0.5f, -0.5f, 0.0f - }; +int main() { + GLfloat points[] = { 0.0f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f }; GLuint vbo; GLuint vao; char vertex_shader[1024 * 256]; char fragment_shader[1024 * 256]; GLuint vs, fs, shader_programme; - const GLchar* p; + const GLchar *p; int params = -1; GLint colour_loc; - restart_gl_log (); - start_gl (); + restart_gl_log(); + start_gl(); /* tell GL to only draw onto a pixel if the shape is closer to the viewer*/ - glEnable (GL_DEPTH_TEST); /* enable depth-testing */ - glDepthFunc (GL_LESS); /* depth-testing interprets a smaller value as - "closer" */ + glEnable( GL_DEPTH_TEST ); /* enable depth-testing */ + glDepthFunc( GL_LESS ); /* depth-testing interprets a smaller value as + "closer" */ + glGenBuffers( 1, &vbo ); + glBindBuffer( GL_ARRAY_BUFFER, vbo ); + glBufferData( GL_ARRAY_BUFFER, 9 * sizeof( GLfloat ), points, GL_STATIC_DRAW ); - glGenBuffers (1, &vbo); - glBindBuffer (GL_ARRAY_BUFFER, vbo); - glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (GLfloat), points, - GL_STATIC_DRAW); - - glGenVertexArrays (1, &vao); - glBindVertexArray (vao); - glEnableVertexAttribArray (0); - glBindBuffer (GL_ARRAY_BUFFER, vbo); - glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL); + glGenVertexArrays( 1, &vao ); + glBindVertexArray( vao ); + glEnableVertexAttribArray( 0 ); + glBindBuffer( GL_ARRAY_BUFFER, vbo ); + glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, NULL ); /* load shaders from files here */ - parse_file_into_str ("test_vs.glsl", vertex_shader, 1024 * 256); - parse_file_into_str ("test_fs.glsl", fragment_shader, 1024 * 256); + parse_file_into_str( "test_vs.glsl", vertex_shader, 1024 * 256 ); + parse_file_into_str( "test_fs.glsl", fragment_shader, 1024 * 256 ); - vs = glCreateShader (GL_VERTEX_SHADER); - p = (const GLchar*)vertex_shader; - glShaderSource (vs, 1, &p, NULL); - glCompileShader (vs); + vs = glCreateShader( GL_VERTEX_SHADER ); + p = (const GLchar *)vertex_shader; + glShaderSource( vs, 1, &p, NULL ); + glCompileShader( vs ); /* check for shader compile errors - very important! */ - glGetShaderiv (vs, GL_COMPILE_STATUS, ¶ms); - if (GL_TRUE != params) { - fprintf (stderr, "ERROR: GL shader index %i did not compile\n", vs); - _print_shader_info_log (vs); + glGetShaderiv( vs, GL_COMPILE_STATUS, ¶ms ); + if ( GL_TRUE != params ) { + fprintf( stderr, "ERROR: GL shader index %i did not compile\n", vs ); + _print_shader_info_log( vs ); return 1; /* or exit or something */ } - fs = glCreateShader (GL_FRAGMENT_SHADER); - p = (const GLchar*)fragment_shader; - glShaderSource (fs, 1, &p, NULL); - glCompileShader (fs); + fs = glCreateShader( GL_FRAGMENT_SHADER ); + p = (const GLchar *)fragment_shader; + glShaderSource( fs, 1, &p, NULL ); + glCompileShader( fs ); /* check for compile errors */ - glGetShaderiv (fs, GL_COMPILE_STATUS, ¶ms); - if (GL_TRUE != params) { - fprintf (stderr, "ERROR: GL shader index %i did not compile\n", fs); - _print_shader_info_log (fs); + glGetShaderiv( fs, GL_COMPILE_STATUS, ¶ms ); + if ( GL_TRUE != params ) { + fprintf( stderr, "ERROR: GL shader index %i did not compile\n", fs ); + _print_shader_info_log( fs ); return 1; /* or exit or something */ } - shader_programme = glCreateProgram (); - glAttachShader (shader_programme, fs); - glAttachShader (shader_programme, vs); - glLinkProgram (shader_programme); + shader_programme = glCreateProgram(); + glAttachShader( shader_programme, fs ); + glAttachShader( shader_programme, vs ); + glLinkProgram( shader_programme ); /* check for shader linking errors - very important! */ - glGetProgramiv (shader_programme, GL_LINK_STATUS, ¶ms); - if (GL_TRUE != params) { - fprintf ( - stderr, - "ERROR: could not link shader programme GL index %i\n", - shader_programme - ); - _print_programme_info_log (shader_programme); + glGetProgramiv( shader_programme, GL_LINK_STATUS, ¶ms ); + if ( GL_TRUE != params ) { + fprintf( stderr, "ERROR: could not link shader programme GL index %i\n", + shader_programme ); + _print_programme_info_log( shader_programme ); return 1; } - print_all (shader_programme); - bool result = is_valid (shader_programme); - assert (result); + print_all( shader_programme ); + bool result = is_valid( shader_programme ); + assert( result ); - colour_loc = glGetUniformLocation (shader_programme, "inputColour"); - assert (colour_loc > -1); - glUseProgram (shader_programme); - glUniform4f (colour_loc, 1.0f, 0.0f, 0.0f, 1.0f); + colour_loc = glGetUniformLocation( shader_programme, "inputColour" ); + assert( colour_loc > -1 ); + glUseProgram( shader_programme ); + glUniform4f( colour_loc, 1.0f, 0.0f, 0.0f, 1.0f ); - while (!glfwWindowShouldClose (g_window)) { - _update_fps_counter (g_window); + while ( !glfwWindowShouldClose( g_window ) ) { + _update_fps_counter( g_window ); /* wipe the drawing surface clear */ - glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glViewport (0, 0, g_gl_width, g_gl_height); + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + glViewport( 0, 0, g_gl_width, g_gl_height ); - glUseProgram (shader_programme); - glBindVertexArray (vao); + glUseProgram( shader_programme ); + glBindVertexArray( vao ); /* draw points 0-3 from the currently bound VAO with current in-use - shader */ - glDrawArrays (GL_TRIANGLES, 0, 3); + shader */ + glDrawArrays( GL_TRIANGLES, 0, 3 ); /* update other events like input handling */ - glfwPollEvents (); - if (GLFW_PRESS == glfwGetKey (g_window, GLFW_KEY_ESCAPE)) { - glfwSetWindowShouldClose (g_window, 1); + glfwPollEvents(); + if ( GLFW_PRESS == glfwGetKey( g_window, GLFW_KEY_ESCAPE ) ) { + glfwSetWindowShouldClose( g_window, 1 ); } /* put the stuff we've been drawing onto the display */ - glfwSwapBuffers (g_window); + glfwSwapBuffers( g_window ); } /* close GL context and any other GLFW resources */