Create Transformable class and add it to Triangle

This commit is contained in:
SeanOMik 2021-12-02 23:07:06 -05:00
parent 779df14f67
commit 18e01bde5a
4 changed files with 117 additions and 51 deletions

View File

@ -19,18 +19,15 @@
#include "../../gfx/shader.h"
#include "../../gfx/texture.h"
#include "../../optional.h"
#include "../../translatable.h"
#include "../../transformable.h"
#include <vector>
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<gfx::Texture> texture;
glm::mat4 translation;
public:
std::vector<Vertex> vertices;
gfx::VBO vbo;

View File

@ -0,0 +1,103 @@
#pragma once
#include <glm/detail/qualifier.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <glm/fwd.hpp>
#include <glm/glm.hpp>
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;
}
};
}

View File

@ -1,34 +0,0 @@
#pragma once
#include <glm/detail/qualifier.hpp>
#include <glm/glm.hpp>
namespace simpleengine {
template<typename T = float>
class Translatable {
protected:
public:
glm::mat<4, 4, T, glm::defaultp> translation_matrix;
virtual Translatable<T> operator+(const Translatable<T> rhs) = 0;
virtual Translatable<T> operator-(const Translatable<T> rhs) = 0;
virtual void translate(const glm::mat<4, 4, T, glm::defaultp>& translate_vec) = 0;
virtual void translate(const Translatable<T>& 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;
};
}

View File

@ -1,9 +1,9 @@
#include "shapes/2d/triangle.h"
namespace simpleengine::shapes_2d {
Triangle::Triangle(gfx::Shader shader, std::vector<Vertex> 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<Vertex> 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<GLuint> shader_program, std::vector<Vertex> 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();