glsl-basic/src/graphics.cpp

186 lines
5.5 KiB
C++
Raw Normal View History

2023-10-21 00:35:03 +02:00
#include <boost/algorithm/string.hpp>
2023-10-20 15:10:14 +02:00
#include <boost/algorithm/string/case_conv.hpp>
2023-10-20 11:20:01 +02:00
#include <boost/log/trivial.hpp>
2023-10-20 10:52:56 +02:00
#include <exception>
2023-10-18 19:48:48 +02:00
#include <glad/glad.c>
// glad must be loaded before GLFW
2023-10-18 11:02:08 +02:00
#include "graphics.hpp"
2023-10-18 19:48:48 +02:00
#include "main.hpp"
2023-10-18 11:02:08 +02:00
#include <iostream>
2023-10-18 17:03:13 +02:00
using namespace std;
2023-10-20 15:10:14 +02:00
ShaderProgram::Base ShaderProgram::baseFromName(string name) {
string lname = name;
boost::algorithm::to_lower(lname); // yes, cpp needs a library to do this.
2023-10-21 00:35:03 +02:00
// luckily I'm already using boost so I
// don't need to worry too much about it.
2023-10-20 15:10:14 +02:00
// can't do a switch over strings in cpp.
if (lname == "empty") {
return Empty;
}
if (lname == "triangle") {
return Triangle;
2023-10-21 00:35:03 +02:00
} else {
2023-10-20 15:10:14 +02:00
BOOST_LOG_TRIVIAL(fatal) << "unknown base program \"" << name << "\"";
exit(EXIT_USAGE);
}
}
2023-10-18 19:48:48 +02:00
/* class ShaderProgram Implementations */
2023-10-18 17:03:13 +02:00
string ShaderProgram::baseName() {
2023-10-20 11:20:01 +02:00
/*
BOOST_LOG_TRIVIAL(debug) << "this->base: " << this->base
<< " Base::Empty: " << Base::Empty
<< " Base::Triangle: " << Base::Triangle;
*/
2023-10-18 17:03:13 +02:00
switch (this->base) {
2023-10-20 11:20:01 +02:00
case ShaderProgram::Base::Empty:
2023-10-18 19:48:48 +02:00
return "Empty";
2023-10-20 11:20:01 +02:00
case ShaderProgram::Base::Triangle:
2023-10-18 19:48:48 +02:00
return "Triangle";
2023-10-20 10:52:56 +02:00
default:
2023-10-20 11:20:01 +02:00
// this should never occur, as the switch case should cover all variants.
throw std::runtime_error("Undefined base Program");
2023-10-18 17:03:13 +02:00
}
}
2023-10-21 19:20:15 +02:00
/**
* @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();
2023-10-20 11:20:01 +02:00
this->base = base;
BOOST_LOG_TRIVIAL(info) << "With base program \"" << this->baseName() << "\"";
2023-10-20 10:52:56 +02:00
// 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.
2023-10-19 16:15:28 +02:00
BOOST_LOG_TRIVIAL(trace) << "creating empty shaders";
this->vertex = glCreateShader(GL_VERTEX_SHADER);
this->frag = glCreateShader(GL_FRAGMENT_SHADER);
2023-10-21 19:20:15 +02:00
2023-10-19 16:15:28 +02:00
BOOST_LOG_TRIVIAL(trace) << "loading vertex shader";
2023-10-21 19:20:15 +02:00
glShaderSource(this->vertex, 1, &vertexSource_c, NULL);
2023-10-19 16:15:28 +02:00
BOOST_LOG_TRIVIAL(trace) << "compiling vertex shader";
glCompileShader(this->vertex);
2023-10-21 19:20:15 +02:00
// check if it worked
2023-10-21 00:35:03 +02:00
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"
2023-10-21 19:20:15 +02:00
<< infoLog << std::endl;
2023-10-21 00:35:03 +02:00
exit(EXIT_SHADER);
}
2023-10-21 19:20:15 +02:00
/* 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
2023-10-21 00:35:03 +02:00
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"
2023-10-21 19:20:15 +02:00
<< infoLog << std::endl;
2023-10-21 00:35:03 +02:00
exit(EXIT_SHADER);
}
2023-10-21 19:20:15 +02:00
*/
2023-10-19 16:15:28 +02:00
}
2023-10-18 11:02:08 +02:00
2023-10-21 00:35:03 +02:00
/** defines the base program behavior.
2023-10-20 11:20:01 +02:00
*
* will be called in `mainWindow`.
*/
2023-10-21 00:35:03 +02:00
int ShaderProgram::run(GLFWwindow *window) {
if (this->base == Triangle) {
float vTriangle[] = {
-0.5f, -0.5f, 0.0f, // opengl
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);
glfwSwapBuffers(window);
glfwPollEvents();
}
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;
2023-10-20 11:20:01 +02:00
}
}
2023-10-18 11:02:08 +02:00
2023-10-20 15:10:14 +02:00
/* functions that run out graphics stuff */
2023-10-18 11:02:08 +02:00
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height);
}
2023-10-20 11:20:01 +02:00
GLFWwindow *initGl() {
2023-10-18 11:02:08 +02:00
/* graphics stuff */
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
GLFWwindow *window = glfwCreateWindow(800, 600, "Shader Loader", NULL, NULL);
if (window == NULL) {
printf("Failed to create GLFW window\n");
glfwTerminate();
2023-10-20 11:20:01 +02:00
exit(EXIT_GL);
2023-10-18 11:02:08 +02:00
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
printf("Failed to initialize GLAD\n");
2023-10-20 11:20:01 +02:00
exit(EXIT_GL);
2023-10-18 11:02:08 +02:00
}
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
2023-10-20 11:20:01 +02:00
return window;
2023-10-19 16:15:28 +02:00
}
2023-10-20 11:20:01 +02:00
int mainWindow(ShaderProgram *shaderProgram, GLFWwindow *window) {
int result = 0;
2023-10-18 11:02:08 +02:00
2023-10-21 00:35:03 +02:00
BOOST_LOG_TRIVIAL(trace) << "Base program";
result = shaderProgram->run(window);
BOOST_LOG_TRIVIAL(trace) << "Left base program";
2023-10-18 11:02:08 +02:00
glfwTerminate();
2023-10-18 17:03:13 +02:00
return result;
2023-10-18 11:02:08 +02:00
}
2023-10-20 11:20:01 +02:00
void processInput(GLFWwindow *window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}