vsg 1.1.10
VulkanSceneGraph library
Loading...
Searching...
No Matches
transform.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
13</editor-fold> */
14
15#include <vsg/maths/common.h>
16
17namespace vsg
18{
20 template<typename T>
21 constexpr t_mat4<T> rotate(const t_quat<T>& q)
22 {
23 T qxx(q.x * q.x);
24 T qyy(q.y * q.y);
25 T qzz(q.z * q.z);
26 T qxy(q.x * q.y);
27 T qxz(q.x * q.z);
28 T qyz(q.y * q.z);
29 T qwx(q.w * q.x);
30 T qwy(q.w * q.y);
31 T qwz(q.w * q.z);
32
33 T zero(0.0);
34 T one(1.0);
35 T two(2.0);
36
37 return t_mat4<T>(one - two * (qyy + qzz), two * (qxy + qwz), two * (qxz - qwy), zero,
38 two * (qxy - qwz), one - two * (qxx + qzz), two * (qyz + qwx), zero,
39 two * (qxz + qwy), two * (qyz - qwx), one - two * (qxx + qyy), zero,
40 zero, zero, zero, 1.0);
41 }
42
44 template<typename T>
45 t_mat4<T> rotate(T angle_radians, T x, T y, T z)
46 {
47 const T c = std::cos(angle_radians);
48 const T s = std::sin(angle_radians);
49 const T one_minus_c = 1 - c;
50 return t_mat4<T>(x * x * one_minus_c + c, y * x * one_minus_c + z * s, x * z * one_minus_c - y * s, 0,
51 x * y * one_minus_c - z * s, y * y * one_minus_c + c, y * z * one_minus_c + x * s, 0,
52 x * z * one_minus_c + y * s, y * z * one_minus_c - x * s, z * z * one_minus_c + c, 0,
53 0, 0, 0, 1);
54 }
55
57 template<typename T>
58 t_mat4<T> rotate(T angle_radians, const t_vec3<T>& v)
59 {
60 return rotate(angle_radians, v.value[0], v.value[1], v.value[2]);
61 }
62
64 template<typename T>
65 constexpr t_mat4<T> translate(T x, T y, T z)
66 {
67 return t_mat4<T>(1, 0, 0, 0,
68 0, 1, 0, 0,
69 0, 0, 1, 0,
70 x, y, z, 1);
71 }
72
74 template<typename T>
75 constexpr t_mat4<T> translate(const t_vec3<T>& v)
76 {
77 return translate(v.value[0], v.value[1], v.value[2]);
78 }
79
81 template<typename T>
82 constexpr t_mat4<T> scale(T s)
83 {
84 return t_mat4<T>(s, 0, 0, 0,
85 0, s, 0, 0,
86 0, 0, s, 0,
87 0, 0, 0, 1);
88 }
89
91 template<typename T>
92 constexpr t_mat4<T> scale(T sx, T sy, T sz)
93 {
94 return t_mat4<T>(sx, 0, 0, 0,
95 0, sy, 0, 0,
96 0, 0, sz, 0,
97 0, 0, 0, 1);
98 }
99
101 template<typename T>
102 constexpr t_mat4<T> scale(const t_vec3<T>& v)
103 {
104 return scale(v.value[0], v.value[1], v.value[2]);
105 }
106
108 template<typename T>
109 constexpr t_mat3<T> transpose(const t_mat3<T>& m)
110 {
111 return t_mat3<T>(m[0][0], m[1][0], m[2][0],
112 m[0][1], m[1][1], m[2][1],
113 m[0][2], m[1][2], m[2][2]);
114 }
115
117 template<typename T>
118 constexpr t_mat4<T> transpose(const t_mat4<T>& m)
119 {
120 return t_mat4<T>(m[0][0], m[1][0], m[2][0], m[3][0],
121 m[0][1], m[1][1], m[2][1], m[3][1],
122 m[0][2], m[1][2], m[2][2], m[3][2],
123 m[0][3], m[1][3], m[2][3], m[3][3]);
124 }
125
130 //. https://vincent-p.github.io/posts/vulkan_perspective_matrix/
131 template<typename T>
132 constexpr t_mat4<T> perspective(T fovy_radians, T aspectRatio, T zNear, T zFar)
133 {
134 T f = static_cast<T>(1.0 / std::tan(fovy_radians * 0.5));
135 T r = static_cast<T>(1.0 / (zFar - zNear));
136 return t_mat4<T>(f / aspectRatio, 0, 0, 0,
137 0, -f, 0, 0,
138 0, 0, zNear * r, -1,
139 0, 0, (zFar * zNear) * r, 0);
140 }
141
143 template<typename T>
144 constexpr t_mat4<T> perspective(T left, T right, T bottom, T top, T zNear, T zFar)
145 {
146 return t_mat4<T>(2.0 * zNear / (right - left), 0.0, 0.0, 0.0,
147 0.0, 2.0 * zNear / (bottom - top), 0.0, 0.0,
148 (right + left) / (right - left), (bottom + top) / (bottom - top), zNear / (zFar - zNear), -1.0,
149 0.0, 0.0, zNear * zFar / (zFar - zNear), 0.0);
150 }
151
153 template<typename T>
154 constexpr t_mat4<T> orthographic(T left, T right, T bottom, T top, T zNear, T zFar)
155 {
156 return t_mat4<T>(2.0 / (right - left), 0.0, 0.0, 0.0,
157 0.0, 2.0 / (bottom - top), 0.0, 0.0,
158 0.0, 0.0, 1.0 / (zFar - zNear), 0.0,
159 -(right + left) / (right - left), -(bottom + top) / (bottom - top), zFar / (zFar - zNear), 1.0);
160 }
161
162 template<typename T>
163 constexpr t_mat4<T> lookAt(const t_vec3<T>& eye, const t_vec3<T>& center, const t_vec3<T>& up)
164 {
165 using vec_type = t_vec3<T>;
166
167 vec_type forward = normalize(center - eye);
168 vec_type up_normal = normalize(up);
169 vec_type side = normalize(cross(forward, up_normal));
170 vec_type u = normalize(cross(side, forward));
171
172 return t_mat4<T>(side[0], u[0], -forward[0], 0,
173 side[1], u[1], -forward[1], 0,
174 side[2], u[2], -forward[2], 0,
175 0, 0, 0, 1) *
176 vsg::translate(-eye.x, -eye.y, -eye.z);
177 }
178
179 template<typename T>
180 constexpr t_mat4<T> computeBillboardMatrix(const t_vec3<T>& centerEye, T autoscaleDistance)
181 {
182 auto distance = -centerEye.z;
183
184 auto scale = (distance < autoscaleDistance) ? distance / autoscaleDistance : 1.0;
185 t_mat4<T> mS(scale, 0.0, 0.0, 0.0,
186 0.0, scale, 0.0, 0.0,
187 0.0, 0.0, scale, 0.0,
188 0.0, 0.0, 0.0, 1.0);
189
190 t_mat4<T> mT(1.0, 0.0, 0.0, 0.0,
191 0.0, 1.0, 0.0, 0.0,
192 0.0, 0.0, 1.0, 0.0,
193 centerEye.x, centerEye.y, centerEye.z, 1.0);
194
195 return mT * mS;
196 }
197
199 enum class CoordinateConvention
200 {
201 NO_PREFERENCE,
202 X_UP, // x up, y left/west, z out/south
203 Y_UP, // x right/east, y up, z out/south
204 Z_UP // x right/east, y forward/north, z up
205 };
206
209 extern VSG_DECLSPEC bool transform(CoordinateConvention source, CoordinateConvention destination, dmat4& matrix);
210
212 extern VSG_DECLSPEC mat3 inverse_3x3(const mat4& m);
213
215 extern VSG_DECLSPEC dmat3 inverse_3x3(const dmat4& m);
216
218 extern VSG_DECLSPEC mat4 inverse_4x3(const mat4& m);
219
221 extern VSG_DECLSPEC dmat4 inverse_4x3(const dmat4& m);
222
224 extern VSG_DECLSPEC mat4 inverse_4x4(const mat4& m);
225
227 extern VSG_DECLSPEC dmat4 inverse_4x4(const dmat4& m);
228
230 extern VSG_DECLSPEC mat4 inverse(const mat4& m);
231
233 extern VSG_DECLSPEC dmat4 inverse(const dmat4& m);
234
236 extern VSG_DECLSPEC float determinant(const mat4& m);
237
239 extern VSG_DECLSPEC double determinant(const dmat4& m);
240
244 extern VSG_DECLSPEC bool decompose(const mat4& m, vec3& translation, quat& rotation, vec3& scale);
245
249 extern VSG_DECLSPEC bool decompose(const dmat4& m, dvec3& translation, dquat& rotation, dvec3& scale);
250
254 extern VSG_DECLSPEC bool decompose(const ldmat4& m, ldvec3& translation, ldquat& rotation, ldvec3& scale);
255
257 extern VSG_DECLSPEC sphere computeFrustumBound(const mat4& m);
258
260 extern VSG_DECLSPEC dsphere computeFrustumBound(const dmat4& m);
261
264 struct VSG_DECLSPEC ComputeTransform : public ConstVisitor
265 {
266 dvec3 origin;
267 dmat4 matrix;
268
269 void apply(const Transform& transform) override;
270 void apply(const MatrixTransform& mt) override;
271 void apply(const CoordinateFrame& cf) override;
272 void apply(const Camera& camera) override;
273 };
274
276 template<typename T>
277 dmat4 computeTransform(const T& nodePath)
278 {
279 return visit<ComputeTransform>(nodePath).matrix;
280 }
281
282} // namespace vsg
Definition Camera.h:27
CoordinateFrame provides support for astronomically large coordinates.
Definition CoordinateFrame.h:22
Definition MatrixTransform.h:24
Transform node is a pure virtual base class for positioning/scaling/rotating subgraphs.
Definition Transform.h:22
Definition transform.h:265
t_mat3 template class that represents a 3x3 matrix.
Definition mat3.h:23
t_mat4 template class that represents a 4x4 matrix.
Definition mat4.h:25
t_quat template class that represents a quaternion
Definition quat.h:35
t_vec3 template class that represents a 3D vector
Definition vec3.h:34