reading glsl files

This commit is contained in:
Christoph J. Scherr 2023-10-20 10:52:56 +02:00
parent 2c393b1851
commit 0820aa31ac
4 changed files with 45 additions and 28 deletions

View File

@ -0,0 +1 @@
# empty

View File

@ -1,3 +1,4 @@
#include <exception>
#include <glad/glad.c> #include <glad/glad.c>
// glad must be loaded before GLFW // glad must be loaded before GLFW
#include "graphics.hpp" #include "graphics.hpp"
@ -14,21 +15,26 @@ string ShaderProgram::baseName() {
return "Empty"; return "Empty";
case Triangle: case Triangle:
return "Triangle"; return "Triangle";
default:
throw exception();
} }
} }
ShaderProgram::ShaderProgram(Base base, string vertexSource, ShaderProgram::ShaderProgram(Base base, char** vertexSource,
string fragSource) { 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"; BOOST_LOG_TRIVIAL(trace) << "creating empty shaders";
this->vertex = glCreateShader(GL_VERTEX_SHADER); this->vertex = glCreateShader(GL_VERTEX_SHADER);
this->frag = glCreateShader(GL_FRAGMENT_SHADER); this->frag = glCreateShader(GL_FRAGMENT_SHADER);
BOOST_LOG_TRIVIAL(trace) << "loading vertex shader"; BOOST_LOG_TRIVIAL(trace) << "loading vertex shader";
glShaderSource(this->vertex, 1, vertexSource.c_str(), glShaderSource(this->vertex, 1, vertexSource,
NULL); NULL);
BOOST_LOG_TRIVIAL(trace) << "compiling vertex shader"; BOOST_LOG_TRIVIAL(trace) << "compiling vertex shader";
glCompileShader(this->vertex); glCompileShader(this->vertex);
BOOST_LOG_TRIVIAL(trace) << "loading fragments shader"; 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"; BOOST_LOG_TRIVIAL(trace) << "compiling fragments shader";
glCompileShader(this->frag); glCompileShader(this->frag);
} }

View File

@ -9,7 +9,7 @@ class ShaderProgram {
public: public:
enum Base { Empty, Triangle }; enum Base { Empty, Triangle };
std::string baseName(); std::string baseName();
ShaderProgram(Base base, std::string vertexSource, std::string fragSource); ShaderProgram(Base base, char** vertexSource, char** fragSource);
int run(); int run();
protected: protected:

View File

@ -1,3 +1,6 @@
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
@ -46,40 +49,47 @@ int main(int argc, char **argv) {
if (vm.count("verbose")) { if (vm.count("verbose")) {
boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::core::get()->set_filter(boost::log::trivial::severity >=
boost::log::trivial::trace); 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 */ /* init graphics stuff */
result = initGl(); result = initGl();
if (result != 0) { return EXIT_GL;} if (result != 0) {
return EXIT_GL;
}
/* setup the shader program */ /* setup the shader program */
BOOST_LOG_TRIVIAL(trace) << "Reading shader files"; BOOST_LOG_TRIVIAL(trace) << "Reading shader files";
ifstream vertexFileStream(vm["vert"].as<string>()); auto fvert = fopen(vm["vert"].as<string>().c_str(), "r");
ifstream fragFileStream(vm["frag"].as<string>()); auto ffrag = fopen(vm["frag"].as<string>().c_str(), "r");
std::stringstream vertexSource; // get file sizes
std::stringstream fragSource; BOOST_LOG_TRIVIAL(trace) << "Reading shader file sizes";
vertexSource << vertexFileStream.rdbuf(); fseek(fvert, 0L, SEEK_END);
fragSource << fragFileStream.rdbuf(); fseek(ffrag, 0L, SEEK_END);
size_t svert = ftell(fvert);
if (vertexFileStream.fail()) { size_t sfrag = ftell(ffrag);
BOOST_LOG_TRIVIAL(fatal) fseek(fvert, 0L, SEEK_SET);
<< "Error while reading the vertex shader file."; fseek(ffrag, 0L, SEEK_SET);
return EXIT_IO; BOOST_LOG_TRIVIAL(trace) << "allocating " << svert
} << " B for vert source file";
if (fragFileStream.fail()) { BOOST_LOG_TRIVIAL(trace) << "allocating " << sfrag
BOOST_LOG_TRIVIAL(fatal) << " B for frag source file";
<< "Error while reading the frag shader file."; char *vert = (char *)malloc(svert + 1);
return EXIT_IO; char *frag = (char *)malloc(sfrag + 1);
} fread(vert, sizeof(char), svert, fvert);
vertexFileStream.close(); fread(frag, sizeof(char), sfrag, ffrag);
fragFileStream.close(); 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"; BOOST_LOG_TRIVIAL(trace) << "Creating ShaderProgram";
// TODO: determinde ShaderProgram::Base with a CLI arg // TODO: determinde ShaderProgram::Base with a CLI arg
ShaderProgram shaderProgram(ShaderProgram::Base::Empty, vertexSource.str(), ShaderProgram shaderProgram(ShaderProgram::Base::Empty, &vert, &frag);
fragSource.str());
/* Run our main program */ /* Run our main program */
BOOST_LOG_TRIVIAL(trace) << "Starting mainWindow"; BOOST_LOG_TRIVIAL(trace) << "Starting mainWindow";