From e48f52da75cfce1808229c3c6f3ad42d99f6bc4c Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Tue, 30 Nov 2021 18:30:19 -0500 Subject: [PATCH] Start working on translations --- .../resources/shaders/fragment_core.glsl | 1 + .../resources/shaders/vertex_core.glsl | 6 +++- include/simpleengine/shapes/2d/triangle.h | 1 + include/simpleengine/translatable.h | 34 +++++++++++++++++++ src/shapes/2d/triangle.cpp | 15 +++++--- 5 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 include/simpleengine/translatable.h diff --git a/examples/dev_testing/resources/shaders/fragment_core.glsl b/examples/dev_testing/resources/shaders/fragment_core.glsl index 5880045..b1ff237 100644 --- a/examples/dev_testing/resources/shaders/fragment_core.glsl +++ b/examples/dev_testing/resources/shaders/fragment_core.glsl @@ -1,6 +1,7 @@ #version 440 in vec3 vs_position; +in mat4 vs_transform; in vec3 vs_color; in vec2 vs_texcoord; diff --git a/examples/dev_testing/resources/shaders/vertex_core.glsl b/examples/dev_testing/resources/shaders/vertex_core.glsl index 485cfda..8babb47 100644 --- a/examples/dev_testing/resources/shaders/vertex_core.glsl +++ b/examples/dev_testing/resources/shaders/vertex_core.glsl @@ -7,11 +7,15 @@ layout (location = 2) in vec2 vertex_texcoord; out vec3 vs_position; out vec3 vs_color; out vec2 vs_texcoord; +out mat4 vs_transform; + +uniform mat4 transform; void main() { vs_position = vertex_position; + vs_transform = transform; vs_color = vertex_color; vs_texcoord = vertex_texcoord; - gl_Position = vec4(vertex_position, 1.f); + gl_Position = transform * vec4(vertex_position, 1.0f); } \ No newline at end of file diff --git a/include/simpleengine/shapes/2d/triangle.h b/include/simpleengine/shapes/2d/triangle.h index d34811a..4ae6a9d 100644 --- a/include/simpleengine/shapes/2d/triangle.h +++ b/include/simpleengine/shapes/2d/triangle.h @@ -25,6 +25,7 @@ namespace simpleengine::shapes_2d { 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/translatable.h b/include/simpleengine/translatable.h new file mode 100644 index 0000000..4f77601 --- /dev/null +++ b/include/simpleengine/translatable.h @@ -0,0 +1,34 @@ +#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 81bb401..2e81ce6 100644 --- a/src/shapes/2d/triangle.cpp +++ b/src/shapes/2d/triangle.cpp @@ -2,7 +2,8 @@ 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) { + shader(shader), vertices(vertices), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), + texture(nonstd::nullopt), translation(glm::mat4(1.0f)) { vao.bind(); vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size()); @@ -11,7 +12,13 @@ namespace simpleengine::shapes_2d { vao.enable_attrib(vbo, 2, 2, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, tex_coord)); // Tell the shader that there is no texture. - shader.set_uniform_int("texture_is_set", false); + 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); @@ -30,10 +37,10 @@ namespace simpleengine::shapes_2d { } void Triangle::update(const float& delta_time) { - for (Vertex& vertex : vertices) { + /* for (Vertex& vertex : vertices) { vertex.position.translate_x(0.01f); vertex.position.translate_y(0.01f); - } + } */ } void Triangle::render(std::shared_ptr target) {