API Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
MatrixDefinitions.h
Go to the documentation of this file.
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{
41 /**--------------------------------------------------------------------------------------------------
42 \brief Provides common definitions for creating 4x4 matrices of certain types.
43 **/
44 template<class t_type>
46 {
48 t_type left
49 , t_type right
50 , t_type bottom
51 , t_type top
52 , t_type near
53 , t_type far)
54 {
55 return Matrix<t_type>(
56 2 * near / (right - left), cast<t_type>(0), cast<t_type>(0), cast<t_type>(0)
57 , cast<t_type>(0), 2 * near / (top - bottom), cast<t_type>(0), cast<t_type>(0)
58 , (right + left) / (right - left), (top + bottom) / (top - bottom), -(far + near) / (far - near), cast<t_type>(-1)
59 , cast<t_type>(0), cast<t_type>(0), -(2 * far * near) / (far - near), cast<t_type>(0));
60 }
61 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)
62 {
63 return Matrix<t_type>(
64 2 / (right - left), cast<t_type>(0), cast<t_type>(0), cast<t_type>(0)
65 , cast<t_type>(0), 2 / (top - bottom), cast<t_type>(0), cast<t_type>(0)
66 , cast<t_type>(0), cast<t_type>(0), cast<t_type>(0), cast<t_type>(1) / (far_plane - near_plane)
68 }
69 template<class t_angle_type>
70 static constexpr Matrix<t_type> FrustumTransform(Angle< t_angle_type> y_field_of_view, t_type aspect_ratio, t_type near, t_type far)
71 {
72 t_type height = near * tan(y_field_of_view / 2);
73 t_type width = height * aspect_ratio;
74 return FrustumTransform(-width, width, -height, height, near, far);
75 }
76
77 static constexpr Matrix<t_type> OrthoFrustum(const Vector<2, t_type>& span, t_type n, t_type f)
78 {
80 mat[0][0] = cast<t_type>(2) / span[X];
81 mat[1][1] = cast<t_type>(2) / span[Y];
82 mat[2][2] = -cast<t_type>(2) / (f - n);
83
84 mat[3][0] = 0;
85 mat[3][1] = 0;
86 mat[3][2] = -(f + n) / (f - n);
87 mat[3][3] = cast<t_type>(1);
88
89 Matrix<t_type> to_vulkan;
90 to_vulkan[0][0] = 1.0f;
91 to_vulkan[1][1] = -1.0f;
92 to_vulkan[2][2] = 0.5f;
93 to_vulkan[3][2] = 0.5f;
94 to_vulkan[3][3] = 1.0f;
95 return to_vulkan * mat;
96 }
97 static constexpr Matrix<t_type> ViewMatrix(const Vector<3, t_type>& location, const Vector<3, t_type>& lookat, const Vector<3, t_type>& up)
98 {
99 const Vector<3, t_type> f = lookat.template normalized<t_type>();
100 Vector<3, t_type> s = cross(f, up).template normalized<t_type>();
101 if (IsInvalid(s))
102 s = Vector<3, t_type>(0, 1, 0);
103 Vector<3, t_type> u = cross(s, f).template normalized<t_type>();
104 if (IsInvalid(u))
105 u = Vector<3, t_type>(-1, 0, 0);
106 return Matrix<t_type>(
107 s[X], u[X], -f[X], cast<t_type>(0)
108 , s[Y], u[Y], -f[Y], cast<t_type>(0)
109 , s[Z], u[Z], -f[Z], cast<t_type>(0)
110 , dot(-s, location), dot(-u, location), dot(f, location), cast<t_type>(1));
111 };
112 static constexpr Matrix<t_type> RotationMatrix(const Vector<3, t_type>& lookat, const Vector<3, t_type>& up)
113 {
114 const Vector<3, t_type> f = lookat.template normalized<t_type>();
115 Vector<3, t_type> s = cross(f, up).template normalized<t_type>();
116 if (IsInvalid(s))
117 s = Vector<3, t_type>(0, 1, 0);
118 Vector<3, t_type> u = cross(s, f).template normalized<t_type>();
119 if (IsInvalid(u))
120 u = Vector<3, t_type>(-1, 0, 0);
121 return Matrix<t_type>(
122 s[X], u[X], -f[X], cast<t_type>(0)
123 , s[Y], u[Y], -f[Y], cast<t_type>(0)
124 , s[Z], u[Z], -f[Z], cast<t_type>(0)
125 , 0, 0, 0, cast<t_type>(1));
126 };
127
128 constexpr static Matrix<t_type> Perspective(t_type fovy, t_type aspect, t_type near_plane, t_type far_plane)
129 {
130 return Matrix<t_type>(
131 fovy / aspect, 0, 0, 0
132 , 0, fovy, 0, 0
133 , 0, 0, (far_plane + near_plane) / (near_plane - far_plane), (2 * near_plane * far_plane) / (near_plane - far_plane)
134 , 0, 0, -1, 0);
135 }
136 };
137
138}
139
The primary angle storage class for this API. Stores an angle in an optimized format.
Definition StringStream.h:540
Definition Matrix.hpp:176
A fixed-size array with better performance compared to dynamic containers.
Definition Vector.hpp:60
Definition ACIColor.h:37
constexpr bool IsInvalid(const t_type &value)
Query if 'value' is valid or invalid. Invalid values should return invalid if used for calculations o...
Definition BaseFunctions.hpp:170
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...
Definition AngleFunctions.h:156
t_type dot(const Vector< t_dims, t_type > &v1, const Vector< t_dims, t_type > &v2)
Definition VectorFunctions.hpp:1030
constexpr Vector< 1, t_type > cross(const Vector< 1, t_type > &, const Vector< 1, t_type > &)
Definition VectorFunctions.hpp:898
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:375
@ Y
Definition BaseValues.hpp:169
@ X
Definition BaseValues.hpp:167
@ Z
Definition BaseValues.hpp:171
Provides common definitions for creating 4x4 matrices of certain types.
Definition MatrixDefinitions.h:46
static constexpr Matrix< t_type > RotationMatrix(const Vector< 3, t_type > &lookat, const Vector< 3, t_type > &up)
Definition MatrixDefinitions.h:112
static constexpr Matrix< t_type > OrthoFrustum(const Vector< 2, t_type > &span, t_type n, t_type f)
Definition MatrixDefinitions.h:77
static constexpr Matrix< t_type > Perspective(t_type fovy, t_type aspect, t_type near_plane, t_type far_plane)
Definition MatrixDefinitions.h:128
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)
Definition MatrixDefinitions.h:61
static constexpr Matrix< t_type > ViewMatrix(const Vector< 3, t_type > &location, const Vector< 3, t_type > &lookat, const Vector< 3, t_type > &up)
Definition MatrixDefinitions.h:97
static constexpr Matrix< t_type > FrustumTransform(t_type left, t_type right, t_type bottom, t_type top, t_type near, t_type far)
Definition MatrixDefinitions.h:47
static constexpr Matrix< t_type > FrustumTransform(Angle< t_angle_type > y_field_of_view, t_type aspect_ratio, t_type near, t_type far)
Definition MatrixDefinitions.h:70