Compare commits

...

12 Commits

9 changed files with 373 additions and 57 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ CMakeDoxygenDefaults.cmake
Makefile Makefile
cmake_install.cmake cmake_install.cmake
bin/loader/loader bin/loader/loader
lib/glfw

View File

@ -5,7 +5,6 @@ add_subdirectory(lib/glfw)
include_directories( include_directories(
include/ include/
lib/glad/include/
lib/glfw/include/ lib/glfw/include/
) )
file( file(
@ -31,7 +30,7 @@ source_group("sources" FILES ${PROJECT_SOURCES})
source_group("vendors" FILES ${VENDORS_SOURCES}) source_group("vendors" FILES ${VENDORS_SOURCES})
find_package(Boost 1.56 REQUIRED COMPONENTS find_package(Boost 1.56 REQUIRED COMPONENTS
program_options) program_options log)
set(EXE_1_NAME loader) set(EXE_1_NAME loader)
file( file(
@ -42,7 +41,7 @@ set(CMAKE_BINARY_DIR "bin")
add_executable(${EXE_1_NAME} ${EXE_1_SOURCES} ${PROJECT_HEADERS} add_executable(${EXE_1_NAME} ${EXE_1_SOURCES} ${PROJECT_HEADERS}
${PROJECT_SHADERS} ${VENDORS_SOURCES}) ${PROJECT_SHADERS} ${VENDORS_SOURCES})
target_link_libraries(${EXE_1_NAME} glfw Boost::program_options) target_link_libraries(${EXE_1_NAME} glfw Boost::program_options Boost::log)
target_include_directories(${EXE_1_NAME} PRIVATE include) target_include_directories(${EXE_1_NAME} PRIVATE include)
set_target_properties(${EXE_1_NAME} PROPERTIES set_target_properties(${EXE_1_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${EXE_1_NAME}) RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${EXE_1_NAME})

View File

@ -5,4 +5,5 @@ cmake $flags .
cmake --build . cmake --build .
echo -e "running the program, passing the given args" echo -e "running the program, passing the given args"
echo -e "========================================" echo -e "========================================"
# shellcheck disable=SC2046 # Intended splitting of OPTIONS
./run.sh $@ ./run.sh $@

View File

@ -0,0 +1,7 @@
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}

1
run.sh
View File

@ -1,3 +1,4 @@
#!/bin/bash #!/bin/bash
set -e set -e
# shellcheck disable=SC2046 # Intended splitting of OPTIONS
./bin/loader/loader $@ ./bin/loader/loader $@

View File

@ -1,23 +1,214 @@
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/log/trivial.hpp>
#include <exception>
#include <glad/glad.c> #include <glad/glad.c>
// glad must be loaded before GLFW // glad must be loaded before GLFW
#include <GLFW/glfw3.h>
#include "graphics.hpp" #include "graphics.hpp"
#include "main.hpp"
#include <iostream> #include <iostream>
#include <unistd.h>
void processInput(GLFWwindow *window) { using namespace std;
// if user presses ESC, close the window
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) ShaderProgram::Base ShaderProgram::baseFromName(string name) {
glfwSetWindowShouldClose(window, true); string lname = name;
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.
if (lname == "empty") {
return Empty;
}
if (lname == "triangle") {
return Triangle;
} else {
BOOST_LOG_TRIVIAL(fatal) << "unknown base program \"" << name << "\"";
exit(EXIT_USAGE);
}
} }
/* class ShaderProgram Implementations */
string ShaderProgram::baseName() {
/*
BOOST_LOG_TRIVIAL(debug) << "this->base: " << this->base
<< " Base::Empty: " << Base::Empty
<< " Base::Triangle: " << Base::Triangle;
*/
switch (this->base) {
case ShaderProgram::Base::Empty:
return "Empty";
case ShaderProgram::Base::Triangle:
return "Triangle";
default:
// this should never occur, as the switch case should cover all variants.
throw std::runtime_error("Undefined base Program");
}
}
/**
* @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;
BOOST_LOG_TRIVIAL(info) << "With base program \"" << this->baseName() << "\"";
// 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.
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";
glShaderSource(this->vertex, 1, &vertexSource_c, NULL);
BOOST_LOG_TRIVIAL(trace) << "compiling vertex shader";
glCompileShader(this->vertex);
// check if it worked
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);
}
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);
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);
}
// combine it all in a actual program
this->program = glCreateProgram();
glAttachShader(this->program, this->vertex);
glAttachShader(this->program, this->frag);
glLinkProgram(this->program);
glGetProgramiv(this->program, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(this->program, 512, NULL, infoLog);
BOOST_LOG_TRIVIAL(fatal) << "could not link shader program:\n\n"
<< infoLog << std::endl;
exit(EXIT_SHADER);
}
// finalizing our setup
glDeleteShader(this->vertex);
glDeleteShader(this->frag);
}
/** defines the base program behavior.
*
* will be called in `mainWindow`.
*/
int ShaderProgram::run(GLFWwindow *window) {
if (this->base == Triangle) {
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vTriangle[] = {
-0.5f, -0.5f, 0.0f, // left
0.5f, -0.5f, 0.0f, // right
0.0f, 0.5f, 0.0f // top
};
unsigned int VBOTriangle, VAOTriangle;
glGenVertexArrays(1, &VAOTriangle);
glGenBuffers(1, &VBOTriangle);
// bind the Vertex Array Object first, then bind and set vertex buffer(s),
// and then configure vertex attributes(s).
glBindVertexArray(VAOTriangle);
glBindBuffer(GL_ARRAY_BUFFER, VBOTriangle);
glBufferData(GL_ARRAY_BUFFER, sizeof(vTriangle), vTriangle, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float),
(void *)0);
glEnableVertexAttribArray(0);
// note that this is allowed, the call to glVertexAttribPointer registered
// VBO as the vertex attribute's bound vertex buffer object so afterwards we
// can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally
// modify this VAO, but this rarely happens. Modifying other VAOs requires a
// call to glBindVertexArray anyways so we generally don't unbind VAOs (nor
// VBOs) when it's not directly necessary.
glBindVertexArray(0);
// uncomment this call to draw in wireframe polygons.
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
while (!glfwWindowShouldClose(window)) {
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
glUseProgram(this->program);
glBindVertexArray(VAOTriangle); // seeing as we only have a single VAO there's no
// need to bind it every time, but we'll do so to
// keep things a bit more organized
glDrawArrays(GL_TRIANGLES, 0, 3);
// glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released, mouse
// moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &VAOTriangle);
glDeleteBuffers(1, &VBOTriangle);
glDeleteProgram(this->program);
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
} else // Empty
{
// use the compiled shader
glUseProgram(this->program);
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 */
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 mainWindow() { GLFWwindow *initGl() {
/* graphics stuff */ /* graphics stuff */
glfwInit(); glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@ -29,35 +220,32 @@ int mainWindow() {
if (window == NULL) { if (window == NULL) {
printf("Failed to create GLFW window\n"); printf("Failed to create GLFW window\n");
glfwTerminate(); glfwTerminate();
return -1; 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 -1; exit(EXIT_GL);
} }
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
return window;
}
float vertices[] = {-0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.0f}; int mainWindow(ShaderProgram *shaderProgram, GLFWwindow *window) {
unsigned int VBO; int result = 0;
glGenBuffers(1, &VBO); BOOST_LOG_TRIVIAL(trace) << "Base program";
glBindBuffer(GL_ARRAY_BUFFER, VBO); result = shaderProgram->run(window);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); BOOST_LOG_TRIVIAL(trace) << "Left base program";
while (!glfwWindowShouldClose(window)) {
processInput(window);
glClearColor(0.8f, 0.4f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate(); glfwTerminate();
return 0; return result;
}
void processInput(GLFWwindow *window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
} }

View File

@ -1 +1,25 @@
int mainWindow(); #ifndef GRAPHICS_HPP
#define GRAPHICS_HPP
#include <GLFW/glfw3.h>
#include <iostream>
#include <string>
class ShaderProgram {
public:
enum Base { Empty, Triangle };
std::string baseName();
static Base baseFromName(std::string name);
ShaderProgram(Base base, std::string vertexSource, std::string fragSource);
int run(GLFWwindow* window);
protected:
unsigned int vertex;
unsigned int frag;
Base base;
unsigned int program;
};
int mainWindow(ShaderProgram* shaderProgramm, GLFWwindow* window);
GLFWwindow* initGl();
void processInput(GLFWwindow *window);
#endif

View File

@ -1,36 +1,32 @@
#include <boost/program_options.hpp> #include <boost/log/trivial.hpp>
#include <boost/program_options/parsers.hpp> #include <cstddef>
#include <boost/program_options/positional_options.hpp> #include <cstdio>
#include <boost/program_options/variables_map.hpp> #include <cstdlib>
#include <fstream>
#include <exception> #include <iomanip>
#include <iostream> #include <iostream>
#include <string>
#include "graphics.hpp" #include "main.hpp"
namespace po = boost::program_options;
using namespace std;
int main(int argc, char **argv);
void help(char *prog, po::options_description desc);
int main(int argc, char **argv) { int main(int argc, char **argv) {
/* declarations */
int result = 0;
/* Parsing CLI Arguments */ /* Parsing CLI Arguments */
po::options_description desc("Available options"); po::options_description desc("Available options");
desc.add_options() desc.add_options()("help,h", "show help")("verbose,v", "more verbose output")(
("help,h", "show help") "vert", po::value<string>()->required(), "vertex shader file")(
("verbose,v", "more verbose output") "frag", po::value<string>()->required(), "fragment shader file")(
("vert", po::value<string>()->required(), "vertex shader file") "base,b", po::value<string>()->required(), "base program");
("frag", po::value<string>()->required(), "fragment shader file");
/* unused for now */ /* unused for now */
po::positional_options_description pdesc; po::positional_options_description pdesc;
// NOTE: if you want to add a positional argument, // NOTE: if you want to add a positional argument,
// you need to create and option for it as it seems. // you need to create and option for it as it seems.
pdesc.add("vert", 1); /* pdesc.add("vert", 1); */
pdesc.add("frag", 1); /* pdesc.add("frag", 1); */
po::variables_map vm; po::variables_map vm;
try { try {
@ -46,24 +42,98 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
} catch (po::error &e) { } catch (po::error &e) {
cout << e.what() << endl << endl; BOOST_LOG_TRIVIAL(fatal) << e.what() << endl << endl;
help(argv[0], desc); help(argv[0], desc);
return 1; return EXIT_USAGE;
} }
// set boost log level to trace on verbose
// TODO: -v should decrease the default log level by 1 but be repeatable.
// (default should be info)
if (vm.count("verbose")) { if (vm.count("verbose")) {
printf("verbose!"); boost::log::core::get()->set_filter(boost::log::trivial::severity >=
boost::log::trivial::trace);
BOOST_LOG_TRIVIAL(trace)
<< "verbose flag found, setting log level to trace";
} }
/* init graphics stuff */
GLFWwindow *window = initGl();
if (result != 0) {
return EXIT_GL;
}
/* find the requested */
ShaderProgram::Base base =
ShaderProgram::baseFromName(vm["base"].as<string>());
/* setup the shader program */
BOOST_LOG_TRIVIAL(trace) << "Reading shader files";
ifstream fvert(vm["vert"].as<string>());
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
BOOST_LOG_TRIVIAL(trace) << "Reading shader file sizes";
BOOST_LOG_TRIVIAL(debug) << "vert file:\n```glsl\n"
<< vert << "```\n"
<< vert.size() << "B";
BOOST_LOG_TRIVIAL(debug) << "frag file:\n```glsl\n"
<< frag << "```\n"
<< frag.size() << "B";
fvert.close();
ffrag.close();
BOOST_LOG_TRIVIAL(trace) << "Creating ShaderProgram";
// TODO: determinde ShaderProgram::Base with a CLI arg
ShaderProgram shaderProgram(base, vert, frag);
/* Run our main program */ /* Run our main program */
int result = mainWindow(); BOOST_LOG_TRIVIAL(trace) << "Starting mainWindow";
result = mainWindow(&shaderProgram, window);
BOOST_LOG_TRIVIAL(trace) << "mainWindow stopped, goodbye";
return result; return result;
} }
void help(char *prog, po::options_description desc) { void help(char *prog, po::options_description desc) {
cout << "Usage:" << endl cout << "Usage:" << endl
<< prog << " [-vh] [--vert] shaders/vertex.glsl [--frag] shaders/fragment.glsl" << prog
<< endl << endl << " [-vh] [--vert] shaders/vertex.glsl [--frag] shaders/fragment.glsl"
<< endl
<< endl
<< "return values:" << endl
<< "0\tSuccess" << endl
<< "1\tGeneric Failure" << endl
<< "2\tBad arguments" << endl
<< "3\tI/O Error" << endl
<< "4\tOpenGL Error" << endl
<< "5\tShader Source Error" << endl
<< endl
<< endl
<< "base programs:" << endl
<< "Empty\t\t-\tEmpty window with colored background" << endl
<< "Triangle\t-\tA static Triangle" << endl
<< endl
<< endl
<< desc << endl; << desc << endl;
// NOTE: constants for the error codes are defined in main.hpp
} }

25
src/main.hpp Normal file
View File

@ -0,0 +1,25 @@
#ifndef MAIN_HPP
#define MAIN_HPP
#include <boost/program_options.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/positional_options.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include "graphics.hpp"
// EXIT_FAILURE and EXIT_SUCCESS are defined in cstdlib
const int EXIT_USAGE = 2;
const int EXIT_IO = 3;
const int EXIT_GL = 4;
const int EXIT_SHADER = 5;
namespace po = boost::program_options;
using namespace std;
int main(int argc, char **argv);
void help(char *prog, po::options_description desc);
#endif