NDEVR
API Documentation
MatrixDefinitions.h
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: MatrixDefinitions
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "Matrix.hpp"
34#include "Vertex.hpp"
35#ifdef _WIN32
36#undef far//MS name polution
37#undef near
38#endif
39namespace NDEVR
40{
45 template<class t_type>
47 {
59 t_type left
60 , t_type right
61 , t_type bottom
62 , t_type top
63 , t_type near
64 , t_type far)
65 {
66 return Matrix<t_type>(
67 2 * near / (right - left), cast<t_type>(0), cast<t_type>(0), cast<t_type>(0)
68 , cast<t_type>(0), 2 * near / (top - bottom), cast<t_type>(0), cast<t_type>(0)
69 , (right + left) / (right - left), (top + bottom) / (top - bottom), -(far + near) / (far - near), cast<t_type>(-1)
70 , cast<t_type>(0), cast<t_type>(0), -(2 * far * near) / (far - near), cast<t_type>(0));
71 }
72
82 static constexpr Matrix<t_type> createOrtho(t_type left, t_type right, t_type bottom, t_type top, t_type near_plane, t_type far_plane)
83 {
84 return Matrix<t_type>(
85 2 / (right - left), cast<t_type>(0), cast<t_type>(0), cast<t_type>(0)
86 , cast<t_type>(0), 2 / (top - bottom), cast<t_type>(0), cast<t_type>(0)
87 , cast<t_type>(0), cast<t_type>(0), cast<t_type>(0), cast<t_type>(1) / (far_plane - near_plane)
89 }
90
98 template<class t_angle_type>
99 static constexpr Matrix<t_type> FrustumTransform(Angle< t_angle_type> y_field_of_view, t_type aspect_ratio, t_type near, t_type far)
100 {
101 t_type height = near * tan(y_field_of_view / 2);
102 t_type width = height * aspect_ratio;
103 return FrustumTransform(-width, width, -height, height, near, far);
104 }
105
113 static constexpr Matrix<t_type> OrthoFrustum(const Vector<2, t_type>& span, t_type n, t_type f)
114 {
115 Matrix<t_type> mat;
116 mat[0][0] = cast<t_type>(2) / span[X];
117 mat[1][1] = cast<t_type>(2) / span[Y];
118 mat[2][2] = -cast<t_type>(2) / (f - n);
119
120 mat[3][0] = 0;
121 mat[3][1] = 0;
122 mat[3][2] = -(f + n) / (f - n);
123 mat[3][3] = cast<t_type>(1);
124
125 Matrix<t_type> to_vulkan;
126 to_vulkan[0][0] = 1.0f;
127 to_vulkan[1][1] = -1.0f;
128 to_vulkan[2][2] = 0.5f;
129 to_vulkan[3][2] = 0.5f;
130 to_vulkan[3][3] = 1.0f;
131 return to_vulkan * mat;
132 }
133
140 static constexpr Matrix<t_type> ViewMatrix(const Vector<3, t_type>& location, const Vector<3, t_type>& lookat, const Vector<3, t_type>& up)
141 {
142 const Vector<3, t_type> f = lookat.template normalized<t_type>();
143 Vector<3, t_type> s = cross(f, up).template normalized<t_type>();
144 if (IsInvalid(s))
145 s = Vector<3, t_type>(0, 1, 0);
146 Vector<3, t_type> u = cross(s, f).template normalized<t_type>();
147 if (IsInvalid(u))
148 u = Vector<3, t_type>(-1, 0, 0);
149
150 return Matrix<t_type>(
151 s[X], s[Y], s[Z], cast<t_type>(0),
152 u[X], u[Y], u[Z], cast<t_type>(0),
153 -f[X], -f[Y], -f[Z], cast<t_type>(0),
154 location[X], location[Y], location[Z], cast<t_type>(1));
155 }
156
162 static constexpr Matrix<t_type> RotationMatrix(const Vector<3, t_type>& lookat, const Vector<3, t_type>& up)
163 {
164 const Vector<3, t_type> f = lookat.template normalized<t_type>();
165 Vector<3, t_type> s = cross(f, up).template normalized<t_type>();
166 if (IsInvalid(s))
167 s = Vector<3, t_type>(0, 1, 0);
168 Vector<3, t_type> u = cross(s, f).template normalized<t_type>();
169 if (IsInvalid(u))
170 u = Vector<3, t_type>(-1, 0, 0);
171 return Matrix<t_type>(
172 s[X], u[X], -f[X], cast<t_type>(0)
173 , s[Y], u[Y], -f[Y], cast<t_type>(0)
174 , s[Z], u[Z], -f[Z], cast<t_type>(0)
175 , 0, 0, 0, cast<t_type>(1));
176 };
177
186 constexpr static Matrix<t_type> Perspective(t_type fovy, t_type aspect, t_type near_plane, t_type far_plane)
187 {
188 return Matrix<t_type>(
189 fovy / aspect, 0, 0, 0
190 , 0, fovy, 0, 0
191 , 0, 0, (far_plane + near_plane) / (near_plane - far_plane), (2 * near_plane * far_plane) / (near_plane - far_plane)
192 , 0, 0, -1, 0);
193 }
194 };
195
196}
197
Stores an angle in an optimized internal format with support for efficient trigonometric operations.
Definition Angle.h:83
Templated logic for doing matrix multiplication.
Definition Matrix.hpp:182
A fixed-size array with N dimensions used as the basis for geometric and mathematical types.
Definition Vector.hpp:62
The primary namespace for the NDEVR SDK.
constexpr Vector< 1, t_type > cross(const Vector< 1, t_type > &, const Vector< 1, t_type > &)
static constexpr bool IsInvalid(const Angle< t_type > &value)
Checks whether the given Angle holds an invalid value.
Definition Angle.h:388
std::enable_if<!ObjectInfo< t_type >::Float, fltp08 >::type tan(const Angle< t_type > &angle)
Performs optimized tangent operation on the given angle using pre-computed lookup table for optimal s...
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Definition Angle.h:408
Provides common projection, view, and rotation matrix construction functions.
static constexpr Matrix< t_type > Perspective(t_type fovy, t_type aspect, t_type near_plane, t_type far_plane)
Creates a symmetric perspective projection matrix.
static constexpr Matrix< t_type > FrustumTransform(Angle< t_angle_type > y_field_of_view, t_type aspect_ratio, t_type near, t_type far)
Creates a perspective projection matrix from a field of view and aspect ratio.
static constexpr Matrix< t_type > OrthoFrustum(const Vector< 2, t_type > &span, t_type n, t_type f)
Creates an orthographic frustum matrix adjusted for Vulkan coordinate conventions.
static constexpr Matrix< t_type > ViewMatrix(const Vector< 3, t_type > &location, const Vector< 3, t_type > &lookat, const Vector< 3, t_type > &up)
Creates a view matrix from a camera position, look-at direction, and up vector.
static constexpr Matrix< t_type > createOrtho(t_type left, t_type right, t_type bottom, t_type top, t_type near_plane, t_type far_plane)
Creates an orthographic projection matrix from the given bounds.
static constexpr Matrix< t_type > FrustumTransform(t_type left, t_type right, t_type bottom, t_type top, t_type near, t_type far)
Creates a perspective projection matrix from frustum bounds.
static constexpr Matrix< t_type > RotationMatrix(const Vector< 3, t_type > &lookat, const Vector< 3, t_type > &up)
Creates a rotation-only matrix from a look-at direction and up vector.