From 7901413d7b2abb57db0898e4f525adb8f11c43b5 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Sun, 28 Nov 2021 13:21:25 -0500 Subject: [PATCH] Attempt to get a triangle moving --- examples/dev_testing/src/main.cpp | 9 +- include/simpleengine/shapes/2d/triangle.h | 2 +- include/simpleengine/vector.h | 128 ++++++++++++++++++++++ include/simpleengine/vertex.h | 5 +- src/shapes/2d/triangle.cpp | 5 +- 5 files changed, 142 insertions(+), 7 deletions(-) create mode 100644 include/simpleengine/vector.h diff --git a/examples/dev_testing/src/main.cpp b/examples/dev_testing/src/main.cpp index c29b666..83f6881 100644 --- a/examples/dev_testing/src/main.cpp +++ b/examples/dev_testing/src/main.cpp @@ -1,5 +1,6 @@ #include "simpleengine/gfx/texture.h" #include "simpleengine/shapes/2d/square.h" +#include "simpleengine/vector.h" #include #include #include @@ -43,13 +44,13 @@ int main(int argc, char *argv[]) { simpleengine::gfx::Texture crate_texture("resources/container.jpg", true, true); std::vector vertices = { - {glm::vec3(-0.5f, -0.5f, 0.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 0.f)}, // bottom left - {glm::vec3(0.5f, -0.5f, 0.f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f)}, // bottom right - {glm::vec3(0.f, 0.5f, 0.f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.5f, 1.0f)}, // top + { simpleengine::Vectorf(-0.5f, -0.5f, 0.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 0.f) }, // bottom left + { simpleengine::Vectorf(0.5f, -0.5f, 0.f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, // bottom right + { simpleengine::Vectorf(0.f, 0.5f, 0.f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.5f, 1.0f) }, // top }; auto tri = std::make_shared(base_shader_program, vertices); - tri->set_texture(wall_texture); + //tri->set_texture(wall_texture); game.add_event(tri); /* std::vector vertices = { diff --git a/include/simpleengine/shapes/2d/triangle.h b/include/simpleengine/shapes/2d/triangle.h index 5379c17..d34811a 100644 --- a/include/simpleengine/shapes/2d/triangle.h +++ b/include/simpleengine/shapes/2d/triangle.h @@ -14,6 +14,7 @@ #include "../../gfx/shader.h" #include "../../gfx/texture.h" #include "../../optional.h" +#include "../../translatable.h" #include @@ -38,7 +39,6 @@ namespace simpleengine::shapes_2d { void set_texture(gfx::Texture texture); virtual void update(const float& delta_time) override; - virtual void render(std::shared_ptr target) override; }; } \ No newline at end of file diff --git a/include/simpleengine/vector.h b/include/simpleengine/vector.h new file mode 100644 index 0000000..691cd38 --- /dev/null +++ b/include/simpleengine/vector.h @@ -0,0 +1,128 @@ +#pragma once + +#include +#include +#include +#include + +namespace simpleengine { + template + class Vector { + private: + glm::vec<3, VectorType, glm::defaultp> inner_vec; + public: + Vector(VectorType x, VectorType y, VectorType z) : inner_vec(x, y, z) { + + } + + Vector(glm::vec<3, VectorType, glm::defaultp> glm_vec) : inner_vec(glm_vec) { + + } + + Vector(const glm::vec<3, VectorType, glm::defaultp>& glm_vec) : inner_vec(glm_vec) { + + } + + operator glm::vec<3, VectorType, glm::defaultp>() const { + return inner_vec; + } + + VectorType& x() { + return inner_vec.x; + } + + VectorType& y() { + return inner_vec.y; + } + + VectorType& z() { + return inner_vec.z; + } + + void set_x(const VectorType x) { + inner_vec.x = x; + } + + void set_y(const VectorType y) { + inner_vec.y = y; + } + + void set_z(const VectorType z) { + inner_vec.z = z; + } + + Vector operator+(const Vector& vector) { + this->inner_vec += vector.inner_vec; + } + + Vector operator-(const Vector& vector) { + this->inner_vec -= vector.inner_vec; + } + + void translate(const Vector& vector) { + this += vector; + } + + void translate(const VectorType& x, const VectorType& y, const VectorType& z) { + this->inner_vec.x += x; + this->inner_vec.y += y; + this->inner_vec.z += z; + } + + void translate_x(const VectorType x) { + this->inner_vec.x += x; + } + + void translate_y(const VectorType y) { + this->inner_vec.y += y; + } + + void translate_z(const VectorType z) { + this->inner_vec.z += z; + } + + glm::mat4 rotation_matrix(float degrees, glm::vec3 rotation_axis) const { + glm::mat4 trans = glm::mat4(1.0f); + trans = glm::rotate(trans, glm::radians(degrees), rotation_axis); + return trans; + } + + glm::mat4 rotation_x_matrix(float degrees) const { + return rotation_axis(degrees, glm::vec3(1.f, 0.f, 0.f)); + } + + glm::mat4 rotation_y_matrix(float degrees) const { + return rotation_axis(degrees, glm::vec3(0.f, 1.f, 0.f)); + } + + glm::mat4 rotation_z_matrix(float degrees) const { + return rotation_axis(degrees, glm::vec3(0.f, 0.f, 1.f)); + } + + void rotate(float degrees, glm::vec3 rotation_axis) { + glm::mat4 trans = glm::mat4(1.0f); + trans = glm::rotate(trans, glm::radians(degrees), rotation_axis); + this->inner_vec = trans * this->inner_vec; + } + + void rotate_x(float degrees) { + rotate(degrees, glm::vec3(1.f, 0.f, 0.f)); + } + + void rotate_y(float degrees) { + rotate(degrees, glm::vec3(0.f, 1.f, 0.f)); + } + + void rotate_z(float degrees) { + rotate(degrees, glm::vec3(0.f, 0.f, 1.f)); + } + + void scale(float scalar) { + glm::mat4 trans = glm::mat4(1.0f); + trans = glm::scale(trans, glm::vec3(scalar, scalar, scalar)); + this->inner_vec = trans * this->inner_vec; + } + }; + + typedef Vector Vectorf; +} \ No newline at end of file diff --git a/include/simpleengine/vertex.h b/include/simpleengine/vertex.h index af98173..0992a0d 100644 --- a/include/simpleengine/vertex.h +++ b/include/simpleengine/vertex.h @@ -5,10 +5,13 @@ #include +#include "vector.h" + namespace simpleengine { class Vertex { public: - glm::vec3 position; + //glm::vec3 position; + simpleengine::Vectorf position; glm::vec3 color; glm::vec2 tex_coord; }; diff --git a/src/shapes/2d/triangle.cpp b/src/shapes/2d/triangle.cpp index a791a82..81bb401 100644 --- a/src/shapes/2d/triangle.cpp +++ b/src/shapes/2d/triangle.cpp @@ -30,7 +30,10 @@ namespace simpleengine::shapes_2d { } void Triangle::update(const float& delta_time) { - + for (Vertex& vertex : vertices) { + vertex.position.translate_x(0.01f); + vertex.position.translate_y(0.01f); + } } void Triangle::render(std::shared_ptr target) {