From ffd2cca5c9387f0bd4da51b4e1dc3e5e71664927 Mon Sep 17 00:00:00 2001 From: Aaro Saila Date: Wed, 4 Feb 2026 21:17:07 +0200 Subject: [PATCH] started work on CompoundShape --- meson.build | 3 ++- src/CompoundShape.cpp | 24 ++++++++++++++++++ src/CompoundShape.hpp | 17 +++++++++++++ src/Shape.cpp | 47 +++++++++++++++++++++-------------- src/headers/CompoundShape.hpp | 21 ++++++++++++++++ src/headers/IShape.hpp | 17 +++++++++++++ src/headers/Shape.hpp | 14 +++++++---- src/main.cpp | 30 +++++++++++----------- 8 files changed, 134 insertions(+), 39 deletions(-) create mode 100644 src/CompoundShape.cpp create mode 100644 src/CompoundShape.hpp create mode 100644 src/headers/CompoundShape.hpp create mode 100644 src/headers/IShape.hpp diff --git a/meson.build b/meson.build index ec5d63d..a687329 100644 --- a/meson.build +++ b/meson.build @@ -18,6 +18,7 @@ sources = [ 'src/util.cpp', 'src/settings.cpp', 'src/Shape.cpp', + 'src/CompoundShape.cpp', 'src/Shader.cpp', 'src/Camera.cpp', @@ -26,7 +27,7 @@ sources = [ cpp_args = [ '-DVERTEX_SHADER_PATH="../src/shaders/shader.vert"', - '-DFRAGMENT_SHADER_PATH="../src/shaders/shader.frag"' + '-DFRAGMENT_SHADER_PATH="../src/shaders/shader.frag"', ] exe = executable( diff --git a/src/CompoundShape.cpp b/src/CompoundShape.cpp new file mode 100644 index 0000000..bdb17ef --- /dev/null +++ b/src/CompoundShape.cpp @@ -0,0 +1,24 @@ +#include "CompoundShape.hpp" + +// constructors + +CompoundShape::CompoundShape(const std::vector shapes) + : shapes { shapes } + , transform { 1.0f } { + + for (auto& shape : this->shapes) { + shape.init(); + } +} + +// public + +void CompoundShape::draw() { + for (auto& shape : this->shapes) { + glm::mat4 orig_transform { shape.transform }; + shape.transform *= shape.transform; + shape.draw(); + shape.transform = orig_transform; + } +} + diff --git a/src/CompoundShape.hpp b/src/CompoundShape.hpp new file mode 100644 index 0000000..a40aeaa --- /dev/null +++ b/src/CompoundShape.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include + +#include "Shape.hpp" + +class CompoundShape { +public: + std::vector shapes; + + CompoundShape(const std::vector shapes); + + void draw(); + +private: + glm::mat4 transform; +}; diff --git a/src/Shape.cpp b/src/Shape.cpp index d9d8b77..885fba9 100644 --- a/src/Shape.cpp +++ b/src/Shape.cpp @@ -16,12 +16,9 @@ Shape::Shape( std::shared_ptr shader, - ShapeInfo& shapeInfo, - glm::vec3 pos + ShapeInfo& shapeInfo ) - : pos { pos } - , rotation { 0.0f, 0.0f, 0.0f } - , scale { 1.0f, 1.0f, 1.0f } + : transform { 1.0f } , vertices { shapeInfo.vertices } , indices { shapeInfo.indices } , vao { 0 } @@ -29,6 +26,17 @@ Shape::Shape( , ebo { 0 } , shader {shader} { +} + +Shape::~Shape() { + glDeleteVertexArrays(1, &this->vao); + glDeleteBuffers(1, &this->vbo); + glDeleteBuffers(1, &this->ebo); +} + +// public + +void Shape::init() { int orig_vao { 0 }; int orig_vbo { 0 }; int orig_ebo { 0 }; @@ -61,24 +69,27 @@ Shape::Shape( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, orig_ebo); } -// private +void Shape::reset_transform() { + this->transform = glm::mat4 { 1.0f }; +} + +void Shape::translate(const glm::vec3& translation) { + this->transform = glm::translate(this->transform, translation); +} + +void Shape::rotate(const float rotation_rad, const glm::vec3& axis) { + this->transform = glm::rotate(this->transform, rotation_rad, axis); +} + +void Shape::scale(const glm::vec3& scale_amount) { + this->transform = glm::scale(this->transform, scale_amount); +} void Shape::draw() const { int orig_vao { 0 }; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &orig_vao); - glm::mat4 model { 1.0f }; - model = glm::translate(model, this->pos); - for (int i { 0 }; i < this->rotation.length(); i++) { - const float rot_amount { this->rotation[i] }; - if (rot_amount != 0.0f) { - glm::vec3 rot_axis { 0.0f }; - rot_axis[i] = 1.0f; - model = glm::rotate(model, glm::radians(rot_amount), rot_axis); - } - } - model = glm::scale(model, this->scale); - this->shader->set_mat4("model", model); + this->shader->set_mat4("model", transform); glBindVertexArray(this->vao); glDrawElements(GL_TRIANGLES, this->indices.size(), GL_UNSIGNED_INT, nullptr); diff --git a/src/headers/CompoundShape.hpp b/src/headers/CompoundShape.hpp new file mode 100644 index 0000000..37403ef --- /dev/null +++ b/src/headers/CompoundShape.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include + +#include "Shape.hpp" + +class CompoundShape { +public: + std::vector shapes; + + CompoundShape(const std::vector shapes); + + void draw() const; + void reset_transform(); + void translate(const glm::vec3& translation); + void rotate(const float rotation_rad, const glm::vec3& axis); + void scale(const glm::vec3& scale_amount); + +private: + glm::mat4 transform; +}; diff --git a/src/headers/IShape.hpp b/src/headers/IShape.hpp new file mode 100644 index 0000000..3e8bd15 --- /dev/null +++ b/src/headers/IShape.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include + +class IShape { +public: + glm::mat4 transform; + + virtual ~IShape() {}; + + virtual void init() = 0; + virtual void draw() const = 0; + virtual void reset_transform() = 0; + virtual void translate(const glm::vec3& translation) = 0; + virtual void rotate(const float rotation_rad, const glm::vec3& axis) = 0; + virtual void scale(const glm::vec3& scale_amount) = 0; +}; diff --git a/src/headers/Shape.hpp b/src/headers/Shape.hpp index b32940a..2f14166 100644 --- a/src/headers/Shape.hpp +++ b/src/headers/Shape.hpp @@ -14,17 +14,21 @@ struct ShapeInfo { class Shape { public: - glm::vec3 pos; - glm::vec3 rotation; - glm::vec3 scale; + glm::mat4 transform; Shape( std::shared_ptr shader, - ShapeInfo& shapeInfo, - glm::vec3 pos = { 0.0f, 0.0f, 0.0f } + ShapeInfo& shapeInfo ); + ~Shape(); + + void init(); void draw() const; + void reset_transform(); + void translate(const glm::vec3& translation); + void rotate(const float rotation_rad, const glm::vec3& axis); + void scale(const glm::vec3& scale_amount); private: std::vector vertices; diff --git a/src/main.cpp b/src/main.cpp index 0d0d451..d0ce4f6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "glad/glad.h" #include @@ -14,6 +15,7 @@ #include "Camera.hpp" #include "Shader.hpp" #include "Shape.hpp" +#include "CompoundShape.hpp" #include "quit.hpp" #include "settings.hpp" @@ -176,17 +178,22 @@ int main() { auto shader { std::make_shared(settings.vertex_shader_path, settings.fragment_shader_path) }; shader->use(); - Shape plane { shader, shapes.at("square") }; - plane.pos.y = -1.0f; - plane.rotation.x = 90.0f; - plane.scale *= 100; + plane.init(); + plane.translate(glm::vec3 { 0.0f, -1.0f, 0.0f }); + plane.rotate(glm::radians(90.0f), glm::vec3 { 1.0f, 0.0f, 0.0f }); + plane.scale(glm::vec3 { 1.0f } * 100.0f); - Shape square { shader, shapes.at("square") }; - Shape cube { shader, shapes.at("cube") }; + Shape tri1 { shader, shapes.at("triangle") }; + Shape tri2 { shader, shapes.at("triangle") }; + tri2.rotate(glm::radians(180.0f), glm::vec3 { 0.0f, 0.0f, 1.0f }); + CompoundShape rectangle { { + tri1, tri2 + } }; long unsigned int frame { 0 }; double last_frame_time { 0 }; + while (!glfwWindowShouldClose(window)) { const double current_time { glfwGetTime() }; delta = current_time - last_frame_time; @@ -220,15 +227,8 @@ int main() { shader->set_vec4("color", glm::normalize(glm::vec4 { 207.0f, 90.0f, 28.0f, 1.0f })); plane.draw(); - shader->set_vec4("color", { 0.0f, 0.0f, 1.0f, 1.0f }); - square.pos.x = std::cos(current_time); - square.pos.y = std::sin(current_time); - square.rotation.z = glm::degrees(current_time) + 45.0f; - square.draw(); - - // shader->set_vec4("color", { 1.0f, 1.0f, 1.0f, 1.0f }); - // cube.rotation.y += 45.0f * delta; - // cube.draw(); + shader->set_vec4("color", glm::vec4 { 0.0f, 0.0f, 0.0f, 1.0f }); + rectangle.draw(); const double time_since_last_frame { glfwGetTime() - last_frame_time }; if (time_since_last_frame < fps_lock) {