diff --git a/data/shader/frag/basic.glsl b/data/shader/frag/basic.glsl index e69de29..1bb8bf6 100644 --- a/data/shader/frag/basic.glsl +++ b/data/shader/frag/basic.glsl @@ -0,0 +1 @@ +# empty diff --git a/src/graphics.cpp b/src/graphics.cpp index 173f40e..5d36263 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -1,3 +1,4 @@ +#include #include // glad must be loaded before GLFW #include "graphics.hpp" @@ -14,21 +15,26 @@ string ShaderProgram::baseName() { return "Empty"; case Triangle: return "Triangle"; + default: + throw exception(); } } -ShaderProgram::ShaderProgram(Base base, string vertexSource, - string fragSource) { +ShaderProgram::ShaderProgram(Base base, char** vertexSource, + char** fragSource) { + // NOTE: char arrays and char pointers are not actually the same. + // glShaderSource requires a char[] as it appears. We pass the + // variable by reference to avoid duplication in memory. 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.c_str(), + glShaderSource(this->vertex, 1, vertexSource, NULL); BOOST_LOG_TRIVIAL(trace) << "compiling vertex shader"; glCompileShader(this->vertex); BOOST_LOG_TRIVIAL(trace) << "loading fragments shader"; - glShaderSource(this->frag, 2, fragSource.c_str(), NULL); + glShaderSource(this->frag, 2, fragSource, NULL); BOOST_LOG_TRIVIAL(trace) << "compiling fragments shader"; glCompileShader(this->frag); } diff --git a/src/graphics.hpp b/src/graphics.hpp index 79b9ab4..c6a888c 100644 --- a/src/graphics.hpp +++ b/src/graphics.hpp @@ -9,7 +9,7 @@ class ShaderProgram { public: enum Base { Empty, Triangle }; std::string baseName(); - ShaderProgram(Base base, std::string vertexSource, std::string fragSource); + ShaderProgram(Base base, char** vertexSource, char** fragSource); int run(); protected: diff --git a/src/main.cpp b/src/main.cpp index 4ad11ae..5067c89 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,6 @@ +#include +#include +#include #include #include @@ -46,40 +49,47 @@ int main(int argc, char **argv) { if (vm.count("verbose")) { boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::trace); - BOOST_LOG_TRIVIAL(trace) << "verbose flag found, setting log level to trace"; + BOOST_LOG_TRIVIAL(trace) + << "verbose flag found, setting log level to trace"; } /* init graphics stuff */ result = initGl(); - if (result != 0) { return EXIT_GL;} + if (result != 0) { + return EXIT_GL; + } /* setup the shader program */ BOOST_LOG_TRIVIAL(trace) << "Reading shader files"; - ifstream vertexFileStream(vm["vert"].as()); - ifstream fragFileStream(vm["frag"].as()); - std::stringstream vertexSource; - std::stringstream fragSource; - vertexSource << vertexFileStream.rdbuf(); - fragSource << fragFileStream.rdbuf(); - - if (vertexFileStream.fail()) { - BOOST_LOG_TRIVIAL(fatal) - << "Error while reading the vertex shader file."; - return EXIT_IO; - } - if (fragFileStream.fail()) { - BOOST_LOG_TRIVIAL(fatal) - << "Error while reading the frag shader file."; - return EXIT_IO; - } - vertexFileStream.close(); - fragFileStream.close(); + auto fvert = fopen(vm["vert"].as().c_str(), "r"); + auto ffrag = fopen(vm["frag"].as().c_str(), "r"); + // 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"; + BOOST_LOG_TRIVIAL(debug) << "frag file:\n```glsl\n" << frag << "```\n" + << sfrag << "B"; + fclose(fvert); + fclose(ffrag); BOOST_LOG_TRIVIAL(trace) << "Creating ShaderProgram"; // TODO: determinde ShaderProgram::Base with a CLI arg - ShaderProgram shaderProgram(ShaderProgram::Base::Empty, vertexSource.str(), - fragSource.str()); + ShaderProgram shaderProgram(ShaderProgram::Base::Empty, &vert, &frag); /* Run our main program */ BOOST_LOG_TRIVIAL(trace) << "Starting mainWindow";