Mathter
A configurable 3D math library for game developers.
OrthographicBuilder.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include "../Matrix/MatrixImpl.hpp"
5 #include "../Vector.hpp"
6 #include "IdentityBuilder.hpp"
7 
8 
9 namespace mathter {
10 
11 
12 template <class T, int Dim, bool Packed>
14  static_assert(!std::is_integral_v<T>);
16 public:
17  OrthographicBuilder(const VectorT& minBounds, const VectorT& maxBounds, T projNearPlane, T projFarPlane)
18  : minBounds(minBounds), maxBounds(maxBounds), projNearPlane(projNearPlane), projFarPlane(projFarPlane) {}
20 
21  template <class U, eMatrixOrder Order, eMatrixLayout Layout, bool MPacked>
24  Set(m);
25  return m;
26  }
27 
28  template <class U, eMatrixLayout Layout, bool MPacked>
31  Set(m);
32  return m;
33  }
34 
35  template <class U, eMatrixLayout Layout, bool MPacked>
38  Set(m);
39  return m;
40  }
41 
42 private:
43  template <class U, int Rows, int Columns, eMatrixOrder Order, eMatrixLayout Layout, bool MPacked>
46 
47  VectorT volumeSize = maxBounds - minBounds;
48  VectorT scale = T(2) / volumeSize;
49  scale[scale.Dimension() - 1] *= T(0.5) * (projFarPlane - projNearPlane);
50  VectorT offset = -(maxBounds + minBounds) / T(2) * scale;
51  offset[offset.Dimension() - 1] += (projFarPlane + projNearPlane) / 2;
52 
53  m = Identity();
54  for (int i = 0; i < scale.Dimension(); ++i) {
55  m(i, i) = scale(i);
56  (Order == eMatrixOrder::FOLLOW_VECTOR ? m(scale.Dimension(), i) : m(i, scale.Dimension())) = offset(i);
57  }
58  }
59 
60  const Vector<T, Dim, Packed> minBounds, maxBounds;
61  T projNearPlane, projFarPlane;
62 };
63 
64 
72 template <class T, int Dim, bool Packed>
73 auto Orthographic(const Vector<T, Dim, Packed>& minBounds, const Vector<T, Dim, Packed>& maxBounds, T projNearPlane = T(0), T projFarPlane = T(1)) {
74  if constexpr (std::is_integral_v<T>) {
75  using VectorT = Vector<float, Dim, false>;
76  return OrthographicBuilder(VectorT(minBounds), VectorT(maxBounds), float(projNearPlane), float(projFarPlane));
77  }
78  else {
79  return OrthographicBuilder(minBounds, maxBounds, projNearPlane, projFarPlane);
80  }
81 }
82 
83 
84 } // namespace mathter
auto Identity()
Creates an identity matrix or identity quaternion.
Definition: IdentityBuilder.hpp:42
auto Orthographic(const Vector< T, Dim, Packed > &minBounds, const Vector< T, Dim, Packed > &maxBounds, T projNearPlane=T(0), T projFarPlane=T(1))
Creates an orthographics projection matrix. The volume before projection is an axis-aligned hypercube...
Definition: OrthographicBuilder.hpp:73
Represents a vector in N-dimensional space.
Definition: Definitions.hpp:57
Definition: OrthographicBuilder.hpp:13
Definition: Approx.hpp:11
Definition: Definitions.hpp:63
constexpr int Dimension() const
Returns the number of dimensions of the vector.
Definition: VectorImpl.hpp:402
OrthographicBuilder & operator=(const OrthographicBuilder &)=delete
OrthographicBuilder(const VectorT &minBounds, const VectorT &maxBounds, T projNearPlane, T projFarPlane)
Definition: OrthographicBuilder.hpp:17