check shader compilation

This commit is contained in:
Christoph J. Scherr 2023-10-21 00:35:03 +02:00
parent 743e7d14ca
commit 1fe0cc1d40
4 changed files with 65 additions and 24 deletions

View File

@ -1,6 +1,6 @@
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/case_conv.hpp> #include <boost/algorithm/string/case_conv.hpp>
#include <boost/log/trivial.hpp> #include <boost/log/trivial.hpp>
#include <boost/algorithm/string.hpp>
#include <exception> #include <exception>
#include <glad/glad.c> #include <glad/glad.c>
// glad must be loaded before GLFW // glad must be loaded before GLFW
@ -14,14 +14,15 @@ using namespace std;
ShaderProgram::Base ShaderProgram::baseFromName(string name) { ShaderProgram::Base ShaderProgram::baseFromName(string name) {
string lname = name; string lname = name;
boost::algorithm::to_lower(lname); // yes, cpp needs a library to do this. boost::algorithm::to_lower(lname); // yes, cpp needs a library to do this.
// luckily I'm already using boost so I
// don't need to worry too much about it.
// can't do a switch over strings in cpp. // can't do a switch over strings in cpp.
if (lname == "empty") { if (lname == "empty") {
return Empty; return Empty;
} }
if (lname == "triangle") { if (lname == "triangle") {
return Triangle; return Triangle;
} } else {
else {
BOOST_LOG_TRIVIAL(fatal) << "unknown base program \"" << name << "\""; BOOST_LOG_TRIVIAL(fatal) << "unknown base program \"" << name << "\"";
exit(EXIT_USAGE); exit(EXIT_USAGE);
} }
@ -63,23 +64,66 @@ ShaderProgram::ShaderProgram(Base base, char **vertexSource,
glShaderSource(this->frag, 2, fragSource, 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);
// check if the compilation was successful
int success;
char infoLog[512];
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;
exit(EXIT_SHADER);
}
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;
exit(EXIT_SHADER);
}
} }
/** runs for every frame /** defines the base program behavior.
* *
* will be called in `mainWindow`. * will be called in `mainWindow`.
*
* FIXME: dont run the switch case every frame.
*/ */
int ShaderProgram::run() { int ShaderProgram::run(GLFWwindow *window) {
switch (this->base) { if (this->base == Triangle) {
case Empty: // same as default float vTriangle[] = {
default: -0.5f, -0.5f, 0.0f, // opengl
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); 0.5f, -0.5f, 0.0f, // treats 3d arrays
0.0f, 0.5f, 0.0f // as large 1d arrays
};
unsigned int oTriangle;
glGenBuffers(1, &oTriangle);
glBindBuffer(GL_ARRAY_BUFFER, oTriangle);
glBufferData(GL_ARRAY_BUFFER, sizeof(vTriangle), vTriangle, GL_STATIC_DRAW);
while (!glfwWindowShouldClose(window)) {
processInput(window);
glClearColor(0.9f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
break;
glfwSwapBuffers(window);
glfwPollEvents();
} }
return 0; return 0;
} else // Empty
{
return 0;
while (!glfwWindowShouldClose(window)) {
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
return 0;
}
} }
/* functions that run out graphics stuff */ /* functions that run out graphics stuff */
@ -117,14 +161,9 @@ int mainWindow(ShaderProgram *shaderProgram, GLFWwindow *window) {
int result = 0; int result = 0;
BOOST_LOG_TRIVIAL(trace) << "Entering window loop"; BOOST_LOG_TRIVIAL(trace) << "Base program";
while (!glfwWindowShouldClose(window)) { result = shaderProgram->run(window);
processInput(window); BOOST_LOG_TRIVIAL(trace) << "Left base program";
shaderProgram->run();
glfwSwapBuffers(window);
glfwPollEvents();
}
BOOST_LOG_TRIVIAL(trace) << "Left window loop";
glfwTerminate(); glfwTerminate();
return result; return result;

View File

@ -11,7 +11,7 @@ public:
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, char** vertexSource, char** fragSource);
int run(); int run(GLFWwindow* window);
protected: protected:
unsigned int vertex; unsigned int vertex;

View File

@ -116,7 +116,8 @@ void help(char *prog, po::options_description desc) {
<< "1\tGeneric Failure" << endl << "1\tGeneric Failure" << endl
<< "2\tBad arguments" << endl << "2\tBad arguments" << endl
<< "3\tI/O Error" << endl << "3\tI/O Error" << endl
<< "3\tOpenGL Error" << endl << "4\tOpenGL Error" << endl
<< "5\tShader Source Error" << endl
<< endl << endl
<< endl << endl
<< "base programs:" << endl << "base programs:" << endl

View File

@ -14,6 +14,7 @@
const int EXIT_USAGE = 2; const int EXIT_USAGE = 2;
const int EXIT_IO = 3; const int EXIT_IO = 3;
const int EXIT_GL = 4; const int EXIT_GL = 4;
const int EXIT_SHADER = 5;
namespace po = boost::program_options; namespace po = boost::program_options;
using namespace std; using namespace std;