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-18 19:48:48 +02:00
|
|
|
/* class ShaderProgram Implementations */
|
2023-10-18 17:03:13 +02:00
|
|
|
string ShaderProgram::baseName() {
|
|
|
|
switch (this->base) {
|
2023-10-18 19:48:48 +02:00
|
|
|
case Empty:
|
|
|
|
return "Empty";
|
|
|
|
case Triangle:
|
|
|
|
return "Triangle";
|
2023-10-20 10:52:56 +02:00
|
|
|
default:
|
|
|
|
throw exception();
|
2023-10-18 17:03:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-20 10:52:56 +02:00
|
|
|
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.
|
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);
|
|
|
|
BOOST_LOG_TRIVIAL(trace) << "loading vertex shader";
|
2023-10-20 10:52:56 +02:00
|
|
|
glShaderSource(this->vertex, 1, vertexSource,
|
2023-10-19 16:15:28 +02:00
|
|
|
NULL);
|
|
|
|
BOOST_LOG_TRIVIAL(trace) << "compiling vertex shader";
|
|
|
|
glCompileShader(this->vertex);
|
|
|
|
BOOST_LOG_TRIVIAL(trace) << "loading fragments shader";
|
2023-10-20 10:52:56 +02:00
|
|
|
glShaderSource(this->frag, 2, fragSource, NULL);
|
2023-10-19 16:15:28 +02:00
|
|
|
BOOST_LOG_TRIVIAL(trace) << "compiling fragments shader";
|
|
|
|
glCompileShader(this->frag);
|
|
|
|
}
|
2023-10-18 11:02:08 +02:00
|
|
|
|
2023-10-18 19:48:48 +02:00
|
|
|
int ShaderProgram::run() { return 255; }
|
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-19 16:15:28 +02:00
|
|
|
int 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-19 16:15:28 +02:00
|
|
|
return EXIT_GL;
|
2023-10-18 11:02:08 +02:00
|
|
|
}
|
|
|
|
glfwMakeContextCurrent(window);
|
|
|
|
|
|
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
|
|
|
printf("Failed to initialize GLAD\n");
|
2023-10-19 16:15:28 +02:00
|
|
|
return EXIT_GL;
|
2023-10-18 11:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
2023-10-19 16:15:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mainWindow(ShaderProgram *shaderProgram) {
|
2023-10-18 11:02:08 +02:00
|
|
|
|
2023-10-18 19:48:48 +02:00
|
|
|
int result = shaderProgram->run();
|
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
|
|
|
}
|