From a21091cad651febca7873cfd8775dc120847d3ad Mon Sep 17 00:00:00 2001 From: Marcel Plch Date: Fri, 16 Feb 2018 23:48:04 +0100 Subject: [PATCH] triangle render (and correct line endings in code) --- CMakeLists.txt | 74 +++++++-------- Include/shader.h | 14 +-- main.c | 8 +- shader.c | 231 +++++++++++++++++++++++++---------------------- 4 files changed, 176 insertions(+), 151 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7407979..e914e93 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,37 +1,37 @@ -# CMake entry point -cmake_minimum_required (VERSION 3.0) -project (OpenGL_Analog_Clock) - -find_package(OpenGL REQUIRED) -find_package(GLEW REQUIRED) -find_package(glfw3 REQUIRED) - -include_directories( - Include/ - . -) - -set(ALL_LIBS - ${OPENGL_LIBRARY} - glfw - GLEW -) - -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb") - -add_definitions( - -DTW_STATIC - -DTW_NO_LIB_PRAGMA - -DTW_NO_DIRECT3D - -DGLEW_STATIC - -D_CRT_SECURE_NO_WARNINGS -) - -add_executable(clock - main.c - shader.c -) - -target_link_libraries(clock - ${ALL_LIBS} -) +# CMake entry point +cmake_minimum_required (VERSION 3.0) +project(OpenGL_Analog_Clock C) + +find_package(OpenGL REQUIRED) +find_package(GLEW REQUIRED) +find_package(glfw3 REQUIRED) + +include_directories( + Include/ + . +) + +set(ALL_LIBS + ${OPENGL_LIBRARY} + glfw + GLEW +) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb") + +add_definitions( + -DTW_STATIC + -DTW_NO_LIB_PRAGMA + -DTW_NO_DIRECT3D + -DGLEW_STATIC + -D_CRT_SECURE_NO_WARNINGS +) + +add_executable(clock + main.c + shader.c +) + +target_link_libraries(clock + ${ALL_LIBS} +) diff --git a/Include/shader.h b/Include/shader.h index 9d3f5c9..b2d381b 100644 --- a/Include/shader.h +++ b/Include/shader.h @@ -1,6 +1,8 @@ -#ifndef SHADER_HPP -#define SHADER_HPP - -GLuint LoadShaders(const char *vertex_file_path, const char *fragment_file_path); - -#endif +#ifndef SHADER_HPP +#define SHADER_HPP + +#define xfree(p) if (p) free((void *)p) + +int LoadShaders(GLuint *, const char *, const char *); + +#endif diff --git a/main.c b/main.c index c39a4cf..f014c9d 100644 --- a/main.c +++ b/main.c @@ -58,7 +58,13 @@ int main(void) { glBindVertexArray(VertexArrayID); // Create and compile our GLSL program from the shaders - GLuint programID = LoadShaders("../shaders/SimpleVertexShader.vertexshader", "../shaders/SimpleFragmentShader.fragmentshader"); + GLuint programID; + if (LoadShaders(&programID, + "../shaders/SimpleVertexShader.vertexshader", + "../shaders/SimpleFragmentShader.fragmentshader")) { + fprintf(stderr, "Could not load shaders.\n"); + return -1; + } static const GLfloat g_vertex_buffer_data[] = { -1.0f, -1.0f, 0.0f, diff --git a/shader.c b/shader.c index 5a0f16e..75ac32b 100644 --- a/shader.c +++ b/shader.c @@ -1,107 +1,124 @@ -#include -#include -#include - -#include - -#include "shader.h" - -static int load_code(const char *filepath, char **code) { - FILE *file; - int cursor; - char c; - - file = fopen(filepath, "r"); - if (file == NULL) { - fprintf(stderr, "Could not open %s.\n", filepath); - return 1; - } - - *code = malloc(256 * sizeof(char)); - if (code == NULL) { - fprintf(stderr, "Out of memory", filepath); - return 1; - } - cursor = 0; - - while ((c = fgetc(file)) != EOF) { - (*code)[cursor] = c; - cursor++; - } - - return 0; -} - -static int compile_code(const char *filepath, const char *code, - GLuint ShaderID, GLuint *Result) { - int InfoLogLength; - // Compile Shader - printf("Compiling shader : %s\n", filepath); - glShaderSource(ShaderID, 1, (const char **)&code, NULL); - glCompileShader(ShaderID); - - // Check Shader - glGetShaderiv(ShaderID, GL_COMPILE_STATUS, Result); - glGetShaderiv(ShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); - if (InfoLogLength > 0) { - char *ShaderErrorMessage = malloc(InfoLogLength+1); - glGetShaderInfoLog(ShaderID, InfoLogLength, NULL, ShaderErrorMessage); - printf("%s\n", ShaderErrorMessage); - free(ShaderErrorMessage); - return -1; - } - return 0; -} - -GLuint -LoadShaders(const char *vertex_file_path, - const char *fragment_file_path) { - char *vertex_code; - char *fragment_code; - - // Create the shaders - GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER); - GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); - - // Read the Vertex Shader code from the file - load_code(vertex_file_path, &vertex_code); - - // Read the Fragment Shader code from the file - load_code(fragment_file_path, &fragment_code); - - GLint Result = GL_FALSE; - - // Compile Vertex Shader - compile_code(vertex_file_path, vertex_code, VertexShaderID, &Result); - - // Compile Fragment Shader - compile_code(fragment_file_path, fragment_code, FragmentShaderID, &Result); - - // Link the program - printf("Linking program\n"); - GLuint ProgramID = glCreateProgram(); - glAttachShader(ProgramID, VertexShaderID); - glAttachShader(ProgramID, FragmentShaderID); - glLinkProgram(ProgramID); - - GLuint InfoLogLength; - // Check the program - glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result); - glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength); - if (InfoLogLength > 0) { - char *ProgramErrorMessage = malloc(InfoLogLength+1); - glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, ProgramErrorMessage); - printf("%s\n", ProgramErrorMessage); - } - - - glDetachShader(ProgramID, VertexShaderID); - glDetachShader(ProgramID, FragmentShaderID); - - glDeleteShader(VertexShaderID); - glDeleteShader(FragmentShaderID); - - return ProgramID; -} - - +#include +#include +#include + +#include + +#include "shader.h" + +static int +load_code(const char *filepath, char **code) { + FILE *file; + int cursor; + char c; + + file = fopen(filepath, "r"); + if (file == NULL) { + fprintf(stderr, "Could not open %s.\n", filepath); + return 1; + } + + *code = malloc(256 * sizeof(char)); + if (code == NULL) { + fprintf(stderr, "Out of memory", filepath); + return 1; + } + cursor = 0; + + while ((c = fgetc(file)) != EOF) { + (*code)[cursor] = c; + cursor++; + } + + return 0; +} + +static int +compile_code(const char *filepath, const char *code, + GLuint ShaderID, GLuint *Result) { + int InfoLogLength; + // Compile Shader + printf("Compiling shader : %s\n", filepath); + glShaderSource(ShaderID, 1, (const char **)&code, NULL); + glCompileShader(ShaderID); + + // Check Shader + glGetShaderiv(ShaderID, GL_COMPILE_STATUS, Result); + glGetShaderiv(ShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); + if (InfoLogLength > 0) { + char *ShaderErrorMessage = malloc(InfoLogLength+1); + glGetShaderInfoLog(ShaderID, InfoLogLength, NULL, ShaderErrorMessage); + printf("%s\n", ShaderErrorMessage); + free(ShaderErrorMessage); + return -1; + } + return 0; +} + +int +LoadShaders(GLuint *programID, const char *vertex_file_path, + const char *fragment_file_path) { + int retval = -1; + const char *vertex_code = NULL; + const char *fragment_code = NULL; + + // Create the shaders + GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER); + GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); + + // Read the Vertex Shader code from the file + if (load_code(vertex_file_path, (char **)&vertex_code)) { + goto end; + } + + // Read the Fragment Shader code from the file + if (load_code(fragment_file_path, (char **)&fragment_code)) { + goto end; + } + + GLint Result = GL_FALSE; + + // Compile Vertex Shader + if (compile_code(vertex_file_path, vertex_code, VertexShaderID, &Result)) { + goto end; + } + + // Compile Fragment Shader + if (compile_code(fragment_file_path, fragment_code, FragmentShaderID, &Result)) { + goto end; + } + + // Link the program + printf("Linking program\n"); + *programID = glCreateProgram(); + glAttachShader(*programID, VertexShaderID); + glAttachShader(*programID, FragmentShaderID); + glLinkProgram(*programID); + + GLuint InfoLogLength; + // Check the program + glGetProgramiv(*programID, GL_LINK_STATUS, &Result); + glGetProgramiv(*programID, GL_INFO_LOG_LENGTH, &InfoLogLength); + if (InfoLogLength > 0) { + char *ProgramErrorMessage = malloc(InfoLogLength+1); + glGetProgramInfoLog(*programID, InfoLogLength, NULL, ProgramErrorMessage); + printf("%s\n", ProgramErrorMessage); + } + + + glDetachShader(*programID, VertexShaderID); + glDetachShader(*programID, FragmentShaderID); + + glDeleteShader(VertexShaderID); + glDeleteShader(FragmentShaderID); + + // If code got here, it means it was successful + retval = 0; + +end: + xfree(vertex_code); + xfree(fragment_code); + return retval; +} + +