Skip to content

Commit ecdd9ec

Browse files
committed
improved some code
1 parent 4b50ac8 commit ecdd9ec

File tree

13 files changed

+106
-67
lines changed

13 files changed

+106
-67
lines changed

include/omath/Angle.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,11 @@ namespace omath
144144
{
145145
return operator+(-other);
146146
}
147+
148+
[[nodiscard]]
149+
constexpr Angle operator-() const
150+
{
151+
return {-m_angle};
152+
}
147153
};
148154
}

include/omath/Mat.hpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ namespace omath
349349

350350
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR, class Angle>
351351
[[nodiscard]]
352-
Mat<4, 4, Type, St> RotationMatAxisX(const Angle& angle) noexcept
352+
Mat<4, 4, Type, St> MatRotationAxisX(const Angle& angle) noexcept
353353
{
354354
return
355355
{
@@ -362,7 +362,7 @@ namespace omath
362362

363363
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR, class Angle>
364364
[[nodiscard]]
365-
Mat<4, 4, Type, St> RotationMatAxisY(const Angle& angle) noexcept
365+
Mat<4, 4, Type, St> MatRotationAxisY(const Angle& angle) noexcept
366366
{
367367
return
368368
{
@@ -375,21 +375,36 @@ namespace omath
375375

376376
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR, class Angle>
377377
[[nodiscard]]
378-
Mat<4, 4, Type, St> RotationMatAxisZ(const Angle& angle) noexcept
378+
Mat<4, 4, Type, St> MatRotationAxisZ(const Angle& angle) noexcept
379379
{
380380
return
381381
{
382382
{angle.Cos(), -angle.Sin(), 0, 0},
383-
{angle.Sin(), angle.Cos(), 0, 0},
384-
{0, 0, 1, 0},
385-
{0, 0, 0, 1},
383+
{angle.Sin(), angle.Cos(), 0, 0},
384+
{ 0, 0, 1, 0},
385+
{ 0, 0, 0, 1},
386386
};
387387
}
388388

389+
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
390+
[[nodiscard]]
391+
static Mat<4, 4, Type, St> MatCameraView(const Vector3& forward, const Vector3& right, const Vector3& up,
392+
const Vector3& cameraOrigin) noexcept
393+
{
394+
return Mat<4, 4, Type, St>
395+
{
396+
{right.x, right.y, right.z, 0},
397+
{up.x, up.y, up.z, 0},
398+
{forward.x, forward.y, forward.z, 0},
399+
{0, 0, 0, 1},
400+
401+
} * MatTranslation<Type, St>(-cameraOrigin);
402+
}
403+
389404
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR, class ViewAngles>
390405
[[nodiscard]]
391-
Mat<4, 4, Type, St> RotationMat(const ViewAngles& angles) noexcept
406+
Mat<4, 4, Type, St> MatRotation(const ViewAngles& angles) noexcept
392407
{
393-
return RotationMatAxisZ(angles.yaw) * RotationMatAxisY(angles.pitch) * RotationMatAxisX(angles.roll);
408+
return MatRotationAxisZ(angles.yaw) * MatRotationAxisY(angles.pitch) * MatRotationAxisX(angles.roll);
394409
}
395410
} // namespace omath
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Created by Orange on 12/4/2024.
3+
//
4+
#pragma once
5+
6+
#include <omath/Vector3.h>
7+
8+
9+
namespace omath::opengl
10+
{
11+
constexpr Vector3 kAbsUp = {0, 1, 0};
12+
constexpr Vector3 kAbsRight = {1, 0, 0};
13+
constexpr Vector3 kAbsForward = {0, 0, -1};
14+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//
2+
// Created by Orange on 12/4/2024.
3+
//
4+
#pragma once
File renamed without changes.

include/omath/engines/Source/Camera.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ namespace omath::source
1010
class Camera final : public projection::Camera<Mat4x4, ViewAngles>
1111
{
1212
public:
13-
Camera(const Vector3& position, const ViewAngles& viewAngles,
14-
const projection::ViewPort& viewPort, const Angle<float, 0, 180, AngleFlags::Clamped>& fov, float near, const float far) :
15-
projection::Camera<Mat4x4, ViewAngles>(position, viewAngles, viewPort, fov, near, far)
16-
{
17-
}
13+
Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
14+
const Angle<float, 0, 180, AngleFlags::Clamped>& fov, float near, float far);
1815
void LookAt(const Vector3& target) override;
1916
[[nodiscard]] Mat4x4 GetViewMatrix() const override;
2017
[[nodiscard]] Mat4x4 GetProjectionMatrix() const override;

include/omath/engines/Source/Constants.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace omath::source
1515

1616
using Mat4x4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
1717
using Mat3x3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
18-
18+
using Mat1x3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>;
1919
using PitchAngle = Angle<float, -89.f, 89.f, AngleFlags::Clamped>;
2020
using YawAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
2121
using RollAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;

include/omath/engines/Source/Formulas.hpp

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,49 @@ namespace omath::source
1010
[[nodiscard]]
1111
inline Vector3 ForwardVector(const ViewAngles& angles)
1212
{
13-
const auto vec = RotationMat(angles) * MatColumnFromVector(kAbsForward);
13+
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsForward);
1414

1515
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
1616
}
1717

1818
[[nodiscard]]
1919
inline Vector3 RightVector(const ViewAngles& angles)
2020
{
21-
const auto vec = RotationMat(angles) * MatColumnFromVector(kAbsRight);
21+
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsRight);
2222

2323
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
2424
}
2525

2626
[[nodiscard]]
2727
inline Vector3 UpVector(const ViewAngles& angles)
2828
{
29-
const auto vec = RotationMat(angles) * MatColumnFromVector(kAbsUp);
29+
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsUp);
3030

3131
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
3232
}
3333

34-
[[nodiscard]]
35-
constexpr Mat4x4 ViewMatrixFromVecs(const Vector3& forward, const Vector3& right, const Vector3& up,
36-
const Vector3& camera_pos)
34+
[[nodiscard]] inline Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3& cam_origin)
3735
{
38-
return Mat4x4{
39-
{right.x, right.y, right.z, 0},
40-
{-up.x, -up.y, -up.z, 0},
41-
{forward.x, forward.y, forward.z, 0},
42-
{0, 0, 0, 1},
43-
} *
44-
MatTranslation<float, MatStoreType::ROW_MAJOR>(-camera_pos);
45-
}
46-
47-
[[nodiscard]] inline Mat4x4 ViewMatrix(const ViewAngles& angles, const Vector3& cam_origin)
48-
{
49-
return ViewMatrixFromVecs(ForwardVector(angles), RightVector(angles), UpVector(angles), cam_origin);
36+
return MatCameraView(ForwardVector(angles), RightVector(angles), UpVector(angles), cam_origin);
5037
}
5138

5239

5340
[[nodiscard]]
54-
inline Mat4x4 PerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near, const float far)
41+
inline Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
42+
const float far)
5543
{
44+
// NOTE: Needed tp make thing draw normal, since source is wierd
45+
// and use tricky projection matrix formula.
5646
constexpr auto kMultiplyFactor = 0.75f;
5747

5848
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f) * kMultiplyFactor;
5949

6050
return {
61-
{1.f / (aspectRatio * fovHalfTan), 0, 0, 0},
62-
{0, 1.f / (fovHalfTan), 0, 0},
63-
{0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)},
64-
{0, 0, 1, 0},
65-
};
51+
{1.f / (aspectRatio * fovHalfTan), 0, 0, 0},
52+
{0, 1.f / (fovHalfTan), 0, 0},
53+
{0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)},
54+
{0, 0, 1, 0},
55+
56+
};
6657
}
6758
} // namespace omath::source

include/omath/projection/Camera.hpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace omath::projection
2525
return m_width / m_height;
2626
}
2727
};
28+
using FieldOfView = const Angle<float, 0.f, 180.f, AngleFlags::Clamped>;
2829

2930
template<class Mat4x4Type, class ViewAnglesType>
3031
class Camera
@@ -33,7 +34,7 @@ namespace omath::projection
3334
public:
3435
virtual ~Camera() = default;
3536
Camera(const Vector3& position, const ViewAnglesType& viewAngles, const ViewPort& viewPort,
36-
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, const float near, const float far) :
37+
const FieldOfView& fov, const float near, const float far) :
3738
m_viewPort(viewPort), m_fieldOfView(fov), m_farPlaneDistance(far), m_nearPlaneDistance(near),
3839
m_viewAngles(viewAngles), m_origin(position)
3940
{
@@ -48,15 +49,12 @@ namespace omath::projection
4849

4950
[[nodiscard]] Mat4x4Type GetViewProjectionMatrix()
5051
{
51-
if (!m_viewProjectionMatrix)
52-
m_viewProjectionMatrix = GetProjectionMatrix() * GetViewMatrix();
53-
54-
return m_viewProjectionMatrix.value();
52+
return GetProjectionMatrix() * GetViewMatrix();
5553
}
5654

57-
[[nodiscard]] std::expected<Vector3, Error> WorldToScreen(const Vector3& worldPosition)
55+
[[nodiscard]] std::expected<Vector3, Error> WorldToScreen(const Mat4x4Type& viewProj, const Vector3& worldPosition) const
5856
{
59-
auto projected = GetViewProjectionMatrix() * MatColumnFromVector<float, Mat4x4Type::GetStoreOrdering()>(worldPosition);
57+
auto projected = viewProj * MatColumnFromVector<float, Mat4x4Type::GetStoreOrdering()>(worldPosition);
6058

6159
if (projected.At(3, 0) == 0.0f)
6260
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
@@ -66,7 +64,7 @@ namespace omath::projection
6664
if (IsNdcOutOfBounds(projected))
6765
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
6866

69-
return Vector3{++projected.At(0,0) / 2 * m_viewPort.m_width , ++projected.At(1,0) / 2 * m_viewPort.m_height, projected.At(2,0)};
67+
return Vector3{(projected.At(0,0)+1) / 2 * m_viewPort.m_width , (-projected.At(1,0)+1) / 2 * m_viewPort.m_height, projected.At(2,0)};
7068
}
7169

7270
protected:
@@ -81,7 +79,6 @@ namespace omath::projection
8179
Vector3 m_origin;
8280

8381
private:
84-
std::optional<Mat4x4Type> m_viewProjectionMatrix = std::nullopt;
8582
template<class Type>
8683
[[nodiscard]]
8784
constexpr static bool IsNdcOutOfBounds(const Type& ndc)

source/engines/Source/Camera.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,29 @@
88
namespace omath::source
99
{
1010

11+
Camera::Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
12+
const projection::FieldOfView& fov, const float near, const float far) :
13+
projection::Camera<Mat4x4, ViewAngles>(position, viewAngles, viewPort, fov, near, far)
14+
{
15+
}
1116
void Camera::LookAt(const Vector3& target)
1217
{
1318
const float distance = m_origin.DistTo(target);
1419
const auto delta = target - m_origin;
1520

1621

1722
m_viewAngles.pitch = PitchAngle::FromRadians(std::asin(delta.z / distance));
18-
m_viewAngles.yaw = YawAngle::FromRadians(std::atan2(delta.y, delta.x));
23+
m_viewAngles.yaw = -YawAngle::FromRadians(std::atan2(delta.y, delta.x));
1924
m_viewAngles.roll = RollAngle::FromRadians(0.f);
2025
}
2126

2227
Mat4x4 Camera::GetViewMatrix() const
2328
{
24-
return ViewMatrix(m_viewAngles, m_origin);
29+
return CalcViewMatrix(m_viewAngles, m_origin);
2530
}
2631

2732
Mat4x4 Camera::GetProjectionMatrix() const
2833
{
29-
return PerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance, m_farPlaneDistance);
34+
return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance, m_farPlaneDistance);
3035
}
3136
} // namespace omath::source

0 commit comments

Comments
 (0)