Browse Source

Add Transform component

master
Riyyi 4 years ago
parent
commit
29c24ad72a
  1. 55
      inferno/src/inferno/component/transform.cpp
  2. 50
      inferno/src/inferno/component/transform.h

55
inferno/src/inferno/component/transform.cpp

@ -0,0 +1,55 @@
#include <glm/ext/matrix_transform.hpp> // 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;
}
}

50
inferno/src/inferno/component/transform.h

@ -0,0 +1,50 @@
#ifndef TRANSFORM_H
#define TRANSFORM_H
#include <glm/ext/matrix_float4x4.hpp> // glm::mat4
#include <glm/ext/vector_float3.hpp> // 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
Loading…
Cancel
Save