Reworked transformable

This commit is contained in:
Robert 2021-01-22 18:14:48 +01:00
parent fb9c8473fa
commit 6c55ba6339
2 changed files with 70 additions and 56 deletions

View file

@ -58,7 +58,7 @@ namespace oglu
* *
* @param[in] position An array of floats containing three scalars * @param[in] position An array of floats containing three scalars
*/ */
void SetPosition(const float* translation); void SetPosition(const float* position);
/** /**
* @brief Sets the rotation. * @brief Sets the rotation.
@ -234,12 +234,33 @@ namespace oglu
*/ */
const float* GetMatrix(); const float* GetMatrix();
/**
* @brief Get position as a 3D vector.
*
* @returns A pointer to an array of floats
*/
const float* GetPosition();
/**
* @brief Get rotation as a matrix.
*
* @returns A pointer to a 4x4 matrix
*/
const float* GetRotation();
/**
* @brief Get scaling as a 3D vector.
*
* @returns A pointer to an array of floats
*/
const float* GetScaling();
private: private:
// TODO: Separate translation, rotation and scaling matrices. // TODO: Separate translation, rotation and scaling matrices.
// Combine them only when the user wants the transformation matrix // Combine them only when the user wants the transformation matrix
float* translation; ///< Translation matrix float* position; ///< Position vector
float* rotation; ///< Rotation matrix float* rotation; ///< Rotation matrix
float* scaling; ///< Scaling matrix float* scaling; ///< Scaling vector
bool calculateMatrix; ///< Wether GetMatrix() needs to re-calculate the transformation matrix bool calculateMatrix; ///< Wether GetMatrix() needs to re-calculate the transformation matrix
}; };

View file

@ -9,33 +9,23 @@
namespace oglu namespace oglu
{ {
oglu::Transformable::Transformable() : oglu::Transformable::Transformable() :
translation(new float[16]), rotation(new float[16]), scaling(new float[16]), calculateMatrix(false) position(new float[3]{ 0.f }), rotation(new float[16]), scaling(new float[3]{ 1.f, 1.f, 1.f }), calculateMatrix(false)
{ {
glm::mat4 identity(1.0f); glm::mat4 identity(1.0f);
memcpy(
translation,
glm::value_ptr(identity),
16 * sizeof(float)
);
memcpy( memcpy(
rotation, rotation,
glm::value_ptr(identity), glm::value_ptr(identity),
16 * sizeof(float) 16 * sizeof(float)
); );
memcpy(
scaling,
glm::value_ptr(identity),
16 * sizeof(float)
);
} }
Transformable::Transformable(const Transformable& other) : Transformable::Transformable(const Transformable& other) :
translation(new float[16]), rotation(new float[16]), scaling(new float[16]), calculateMatrix(true) position(new float[3]), rotation(new float[16]), scaling(new float[3]), calculateMatrix(true)
{ {
memcpy( memcpy(
this->translation, this->position,
other.translation, other.position,
16 * sizeof(float) 3 * sizeof(float)
); );
memcpy( memcpy(
@ -47,7 +37,7 @@ namespace oglu
memcpy( memcpy(
this->scaling, this->scaling,
other.scaling, other.scaling,
16 * sizeof(float) 3 * sizeof(float)
); );
} }
@ -55,25 +45,23 @@ namespace oglu
{ {
delete[] scaling; delete[] scaling;
delete[] rotation; delete[] rotation;
delete[] translation; delete[] position;
} }
void Transformable::SetPosition(float x, float y, float z) void Transformable::SetPosition(float x, float y, float z)
{ {
memcpy( this->position[0] = x;
this->translation, this->position[1] = y;
glm::value_ptr(glm::translate(glm::mat4(1.0f), glm::vec3(x, y, z))), this->position[2] = z;
16 * sizeof(float)
);
calculateMatrix = true; calculateMatrix = true;
} }
void Transformable::SetPosition(const float* translation) void Transformable::SetPosition(const float* translation)
{ {
memcpy( memcpy(
this->translation, this->position,
glm::value_ptr(glm::translate(glm::mat4(1.0f), glm::make_vec3(translation))), translation,
16 * sizeof(float) 3 * sizeof(float)
); );
calculateMatrix = true; calculateMatrix = true;
} }
@ -138,11 +126,9 @@ namespace oglu
void Transformable::SetScale(float scaleX, float scaleY, float scaleZ) void Transformable::SetScale(float scaleX, float scaleY, float scaleZ)
{ {
memcpy( this->scaling[0] = scaleX;
this->scaling, this->scaling[1] = scaleY;
glm::value_ptr(glm::scale(glm::mat4(1.0f), glm::vec3(scaleX, scaleY, scaleZ))), this->scaling[2] = scaleZ;
16 * sizeof(float)
);
calculateMatrix = true; calculateMatrix = true;
} }
@ -150,29 +136,25 @@ namespace oglu
{ {
memcpy( memcpy(
this->scaling, this->scaling,
glm::value_ptr(glm::scale(glm::mat4(1.0f), glm::make_vec3(scale))), scale,
16 * sizeof(float) 3 * sizeof(float)
); );
calculateMatrix = true; calculateMatrix = true;
} }
void Transformable::Move(float x, float y, float z) void Transformable::Move(float x, float y, float z)
{ {
memcpy( this->position[0] += x;
this->translation, this->position[1] += y;
glm::value_ptr(glm::translate(glm::make_mat4(this->translation), glm::vec3(x, y, z))), this->position[2] += z;
16 * sizeof(float)
);
calculateMatrix = true; calculateMatrix = true;
} }
void Transformable::Move(const float* translation) void Transformable::Move(const float* translation)
{ {
memcpy( this->position[0] += translation[0];
this->translation, this->position[1] += translation[1];
glm::value_ptr(glm::translate(glm::make_mat4(this->translation), glm::make_vec3(translation))), this->position[2] += translation[2];
16 * sizeof(float)
);
calculateMatrix = true; calculateMatrix = true;
} }
@ -234,21 +216,17 @@ namespace oglu
void Transformable::Scale(float scaleX, float scaleY, float scaleZ) void Transformable::Scale(float scaleX, float scaleY, float scaleZ)
{ {
memcpy( this->scaling[0] += scaleX;
this->scaling, this->scaling[1] += scaleY;
glm::value_ptr(glm::scale(glm::make_mat4(this->scaling), glm::vec3(scaleX, scaleY, scaleZ))), this->scaling[2] += scaleZ;
16 * sizeof(float)
);
calculateMatrix = true; calculateMatrix = true;
} }
void Transformable::Scale(const float* scale) void Transformable::Scale(const float* scale)
{ {
memcpy( this->scaling[0] += scale[0];
this->scaling, this->scaling[1] += scale[1];
glm::value_ptr(glm::translate(glm::make_mat4(this->scaling), glm::make_vec3(scale))), this->scaling[2] += scale[2];
16 * sizeof(float)
);
calculateMatrix = true; calculateMatrix = true;
} }
@ -258,10 +236,25 @@ namespace oglu
if (calculateMatrix) if (calculateMatrix)
{ {
transformation = glm::make_mat4(translation) * glm::make_mat4(rotation) * glm::make_mat4(scaling); transformation = glm::translate(glm::mat4(1.0f), glm::make_vec3(position)) * glm::make_mat4(rotation) * glm::scale(glm::mat4(1.0f), glm::make_vec3(scaling));
calculateMatrix = false; calculateMatrix = false;
} }
return glm::value_ptr(transformation); return glm::value_ptr(transformation);
} }
const float* Transformable::GetPosition()
{
return position;
}
const float* Transformable::GetRotation()
{
return rotation;
}
const float* Transformable::GetScaling()
{
return scaling;
}
} }