Skip to content

Commit

Permalink
Add Rotation3D struct
Browse files Browse the repository at this point in the history
A simple class that represents 3D rotations in space
and allows to apply rotations on an 3d matrix.
  • Loading branch information
Notiooo committed Nov 30, 2023
1 parent 1143953 commit 740bdef
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 26 deletions.
1 change: 1 addition & 0 deletions AimGL/src/CMakeLists_Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ set(PROJECT_SOURCES
Renderer/Graphics/3D/Sprite3D.cpp
Renderer/Graphics/3D/Core/Mesh.cpp
Renderer/Graphics/3D/Model.cpp
Renderer/Graphics/3D/Utils/Rotation3D.cpp
Renderer/Renderer.cpp
Resources/ObjLoader.cpp
States/State.cpp
Expand Down
10 changes: 4 additions & 6 deletions AimGL/src/Renderer/Graphics/3D/Sprite3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Sprite3D::Sprite3D(const Texture& texture)
, mPosition(0.0f)
, mScale(1.0f)
, mDimensionsNormalized()
, mRotation(0.f, {0.f, 0.f, 1.f})
{
auto max = static_cast<float>(std::max(mTexture.width(), mTexture.height()));
mDimensionsNormalized = {normalize(mTexture.width(), 0, max),
Expand Down Expand Up @@ -57,7 +56,7 @@ void Sprite3D::updateModel()
{
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(mPosition));
model = glm::rotate(model, glm::radians(mRotation.first), mRotation.second);
model = mRotation.rotate(model);
model = glm::scale(model, glm::vec3(mDimensionsNormalized * mScale / 2.f, 1.0f));
mShader.bind();
mShader.setUniform("model", model);
Expand All @@ -83,9 +82,9 @@ void Sprite3D::setScale(float newScale)
updateModel();
}

void Sprite3D::setRotation(float angle, glm::vec3 axis)
void Sprite3D::setRotation(const Rotation3D& rotation)
{
mRotation = {angle, axis};
mRotation = rotation;
}

void Sprite3D::updateOpacity() const
Expand Down Expand Up @@ -125,8 +124,7 @@ void Sprite3D::showDebugImGui(std::string name)
ImGui::Begin(name.c_str());
ImGui::SliderFloat3("Position", &mPosition[0], -15, 15.f);
ImGui::SliderFloat2("Dimensions Normalized", &mDimensionsNormalized[0], 0, 2.f);
ImGui::SliderFloat3("Rotation Axis", &mRotation.second[0], 0.0f, 1.f);
ImGui::SliderFloat("Rotation Angle", &mRotation.first, 0.f, 360.0f);
mRotation.imGuiRotationSlider();
ImGui::SliderFloat2("Scale", &mScale[0], -4.0f, 4.0f);
ImGui::SliderFloat("Opacity", &mOpacity, 0.0f, 1.0f);
ImGui::End();
Expand Down
10 changes: 5 additions & 5 deletions AimGL/src/Renderer/Graphics/3D/Sprite3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Renderer/Core/VertexArray.h"
#include "Renderer/Graphics/Texture.h"
#include "Renderer/Renderer.h"
#include "Utils/Rotation3D.h"


class Camera;
Expand Down Expand Up @@ -56,11 +57,10 @@ class Sprite3D
void setScale(float newScale);

/**
* \brief Rotates the sprite
* \param angle The angle by which the sprite is to be rotated
* \param axis Axes according to which the object should rotate
* \brief Sets the object's rotation.
* \param rotation The new rotation that the object should take
*/
void setRotation(float angle, glm::vec3 axis);
void setRotation(const Rotation3D& rotation);

/**
* \brief Sets the transparency of the sprite in range 0 - 1
Expand Down Expand Up @@ -113,6 +113,6 @@ class Sprite3D
glm::vec3 mPosition;
glm::vec2 mScale;
glm::vec2 mDimensionsNormalized;
std::pair<float, glm::vec3> mRotation;
Rotation3D mRotation;
float mOpacity;
};
29 changes: 29 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Utils/Rotation3D.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "Rotation3D.h"
#include "pch.h"

glm::mat4 Rotation3D::rotate(glm::mat4 model, std::initializer_list<EulerAngle> ordering) const
{
for (const auto axisRotation: ordering)
{
switch (axisRotation)
{
case EulerAngle::YAW:
model = glm::rotate(model, glm::radians(yaw), glm::vec3(0.0f, 1.0f, 0.0f));
break;
case EulerAngle::PITCH:
model = glm::rotate(model, glm::radians(pitch), glm::vec3(1.0f, 0.0f, 0.0f));
break;
case EulerAngle::ROLL:
model = glm::rotate(model, glm::radians(roll), glm::vec3(0.0f, 0.0f, 1.0f));
break;
}
}
return model;
}

void Rotation3D::imGuiRotationSlider()
{
ImGui::SliderFloat("Roll", &roll, -360, 360.0f);
ImGui::SliderFloat("Pitch", &pitch, -360, 360.0f);
ImGui::SliderFloat("Yaw", &yaw, -360, 360.0f);
}
36 changes: 36 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Utils/Rotation3D.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

/**
* \brief Represents a 3D rotation using Euler angles.
*/
struct Rotation3D
{
/**
* \brief Angles of Euler rotation.
*/
enum class EulerAngle
{
ROLL,
PITCH,
YAW
};

float yaw{0};
float pitch{0};
float roll{0};

/**
* \brief Applies rotation to a 3D model matrix in a specified order.
* \param model The model matrix to be rotated.
* \param ordering The order in which yaw, pitch, and roll are applied.
* \return The rotated model matrix.
*/
[[nodiscard]] glm::mat4 rotate(glm::mat4 model,
std::initializer_list<EulerAngle> ordering = {
EulerAngle::YAW, EulerAngle::PITCH, EulerAngle::ROLL}) const;

/**
* \brief Renders an ImGui slider interface for adjusting the rotation.
*/
void imGuiRotationSlider();
};
27 changes: 16 additions & 11 deletions AimGL/src/World/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ void Camera::handleMouseInputs(const float& deltaTime)
void Camera::calculateCameraDirectionVector()
{
glm::vec3 direction;
direction.x = cos(glm::radians(mYaw)) * cos(glm::radians(mPitch));
direction.y = sin(glm::radians(mPitch));
direction.z = sin(glm::radians(mYaw)) * cos(glm::radians(mPitch));
direction.x = cos(glm::radians(mRotation.yaw)) * cos(glm::radians(mRotation.pitch));
direction.y = sin(glm::radians(mRotation.pitch));
direction.z = sin(glm::radians(mRotation.yaw)) * cos(glm::radians(mRotation.pitch));
mCameraFront = glm::normalize(direction);

direction.x = cos(glm::radians(mYaw));
direction.x = cos(glm::radians(mRotation.yaw));
direction.y = 0;
direction.z = sin(glm::radians(mYaw));
direction.z = sin(glm::radians(mRotation.yaw));
mCameraFrontWithoutPitch = glm::normalize(direction);
}

Expand All @@ -60,19 +60,19 @@ void Camera::calculateCameraAngles(const float& deltaTime)
mouseOffset.x *= mCameraSensitivity * deltaTime;
mouseOffset.y *= mCameraSensitivity * deltaTime * -1;

mYaw += mouseOffset.x;
mPitch += mouseOffset.y;
mRotation.yaw += mouseOffset.x;
mRotation.pitch += mouseOffset.y;
}

void Camera::keepNaturalPitchRanges()
{
if (mPitch > 89.0f)
if (mRotation.pitch > 89.0f)
{
mPitch = 89.0f;
mRotation.pitch = 89.0f;
}
if (mPitch < -89.0f)
if (mRotation.pitch < -89.0f)
{
mPitch = -89.0f;
mRotation.pitch = -89.0f;
}
}

Expand Down Expand Up @@ -152,6 +152,11 @@ void Camera::updateImGui()
ImGui::End();
}

Rotation3D Camera::rotation() const
{
return mRotation;
}

glm::vec3 Camera::direction() const
{
return mCameraFront;
Expand Down
12 changes: 8 additions & 4 deletions AimGL/src/World/Camera.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include "Game.h"

#include <Renderer/Graphics/3D/Utils/Rotation3D.h>
#include <glm/matrix.hpp>
#include <glm/vec3.hpp>

Expand Down Expand Up @@ -99,6 +100,12 @@ class Camera
*/
void updateImGui();

/**
* \brief Retrieves the current rotation of the object.
* \return The object's rotation as a Rotation3D instance.
*/
Rotation3D rotation() const;

private:
/**
* Handle keyboard behavior such as moving the camera inside the game
Expand Down Expand Up @@ -153,10 +160,7 @@ class Camera
glm::vec3 mCameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 mCameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 mCameraFrontWithoutPitch = glm::vec3(0.0f, 0.0f, -1.0f);

float mYaw = -90.f;
float mPitch = 0.f;
float mRoll = 0.f;
Rotation3D mRotation{-90, 0, 0.f};

float mCameraSpeed = 5.f;
float mCameraSensitivity = 4.f;
Expand Down

0 comments on commit 740bdef

Please sign in to comment.