From 29c24ad72a0986061ab2a330210527a9b3fd534a Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sat, 2 Jan 2021 23:12:12 +0100 Subject: [PATCH] Add Transform component --- inferno/src/inferno/component/transform.cpp | 55 +++++++++++++++++++++ inferno/src/inferno/component/transform.h | 50 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 inferno/src/inferno/component/transform.cpp create mode 100644 inferno/src/inferno/component/transform.h diff --git a/inferno/src/inferno/component/transform.cpp b/inferno/src/inferno/component/transform.cpp new file mode 100644 index 0000000..4008b4c --- /dev/null +++ b/inferno/src/inferno/component/transform.cpp @@ -0,0 +1,55 @@ +#include // glm::translate, glm::rotate, glm::scale, glm::radians + +#include "inferno/component/transform.h" + +namespace Inferno { + + void Transform::update() + { + // Identity matrix + m_transform = glm::mat4(1.0f); + + // Translate + m_transform = glm::translate(m_transform, m_translate); + + // Rotate + m_transform = glm::rotate(m_transform, glm::radians(m_rotate.x), {1.0, 0.0, 0.0}); + m_transform = glm::rotate(m_transform, glm::radians(m_rotate.y), {0.0, 1.0, 0.0}); + m_transform = glm::rotate(m_transform, glm::radians(m_rotate.z), {0.0, 0.0, 1.0}); + + // Scale + m_transform = glm::scale(m_transform, m_scale); + } + +// ---------------------------------------- + + const LogStream& operator<<(const LogStream& stream, const Transform& value) + { + stream << "transform " + << value.translate() << " t\n " + << value.rotate() << " r\n " + << value.scale() << " s"; + return stream; + } + + const LogStream& operator<<(const LogStream& stream, const glm::vec3& value) + { + stream << "{ " << value.x << ", " << value.y << ", " << value.z << " }"; + return stream; + } + + const LogStream& operator<<(const LogStream& stream, const glm::vec4& value) + { + stream << "{ " << value.x << ", " << value.y << ", " << value.z << ", " << value.w << " }"; + return stream; + } + + const LogStream& operator<<(const LogStream& stream, const glm::mat4& value) + { + stream << "mat4 " + << value[0] << "\n " << value[1] << "\n " + << value[2] << "\n " << value[3]; + return stream; + } + +} diff --git a/inferno/src/inferno/component/transform.h b/inferno/src/inferno/component/transform.h new file mode 100644 index 0000000..95512dd --- /dev/null +++ b/inferno/src/inferno/component/transform.h @@ -0,0 +1,50 @@ +#ifndef TRANSFORM_H +#define TRANSFORM_H + +#include // glm::mat4 +#include // glm::vec3 + +#include "inferno/log.h" + +namespace Inferno { + + class Transform { + public: + Transform() {} + Transform(glm::vec3 translate, glm::vec3 rotate, glm::vec3 scale) + : m_translate(translate), m_rotate(rotate), m_scale(scale) {} + virtual ~Transform() { dbg(Log::Danger) << "KILLED TRANSFORM"; } + + virtual void update(); + + void setTranslate(glm::vec3 translate) { m_translate = translate; } + void setRotate(glm::vec3 rotation) { m_rotate = rotation; } + void setScale(glm::vec3 scale) { m_scale = scale; } + void setTransform(glm::mat4 transform) { m_transform = transform; } + + void setTranslate(float x, float y, float z) { m_translate = { x, y, z }; } + void setRotate(float x, float y, float z) { m_rotate = { x, y, z }; } + void setScale(float x, float y, float z) { m_scale = { x, y, z }; } + + const glm::vec3& translate() const { return m_translate; } + const glm::vec3& rotate() const { return m_rotate; } + const glm::vec3& scale() const { return m_scale; } + const glm::mat4& transform() const { return m_transform; } + + private: + glm::vec3 m_translate; + glm::vec3 m_rotate; + glm::vec3 m_scale; + glm::mat4 m_transform; + }; + +// ---------------------------------------- + + const LogStream& operator<<(const LogStream& stream, const Transform& value); + const LogStream& operator<<(const LogStream& stream, const glm::vec3& value); + const LogStream& operator<<(const LogStream& stream, const glm::vec4& value); + const LogStream& operator<<(const LogStream& stream, const glm::mat4& value); + +} + +#endif // TRANSFORM_H