From 5a5295231bc2b8dae073c8e7fb8544ebf510703f Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sat, 21 Oct 2023 19:20:15 +0200 Subject: [PATCH] shader loading works? --- data/shader/frag/basic.glsl | 2 +- src/graphics.cpp | 36 ++++++++++++++++--------- src/graphics.hpp | 2 +- src/main.cpp | 53 ++++++++++++++++++++++--------------- 4 files changed, 56 insertions(+), 37 deletions(-) diff --git a/data/shader/frag/basic.glsl b/data/shader/frag/basic.glsl index 1bb8bf6..8b1a393 100644 --- a/data/shader/frag/basic.glsl +++ b/data/shader/frag/basic.glsl @@ -1 +1 @@ -# empty +// empty diff --git a/src/graphics.cpp b/src/graphics.cpp index d2cf4ee..2d9b126 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -46,8 +46,16 @@ string ShaderProgram::baseName() { } } -ShaderProgram::ShaderProgram(Base base, char **vertexSource, - char **fragSource) { +/** + * @param vertexSource C style string with NULL termination + * @param fragSource C style string with NULL termination + */ +ShaderProgram::ShaderProgram(Base base, string vertexSource, + string fragSource) { + int success; + char infoLog[512]; + const char* vertexSource_c = vertexSource.c_str(); + const char* fragSource_c = fragSource.c_str(); this->base = base; BOOST_LOG_TRIVIAL(info) << "With base program \"" << this->baseName() << "\""; // NOTE: char arrays and char pointers are not actually the same. @@ -56,32 +64,34 @@ ShaderProgram::ShaderProgram(Base base, char **vertexSource, BOOST_LOG_TRIVIAL(trace) << "creating empty shaders"; this->vertex = glCreateShader(GL_VERTEX_SHADER); this->frag = glCreateShader(GL_FRAGMENT_SHADER); + BOOST_LOG_TRIVIAL(trace) << "loading vertex shader"; - glShaderSource(this->vertex, 1, vertexSource, NULL); + glShaderSource(this->vertex, 1, &vertexSource_c, NULL); BOOST_LOG_TRIVIAL(trace) << "compiling vertex shader"; glCompileShader(this->vertex); - BOOST_LOG_TRIVIAL(trace) << "loading fragments shader"; - glShaderSource(this->frag, 2, fragSource, NULL); - BOOST_LOG_TRIVIAL(trace) << "compiling fragments shader"; - glCompileShader(this->frag); - - // check if the compilation was successful - int success; - char infoLog[512]; + // check if it worked glGetShaderiv(this->vertex, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(this->vertex, 512, NULL, infoLog); BOOST_LOG_TRIVIAL(fatal) << "could not compile vertex shader:\n\n" - << infoLog << std::endl; + << infoLog << std::endl; exit(EXIT_SHADER); } + + /* FIXME: Optionally include a frag shader + BOOST_LOG_TRIVIAL(trace) << "loading fragments shader"; + glShaderSource(this->frag, 1, &fragSource_c, NULL); + BOOST_LOG_TRIVIAL(trace) << "compiling fragments shader"; + glCompileShader(this->frag); + // check if it worked glGetShaderiv(this->frag, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(this->frag, 512, NULL, infoLog); BOOST_LOG_TRIVIAL(fatal) << "could not compile fragments shader:\n\n" - << infoLog << std::endl; + << infoLog << std::endl; exit(EXIT_SHADER); } + */ } /** defines the base program behavior. diff --git a/src/graphics.hpp b/src/graphics.hpp index 2729069..6bf195d 100644 --- a/src/graphics.hpp +++ b/src/graphics.hpp @@ -10,7 +10,7 @@ public: enum Base { Empty, Triangle }; std::string baseName(); static Base baseFromName(std::string name); - ShaderProgram(Base base, char** vertexSource, char** fragSource); + ShaderProgram(Base base, std::string vertexSource, std::string fragSource); int run(GLFWwindow* window); protected: diff --git a/src/main.cpp b/src/main.cpp index f14d62c..a76c3da 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,11 @@ +#include #include #include #include #include +#include #include +#include #include "main.hpp" @@ -61,41 +64,47 @@ int main(int argc, char **argv) { } /* find the requested */ - ShaderProgram::Base base = ShaderProgram::baseFromName(vm["base"].as()); + ShaderProgram::Base base = + ShaderProgram::baseFromName(vm["base"].as()); /* setup the shader program */ BOOST_LOG_TRIVIAL(trace) << "Reading shader files"; - auto fvert = fopen(vm["vert"].as().c_str(), "r"); - auto ffrag = fopen(vm["frag"].as().c_str(), "r"); + ifstream fvert(vm["vert"].as()); + ifstream ffrag(vm["frag"].as()); + if (fvert.fail()) { + BOOST_LOG_TRIVIAL(fatal) << "failed to read vertex shader file"; + exit(EXIT_IO); + } + if (ffrag.fail()) { + BOOST_LOG_TRIVIAL(fatal) << "failed to read fragments shader file"; + exit(EXIT_IO); + } + string buf; + string vert; + string frag; + while(!fvert.eof()) { + getline(fvert, buf); + vert.append(buf + "\n"); + } + while(!ffrag.eof()) { + getline(ffrag, buf); + frag.append(buf + "\n"); + } // get file sizes BOOST_LOG_TRIVIAL(trace) << "Reading shader file sizes"; - fseek(fvert, 0L, SEEK_END); - fseek(ffrag, 0L, SEEK_END); - size_t svert = ftell(fvert); - size_t sfrag = ftell(ffrag); - fseek(fvert, 0L, SEEK_SET); - fseek(ffrag, 0L, SEEK_SET); - BOOST_LOG_TRIVIAL(trace) << "allocating " << svert - << " B for vert source file"; - BOOST_LOG_TRIVIAL(trace) << "allocating " << sfrag - << " B for frag source file"; - char *vert = (char *)malloc(svert + 1); - char *frag = (char *)malloc(sfrag + 1); - fread(vert, sizeof(char), svert, fvert); - fread(frag, sizeof(char), sfrag, ffrag); BOOST_LOG_TRIVIAL(debug) << "vert file:\n```glsl\n" << vert << "```\n" - << svert << "B"; + << vert.size() << "B"; BOOST_LOG_TRIVIAL(debug) << "frag file:\n```glsl\n" << frag << "```\n" - << sfrag << "B"; - fclose(fvert); - fclose(ffrag); + << frag.size() << "B"; + fvert.close(); + ffrag.close(); BOOST_LOG_TRIVIAL(trace) << "Creating ShaderProgram"; // TODO: determinde ShaderProgram::Base with a CLI arg - ShaderProgram shaderProgram(base, &vert, &frag); + ShaderProgram shaderProgram(base, vert, frag); /* Run our main program */ BOOST_LOG_TRIVIAL(trace) << "Starting mainWindow";