diff --git a/include/simpleengine/shapes/2d/triangle.h b/include/simpleengine/shapes/2d/triangle.h index dd1f8a3..49b5af6 100644 --- a/include/simpleengine/shapes/2d/triangle.h +++ b/include/simpleengine/shapes/2d/triangle.h @@ -19,18 +19,15 @@ #include "../../gfx/shader.h" #include "../../gfx/texture.h" #include "../../optional.h" -#include "../../translatable.h" +#include "../../transformable.h" #include namespace simpleengine::shapes_2d { - class Triangle : public simpleengine::Renderable { - private: - using super = simpleengine::Renderable; + class Triangle : public simpleengine::Renderable, public simpleengine::Transformable { private: gfx::Shader shader; // This only stores the shader program nonstd::optional texture; - glm::mat4 translation; public: std::vector vertices; gfx::VBO vbo; diff --git a/include/simpleengine/transformable.h b/include/simpleengine/transformable.h new file mode 100644 index 0000000..0655ea8 --- /dev/null +++ b/include/simpleengine/transformable.h @@ -0,0 +1,103 @@ +#pragma once + +#include +#include +#include +#include + +namespace simpleengine { + class Transformable { + protected: + + public: + glm::mat4 transform_matrix; + + Transformable() : transform_matrix(glm::mat4(1.f)) { + + } + + Transformable(glm::mat4 transform_matrix) : transform_matrix(transform_matrix) { + + } + + friend Transformable operator+(Transformable lhs, const Transformable& rhs) { + lhs.transform_matrix += rhs.transform_matrix; + return lhs; + } + + friend Transformable operator-(Transformable lhs, const Transformable& rhs) { + lhs.transform_matrix -= rhs.transform_matrix; + return lhs; + } + + friend Transformable operator*(Transformable lhs, const Transformable& rhs) { + lhs.transform_matrix *= rhs.transform_matrix; + return lhs; + } + + friend Transformable operator/(Transformable lhs, const Transformable& rhs) { + lhs.transform_matrix /= rhs.transform_matrix; + return lhs; + } + + virtual void combine_transform(const glm::mat4& transform_matrix) { + this->transform_matrix *= transform_matrix; + } + + virtual void combine_transform(const Transformable& transformable) { + transform_matrix = transformable.transform_matrix; + } + + virtual void translate(float x, float y, float z) { + transform_matrix = glm::translate(transform_matrix, glm::vec3(x, y, z)); + } + + virtual void translate(const float& x, const float& y, const float& z) { + transform_matrix = glm::translate(transform_matrix, glm::vec3(x, y, z)); + } + + virtual void translate(const glm::vec3& vec) { + transform_matrix = glm::translate(transform_matrix, vec); + } + + virtual glm::mat4 rotation_matrix(float degrees, glm::vec3 rotation_axis) const { + return glm::rotate(transform_matrix, glm::radians(degrees), rotation_axis); + } + + virtual glm::mat4 rotation_x_matrix(float degrees) const { + return rotation_matrix(degrees, glm::vec3(1, 0, 0)); + } + + virtual glm::mat4 rotation_y_matrix(float degrees) const { + return rotation_matrix(degrees, glm::vec3(0, 1, 0)); + } + + virtual glm::mat4 rotation_z_matrix(float degrees) const { + return rotation_matrix(degrees, glm::vec3(0, 0, 1)); + } + + virtual void rotate(float degrees, glm::vec3 rotation_axis) { + transform_matrix = rotation_matrix(degrees, rotation_axis); + } + + virtual void rotate_x(float degrees) { + transform_matrix = rotation_x_matrix(degrees); + } + + virtual void rotate_y(float degrees) { + transform_matrix = rotation_y_matrix(degrees); + } + + virtual void rotate_z(float degrees) { + transform_matrix = rotation_z_matrix(degrees); + } + + virtual void scale(glm::vec3 scalar_vec) { + transform_matrix = glm::scale(transform_matrix, scalar_vec); + } + + virtual void scale(float scalar) { + transform_matrix = scalar * transform_matrix; + } + }; +} \ No newline at end of file diff --git a/include/simpleengine/translatable.h b/include/simpleengine/translatable.h deleted file mode 100644 index 4f77601..0000000 --- a/include/simpleengine/translatable.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include -#include - -namespace simpleengine { - template - class Translatable { - protected: - - public: - glm::mat<4, 4, T, glm::defaultp> translation_matrix; - - virtual Translatable operator+(const Translatable rhs) = 0; - virtual Translatable operator-(const Translatable rhs) = 0; - - virtual void translate(const glm::mat<4, 4, T, glm::defaultp>& translate_vec) = 0; - virtual void translate(const Translatable& translate_vec) = 0; - virtual void translate(T x, T y, T z) = 0; - virtual void translate(const T& x, const T& y, const T& z) = 0; - - virtual glm::mat<4, 4, T, glm::defaultp> rotation_matrix(float degrees, glm::vec3 rotation_axis) const = 0; - virtual glm::mat<4, 4, T, glm::defaultp> rotation_x_matrix(float degrees) const = 0; - virtual glm::mat<4, 4, T, glm::defaultp> rotation_y_matrix(float degrees) const = 0; - virtual glm::mat<4, 4, T, glm::defaultp> rotation_z_matrix(float degrees) const = 0; - - virtual void rotate(float degrees, glm::vec3 rotation_axis) = 0; - virtual void rotate_x(float degrees) = 0; - virtual void rotate_y(float degrees) = 0; - virtual void rotate_z(float degrees) = 0; - - virtual void scale(float scalar) = 0; - }; -} \ No newline at end of file diff --git a/src/shapes/2d/triangle.cpp b/src/shapes/2d/triangle.cpp index 2a84bd0..76593e9 100644 --- a/src/shapes/2d/triangle.cpp +++ b/src/shapes/2d/triangle.cpp @@ -1,9 +1,9 @@ #include "shapes/2d/triangle.h" namespace simpleengine::shapes_2d { - Triangle::Triangle(gfx::Shader shader, std::vector vertices) : super(nullptr), - shader(shader), vertices(vertices), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), - texture(nonstd::nullopt), translation(glm::mat4(1.0f)) { + Triangle::Triangle(gfx::Shader shader, std::vector vertices) : simpleengine::Renderable(nullptr), + simpleengine::Transformable(glm::mat4(1.f)), shader(shader), vertices(vertices), + vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), texture(nonstd::nullopt) { vao.bind(); vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size()); @@ -11,17 +11,11 @@ namespace simpleengine::shapes_2d { vao.enable_attrib(vbo, 1, 3, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, color)); vao.enable_attrib(vbo, 2, 2, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, tex_coord)); - // Tell the shader that there is no texture. - shader.use(); - shader.set_uniform_int("texture_is_set", false, false); - - /* translation = glm::rotate(translation, glm::radians(90.0f), glm::vec3(0.0, 0.0, 1.0)); - translation = glm::scale(translation, glm::vec3(0.5, 0.5, 0.5)); */ - shader.set_uniform_matrix_4f("translation", translation, false); - shader.unuse(); - glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); + + // rotate_z(90.f); + // scale(.5f); } Triangle::Triangle(std::shared_ptr shader_program, std::vector vertices) : @@ -46,8 +40,14 @@ namespace simpleengine::shapes_2d { void Triangle::render(GLFWwindow* target) { shader.use(); + shader.set_uniform_matrix_4f("transform", transform_matrix, false); + + // When binding to the texture, also tell the shader if the texture is set or not. if (texture.has_value()) { + shader.set_uniform_int("texture_is_set", true, false); texture.value().bind(); + } else { + shader.set_uniform_int("texture_is_set", false, false); } vao.bind();