Skip to content

Commit 4b50ac8

Browse files
committed
optimized view proj matrix generation
1 parent 0587ee4 commit 4b50ac8

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

include/omath/projection/Camera.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ namespace omath::projection
3737
m_viewPort(viewPort), m_fieldOfView(fov), m_farPlaneDistance(far), m_nearPlaneDistance(near),
3838
m_viewAngles(viewAngles), m_origin(position)
3939
{
40+
4041
}
4142

4243
virtual void LookAt(const Vector3& target) = 0;
@@ -45,12 +46,15 @@ namespace omath::projection
4546

4647
[[nodiscard]] virtual Mat4x4Type GetProjectionMatrix() const = 0;
4748

48-
[[nodiscard]] Mat4x4Type GetViewProjectionMatrix() const
49+
[[nodiscard]] Mat4x4Type GetViewProjectionMatrix()
4950
{
50-
return GetProjectionMatrix() * GetViewMatrix();
51+
if (!m_viewProjectionMatrix)
52+
m_viewProjectionMatrix = GetProjectionMatrix() * GetViewMatrix();
53+
54+
return m_viewProjectionMatrix.value();
5155
}
5256

53-
[[nodiscard]] std::expected<Vector3, Error> WorldToScreen(const Vector3& worldPosition) const
57+
[[nodiscard]] std::expected<Vector3, Error> WorldToScreen(const Vector3& worldPosition)
5458
{
5559
auto projected = GetViewProjectionMatrix() * MatColumnFromVector<float, Mat4x4Type::GetStoreOrdering()>(worldPosition);
5660

@@ -77,7 +81,7 @@ namespace omath::projection
7781
Vector3 m_origin;
7882

7983
private:
80-
84+
std::optional<Mat4x4Type> m_viewProjectionMatrix = std::nullopt;
8185
template<class Type>
8286
[[nodiscard]]
8387
constexpr static bool IsNdcOutOfBounds(const Type& ndc)

0 commit comments

Comments
 (0)