empty base

This commit is contained in:
Christoph J. Scherr 2023-10-20 11:20:01 +02:00
parent 0820aa31ac
commit fe00f536bd
3 changed files with 57 additions and 19 deletions

View File

@ -1,3 +1,4 @@
#include <boost/log/trivial.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
@ -10,18 +11,26 @@ using namespace std;
/* class ShaderProgram Implementations */ /* class ShaderProgram Implementations */
string ShaderProgram::baseName() { string ShaderProgram::baseName() {
/*
BOOST_LOG_TRIVIAL(debug) << "this->base: " << this->base
<< " Base::Empty: " << Base::Empty
<< " Base::Triangle: " << Base::Triangle;
*/
switch (this->base) { switch (this->base) {
case Empty: case ShaderProgram::Base::Empty:
return "Empty"; return "Empty";
case Triangle: case ShaderProgram::Base::Triangle:
return "Triangle"; return "Triangle";
default: default:
throw exception(); // this should never occur, as the switch case should cover all variants.
throw std::runtime_error("Undefined base Program");
} }
} }
ShaderProgram::ShaderProgram(Base base, char** vertexSource, ShaderProgram::ShaderProgram(Base base, char **vertexSource,
char** fragSource) { char **fragSource) {
this->base = base;
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.
// glShaderSource requires a char[] as it appears. We pass the // glShaderSource requires a char[] as it appears. We pass the
// variable by reference to avoid duplication in memory. // variable by reference to avoid duplication in memory.
@ -29,8 +38,7 @@ ShaderProgram::ShaderProgram(Base base, char** vertexSource,
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, 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";
@ -39,13 +47,28 @@ ShaderProgram::ShaderProgram(Base base, char** vertexSource,
glCompileShader(this->frag); glCompileShader(this->frag);
} }
int ShaderProgram::run() { return 255; } /** runs for every frame
*
* will be called in `mainWindow`.
*
* FIXME: dont run the switch case every frame.
*/
int ShaderProgram::run() {
switch (this->base) {
case Empty: // same as default
default:
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
break;
}
return 0;
}
void framebuffer_size_callback(GLFWwindow *window, int width, int height) { void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
} }
int initGl() { GLFWwindow *initGl() {
/* graphics stuff */ /* graphics stuff */
glfwInit(); glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@ -57,23 +80,37 @@ int initGl() {
if (window == NULL) { if (window == NULL) {
printf("Failed to create GLFW window\n"); printf("Failed to create GLFW window\n");
glfwTerminate(); glfwTerminate();
return EXIT_GL; exit(EXIT_GL);
} }
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
printf("Failed to initialize GLAD\n"); printf("Failed to initialize GLAD\n");
return EXIT_GL; exit(EXIT_GL);
} }
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
return 0; return window;
} }
int mainWindow(ShaderProgram *shaderProgram) { int mainWindow(ShaderProgram *shaderProgram, GLFWwindow *window) {
int result = shaderProgram->run(); int result = 0;
BOOST_LOG_TRIVIAL(trace) << "Entering window loop";
while (!glfwWindowShouldClose(window)) {
processInput(window);
shaderProgram->run();
glfwSwapBuffers(window);
glfwPollEvents();
}
BOOST_LOG_TRIVIAL(trace) << "Left window loop";
glfwTerminate(); glfwTerminate();
return result; return result;
} }
void processInput(GLFWwindow *window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}

View File

@ -17,6 +17,7 @@ protected:
unsigned int frag; unsigned int frag;
Base base; Base base;
}; };
int mainWindow(ShaderProgram* shaderProgramm); int mainWindow(ShaderProgram* shaderProgramm, GLFWwindow* window);
int initGl(); GLFWwindow* initGl();
void processInput(GLFWwindow *window);
#endif #endif

View File

@ -54,7 +54,7 @@ int main(int argc, char **argv) {
} }
/* init graphics stuff */ /* init graphics stuff */
result = initGl(); GLFWwindow* window = initGl();
if (result != 0) { if (result != 0) {
return EXIT_GL; return EXIT_GL;
} }
@ -93,8 +93,8 @@ int main(int argc, char **argv) {
/* Run our main program */ /* Run our main program */
BOOST_LOG_TRIVIAL(trace) << "Starting mainWindow"; BOOST_LOG_TRIVIAL(trace) << "Starting mainWindow";
result = mainWindow(&shaderProgram); result = mainWindow(&shaderProgram, window);
/* if (result != 0) { return EXIT_GL;} */ BOOST_LOG_TRIVIAL(trace) << "mainWindow stopped, goodbye";
return result; return result;
} }