From fed672f23ef8f246301c41028f1915a3305a2bf3 Mon Sep 17 00:00:00 2001 From: Aaro Saila Date: Mon, 26 Jan 2026 01:39:22 +0200 Subject: [PATCH] cube --- src/Shape.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ src/headers/Shape.hpp | 1 + src/main.cpp | 11 ++++++++--- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/Shape.cpp b/src/Shape.cpp index ba2429e..e47d349 100644 --- a/src/Shape.cpp +++ b/src/Shape.cpp @@ -20,6 +20,7 @@ Shape::Shape( glm::vec3 pos ) : rotation { 0.0f, 0.0f, 0.0f } + , scale { 1.0f, 1.0f, 1.0f } , vertices { shapeInfo.vertices } , indices { shapeInfo.indices } , pos { pos } @@ -76,6 +77,7 @@ void Shape::draw() const { model = glm::rotate(model, glm::radians(rot_amount), rot_axis); } } + model = glm::scale(model, this->scale); this->shader->set_mat4("model", model); glBindVertexArray(this->vao); @@ -91,6 +93,7 @@ std::map shapes { { "triangle", { + // clang-format off .vertices = { 0.0f, 0.5f, 0.0f, // top -0.5f, -0.5f, 0.0f, // right @@ -99,6 +102,44 @@ std::map shapes { .indices = { 0, 1, 2 } + // clang-format on + } + }, + { + "cube", + { + // clang-format off + .vertices = { + -0.5f, 0.5f, 0.0f, // front top-left, 0 + 0.5f, 0.5f, 0.0f, // front top-right, 1 + 0.5f, -0.5f, 0.0f, // front bottom-right, 2 + -0.5f, -0.5f, 0.0f, // front bottom-left, 3 + + -0.5f, 0.5f, -1.0f, // rear top-left, 4 + 0.5f, 0.5f, -1.0f, // rear top-right, 5 + 0.5f, -0.5f, -1.0f, // rear bottom-right, 6 + -0.5f, -0.5f, -1.0f, // rear bottom-left, 7 + }, + .indices = { + 0, 1, 2, + 0, 2, 3, + + 4, 5, 6, + 4, 6, 7, + + 0, 4, 3, + 3, 7, 4, + + 2, 6, 1, + 1, 5, 6, + + 0, 4, 1, + 1, 5, 4, + + 2, 6, 3, + 3, 7, 6 + } + // clang-format on } } }; diff --git a/src/headers/Shape.hpp b/src/headers/Shape.hpp index d2adb62..c8038c2 100644 --- a/src/headers/Shape.hpp +++ b/src/headers/Shape.hpp @@ -15,6 +15,7 @@ struct ShapeInfo { class Shape { public: glm::vec3 rotation; + glm::vec3 scale; Shape( std::shared_ptr shader, diff --git a/src/main.cpp b/src/main.cpp index f3bae31..106a57b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -157,17 +157,22 @@ int main() { { shader, shapes.at("triangle"), { 0.0f, 0.0f, 0.0f } }, { shader, shapes.at("triangle"), { 0.5f, 0.0f, -0.5f } }, { shader, shapes.at("triangle"), { -0.5f, 0.0f, -0.5f } }, - { shader, shapes.at("triangle"), { 0.0f, 0.0f, -1.0f } } + { shader, shapes.at("triangle"), { 0.0f, 0.0f, -1.0f } }, + { shader, shapes.at("cube"), { 0.0f, 0.0f, -0.25f }} }; created_shapes.at(1).rotation.y = 90.0f; created_shapes.at(2).rotation.y = 90.0f; - std::array colors { + created_shapes.at(4).scale /= 2; + // created_shapes.at(4).scale.z /= 2; + + std::array colors { glm::vec4 { 1.0f, 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, 0.0f, 0.0f }, + { 1.0f, 1.0f, 1.0f, 0.0f } }; long unsigned int frame { 0 }; @@ -200,7 +205,7 @@ int main() { }; shader->set_mat4("projection", projection); - for (int i { 0 }; i < created_shapes.size(); i++) { + for (std::size_t i { 0 }; i < created_shapes.size(); i++) { shader->set_vec4("thecolor", colors[i]); created_shapes[i].draw(); }