API Documentation
Loading...
Searching...
No Matches
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: MatrixFunctions
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 template<class t_type>
43 {
45 t_type left
46 , t_type right
47 , t_type bottom
48 , t_type top
49 , t_type near
50 , t_type far)
51 {
52 return Matrix<t_type>(
53 2 * near / (right - left), cast<t_type>(0), cast<t_type>(0), cast<t_type>(0)
54 , cast<t_type>(0), 2 * near / (top - bottom), cast<t_type>(0), cast<t_type>(0)
55 , (right + left) / (right - left), (top + bottom) / (top - bottom), -(far + near) / (far - near), cast<t_type>(-1)
56 , cast<t_type>(0), cast<t_type>(0), -(2 * far * near) / (far - near), cast<t_type>(0));
57 }
58 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)
59 {
60 return Matrix<t_type>(
61 2 / (right - left), cast<t_type>(0), cast<t_type>(0), cast<t_type>(0)
62 , cast<t_type>(0), 2 / (top - bottom), cast<t_type>(0), cast<t_type>(0)
63 , cast<t_type>(0), cast<t_type>(0), cast<t_type>(0), cast<t_type>(1) / (far_plane - near_plane)
65 }
66 template<class t_angle_type>
67 static constexpr Matrix<t_type> FrustumTransform(Angle< t_angle_type> y_field_of_view, t_type aspect_ratio, t_type near, t_type far)
68 {
69 t_type height = near * tan(y_field_of_view / 2);
70 t_type width = height * aspect_ratio;
71 return FrustumTransform(-width, width, -height, height, near, far);
72 }
73
74 static constexpr Matrix<t_type> OrthoFrustum(const Vector<2, t_type>& span, t_type n, t_type f)
75 {
77 mat[0][0] = cast<t_type>(2) / span[X];
78 mat[1][1] = cast<t_type>(2) / span[Y];
79 mat[2][2] = -cast<t_type>(2) / (f - n);
80
81 mat[3][0] = 0;
82 mat[3][1] = 0;
83 mat[3][2] = -(f + n) / (f - n);
84 mat[3][3] = cast<t_type>(1);
85
86 Matrix<t_type> to_vulkan;
87 to_vulkan[0][0] = 1.0f;
88 to_vulkan[1][1] = -1.0f;
89 to_vulkan[2][2] = 0.5f;
90 to_vulkan[3][2] = 0.5f;
91 to_vulkan[3][3] = 1.0f;
92 return to_vulkan * mat;
93 }
94 static constexpr Matrix<t_type> ViewMatrix(const Vector<3, t_type>& location, const Vector<3, t_type>& lookat, const Vector<3, t_type>& up)
95 {
96 const Vector<3, t_type> f = lookat.template normalized<t_type>();
97 Vector<3, t_type> s = cross(f, up).template normalized<t_type>();
98 if (isNaN(s))
99 s = Vector<3, t_type>(0, 1, 0);
100 Vector<3, t_type> u = cross(s, f).template normalized<t_type>();
101 if (isNaN(u))
102 u = Vector<3, t_type>(-1, 0, 0);
103 return Matrix<t_type>(
104 s[X], u[X], -f[X], cast<t_type>(0)
105 , s[Y], u[Y], -f[Y], cast<t_type>(0)
106 , s[Z], u[Z], -f[Z], cast<t_type>(0)
107 , dot(-s, location), dot(-u, location), dot(f, location), cast<t_type>(1));
108 };
109 static constexpr Matrix<t_type> RotationMatrix(const Vector<3, t_type>& lookat, const Vector<3, t_type>& up)
110 {
111 const Vector<3, t_type> f = lookat.template normalized<t_type>();
112 Vector<3, t_type> s = cross(f, up).template normalized<t_type>();
113 if (isNaN(s))
114 s = Vector<3, t_type>(0, 1, 0);
115 Vector<3, t_type> u = cross(s, f).template normalized<t_type>();
116 if (isNaN(u))
117 u = Vector<3, t_type>(-1, 0, 0);
118 return Matrix<t_type>(
119 s[X], u[X], -f[X], cast<t_type>(0)
120 , s[Y], u[Y], -f[Y], cast<t_type>(0)
121 , s[Z], u[Z], -f[Z], cast<t_type>(0)
122 , 0, 0, 0, cast<t_type>(1));
123 };
124
125 constexpr static Matrix<t_type> Perspective(t_type fovy, t_type aspect, t_type near_plane, t_type far_plane)
126 {
127 return Matrix<t_type>(
128 fovy / aspect, 0, 0, 0
129 , 0, fovy, 0, 0
130 , 0, 0, (far_plane + near_plane) / (near_plane - far_plane), (2 * near_plane * far_plane) / (near_plane - far_plane)
131 , 0, 0, -1, 0);
132 }
133 };
134
135}
136
Stores an angle in an optimized format.
Definition StringStream.h:352
Definition Matrix.hpp:173
An element of a vector space. An element of the real coordinate space Rn Basis vector,...
Definition Vector.hpp:62
Definition ACIColor.h:37
std::enable_if<!ObjectInfo< t_type >::Float, fltp08 >::type tan(const Angle< t_type > &angle)
Definition AngleFunctions.h:182
t_type dot(const Vector< t_dims, t_type > &v1, const Vector< t_dims, t_type > &v2)
Definition VectorFunctions.hpp:1096
constexpr Vector< 1, t_type > cross(const Vector< 1, t_type > &, const Vector< 1, t_type > &)
Definition VectorFunctions.hpp:954
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514
constexpr bool isNaN(const t_type &value)
Query if 'value' is valid or invalid.
Definition BaseFunctions.hpp:200
@ Y
Definition BaseValues.hpp:202
@ X
Definition BaseValues.hpp:200
@ Z
Definition BaseValues.hpp:204
Definition MatrixDefinitions.h:43
static constexpr Matrix< t_type > RotationMatrix(const Vector< 3, t_type > &lookat, const Vector< 3, t_type > &up)
Definition MatrixDefinitions.h:109
static constexpr Matrix< t_type > OrthoFrustum(const Vector< 2, t_type > &span, t_type n, t_type f)
Definition MatrixDefinitions.h:74
static constexpr Matrix< t_type > Perspective(t_type fovy, t_type aspect, t_type near_plane, t_type far_plane)
Definition MatrixDefinitions.h:125
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:58
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:94
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:44
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:67