From 6b1a8ede5636c82198c6c69cddbf92dd152ad735 Mon Sep 17 00:00:00 2001 From: Benjamin Palko Date: Sun, 6 Oct 2024 16:37:08 -0400 Subject: [PATCH] encapsulate in classes --- meson.build | 3 +- src/main.cpp | 93 ++--------------------------------- src/meson.build | 3 +- src/program.cpp | 18 +++++++ src/program.hpp | 12 +++++ src/systems/meson.build | 1 + src/systems/shader-system.cpp | 81 ++++++++++++++++++++++++++++++ src/systems/shader-system.hpp | 17 +++++++ 8 files changed, 137 insertions(+), 91 deletions(-) create mode 100644 src/program.cpp create mode 100644 src/program.hpp create mode 100644 src/systems/meson.build create mode 100644 src/systems/shader-system.cpp create mode 100644 src/systems/shader-system.hpp diff --git a/meson.build b/meson.build index a8cf41a..e6a8aed 100644 --- a/meson.build +++ b/meson.build @@ -1,4 +1,3 @@ - project('test', 'cpp') deps = [] @@ -11,4 +10,4 @@ directories = include_directories('libs') subdir('src') -executable('main', sources, dependencies : deps, include_directories: directories) +executable('main', sources, dependencies: deps, include_directories: directories) diff --git a/src/main.cpp b/src/main.cpp index 9724dab..f95e6c9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,41 +1,8 @@ -#include #define GLFW_INCLUDE_NONE #include - -#include -#include -#include #include -#include -typedef struct Vertex { - vec2 pos; - vec3 col; -} Vertex; - -static const Vertex vertices[3] = {{{-0.6f, -0.4f}, {1.f, 0.f, 0.f}}, - {{0.6f, -0.4f}, {0.f, 1.f, 0.f}}, - {{0.f, 0.6f}, {0.f, 0.f, 1.f}}}; - -static const char *vertex_shader_text = - "#version 330\n" - "uniform mat4 MVP;\n" - "in vec3 vCol;\n" - "in vec2 vPos;\n" - "out vec3 color;\n" - "void main()\n" - "{\n" - " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n" - " color = vCol;\n" - "}\n"; - -static const char *fragment_shader_text = "#version 330\n" - "in vec3 color;\n" - "out vec4 fragment;\n" - "void main()\n" - "{\n" - " fragment = vec4(color, 1.0);\n" - "}\n"; +#include "program.hpp" void error_callback(int error_code, const char *description) { fmt::print("[ERROR - {}] {}\n", error_code, description); @@ -49,7 +16,7 @@ void key_callback(GLFWwindow *window, int key, int scancode, int action, fmt::print("Key pressed {}\n", key_name); } -int main(int argc, char *argv[]) { +int main() { glfwSetErrorCallback(error_callback); @@ -86,60 +53,10 @@ int main(int argc, char *argv[]) { return -1; } - GLuint vertex_buffer; - glGenBuffers(1, &vertex_buffer); - glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + ShaderSystem shaderSystem = ShaderSystem(); + Program program = Program(window, shaderSystem); - const GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL); - glCompileShader(vertex_shader); - - const GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL); - glCompileShader(fragment_shader); - - const GLuint program = glCreateProgram(); - glAttachShader(program, vertex_shader); - glAttachShader(program, fragment_shader); - glLinkProgram(program); - - const GLint mvp_location = glGetUniformLocation(program, "MVP"); - const GLint vpos_location = glGetAttribLocation(program, "vPos"); - const GLint vcol_location = glGetAttribLocation(program, "vCol"); - - GLuint vertex_array; - glGenVertexArrays(1, &vertex_array); - glBindVertexArray(vertex_array); - glEnableVertexAttribArray(vpos_location); - glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), - (void *)offsetof(Vertex, pos)); - glEnableVertexAttribArray(vcol_location); - glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), - (void *)offsetof(Vertex, col)); - - while (!glfwWindowShouldClose(window)) { - int width, height; - glfwGetFramebufferSize(window, &width, &height); - const float ratio = width / (float)height; - - glViewport(0, 0, width, height); - glClear(GL_COLOR_BUFFER_BIT); - - mat4x4 m, p, mvp; - mat4x4_identity(m); - mat4x4_rotate_Z(m, m, (float)glfwGetTime()); - mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f); - mat4x4_mul(mvp, p, m); - - glUseProgram(program); - glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp); - glBindVertexArray(vertex_array); - glDrawArrays(GL_TRIANGLES, 0, 3); - - glfwSwapBuffers(window); - glfwPollEvents(); - } + program.Loop(); glfwDestroyWindow(window); diff --git a/src/meson.build b/src/meson.build index 67a5b37..dd34cd3 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1 +1,2 @@ -sources = files('main.cpp') +sources = files('main.cpp', 'program.cpp', 'program.hpp') +subdir('systems') diff --git a/src/program.cpp b/src/program.cpp new file mode 100644 index 0000000..3eb43c7 --- /dev/null +++ b/src/program.cpp @@ -0,0 +1,18 @@ +#include "program.hpp" +#include + +Program::Program(GLFWwindow *window, class ShaderSystem shaderSystem) { + this->window = window; + this->shaderSystem = shaderSystem; +} + +int Program::Loop() { + while (!glfwWindowShouldClose(window)) { + int width, height; + glfwGetFramebufferSize(window, &width, &height); + shaderSystem.Draw(width, height, (float)glfwGetTime()); + glfwSwapBuffers(window); + glfwPollEvents(); + } + return -1; +} diff --git a/src/program.hpp b/src/program.hpp new file mode 100644 index 0000000..04e33c5 --- /dev/null +++ b/src/program.hpp @@ -0,0 +1,12 @@ +#define GLFW_INCLUDE_NONE +#include +#include "systems/shader-system.hpp" + +class Program { + GLFWwindow *window; + ShaderSystem shaderSystem; + +public: + Program(GLFWwindow *window, class ShaderSystem shaderSystem); + int Loop(); +}; diff --git a/src/systems/meson.build b/src/systems/meson.build new file mode 100644 index 0000000..0277d1f --- /dev/null +++ b/src/systems/meson.build @@ -0,0 +1 @@ +sources += files('shader-system.cpp', 'shader-system.hpp') diff --git a/src/systems/shader-system.cpp b/src/systems/shader-system.cpp new file mode 100644 index 0000000..633f9b5 --- /dev/null +++ b/src/systems/shader-system.cpp @@ -0,0 +1,81 @@ +#include "shader-system.hpp" + +typedef struct Vertex { + vec2 pos; + vec3 col; +} Vertex; + +static const Vertex vertices[3] = {{{-0.6f, -0.4f}, {1.f, 0.f, 0.f}}, + {{0.6f, -0.4f}, {0.f, 1.f, 0.f}}, + {{0.f, 0.6f}, {0.f, 0.f, 1.f}}}; + +static const char *vertex_shader_text = + "#version 330\n" + "uniform mat4 MVP;\n" + "in vec3 vCol;\n" + "in vec2 vPos;\n" + "out vec3 color;\n" + "void main()\n" + "{\n" + " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n" + " color = vCol;\n" + "}\n"; + +static const char *fragment_shader_text = "#version 330\n" + "in vec3 color;\n" + "out vec4 fragment;\n" + "void main()\n" + "{\n" + " fragment = vec4(color, 1.0);\n" + "}\n"; + +ShaderSystem::ShaderSystem() { + glGenBuffers(1, &vertex_buffer); + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + + vertex_shader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL); + glCompileShader(vertex_shader); + + fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL); + glCompileShader(fragment_shader); + + program = glCreateProgram(); + glAttachShader(program, vertex_shader); + glAttachShader(program, fragment_shader); + glLinkProgram(program); + + mvp_location = glGetUniformLocation(program, "MVP"); + vpos_location = glGetAttribLocation(program, "vPos"); + vcol_location = glGetAttribLocation(program, "vCol"); + + glGenVertexArrays(1, &vertex_array); + glBindVertexArray(vertex_array); + glEnableVertexAttribArray(vpos_location); + glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), + (void *)offsetof(Vertex, pos)); + glEnableVertexAttribArray(vcol_location); + glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), + (void *)offsetof(Vertex, col)); +} + +int ShaderSystem::Draw(int width, int height, float time) { + const float ratio = width / (float)height; + + glViewport(0, 0, width, height); + glClear(GL_COLOR_BUFFER_BIT); + + mat4x4 m, p, mvp; + mat4x4_identity(m); + mat4x4_rotate_Z(m, m, time); + mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f); + mat4x4_mul(mvp, p, m); + + glUseProgram(program); + glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp); + glBindVertexArray(vertex_array); + glDrawArrays(GL_TRIANGLES, 0, 3); + return 0; +} diff --git a/src/systems/shader-system.hpp b/src/systems/shader-system.hpp new file mode 100644 index 0000000..801fe9b --- /dev/null +++ b/src/systems/shader-system.hpp @@ -0,0 +1,17 @@ +#include +#include + +class ShaderSystem { + GLuint vertex_buffer; + GLuint vertex_shader; + GLuint fragment_shader; + GLuint program; + GLint mvp_location; + GLint vpos_location; + GLint vcol_location; + GLuint vertex_array; + +public: + ShaderSystem(); + int Draw(int width, int height, float time); +};