From 4400839ae190b60ed6d53d9776721ff58ddabf6e Mon Sep 17 00:00:00 2001 From: Cat Flynn Date: Mon, 31 Jul 2023 00:08:17 +0200 Subject: [PATCH] chore: extract shader compilation functions to file --- CMakeLists.txt | 7 ++++++- src/gfx.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/gfx.hpp | 8 +++++++ src/hello.cpp | 55 ++---------------------------------------------- 4 files changed, 73 insertions(+), 54 deletions(-) create mode 100644 src/gfx.cpp create mode 100644 src/gfx.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2cc85e0..c0bea94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,12 @@ elseif(APPLE) message(FATAL_ERROR "macOS is not supported yet.") endif() -add_executable(${PROJECT_NAME} src/hello.cpp src/io.cpp src/triangle.cpp) +add_executable(${PROJECT_NAME} + src/hello.cpp + src/io.cpp + src/triangle.cpp + src/gfx.cpp +) if(WIN32) add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD diff --git a/src/gfx.cpp b/src/gfx.cpp new file mode 100644 index 0000000..d1996ca --- /dev/null +++ b/src/gfx.cpp @@ -0,0 +1,57 @@ +#include "gfx.hpp" + +#include + +#include "io.hpp" + +GLuint compileShader(const std::string& shaderPath, GLenum shaderType) +{ + GLuint shader; + GLint success; + + std::string shaderSource = readFile(shaderPath); + const char* source = shaderSource.c_str(); + + shader = glCreateShader(shaderType); + glShaderSource(shader, 1, &source, NULL); + glCompileShader(shader); + + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); + if (!success) + { + GLchar infoLog[512]; + glGetShaderInfoLog(shader, 512, NULL, infoLog); + std::cerr << "shader compilation failed" << std::endl + << infoLog << std::endl; + } + + return shader; +} + +GLuint compileShaderProgram() +{ + GLuint vertShader = compileShader("./vert.glsl", GL_VERTEX_SHADER); + GLuint fragShader = compileShader("./frag.glsl", GL_FRAGMENT_SHADER); + + GLuint shaderProgram = glCreateProgram(); + glAttachShader(shaderProgram, vertShader); + glAttachShader(shaderProgram, fragShader); + glLinkProgram(shaderProgram); + + GLint success; + GLchar infoLog[512]; + glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); + if (!success) + { + glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); + std::cerr << "shader linking failed" << std::endl + << infoLog << std::endl; + } + + // We no longer need the individual shaders + glDeleteShader(vertShader); + glDeleteShader(fragShader); + + return shaderProgram; +} + diff --git a/src/gfx.hpp b/src/gfx.hpp new file mode 100644 index 0000000..3704574 --- /dev/null +++ b/src/gfx.hpp @@ -0,0 +1,8 @@ +#pragma once + +#include + +#include "GL/glew.h" + +GLuint compileShaderProgram(); +GLuint compileShader(const std::string& shaderPath, GLenum shaderType); \ No newline at end of file diff --git a/src/hello.cpp b/src/hello.cpp index 82ba3ed..46b039f 100644 --- a/src/hello.cpp +++ b/src/hello.cpp @@ -44,33 +44,9 @@ #include -#include "io.hpp" +#include "gfx.hpp" #include "triangle.hpp" -GLuint compileShader(const std::string& shaderPath, GLenum shaderType) -{ - GLuint shader; - GLint success; - - std::string shaderSource = readFile(shaderPath); - const char* source = shaderSource.c_str(); - - shader = glCreateShader(shaderType); - glShaderSource(shader, 1, &source, NULL); - glCompileShader(shader); - - glGetShaderiv(shader, GL_COMPILE_STATUS, &success); - if (!success) - { - GLchar infoLog[512]; - glGetShaderInfoLog(shader, 512, NULL, infoLog); - std::cerr << "shader compilation failed" << std::endl - << infoLog << std::endl; - } - - return shader; -} - std::chrono::steady_clock::time_point startTime = std::chrono::steady_clock::now(); float getTime() { @@ -114,33 +90,6 @@ int initGraphics(GLFWwindow** window) return 0; } -GLuint getShaderProgram() -{ - GLuint vertShader = compileShader("./vert.glsl", GL_VERTEX_SHADER); - GLuint fragShader = compileShader("./frag.glsl", GL_FRAGMENT_SHADER); - - GLuint shaderProgram = glCreateProgram(); - glAttachShader(shaderProgram, vertShader); - glAttachShader(shaderProgram, fragShader); - glLinkProgram(shaderProgram); - - GLint success; - GLchar infoLog[512]; - glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); - if (!success) - { - glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); - std::cerr << "shader linking failed" << std::endl - << infoLog << std::endl; - } - - // We no longer need the individual shaders - glDeleteShader(vertShader); - glDeleteShader(fragShader); - - return shaderProgram; -} - int main() { // Calculate period of ISS orbit around the Earth @@ -157,7 +106,7 @@ int main() if (initGraphics(&window) != 0) return -1; - Triangle triangle(getShaderProgram()); + Triangle triangle(compileShaderProgram()); // Main loop while (!glfwWindowShouldClose(window))