Add cmrc for resource compiling, add shaders, create a 2d triangle

This commit is contained in:
SeanOMik 2021-11-21 01:23:53 -05:00
parent 9feff47a35
commit 741b0c5b07
15 changed files with 557 additions and 17 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "cmrc"]
path = cmrc
url = https://github.com/vector-of-bool/cmrc.git

View File

@ -2,6 +2,8 @@ cmake_minimum_required (VERSION 3.6)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake;${CMAKE_MODULE_PATH}") set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake;${CMAKE_MODULE_PATH}")
project(SimpleEngine) project(SimpleEngine)
include(cmrc/CMakeRC.cmake)
# Add some CMake options: # Add some CMake options:
option(SIMPLE_ENGINE_BUILD_EXAMPLES "Build example projects" ON) option(SIMPLE_ENGINE_BUILD_EXAMPLES "Build example projects" ON)

1
cmrc Submodule

@ -0,0 +1 @@
Subproject commit a64bea50c05594c8e7cf1f08e441bb9507742e2e

View File

@ -8,8 +8,18 @@ file(GLOB_RECURSE source_list src/*.cpp)
target_sources(dev_testing PRIVATE ${source_list}) target_sources(dev_testing PRIVATE ${source_list})
target_include_directories(dev_testing PUBLIC include) target_include_directories(dev_testing PUBLIC include)
# Embed shaders
file(GLOB_RECURSE shaders_list resources/shaders/*.glsl)
cmrc_add_resource_library(
resource_shaders
WHENCE resources/shaders
PREFIX shaders
${shaders_list}
)
# Link simpleengine # Link simpleengine
target_link_libraries(dev_testing PUBLIC simpleengine) target_link_libraries(dev_testing PUBLIC simpleengine)
target_link_libraries(dev_testing PRIVATE resource_shaders)
# Set standard to C++20 # Set standard to C++20
set_target_properties(dev_testing PROPERTIES CXX_STANDARD 20 CXX_EXTENSIONS OFF) set_target_properties(dev_testing PROPERTIES CXX_STANDARD 20 CXX_EXTENSIONS OFF)

View File

@ -0,0 +1,11 @@
#version 440
in vec3 vs_position;
in vec3 vs_color;
in vec2 vs_texcoord;
out vec4 fs_color;
void main() {
fs_color = vec4(vs_color, 1.f);
}

View File

@ -0,0 +1,17 @@
#version 440
layout (location = 0) in vec3 vertex_position;
layout (location = 1) in vec3 vertex_color;
layout (location = 2) in vec2 vertex_texcoord;
out vec3 vs_position;
out vec3 vs_color;
out vec2 vs_texcoord;
void main() {
vs_position = vertex_position;
vs_color = vertex_color;
vs_texcoord = vec2(vertex_texcoord.x, vertex_texcoord.y);
gl_Position = vec4(vertex_position, 1.f);
}

View File

@ -3,13 +3,45 @@
// Github: https://github.com/SeanOMik // Github: https://github.com/SeanOMik
// //
#include <simpleengine/shader.h>
#include <simpleengine/renderable.h>
#include <simpleengine/event/event.h>
#include <simpleengine/shader_program.h>
#include <simpleengine/game.h> #include <simpleengine/game.h>
#include <simpleengine/shapes/2d/triangle.h>
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>
#include <sstream>
#include <cmrc/cmrc.hpp>
#include <stdint.h>
CMRC_DECLARE(resource_shaders);
std::string read_resource_shader(const std::string& path) {
auto fs = cmrc::resource_shaders::get_filesystem();
cmrc::file vertex_file = fs.open(path);
return std::string(vertex_file.begin());
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
simpleengine::Game game(1280, 720, "SimpleEngine - Developer Testing", false); simpleengine::Game game(1280, 720, "SimpleEngine 3D OpenGL - Developer Testing", false);
// Load shaders
std::string vertex_core = read_resource_shader("shaders/vertex_core.glsl");
std::string fragment_core = read_resource_shader("shaders/fragment_core.glsl");
// Create shader program
simpleengine::ShaderProgram shader_prog;
shader_prog.add_shader_from_source(simpleengine::ShaderType::Vertex, vertex_core);
shader_prog.add_shader_from_source(simpleengine::ShaderType::Fragment, fragment_core);
shader_prog.link();
std::shared_ptr<GLuint> base_shader_program = shader_prog.program;
// Create just a simple 2d triangle
std::shared_ptr<simpleengine::Event> tri(new simpleengine::shapes_2d::Triangle(base_shader_program));
game.add_event(tri);
return game.run(); return game.run();
} }

View File

@ -0,0 +1,24 @@
//
// Created by SeanOMik on 3/12/2021.
// Github: https://github.com/SeanOMik
//
#ifndef SIMPLEENGINE_DESTRUCTABLE_H
#define SIMPLEENGINE_DESTRUCTABLE_H
namespace simpleengine {
class Destructable {
public:
virtual void destroy() {
destroying = true;
}
virtual const bool& is_destroying() const {
return destroying;
}
protected:
bool destroying = false;
};
}
#endif //SIMPLEENGINE_DESTRUCTABLE_H

View File

@ -0,0 +1,28 @@
//
// Created by SeanOMik on 7/2/2020.
// Github: https://github.com/SeanOMik
//
#ifndef SIMPLEENGINE_EVENT_H
#define SIMPLEENGINE_EVENT_H
#include "../destructable.h"
#include <memory>
#include <GLFW/glfw3.h>
namespace simpleengine {
class Event : public simpleengine::Destructable {
public:
explicit Event(std::shared_ptr<GLFWwindow> window = nullptr) : window(window) {}
virtual ~Event() = default;
virtual void update(const float& delta_time) = 0;
virtual void render(std::shared_ptr<GLFWwindow> target) = 0;
protected:
std::shared_ptr<GLFWwindow> window;
};
}
#endif //GAMEENGINE_EVENT_H

View File

@ -8,15 +8,18 @@
#include <string> #include <string>
#include <memory> #include <memory>
#include <vector>
#include <gl/glew.h> #include <gl/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "event/event.h"
namespace simpleengine { namespace simpleengine {
class Game { class Game {
private:
using self = simpleengine::Game;
public: public:
friend class CollisionHandler;
/** /**
* @brief Construct a new Game object. Initializes GLEW and OpenGL * @brief Construct a new Game object. Initializes GLEW and OpenGL
* *
@ -24,10 +27,14 @@ namespace simpleengine {
* @param h Height of viewport * @param h Height of viewport
* @param window_name The name of the window * @param window_name The name of the window
*/ */
Game(int w, int h, const std::string& window_name, const bool& resizeable = false); Game(int w, int h, const std::string& window_name, const int& gl_profile = GLFW_OPENGL_CORE_PROFILE, const int& major_version = 4,
const int& minor_version = 4, const bool& resizeable = false, const int& forward_compat = GL_TRUE);
virtual ~Game(); virtual ~Game();
void add_event(std::shared_ptr<simpleengine::Event> event);
void update(); void update();
void handle_input();
void render_window(); void render_window();
void render_items(); void render_items();
void exit(); void exit();
@ -38,7 +45,12 @@ namespace simpleengine {
private: private:
static void framebuffer_resize_callback(GLFWwindow*, int fbW, int fbH); static void framebuffer_resize_callback(GLFWwindow*, int fbW, int fbH);
void initialize(const int& gl_profile, const int& major_version, const int& minor_version,
const bool& resizeable, const int& forward_compat = GL_TRUE);
std::shared_ptr<GLFWwindow> window; std::shared_ptr<GLFWwindow> window;
std::vector<std::shared_ptr<simpleengine::Event>> events;
const bool& window_resizeable;
}; };
} }

View File

@ -0,0 +1,25 @@
//
// Created by SeanOMik on 7/2/2020.
// Github: https://github.com/SeanOMik
//
#ifndef SIMPLEENGINE_RENDERABLE_H
#define SIMPLEENGINE_RENDERABLE_H
#include "event/event.h"
#include <memory>
#include <GLFW/glfw3.h>
namespace simpleengine {
class Renderable : public simpleengine::Event {
private:
using super = simpleengine::Event;
public:
explicit Renderable(std::shared_ptr<GLFWwindow> window = nullptr) : super(window) {}
virtual ~Renderable() = default;
};
}
#endif //SIMPLEENGINE_RENDERABLE_H

View File

@ -0,0 +1,165 @@
//
// Created by SeanOMik on 7/2/2020.
// Github: https://github.com/SeanOMik
//
#ifndef SIMPLEENGINE_SHADER_H
#define SIMPLEENGINE_SHADER_H
#include <gl/glew.h>
#include <gl/gl.h>
#include <GLFW/glfw3.h>
#include "event/event.h"
#include <exception>
#include <fstream>
#include <gl/gl.h>
#include <memory>
#include <string>
#include <iostream>
#include <sstream>
namespace simpleengine {
class ShaderException : public std::exception {
public:
explicit ShaderException(char const* const msg) : std::exception(msg) {
}
};
enum ShaderType {
Vertex = GL_VERTEX_SHADER,
Fragment = GL_FRAGMENT_SHADER,
};
class Shader : public simpleengine::Event {
private:
using super = simpleengine::Event;
protected:
Shader() {
}
public:
static Shader from_source(const ShaderType& type, std::string& shader_source) {
Shader shd = Shader::from_source(std::make_shared<GLuint>(glCreateProgram()), type, shader_source);
shd.link();
shd.delete_shader();
return shd;
}
static Shader from_source(std::shared_ptr<GLuint> program, const ShaderType& type, std::string& shader_source) {
Shader shd;
shd.program = program;
shd.shader = glCreateShader(type);
const GLchar* vert_src = shader_source.c_str();
glShaderSource(shd.shader, 1, &vert_src, NULL);
glCompileShader(shd.shader);
GLint success = false;
glGetShaderiv(shd.shader, GL_COMPILE_STATUS, &success);
if (!success) {
char log[512];
glGetShaderInfoLog(shd.shader, 512, NULL, log);
std::cerr << "Failed to load shader from source:" << std::endl << log << std::endl;
throw ShaderException("Failed to compile shader!");
}
glAttachShader(*shd.program, shd.shader);
return shd;
}
/**
* @brief Load a shader from a filepath.
*
* @param type The type of the shader.
* @param shader_path The path of the shader source.
*/
static Shader from_filepath(const ShaderType& type, const std::string& shader_path) {
Shader shd = Shader::from_filepath(std::make_shared<GLuint>(glCreateProgram()), type, shader_path);
shd.link();
shd.delete_shader();
return shd;
}
/**
* @brief Load a shader from a filepath.
*
* @param program The shader program that this shader will be attached to.
* @param type The type of the shader.
* @param shader_path The path of shader source.
*/
static Shader from_filepath(std::shared_ptr<GLuint> program, const ShaderType& type,
const std::string& shader_path) {
std::ifstream fstream(shader_path, std::ios::in);
if (!fstream.is_open()) {
std::cerr << "Failed to open shader file: " << shader_path << std::endl;
throw ShaderException("Failed to open shader file!");
}
std::stringstream ss;
{
std::string str;
while (std::getline(fstream, str))
{
ss << str << std::endl;
}
}
std::string ss_str = ss.str();
return Shader::from_source(type, ss_str);
}
virtual ~Shader() {
delete_shader();
}
/**
* @brief Link the shader program and checks for errors.
*
*/
void link() {
glLinkProgram(*program);
GLint success = false;
glGetProgramiv(*program, GL_LINK_STATUS, &success);
if (!success) {
std::cerr << "Failed to link shader program!" << std::endl;
throw ShaderException("Failed to link shader program!");
}
}
/**
* @brief Delete the shader with glDeleteShader. Only do this after we've linked.
*
*/
void delete_shader() {
glDeleteShader(shader);
}
virtual void update(const float& delta_time) override {
//super::update(delta_time);
}
virtual void render(std::shared_ptr<GLFWwindow> target) override {
//super::render(target);
}
std::shared_ptr<GLuint> program;
private:
GLuint shader;
};
}
#endif //SIMPLEENGINE_SHADER_H

View File

@ -0,0 +1,122 @@
//
// Created by SeanOMik on 7/2/2020.
// Github: https://github.com/SeanOMik
//
#ifndef SIMPLEENGINE_SHADER_PROGRAM_H
#define SIMPLEENGINE_SHADER_PROGRAM_H
#include <gl/glew.h>
#include <gl/gl.h>
#include <GLFW/glfw3.h>
#include "event/event.h"
#include "shader.h"
#include <exception>
#include <fstream>
#include <memory>
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
namespace simpleengine {
class ShaderProgram : public simpleengine::Event {
private:
using super = simpleengine::Event;
public:
ShaderProgram() : program(std::make_shared<GLuint>(glCreateProgram())) {
}
virtual ~ShaderProgram() {
glDeleteProgram(*program);
}
/**
* @brief Add a shader to this shader program. Also checks that its using the same `program` as this.
*
* @see ShaderProgram::add_shader(const ShaderType& type, const std::string& shader_path)
* @param shader The shader to add.
* @return ShaderProgram& self
*/
ShaderProgram& add_shader(Shader& shader) {
if (shader.program != this->program) {
throw std::exception("The added shader does not have the same program as this shade program!");
}
shaders.push_back(shader);
return *this;
}
/**
* @brief Create and add a shader from a string to this shader program.
*
* @param type The type of the shader.
* @param shader_path The path of the shader.
* @return ShaderProgram& self
*/
ShaderProgram& add_shader_from_source(const ShaderType& type, std::string& shader_source) {
Shader shd = Shader::from_source(program, type, shader_source);
shaders.emplace_back(shd);
return *this;
}
/**
* @brief Create and add a shader from a filepath to this shader program.
*
* @param type The type of the shader.
* @param shader_path The path of the shader.
* @return ShaderProgram& self
*/
ShaderProgram& add_shader_from_path(const ShaderType& type, const std::string& shader_path) {
Shader shd = Shader::from_filepath(program, type, shader_path);
shaders.emplace_back(shd);
return *this;
}
/**
* @brief Link the shader program. Also removes unused shader resources.
*
*/
void link() {
if (shaders.empty()) {
throw std::exception("Shaders cannot be empty when running simpleengine::ShaderProgram::link()!");
}
glLinkProgram(*program);
GLint success = false;
glGetProgramiv(*program, GL_LINK_STATUS, &success);
if (!success) {
std::cerr << "Failed to link shader program!" << std::endl;
throw ShaderException("Failed to link shader program!");
}
for (Shader& shader : shaders) {
shader.delete_shader();
}
}
virtual void update(const float& delta_time) {
}
virtual void render(std::shared_ptr<GLFWwindow> target) {
glUseProgram(*program);
}
std::shared_ptr<GLuint> program;
std::vector<Shader> shaders;
};
}
#endif //SIMPLEENGINE_SHADER_PROGRAM_H

View File

@ -0,0 +1,66 @@
//
// Created by SeanOMik on 7/2/2020.
// Github: https://github.com/SeanOMik
//
#ifndef SIMPLEENGINE_TRIANGLE_H
#define SIMPLEENGINE_TRIANGLE_H
#include <gl/glew.h>
#include <gl/gl.h>
#include <GLFW/glfw3.h>
#include "../../renderable.h"
namespace simpleengine::shapes_2d {
class Triangle : public simpleengine::Renderable {
private:
using super = simpleengine::Renderable;
private:
std::shared_ptr<GLuint> shader_program;
public:
float vertices[9];
uint32_t vbo;
uint32_t vao;
Triangle(std::shared_ptr<GLuint> shader_program) : super(nullptr), shader_program(shader_program), vertices{
-0.5f, -0.5f, 0.0f, // left
0.5f, -0.5f, 0.0f, // right
0.0f, 0.5f, 0.0f // top
} {
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, 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);
}
virtual ~Triangle() = default;
virtual void update(const float& delta_time) override {
}
virtual void render(std::shared_ptr<GLFWwindow> target) override {
glUseProgram(*shader_program);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
};
}
#endif //SIMPLEENGINE_TRIANGLE_H

View File

@ -4,6 +4,7 @@
// //
#include "game.h" #include "game.h"
#include "event/event.h"
#include <iostream> #include <iostream>
@ -12,17 +13,11 @@
#include <gl/gl.h> #include <gl/gl.h>
simpleengine::Game::Game(int w, int h, const std::string& window_name, const bool& resizeable) { simpleengine::Game::Game(int w, int h, const std::string& window_name, const int& gl_profile, const int& major_version,
const int& minor_version, const bool& resizeable, const int& forward_compat) : window_resizeable(resizeable) {
initialize(gl_profile, major_version, minor_version, window_resizeable, forward_compat);
// Create a window // Create a window
glfwInit();
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_RESIZABLE, resizeable);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
window = std::shared_ptr<GLFWwindow>(glfwCreateWindow(w, h, window_name.c_str(), NULL, NULL)); window = std::shared_ptr<GLFWwindow>(glfwCreateWindow(w, h, window_name.c_str(), NULL, NULL));
// If we're not resizeable, we need to set the viewport size. // If we're not resizeable, we need to set the viewport size.
@ -45,22 +40,49 @@ simpleengine::Game::Game(int w, int h, const std::string& window_name, const boo
} }
} }
void simpleengine::Game::initialize(const int& gl_profile, const int& major_version, const int& minor_version,
const bool& resizeable, const int& forward_compat) {
glfwInit();
glfwWindowHint(GLFW_OPENGL_PROFILE, gl_profile);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major_version);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor_version);
glfwWindowHint(GLFW_RESIZABLE, window_resizeable);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, forward_compat);
}
simpleengine::Game::~Game() { simpleengine::Game::~Game() {
} }
void simpleengine::Game::add_event(std::shared_ptr<simpleengine::Event> event) {
events.push_back(event);
}
void simpleengine::Game::update() { void simpleengine::Game::update() {
handle_input();
// Update items
for (const std::shared_ptr<Event>& event : events) {
event->update(0.f);
}
}
void simpleengine::Game::handle_input() {
} }
void simpleengine::Game::render_window() { void simpleengine::Game::render_window() {
glClearColor(0.f, 0.f, 0.f, 1.f); glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
render_items(); render_items();
} }
void simpleengine::Game::render_items() { void simpleengine::Game::render_items() {
for (const std::shared_ptr<Event>& event : events) {
event->render(window);
}
} }
int simpleengine::Game::run() { int simpleengine::Game::run() {