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>
// 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);
}

View File

@ -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:

View File

@ -1,3 +1,6 @@
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
@ -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<string>());
ifstream fragFileStream(vm["frag"].as<string>());
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<string>().c_str(), "r");
auto ffrag = fopen(vm["frag"].as<string>().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";