shader loading works?

This commit is contained in:
Christoph J. Scherr 2023-10-21 19:20:15 +02:00
parent 1fe0cc1d40
commit 5a5295231b
4 changed files with 56 additions and 37 deletions

View File

@ -1 +1 @@
# empty // empty

View File

@ -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; this->base = base;
BOOST_LOG_TRIVIAL(info) << "With base program \"" << this->baseName() << "\""; BOOST_LOG_TRIVIAL(info) << "With base program \"" << this->baseName() << "\"";
// NOTE: char arrays and char pointers are not actually the same. // NOTE: char arrays and char pointers are not actually the same.
@ -56,18 +64,12 @@ ShaderProgram::ShaderProgram(Base base, char **vertexSource,
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, NULL); glShaderSource(this->vertex, 1, &vertexSource_c, 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"; // check if it worked
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];
glGetShaderiv(this->vertex, GL_COMPILE_STATUS, &success); glGetShaderiv(this->vertex, GL_COMPILE_STATUS, &success);
if (!success) { if (!success) {
glGetShaderInfoLog(this->vertex, 512, NULL, infoLog); glGetShaderInfoLog(this->vertex, 512, NULL, infoLog);
@ -75,6 +77,13 @@ ShaderProgram::ShaderProgram(Base base, char **vertexSource,
<< infoLog << std::endl; << infoLog << std::endl;
exit(EXIT_SHADER); 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); glGetShaderiv(this->frag, GL_COMPILE_STATUS, &success);
if (!success) { if (!success) {
glGetShaderInfoLog(this->frag, 512, NULL, infoLog); glGetShaderInfoLog(this->frag, 512, NULL, infoLog);
@ -82,6 +91,7 @@ ShaderProgram::ShaderProgram(Base base, char **vertexSource,
<< infoLog << std::endl; << infoLog << std::endl;
exit(EXIT_SHADER); exit(EXIT_SHADER);
} }
*/
} }
/** defines the base program behavior. /** defines the base program behavior.

View File

@ -10,7 +10,7 @@ public:
enum Base { Empty, Triangle }; enum Base { Empty, Triangle };
std::string baseName(); std::string baseName();
static Base baseFromName(std::string name); 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); int run(GLFWwindow* window);
protected: protected:

View File

@ -1,8 +1,11 @@
#include <boost/log/trivial.hpp>
#include <cstddef> #include <cstddef>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <fstream> #include <fstream>
#include <iomanip>
#include <iostream> #include <iostream>
#include <string>
#include "main.hpp" #include "main.hpp"
@ -61,41 +64,47 @@ int main(int argc, char **argv) {
} }
/* find the requested */ /* find the requested */
ShaderProgram::Base base = ShaderProgram::baseFromName(vm["base"].as<string>()); ShaderProgram::Base base =
ShaderProgram::baseFromName(vm["base"].as<string>());
/* setup the shader program */ /* setup the shader program */
BOOST_LOG_TRIVIAL(trace) << "Reading shader files"; BOOST_LOG_TRIVIAL(trace) << "Reading shader files";
auto fvert = fopen(vm["vert"].as<string>().c_str(), "r"); ifstream fvert(vm["vert"].as<string>());
auto ffrag = fopen(vm["frag"].as<string>().c_str(), "r"); ifstream ffrag(vm["frag"].as<string>());
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 // get file sizes
BOOST_LOG_TRIVIAL(trace) << "Reading shader 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" BOOST_LOG_TRIVIAL(debug) << "vert file:\n```glsl\n"
<< vert << "```\n" << vert << "```\n"
<< svert << "B"; << vert.size() << "B";
BOOST_LOG_TRIVIAL(debug) << "frag file:\n```glsl\n" BOOST_LOG_TRIVIAL(debug) << "frag file:\n```glsl\n"
<< frag << "```\n" << frag << "```\n"
<< sfrag << "B"; << frag.size() << "B";
fclose(fvert); fvert.close();
fclose(ffrag); ffrag.close();
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(base, &vert, &frag); ShaderProgram shaderProgram(base, vert, frag);
/* Run our main program */ /* Run our main program */
BOOST_LOG_TRIVIAL(trace) << "Starting mainWindow"; BOOST_LOG_TRIVIAL(trace) << "Starting mainWindow";