Reworked transformable

This commit is contained in:
Robert 2021-01-23 16:23:49 +01:00
parent 3e6884356f
commit a17c73d3c9
7 changed files with 177 additions and 68 deletions

View file

@ -28,7 +28,7 @@ add_library(openglu SHARED
${include_files} ${include_files}
${source_files} ${source_files}
"cpp.hint" "cpp.hint"
"src/transformable.cpp") )
target_compile_definitions(openglu PRIVATE OGLU_BUILD_DLL) target_compile_definitions(openglu PRIVATE OGLU_BUILD_DLL)
@ -48,7 +48,6 @@ if(${build_documentation})
COMMAND ${DOXYGEN_EXECUTABLE} "${CMAKE_BINARY_DIR}/doxyfile" COMMAND ${DOXYGEN_EXECUTABLE} "${CMAKE_BINARY_DIR}/doxyfile"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Building documentation..." COMMENT "Building documentation..."
VERBATIM
) )
endif() endif()

View file

@ -1,4 +1,4 @@
add_executable(debug "main.cpp") add_executable(debug "main.cpp" "shaders/fragmentShader.frag" "shaders/vertexShader.vert")
find_package(glfw3 REQUIRED) find_package(glfw3 REQUIRED)

View file

@ -86,13 +86,11 @@ int main(int argc, char** argv)
oglu::Texture crate = oglu::MakeTexture("assets/crate.jpg"); oglu::Texture crate = oglu::MakeTexture("assets/crate.jpg");
oglu::Texture opengl = oglu::MakeTexture("assets/opengl.png"); oglu::Texture opengl = oglu::MakeTexture("assets/opengl.png");
glm::mat4 view = glm::mat4(1.0f); oglu::Camera camera;
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -5.0f)); camera.Move(0.0f, 0.0f, -5.0f);
glm::mat4 projection;
projection = glm::perspective(glm::radians(45.f), 1.0f, 0.1f, 100.0f);
// Window loop // Window loop
float t = 0.0f;
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
processInput(window); processInput(window);
@ -101,15 +99,14 @@ int main(int argc, char** argv)
square.Rotate(6.0f, 0.0f, 0.0f); square.Rotate(6.0f, 0.0f, 0.0f);
square2.Rotate(-6.0f, 0.0f, 0.0f); square2.Rotate(-6.0f, 0.0f, 0.0f);
camera.Rotate(0.0f, 1.0f, 0.0f);
view = glm::rotate(view, glm::radians(1.0f), glm::vec3(0.0f, 1.0f, 0.0f));
shader->Use(); shader->Use();
shader->SetUniform("texture1", crate, 0); shader->SetUniform("texture1", crate, 0);
shader->SetUniform("texture2", opengl, 1); shader->SetUniform("texture2", opengl, 1);
shader->SetUniform("model", square); shader->SetUniform("model", square);
shader->SetUniformMatrix4fv("view", 1, GL_FALSE, glm::value_ptr(view)); shader->SetUniform("view", camera);
shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(projection)); shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, camera.GetProjectionMatrix());
square.Render(); square.Render();
@ -118,6 +115,8 @@ int main(int argc, char** argv)
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
t += 0.1f;
} }
glfwTerminate(); glfwTerminate();

View file

@ -88,6 +88,8 @@ namespace oglu
*/ */
void LookAt(const Transformable& target); void LookAt(const Transformable& target);
// void Move(float x, float y, float z) override;
/** /**
* @brief Gets the projection matrix of the camera. * @brief Gets the projection matrix of the camera.
* *

View file

@ -50,7 +50,7 @@ namespace oglu
* @param[in] y New y position * @param[in] y New y position
* @param[in] z New z position * @param[in] z New z position
*/ */
void SetPosition(float x, float y, float z); virtual void SetPosition(float x, float y, float z);
/** /**
* @brief Sets the position. * @brief Sets the position.
@ -60,7 +60,17 @@ 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* position); virtual void SetPosition(const float* position);
/**
* @brief Sets the position.
*
* This sets an absolute position, means that it resets any previous
* translations.
*
* @param[in] position A 3D position vector
*/
virtual void SetPosition(const glm::vec3& position);
/** /**
* @brief Sets the rotation. * @brief Sets the rotation.
@ -72,7 +82,7 @@ namespace oglu
* @param[in] rotY New rotation around y axis * @param[in] rotY New rotation around y axis
* @param[in] rotZ New rotation around z axis * @param[in] rotZ New rotation around z axis
*/ */
void SetRotation(float rotX, float rotY, float rotZ); virtual void SetRotation(float rotX, float rotY, float rotZ);
/** /**
* @brief Sets the rotation. * @brief Sets the rotation.
@ -82,7 +92,17 @@ namespace oglu
* *
* @param[in] rotation An array of floats containing three scalars * @param[in] rotation An array of floats containing three scalars
*/ */
void SetRotation(const float* rotation); virtual void SetRotation(const float* rotation);
/**
* @brief Sets the rotation.
*
* This sets an absolute rotation, means that it resets any previous
* rotations.
*
* @param[in] rotation A glm::vec3 containing Euler angles
*/
virtual void SetRotation(const glm::vec3& rotation);
/** /**
* @brief Sets the rotation. * @brief Sets the rotation.
@ -95,7 +115,7 @@ namespace oglu
* @param[in] yAxis The y component of the rotation axis * @param[in] yAxis The y component of the rotation axis
* @param[in] zAxis The z component of the rotation axis * @param[in] zAxis The z component of the rotation axis
*/ */
void SetRotation(float angle, float xAxis, float yAxis, float zAxis); virtual void SetRotation(float angle, float xAxis, float yAxis, float zAxis);
/** /**
* @brief Sets the rotation. * @brief Sets the rotation.
@ -106,7 +126,18 @@ namespace oglu
* @param[in] angle The angle to rotate by * @param[in] angle The angle to rotate by
* @param[in] axis An array of floats containing the three rotation axis components * @param[in] axis An array of floats containing the three rotation axis components
*/ */
void SetRotation(float angle, const float* axis); virtual void SetRotation(float angle, const float* axis);
/**
* @brief Sets the rotation.
*
* This sets an absolute rotation, means that it resets any previous
* rotations.
*
* @param[in] angle The angle to rotate by
* @param[in] axis The axis to rotate around
*/
virtual void SetRotation(float angle, const glm::vec3& axis);
/** /**
* @brief Sets the scaling. * @brief Sets the scaling.
@ -118,7 +149,7 @@ namespace oglu
* @param[in] scaleY The scaling in y direction * @param[in] scaleY The scaling in y direction
* @param[in] scaleZ The scaling in z direction * @param[in] scaleZ The scaling in z direction
*/ */
void SetScale(float scaleX, float scaleY, float scaleZ); virtual void SetScale(float scaleX, float scaleY, float scaleZ);
/** /**
* @brief Sets the scaling. * @brief Sets the scaling.
@ -128,7 +159,17 @@ namespace oglu
* *
* @param[in] scale An array of floats containing three scalars * @param[in] scale An array of floats containing three scalars
*/ */
void SetScale(const float* scale); virtual void SetScale(const float* scale);
/**
* @brief Sets the scaling.
*
* This sets an absolute scaling, means that it resets any previous
* scaling.
*
* @param[in] scale A 3D scaling vector
*/
virtual void SetScale(const glm::vec3& scale);
/** /**
* @brief Performs a translation. * @brief Performs a translation.
@ -140,7 +181,7 @@ namespace oglu
* @param[in] y Offset along the y axis * @param[in] y Offset along the y axis
* @param[in] z Offset along the z axis * @param[in] z Offset along the z axis
*/ */
void Move(float x, float y, float z); virtual void Move(float x, float y, float z);
/** /**
* @brief Performs a translation. * @brief Performs a translation.
@ -150,7 +191,17 @@ namespace oglu
* *
* @param[in] position An array of floats containing the offset values * @param[in] position An array of floats containing the offset values
*/ */
void Move(const float* translation); virtual void Move(const float* translation);
/**
* @brief Performs a translation.
*
* This function applies a translation to the object, it operates
* operates on the current position.
*
* @param[in] position A 3D displacement vector
*/
virtual void Move(const glm::vec3& translation);
/** /**
* @brief Performs a rotation. * @brief Performs a rotation.
@ -162,7 +213,7 @@ namespace oglu
* @param[in] rotY Rotation around the y axis * @param[in] rotY Rotation around the y axis
* @param[in] rotZ Rotation around the z axis * @param[in] rotZ Rotation around the z axis
*/ */
void Rotate(float rotX, float rotY, float rotZ); virtual void Rotate(float rotX, float rotY, float rotZ);
/** /**
* @brief Performs a rotation. * @brief Performs a rotation.
@ -172,7 +223,17 @@ namespace oglu
* *
* @param[in] rotation An array of floats containing the rotation values * @param[in] rotation An array of floats containing the rotation values
*/ */
void Rotate(const float* rotation); virtual void Rotate(const float* rotation);
/**
* @brief Performs a rotation.
*
* This function applies a rotation to the object, it operates
* operates on the current rotation.
*
* @param[in] rotation An 3D vector containing Euler angles
*/
virtual void Rotate(const glm::vec3& rotation);
/** /**
* @brief Performs a rotation. * @brief Performs a rotation.
@ -185,7 +246,7 @@ namespace oglu
* @param[in] yAxis y component of the rotation axis * @param[in] yAxis y component of the rotation axis
* @param[in] zAxis z component of the rotation axis * @param[in] zAxis z component of the rotation axis
*/ */
void Rotate(float angle, float xAxis, float yAxis, float zAxis); virtual void Rotate(float angle, float xAxis, float yAxis, float zAxis);
/** /**
* @brief Performs a rotation. * @brief Performs a rotation.
@ -194,9 +255,20 @@ namespace oglu
* operates on the current rotation. * operates on the current rotation.
* *
* @param[in] angle The angle to rotate by * @param[in] angle The angle to rotate by
* @param[in] An array of floats containing the components of the rotation axis * @param[in] axis An array of floats containing the components of the rotation axis
*/ */
void Rotate(float angle, const float* axis); virtual void Rotate(float angle, const float* axis);
/**
* @brief Performs a rotation.
*
* This function applies a rotation to the object, it operates
* operates on the current rotation.
*
* @param[in] angle The angle to rotate by
* @param[in] axis The axis to rotate around
*/
virtual void Rotate(float angle, const glm::vec3& axis);
/** /**
* @brief Performs scaling. * @brief Performs scaling.
@ -208,7 +280,7 @@ namespace oglu
* @param[in] scaleX Scaling in y direction * @param[in] scaleX Scaling in y direction
* @param[in] scaleX Scaling in z direction * @param[in] scaleX Scaling in z direction
*/ */
void Scale(float scaleX, float scaleY, float scaleZ); virtual void Scale(float scaleX, float scaleY, float scaleZ);
/** /**
* @brief Performs scaling. * @brief Performs scaling.
@ -218,20 +290,21 @@ namespace oglu
* *
* @param[in] scale An array of floats containing three scaling values * @param[in] scale An array of floats containing three scaling values
*/ */
void Scale(const float* scale); virtual void Scale(const float* scale);
/**
* @brief Performs scaling.
*
* This function applies scaling to the object, it operates
* operates on the current scaling.
*
* @param[in] scale A 3D scaling vector
*/
virtual void Scale(const glm::vec3& scale);
/** /**
* @brief Returns a transformation matrix. * @brief Returns a transformation matrix.
* *
* Internally, this function multiplies the translation, rotation,
* and scaling matrices into one transformation matrix which is
* then returned.
*
* This multiplication is only performed when a change occured to one
* of those matrices (so when there was a translation, rotation or scaling).
* So it is okay to call this function multiple times without huge performance
* loss, as long as no transformations occur inbetween.
*
* @return An array of 16 floats representing the transformation matrix * @return An array of 16 floats representing the transformation matrix
*/ */
const float* GetMatrix(); const float* GetMatrix();

View file

@ -13,6 +13,10 @@ namespace oglu
Camera::Camera() : Camera::Camera() :
fov(45.0f), aspectRatio(0.0f), zNear(0.1f), zFar(100.0f), projection(new float[16]{ 0.0f }) fov(45.0f), aspectRatio(0.0f), zNear(0.1f), zFar(100.0f), projection(new float[16]{ 0.0f })
{ {
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
aspectRatio = (float)viewport[2] / (float)viewport[3];
memcpy( memcpy(
projection, projection,
glm::value_ptr(glm::perspective(glm::radians(fov), aspectRatio, zNear, zFar)), glm::value_ptr(glm::perspective(glm::radians(fov), aspectRatio, zNear, zFar)),

View file

@ -27,105 +27,137 @@ namespace oglu
void Transformable::SetPosition(float x, float y, float z) void Transformable::SetPosition(float x, float y, float z)
{ {
glm::decompose(transformation, scale, orientation, translation, skew, perspective); SetPosition(glm::vec3(x, y, z));
translation = glm::vec3(x, y, z) - translation;
transformation = glm::translate(transformation, translation);
} }
void Transformable::SetPosition(const float* translation) void Transformable::SetPosition(const float* translation)
{
SetPosition(glm::make_vec3(translation));
}
void Transformable::SetPosition(const glm::vec3& position)
{ {
glm::decompose(transformation, scale, orientation, this->translation, skew, perspective); glm::decompose(transformation, scale, orientation, this->translation, skew, perspective);
this->translation = glm::make_vec3(translation) - this->translation; this->translation = translation - this->translation;
transformation = glm::translate(transformation, this->translation); transformation = glm::translate(transformation, this->translation);
} }
void Transformable::SetRotation(float rotX, float rotY, float rotZ) void Transformable::SetRotation(float rotX, float rotY, float rotZ)
{ {
glm::decompose(transformation, scale, orientation, translation, skew, perspective); SetRotation(glm::vec3(rotX, rotY, rotZ));
orientation = glm::quat(glm::vec3(rotX, rotY, rotZ)) - orientation;
transformation = glm::rotate(transformation, orientation.w, glm::vec3(orientation.x, orientation.y, orientation.z));
} }
void Transformable::SetRotation(const float* rotation) void Transformable::SetRotation(const float* rotation)
{
SetRotation(glm::make_vec3(rotation));
}
void Transformable::SetRotation(const glm::vec3& rotation)
{ {
glm::decompose(transformation, scale, orientation, translation, skew, perspective); glm::decompose(transformation, scale, orientation, translation, skew, perspective);
orientation = glm::quat(glm::make_vec3(rotation)) - orientation; orientation = glm::quat(glm::radians(rotation)) * (-orientation);
transformation = glm::rotate(transformation, orientation.w, glm::vec3(orientation.x, orientation.y, orientation.z)); transformation = glm::rotate(transformation, glm::angle(orientation), glm::axis(orientation));
} }
void Transformable::SetRotation(float angle, float xAxis, float yAxis, float zAxis) void Transformable::SetRotation(float angle, float xAxis, float yAxis, float zAxis)
{ {
glm::decompose(transformation, scale, orientation, translation, skew, perspective); SetRotation(angle, glm::vec3(xAxis, yAxis, zAxis));
orientation = glm::quat(glm::vec4(xAxis, yAxis, zAxis, angle)) - orientation;
transformation = glm::rotate(transformation, orientation.w, glm::vec3(orientation.x, orientation.y, orientation.z));
} }
void Transformable::SetRotation(float angle, const float* axis) void Transformable::SetRotation(float angle, const float* axis)
{
SetRotation(angle, glm::make_vec3(axis));
}
void Transformable::SetRotation(float angle, const glm::vec3& axis)
{ {
glm::decompose(transformation, scale, orientation, translation, skew, perspective); glm::decompose(transformation, scale, orientation, translation, skew, perspective);
orientation = glm::quat(glm::vec4(axis[0], axis[1], axis[2], angle)) - orientation; orientation = glm::angleAxis(glm::radians(angle), axis) * (-orientation);
transformation = glm::rotate(transformation, orientation.w, glm::vec3(orientation.x, orientation.y, orientation.z)); transformation = glm::rotate(transformation, glm::angle(orientation), glm::axis(orientation));
} }
void Transformable::SetScale(float scaleX, float scaleY, float scaleZ) void Transformable::SetScale(float scaleX, float scaleY, float scaleZ)
{ {
glm::decompose(transformation, scale, orientation, translation, skew, perspective); SetScale(glm::vec3(scaleX, scaleY, scaleZ));
scale = glm::vec3(scaleX, scaleY, scaleZ) - scale;
transformation = glm::scale(transformation, scale);
} }
void Transformable::SetScale(const float* scale) void Transformable::SetScale(const float* scale)
{
SetScale(glm::make_vec3(scale));
}
void Transformable::SetScale(const glm::vec3& scale)
{ {
glm::decompose(transformation, this->scale, orientation, translation, skew, perspective); glm::decompose(transformation, this->scale, orientation, translation, skew, perspective);
this->scale = glm::make_vec3(scale) - this->scale; this->scale = scale / this->scale;
if (this->scale.x == INFINITY) this->scale.x = scale.x;
if (this->scale.y == INFINITY) this->scale.y = scale.y;
if (this->scale.z == INFINITY) this->scale.z = scale.z;
transformation = glm::scale(transformation, this->scale); transformation = glm::scale(transformation, this->scale);
} }
void Transformable::Move(float x, float y, float z) void Transformable::Move(float x, float y, float z)
{ {
transformation = glm::translate(transformation, glm::vec3(x, y, z)); Move(glm::vec3(x, y, z));
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
} }
void Transformable::Move(const float* translation) void Transformable::Move(const float* translation)
{ {
transformation = glm::translate(transformation, glm::make_vec3(translation)); Move(glm::make_vec3(translation));
}
void Transformable::Move(const glm::vec3& translation)
{
transformation = glm::translate(transformation, translation);
glm::decompose(transformation, scale, orientation, this->translation, skew, perspective); glm::decompose(transformation, scale, orientation, this->translation, skew, perspective);
} }
void Transformable::Rotate(float rotX, float rotY, float rotZ) void Transformable::Rotate(float rotX, float rotY, float rotZ)
{ {
transformation = glm::rotate(transformation, glm::radians(1.0f), glm::vec3(rotX, rotY, rotZ)); Rotate(glm::vec3(rotX, rotY, rotZ));
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
} }
void Transformable::Rotate(const float* rotation) void Transformable::Rotate(const float* rotation)
{ {
transformation = glm::rotate(transformation, 1.0f, glm::make_vec3(rotation)); Rotate(glm::make_vec3(rotation));
}
void Transformable::Rotate(const glm::vec3& rotation)
{
glm::quat rot = glm::quat(glm::radians(rotation));
transformation = glm::rotate(transformation, glm::angle(rot), glm::axis(rot));
glm::decompose(transformation, scale, orientation, translation, skew, perspective); glm::decompose(transformation, scale, orientation, translation, skew, perspective);
} }
void Transformable::Rotate(float angle, float xAxis, float yAxis, float zAxis) void Transformable::Rotate(float angle, float xAxis, float yAxis, float zAxis)
{ {
transformation = glm::rotate(transformation, angle, glm::vec3(xAxis, yAxis, zAxis)); Rotate(angle, glm::vec3(xAxis, yAxis, zAxis));
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
} }
void Transformable::Rotate(float angle, const float* axis) void Transformable::Rotate(float angle, const float* axis)
{ {
transformation = glm::rotate(transformation, angle, glm::make_vec3(axis)); Rotate(angle, glm::make_vec3(axis));
}
void Transformable::Rotate(float angle, const glm::vec3& axis)
{
transformation = glm::rotate(transformation, glm::radians(angle), axis);
glm::decompose(transformation, scale, orientation, translation, skew, perspective); glm::decompose(transformation, scale, orientation, translation, skew, perspective);
} }
void Transformable::Scale(float scaleX, float scaleY, float scaleZ) void Transformable::Scale(float scaleX, float scaleY, float scaleZ)
{ {
transformation = glm::scale(transformation, glm::vec3(scaleX, scaleY, scaleZ)); Scale(glm::vec3(scaleX, scaleY, scaleZ));
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
} }
void Transformable::Scale(const float* scale) void Transformable::Scale(const float* scale)
{ {
transformation = glm::scale(transformation, glm::make_vec3(scale)); Scale(glm::make_vec3(scale));
}
void Transformable::Scale(const glm::vec3& scale)
{
transformation = glm::scale(transformation, scale);
glm::decompose(transformation, this->scale, orientation, translation, skew, perspective); glm::decompose(transformation, this->scale, orientation, translation, skew, perspective);
} }