From 4cdea486756555e48041da3af252516b7f9843d9 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 25 Oct 2022 17:40:20 +0200 Subject: [PATCH] Refactor & added renderer --- CMakeLists.txt | 8 +++-- headers/renderer.hpp | 29 ++++++++++++++++ headers/shaders.hpp | 2 ++ headers/textures.hpp | 4 +-- src/main.cpp | 79 +++++--------------------------------------- src/renderer.cpp | 54 ++++++++++++++++++++++++++++++ 6 files changed, 101 insertions(+), 75 deletions(-) create mode 100644 headers/renderer.hpp create mode 100644 src/renderer.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f34a33c..0137acc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,9 +3,13 @@ cmake_minimum_required(VERSION 3.24.2) project(Hohmann VERSION 1.0) #### Sources -file(GLOB SOURCES src/*.cpp headers/*.hpp) -add_executable(Hohmann ${SOURCES}) +file(GLOB HEADERS headers/*.hpp headers/*.h) +file(GLOB SOURCES src/*.cpp src/*.c) +add_executable(Hohmann src/main.cpp) + +include_directories(headers) +target_sources(Hohmann PRIVATE ${SOURCES}) #### Libs # GLFW diff --git a/headers/renderer.hpp b/headers/renderer.hpp new file mode 100644 index 0000000..c7165e9 --- /dev/null +++ b/headers/renderer.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include "shaders.hpp" +#include "textures.hpp" + +#define VERTEX_ATTRIB_PTR_SIZE 8 * sizeof(float) + +#define VERT_SHADER_SRC_FILE "shaders/vertex.glsl" +#define FRAG_SHADER_SRC_FILE "shaders/fragment.glsl" + +#define RUSTY_METAL_TEXTURE "assets/textures/rusty_metal.jpg" + +namespace Renderer { + class Obj2D { + public: + Obj2D(unsigned int indices[], unsigned int icount, float verts[], unsigned int vcount); + Shaders::Shader shader; + Textures::Texture2D texture; + + void transform(glm::mat4 T); + void bind_texture(Textures::Texture2D texture); + void render(); + private: + unsigned int EBO; + unsigned int VBO; + unsigned int VAO; + }; +} diff --git a/headers/shaders.hpp b/headers/shaders.hpp index 95ebfd5..ad2ba5d 100644 --- a/headers/shaders.hpp +++ b/headers/shaders.hpp @@ -1,3 +1,5 @@ +#pragma once + #include "glm/fwd.hpp" #include #include diff --git a/headers/textures.hpp b/headers/textures.hpp index 0bdb916..5029cfd 100644 --- a/headers/textures.hpp +++ b/headers/textures.hpp @@ -1,5 +1,4 @@ -#ifndef TEXTURES_HPP -#define TEXTURES_HPP +#pragma once namespace Textures { class Texture2D { @@ -14,4 +13,3 @@ namespace Textures { }; } -#endif diff --git a/src/main.cpp b/src/main.cpp index bfc3fe6..b84b401 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,8 +7,9 @@ #include #include -#include "../headers/shaders.hpp" -#include "../headers/textures.hpp" +#include "../headers/renderer.hpp" +// #include "../headers/shaders.hpp" +// #include "../headers/textures.hpp" #define WINDOW_WIDTH 640 #define WINDOW_HEIGHT 480 @@ -29,70 +30,12 @@ void processInput(GLFWwindow *win) { } } -struct RenderObj { - unsigned int EBO; - Shaders::Shader shader; - Textures::Texture2D texture; -}; - -RenderObj preRenderCallback(unsigned int indices[], unsigned int indices_count, float verts[], unsigned int verts_count) { - // Vertex buffer object - unsigned int VBO; - glGenBuffers(1, &VBO); - - // Vertex Array Object - unsigned int VAO; - glGenVertexArrays(1, &VAO); // gen the VAO - glBindVertexArray(VAO); // bind it - - // Copy verts into buffer - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, verts_count, verts, GL_DYNAMIC_DRAW); // for moving stuff - //glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); - - unsigned int EBO; - glGenBuffers(1, &EBO); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices_count, indices, GL_STATIC_DRAW); - - // Set attrib pointers - #define VERTEX_ATTRIB_PTR_SIZE 8 * sizeof(float) - - // Pos - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)0); - glEnableVertexAttribArray(0); - - // Color - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(3*sizeof(float))); - glEnableVertexAttribArray(1); - - // Texture - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(6*sizeof(float))); - glEnableVertexAttribArray(2); - - Shaders::Shader shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE); - Textures::Texture2D texture(RUSTY_METAL_TEXTURE); - - return RenderObj {EBO, shader, texture}; -} - void renderCallback() { // Make background glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); } -void renderObject(RenderObj ro, glm::mat4 T) { - // Bind the texture - ro.texture.bind(); - - ro.shader.setMat4("transform", T); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ro.EBO); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); -} - int main() { glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); @@ -131,7 +74,7 @@ int main() { float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); - RenderObj ro = preRenderCallback(indices, sizeof(indices), verts, sizeof(verts)); + Renderer::Obj2D ro(indices, sizeof(indices), verts, sizeof(verts)); while (!glfwWindowShouldClose(win)) { // Handle input @@ -144,25 +87,21 @@ int main() { float time = glfwGetTime(); float gVal = (sin(time) / 1.5f) + 0.5f; - // Coloring uniform - ro.shader.use(); - // ro.shader.setFloat("r", gVal); - // ro.shader.setFloat("g", gVal); - // ro.shader.setFloat("b", gVal); - // Transformation float rotang = time; glm::mat4 T = glm::mat4(1.0f); T = glm::rotate(T, rotang, glm::vec3(0.0, 0.707, 0.707)); T = glm::scale(T, glm::vec3(0.5, 0.5, 0.5)); - renderObject(ro, T); + ro.transform(T); + ro.render(); glm::mat4 T2 = glm::mat4(1.0f); - T2 = glm::translate(T2, glm::vec3(-0.5, 0.4, 0.0)); T2 = glm::rotate(T2, rotang, glm::vec3(0.707, 0.707, 0.0)); + T2 = glm::translate(T2, glm::vec3(-0.5, 0.4, 0.0)); T2 = glm::scale(T2, glm::vec3(0.5, 0.5, 0.2)); - renderObject(ro, T2); + ro.transform(T2); + ro.render(); // glfw glfwSwapBuffers(win); diff --git a/src/renderer.cpp b/src/renderer.cpp new file mode 100644 index 0000000..6f76410 --- /dev/null +++ b/src/renderer.cpp @@ -0,0 +1,54 @@ +#include +#include +#include + +#include "renderer.hpp" +#include "shaders.hpp" + +namespace Renderer { + void Obj2D::transform(glm::mat4 T) { + shader.setMat4("transform", T); + } + + void Obj2D::render() { + shader.use(); + texture.bind(); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + } + + Obj2D::Obj2D(unsigned int indices[], unsigned int icount, float verts[], unsigned int vcount) + : shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE), texture(RUSTY_METAL_TEXTURE) { + + // Vertex buffer object + glGenBuffers(1, &VBO); + + // Vertex Array Object + glGenVertexArrays(1, &VAO); // gen the VAO + glBindVertexArray(VAO); // bind it + + // Copy verts into buffer + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, vcount, verts, GL_DYNAMIC_DRAW); // for moving stuff + //glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); + + glGenBuffers(1, &EBO); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, icount, indices, GL_STATIC_DRAW); + + // Pos + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)0); + glEnableVertexAttribArray(0); + + // Color + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(3*sizeof(float))); + glEnableVertexAttribArray(1); + + // Texture + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(6*sizeof(float))); + glEnableVertexAttribArray(2); + } + +}