Start working on translations

This commit is contained in:
SeanOMik 2021-11-30 18:30:19 -05:00
parent d89715a123
commit e48f52da75
5 changed files with 52 additions and 5 deletions

View File

@ -1,6 +1,7 @@
#version 440
in vec3 vs_position;
in mat4 vs_transform;
in vec3 vs_color;
in vec2 vs_texcoord;

View File

@ -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);
}

View File

@ -25,6 +25,7 @@ namespace simpleengine::shapes_2d {
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,34 @@
#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

@ -2,7 +2,8 @@
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) {
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<GLFWwindow> target) {