Skip to content

Commit

Permalink
refactor: add comments and remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
tomezpl committed Oct 24, 2023
1 parent c759151 commit cbd6738
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 69 deletions.
38 changes: 0 additions & 38 deletions src/L3D/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,63 +58,25 @@ namespace lepus

lepus::math::Matrix4x4 BuildViewMatrix()
{

/*
rot[0] = vec4(c+axis.x*axis.x*(1.0 - c), axis.y*axis.x*(1.0 - c)+axis.z*s, axis.z*axis.x*(1.0 - c)-axis.y*s, 0.0);
rot[1] = vec4(axis.y*axis.x*(1.0 - c)-axis.z*s, c+axis.y*axis.y*(1.0 - c), axis.z*axis.y*(1.0 - c)+axis.x*s, 0.0);
rot[2] = vec4(axis.z*axis.x*(1.0 - c)+axis.y*s, axis.z*axis.y*(1.0 - c)-axis.x*s, c+axis.z*axis.z*(1.0 - c), 0.0);
rot[3] = vec4(0.0, 0.0, 0.0, 1.0);
*/

lepus::types::Vector3 axis = m_Transform.Rotation().Axis();

auto fwdDot = lepus::types::Vector3::Dot(m_Transform.Forward(), lepus::types::Vector3(0.f, 0.f, -1.f));
auto rgtDot = lepus::types::Vector3::Dot(m_Transform.Right(), lepus::types::Vector3(1.f, 0.f, 0.f));
auto upDot = lepus::types::Vector3::Dot(m_Transform.Up(), lepus::types::Vector3(0.f, 1.f, 0.f));

float angle = m_Transform.Rotation().Angle();


/*axis.x(axis.x() * rgtDot);
axis.y(axis.y() * upDot);
axis.z(axis.z() * fwdDot);*/

// TODO: shouldn't need the non-const float* cast
lepus::math::Matrix4x4 rot = lepus::math::Transform::AxisAngle(axis, angle);
lepus::math::Matrix4x4 pos = lepus::math::Matrix4x4::Identity();

//LepusEngine::ConsoleLogger::Global().LogInfo("Camera", "BuildViewMatrix", (char*)std::to_string(angle).c_str());
//LepusEngine::ConsoleLogger::Global().LogInfo("Camera", "BuildViewMatrix", (char*)std::to_string(fwdDot).c_str());



auto f = m_Transform.Forward() * (1.f / m_Transform.Forward().Magnitude());
auto s = m_Transform.Right() * (1.f / m_Transform.Right().Magnitude());
auto u = m_Transform.Up() * (1.f / m_Transform.Up().Magnitude());

//LepusEngine::ConsoleLogger::Global().LogInfo("Camera", "BuildViewMatrix", (char*)s.ToString().c_str());

auto e = m_Transform.Origin();

pos.set<0, 3>(-e.x());
pos.set<1, 3>(-e.y());
pos.set<2, 3>(-e.z());

/*pos.set<0, 0>(s.x());pos.set<0, 1>(u.x());pos.set<0, 2>(f.x());
pos.set<1, 0>(s.y());pos.set<1, 1>(u.y());pos.set<1, 2>(f.y());
pos.set<2, 0>(s.z());pos.set<2, 1>(u.z());pos.set<2, 2>(f.z());*/

//return pos;


lepus::math::Matrix4x4 lookAt = lepus::math::Matrix4x4::Identity();

lookAt.set<0, 0>(s.x());lookAt.set<0, 1>(s.y());lookAt.set<0, 2>(s.z());
lookAt.set<1, 0>(u.x());lookAt.set<1, 1>(u.y());lookAt.set<1, 2>(u.z());
lookAt.set<2, 0>(f.x());lookAt.set<2, 1>(f.y());lookAt.set<2, 2>(f.z());

return lookAt.Multiply(pos);
return rot.Multiply(pos);
}
};
}
Expand Down
24 changes: 14 additions & 10 deletions src/LUtility/Types/Quaternion.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,30 @@ namespace lepus

Quaternion(const Vector3& axis, float angle) : Quaternion(axis.x(), axis.y(), axis.z(), angle) {}

inline Vector3 Axis()
/// @brief Get the axis of rotation for this Quaternion.
/// @return A Vector3 representing the axis of rotation calculated from the Quaternion's imaginary part.
inline Vector3 Axis() const
{
float invW = fmax(0.f, 1.f - w() * w());
float sqrtInvW = sqrt(invW);
std::string wStr = "invW = " + std::to_string(invW) + ", sqrtInvW = " + std::to_string(sqrtInvW);
//LepusEngine::ConsoleLogger::Global().LogInfo("Quaternion", "Axis", (char*)wStr.c_str());
float sqrtInvW = sqrt(fmax(0.f, 1.f - w() * w()));
if (sqrtInvW == 0.f)
{
// just return Vector3.Up if divide by 0
LepusEngine::ConsoleLogger::Global().LogInfo("Quaternion", "Axis", "Up");
return Vector3(0.f, 1.f, 0.f);
}

Vector3 vec = Vector3(x() / sqrtInvW, y() / sqrtInvW, z() / sqrtInvW);
return vec * (1.f / vec.Magnitude());
}

/// @brief Get the angle of rotation for this Quaternion.
/// @return A scalar describing the angle of rotation calculated from the Quaternion's real part (arccos(w) * 2).
inline float Angle() const
{
return acosf(fmax(-1.f, fmin(1.f, w()))) * 2.f;
}

inline Quaternion operator*(const Quaternion& b)
inline Quaternion operator*(const Quaternion& b) const
{
// TODO: use scalar/vector notation for multiplying quaternions
Quaternion result = Quaternion();

lepus::types::Vector3 va = lepus::types::Vector3((float*)GetData());
Expand All @@ -76,7 +75,10 @@ namespace lepus
return result;
}

inline lepus::types::Vector3 Rotate(const lepus::types::Vector3& v)
/// @brief Rotates a Vector3 v by the axis and angle described by this Quaternion.
/// @param v Vector3 representing a point in 3D space to rotate.
/// @return A Vector3 containing the point v rotated around the origin.
inline lepus::types::Vector3 Rotate(const lepus::types::Vector3& v) const
{
Quaternion p = Quaternion();
p.w(0.f);
Expand All @@ -90,7 +92,9 @@ namespace lepus
return lepus::types::Vector3((float*)(*this * p * conjugate).GetData());
}

std::string ToString()
/// @brief Create a text representation of the Quaternion. Useful for debugging.
/// @return A string representing the Quaternion formatted as: "X = x, Y = y, Z = z, W = w"
std::string ToString() const
{
return std::string("X = ").append(std::to_string(x())).append(", Y = ").append(std::to_string(y())).append(", Z = ").append(std::to_string(z())).append(", W = ").append(std::to_string(w()));
}
Expand Down
31 changes: 10 additions & 21 deletions src/LUtility/Types/Transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,51 +27,40 @@ namespace lepus
m_Rotation = lepus::types::Quaternion();
}

inline lepus::types::Vector3& Origin() { return m_Origin; }
inline lepus::types::Vector3 Origin() { return m_Origin; }
inline void Origin(const lepus::types::Vector3& vec)
{
m_Origin.x(vec.x());
m_Origin.y(vec.y());
m_Origin.z(vec.z());
}

inline lepus::types::Vector3 Forward() { return lepus::types::Vector3(m_Forward); }
inline lepus::types::Vector3 Right() { return lepus::types::Vector3(m_Right); }
inline lepus::types::Vector3 Up() { return lepus::types::Vector3(m_Up); }
inline lepus::types::Quaternion& Rotation() { return m_Rotation; }
inline void Rotation(lepus::types::Quaternion& newRotation)

inline lepus::types::Quaternion Rotation() { return m_Rotation; }
inline void Rotation(const lepus::types::Quaternion& newRotation)
{
m_Rotation.x(newRotation.x());
m_Rotation.y(newRotation.y());
m_Rotation.z(newRotation.z());
m_Rotation.w(newRotation.w());
}

inline void Rotate(lepus::types::Quaternion quat)
inline void Rotate(const lepus::types::Quaternion& quat)
{
m_Rotation = m_Rotation * quat;


auto combined = m_Rotation * quat;
auto newAxis = combined.Axis();
auto newAngle = combined.Angle();
//if (abs(newAngle) > 0.001f)
{
m_Rotation = combined;
}
//LepusEngine::ConsoleLogger::Global().LogInfo("Transform", "Rotate", (char*)std::to_string(newAngle).c_str());
//LepusEngine::ConsoleLogger::Global().LogInfo("Transform", "Rotate", (char*)newAxis.ToString().c_str());
lepus::math::Matrix4x4 rotationMatrix = AxisAngle(newAxis, newAngle);
/*auto newForward = rotationMatrix.Multiply(lepus::types::Vector4(0.f, 0.f, -1.f, 1.f));
auto newRight = rotationMatrix.Multiply(lepus::types::Vector4(1.f, 0.f, 0.f, 1.f));
auto newUp = rotationMatrix.Multiply(lepus::types::Vector4(0.f, 1.f, 0.f, 1.f));*/

// Rotate the basis vectors
auto newForward = quat.Rotate(m_Forward);
auto newRight = quat.Rotate(m_Right);
auto newUp = quat.Rotate(m_Up);

// Set basis vectors to rotated
m_Forward.x(newForward.x());m_Forward.y(newForward.y());m_Forward.z(newForward.z());
m_Right.x(newRight.x());m_Right.y(newRight.y());m_Right.z(newRight.z());
m_Up.x(newUp.x());m_Up.y(newUp.y());m_Up.z(newUp.z());

// TODO: Set the rotation to a multiplication of currentRot by the delta quaternion
}

static inline lepus::math::Matrix4x4 AxisAngle(lepus::types::Vector3& axis, float angle)
Expand Down

0 comments on commit cbd6738

Please sign in to comment.