OpenGL-utility/src/transformable.cpp

152 lines
5.3 KiB
C++
Raw Normal View History

2021-01-22 00:13:01 +00:00
#include "transformable.hpp"
#include <algorithm>
#include <glm/gtc/matrix_transform.hpp>
2021-01-23 12:19:13 +00:00
#include <glm/gtx/matrix_decompose.hpp>
#include <glm/gtx/quaternion.hpp>
2021-01-22 00:13:01 +00:00
#include <glm/gtc/type_ptr.hpp>
namespace oglu
{
oglu::Transformable::Transformable() :
2021-01-23 12:19:13 +00:00
transformation(glm::mat4(1.0f))
2021-01-22 00:13:01 +00:00
{
2021-01-23 12:19:13 +00:00
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
2021-01-22 00:13:01 +00:00
}
Transformable::Transformable(const Transformable& other) :
2021-01-23 12:19:13 +00:00
transformation(other.transformation)
2021-01-22 00:13:01 +00:00
{
2021-01-23 12:19:13 +00:00
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
2021-01-22 00:13:01 +00:00
}
Transformable::~Transformable()
{
}
void Transformable::SetPosition(float x, float y, float z)
{
2021-01-23 12:19:13 +00:00
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
translation = glm::vec3(x, y, z) - translation;
transformation = glm::translate(transformation, translation);
2021-01-22 00:13:01 +00:00
}
void Transformable::SetPosition(const float* translation)
{
2021-01-23 12:19:13 +00:00
glm::decompose(transformation, scale, orientation, this->translation, skew, perspective);
this->translation = glm::make_vec3(translation) - this->translation;
transformation = glm::translate(transformation, this->translation);
2021-01-22 00:13:01 +00:00
}
void Transformable::SetRotation(float rotX, float rotY, float rotZ)
{
2021-01-23 12:19:13 +00:00
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
orientation = glm::quat(glm::vec3(rotX, rotY, rotZ)) - orientation;
transformation = glm::rotate(transformation, orientation.w, glm::vec3(orientation.x, orientation.y, orientation.z));
2021-01-22 00:13:01 +00:00
}
void Transformable::SetRotation(const float* rotation)
{
2021-01-23 12:19:13 +00:00
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
orientation = glm::quat(glm::make_vec3(rotation)) - orientation;
transformation = glm::rotate(transformation, orientation.w, glm::vec3(orientation.x, orientation.y, orientation.z));
2021-01-22 00:13:01 +00:00
}
void Transformable::SetRotation(float angle, float xAxis, float yAxis, float zAxis)
{
2021-01-23 12:19:13 +00:00
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
orientation = glm::quat(glm::vec4(xAxis, yAxis, zAxis, angle)) - orientation;
transformation = glm::rotate(transformation, orientation.w, glm::vec3(orientation.x, orientation.y, orientation.z));
2021-01-22 00:13:01 +00:00
}
void Transformable::SetRotation(float angle, const float* axis)
{
2021-01-23 12:19:13 +00:00
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
orientation = glm::quat(glm::vec4(axis[0], axis[1], axis[2], angle)) - orientation;
transformation = glm::rotate(transformation, orientation.w, glm::vec3(orientation.x, orientation.y, orientation.z));
2021-01-22 00:13:01 +00:00
}
void Transformable::SetScale(float scaleX, float scaleY, float scaleZ)
{
2021-01-23 12:19:13 +00:00
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
scale = glm::vec3(scaleX, scaleY, scaleZ) - scale;
transformation = glm::scale(transformation, scale);
2021-01-22 00:13:01 +00:00
}
void Transformable::SetScale(const float* scale)
{
2021-01-23 12:19:13 +00:00
glm::decompose(transformation, this->scale, orientation, translation, skew, perspective);
this->scale = glm::make_vec3(scale) - this->scale;
transformation = glm::scale(transformation, this->scale);
2021-01-22 00:13:01 +00:00
}
void Transformable::Move(float x, float y, float z)
{
2021-01-23 12:19:13 +00:00
transformation = glm::translate(transformation, glm::vec3(x, y, z));
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
2021-01-22 00:13:01 +00:00
}
void Transformable::Move(const float* translation)
{
2021-01-23 12:19:13 +00:00
transformation = glm::translate(transformation, glm::make_vec3(translation));
glm::decompose(transformation, scale, orientation, this->translation, skew, perspective);
2021-01-22 00:13:01 +00:00
}
void Transformable::Rotate(float rotX, float rotY, float rotZ)
{
2021-01-23 12:19:13 +00:00
transformation = glm::rotate(transformation, glm::radians(1.0f), glm::vec3(rotX, rotY, rotZ));
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
2021-01-22 00:13:01 +00:00
}
void Transformable::Rotate(const float* rotation)
{
2021-01-23 12:19:13 +00:00
transformation = glm::rotate(transformation, 1.0f, glm::make_vec3(rotation));
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
2021-01-22 00:13:01 +00:00
}
void Transformable::Rotate(float angle, float xAxis, float yAxis, float zAxis)
{
2021-01-23 12:19:13 +00:00
transformation = glm::rotate(transformation, angle, glm::vec3(xAxis, yAxis, zAxis));
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
2021-01-22 00:13:01 +00:00
}
void Transformable::Rotate(float angle, const float* axis)
{
2021-01-23 12:19:13 +00:00
transformation = glm::rotate(transformation, angle, glm::make_vec3(axis));
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
2021-01-22 00:13:01 +00:00
}
void Transformable::Scale(float scaleX, float scaleY, float scaleZ)
{
2021-01-23 12:19:13 +00:00
transformation = glm::scale(transformation, glm::vec3(scaleX, scaleY, scaleZ));
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
2021-01-22 00:13:01 +00:00
}
void Transformable::Scale(const float* scale)
{
2021-01-23 12:19:13 +00:00
transformation = glm::scale(transformation, glm::make_vec3(scale));
glm::decompose(transformation, this->scale, orientation, translation, skew, perspective);
2021-01-22 00:13:01 +00:00
}
const float* Transformable::GetMatrix()
{
2021-01-23 12:19:13 +00:00
return glm::value_ptr(transformation);
2021-01-22 00:13:01 +00:00
}
2021-01-22 17:14:48 +00:00
2021-01-22 18:46:09 +00:00
const float* Transformable::GetPosition() const
2021-01-22 17:14:48 +00:00
{
2021-01-23 12:19:13 +00:00
return glm::value_ptr(translation);
2021-01-22 17:14:48 +00:00
}
2021-01-22 18:46:09 +00:00
const float* Transformable::GetRotation() const
2021-01-22 17:14:48 +00:00
{
2021-01-23 12:19:13 +00:00
return glm::value_ptr(orientation);
2021-01-22 17:14:48 +00:00
}
2021-01-22 18:46:09 +00:00
const float* Transformable::GetScaling() const
2021-01-22 17:14:48 +00:00
{
2021-01-23 12:19:13 +00:00
return glm::value_ptr(scale);
2021-01-22 17:14:48 +00:00
}
2021-01-22 00:13:01 +00:00
}