2020-01-07 13:10:23 +01:00
|
|
|
//
|
|
|
|
// Created by karl on 04.01.20.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef ECSGAME_TRANSFORM_H
|
|
|
|
#define ECSGAME_TRANSFORM_H
|
|
|
|
|
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
|
|
|
2020-09-29 17:56:07 +02:00
|
|
|
#include "glm/gtx/string_cast.hpp"
|
|
|
|
|
2020-01-07 13:10:23 +01:00
|
|
|
struct Transform {
|
|
|
|
Transform() = default;
|
|
|
|
|
|
|
|
glm::mat4 matrix = glm::mat4(1.0f); // Initialize as identity
|
2020-09-29 21:04:25 +02:00
|
|
|
glm::vec3 origin = glm::vec3(0.0f, 0.0f, 0.0f);
|
2020-01-07 13:10:23 +01:00
|
|
|
|
2021-01-02 15:20:33 +01:00
|
|
|
void translate(glm::vec3 offset) { matrix = glm::translate(matrix, offset); }
|
2020-01-07 13:10:23 +01:00
|
|
|
|
2021-01-02 15:20:33 +01:00
|
|
|
glm::vec3 get_translation() { return glm::vec3(matrix[3]); }
|
2020-09-29 21:04:25 +02:00
|
|
|
|
2021-01-02 15:20:33 +01:00
|
|
|
void uniform_scale(float factor) { scale(glm::vec3(factor, factor, factor)); }
|
2020-01-07 13:10:23 +01:00
|
|
|
|
2021-01-02 15:20:33 +01:00
|
|
|
void scale(glm::vec3 factors) { matrix = glm::scale(matrix, factors); }
|
2020-01-07 13:10:23 +01:00
|
|
|
|
2020-01-07 13:50:47 +01:00
|
|
|
void rotate(float degrees, glm::vec3 axis) {
|
|
|
|
matrix = glm::rotate(matrix, glm::radians(degrees), axis);
|
|
|
|
}
|
|
|
|
|
2021-01-02 15:20:33 +01:00
|
|
|
void set_origin(glm::vec3 position) { origin = position; }
|
2020-09-28 16:52:35 +02:00
|
|
|
|
2021-01-02 15:20:33 +01:00
|
|
|
void add_to_origin(glm::vec3 addition) { origin += addition; }
|
2020-10-02 22:47:10 +02:00
|
|
|
|
2020-09-29 17:56:07 +02:00
|
|
|
void set_rotation_from_quat(glm::quat quaternion) {
|
|
|
|
// Remember translation
|
|
|
|
glm::vec4 save = matrix[3];
|
|
|
|
|
|
|
|
matrix = glm::mat4_cast(quaternion);
|
|
|
|
matrix[3] = save;
|
|
|
|
}
|
|
|
|
|
2021-01-02 15:20:33 +01:00
|
|
|
glm::vec3 get_origin() const { return origin; }
|
2020-01-07 20:02:40 +01:00
|
|
|
|
2021-01-02 15:20:33 +01:00
|
|
|
glm::vec3 forward() const { return matrix * glm::vec4(0.0, 0.0, -1.0, 0.0); }
|
2020-01-07 20:02:40 +01:00
|
|
|
|
2021-01-02 15:20:33 +01:00
|
|
|
glm::vec3 up() const { return matrix * glm::vec4(0.0, 1.0, 0.0, 0.0); }
|
2020-01-07 20:02:40 +01:00
|
|
|
|
2021-01-02 15:20:33 +01:00
|
|
|
glm::vec3 right() const { return matrix * glm::vec4(1.0, 0.0, 0.0, 0.0); }
|
2020-01-07 13:10:23 +01:00
|
|
|
};
|
|
|
|
|
2021-01-02 15:20:33 +01:00
|
|
|
#endif // ECSGAME_TRANSFORM_H
|