2 #include "oxygine-include.h" 24 T _11, T _12, T _13, T _14,
25 T _21, T _22, T _23, T _24,
26 T _31, T _32, T _33, T _34,
27 T _41, T _42, T _43, T _44);
30 operator const T* ()
const;
70 static MatrixT& perspectiveFovLH(
MatrixT& out, T fovy, T aspect, T znear, T zfar);
71 static MatrixT& perspectiveFovRH(
MatrixT& out, T fovy, T aspect, T znear, T zfar);
73 static MatrixT& perspectiveOffCenterLH(
MatrixT& out, T left, T right, T bottom, T top, T znear, T zfar);
74 static MatrixT& perspectiveOffCenterRH(
MatrixT& out, T left, T right, T bottom, T top, T znear, T zfar);
76 static MatrixT& orthoLH(
MatrixT& out, T width, T height, T zNear, T zFar);
110 MatrixT<T>::MatrixT(
const T* p)
112 for (
int i = 0; i < 16; ++i)
117 MatrixT<T>::MatrixT(T _11, T _12, T _13, T _14,
118 T _21, T _22, T _23, T _24,
119 T _31, T _32, T _33, T _34,
120 T _41, T _42, T _43, T _44):
121 m11(_11), m12(_12), m13(_13), m14(_14),
122 m21(_21), m22(_22), m23(_23), m24(_24),
123 m31(_31), m32(_32), m33(_33), m34(_34),
124 m41(_41), m42(_42), m43(_43), m44(_44)
130 void MatrixT<T>::identity()
132 vector4* rows = (vector4*)ml;
133 rows[0] = VectorT4<T>(1, 0, 0, 0);
134 rows[1] = VectorT4<T>(0, 1, 0, 0);
135 rows[2] = VectorT4<T>(0, 0, 1, 0);
136 rows[3] = VectorT4<T>(0, 0, 0, 1);
140 void MatrixT<T>::inverse()
142 inverse(
this, *
this);
149 MatrixT<T> MatrixT<T>::inversed()
const 157 MatrixT<T> MatrixT<T>::ident()
166 void MatrixT<T>::transpose()
168 transpose(*
this, *
this);
173 MatrixT<T> MatrixT<T>::transposed()
const 181 void MatrixT<T>::translate(
const vector3& t)
184 matrix::translation(tm, t);
189 void MatrixT<T>::scale(
const vector3& s)
192 matrix::scaling(sm, s);
197 void MatrixT<T>::buildSRT(
const vector3& s, T angle,
const vector3& t)
200 matrix::scaling(&sm, s);
201 matrix::rotationY(&rm, angle);
202 matrix::translation(&tm, t);
204 *
this = sm * rm * tm;
209 VectorT3<T> MatrixT<T>::transformVec3(
const vector3& v)
const 212 transformVec3(out, v, *
this);
217 VectorT4<T> MatrixT<T>::transformVec4(
const vector4& v)
const 220 transformVec4(out, v, *
this);
225 VectorT3<T> MatrixT<T>::getTranslation()
const 227 vector4* rows = (vector4*)ml;
228 return vector3(rows[3].x, rows[3].y, rows[3].z);
231 #define rcDot4(r, c) (mrows[r].x * nrows[0][c] + mrows[r].y * nrows[1][c] + mrows[r].z * nrows[2][c] + mrows[r].w * nrows[3][c]) 234 MatrixT<T> MatrixT<T>::operator *(
const MatrixT<T>& n)
const 236 const vector4* mrows = (vector4*)ml;
237 const vector4* nrows = (vector4*)n.ml;
240 rcDot4(0, 0), rcDot4(0, 1), rcDot4(0, 2), rcDot4(0, 3),
241 rcDot4(1, 0), rcDot4(1, 1), rcDot4(1, 2), rcDot4(1, 3),
242 rcDot4(2, 0), rcDot4(2, 1), rcDot4(2, 2), rcDot4(2, 3),
243 rcDot4(3, 0), rcDot4(3, 1), rcDot4(3, 2), rcDot4(3, 3)
249 VectorT4<T>& MatrixT<T>::operator [](
int row)
251 vector4* rows = (vector4*)ml;
257 MatrixT<T>::operator T* ()
const 263 MatrixT<T>::operator
const T* ()
const 269 inline MatrixT<T>& MatrixT<T>::lookAtLH(MatrixT& out,
274 vector3 x, y, z, temp;
275 vector3::normalize(z, At - Eye);
276 vector3::normalize(x, vector3::cross(temp, Up, z));
277 vector3::normalize(y, vector3::cross(temp, z, x));
283 -vector3::dot(x, Eye), -vector3::dot(y, Eye), -vector3::dot(z, Eye), 1);
289 inline MatrixT<T>& MatrixT<T>::lookAtRH(MatrixT& out,
294 vector3 x, y, z, temp;
295 vector3::normalize(z, Eye - At);
296 vector3::normalize(x, vector3::cross(temp, Up, z));
297 vector3::normalize(y, vector3::cross(temp, z, x));
303 -vector3::dot(x, Eye), -vector3::dot(y, Eye), -vector3::dot(z, Eye), 1);
309 inline MatrixT<T>& MatrixT<T>::rotationX(MatrixT& out, T angle)
311 T s = scalar::sin(angle);
312 T c = scalar::cos(angle);
323 inline MatrixT<T>& MatrixT<T>::rotationY(MatrixT& out, T angle)
325 T s = scalar::sin(angle);
326 T c = scalar::cos(angle);
336 inline MatrixT<T>& MatrixT<T>::rotationZ(MatrixT& out, T angle)
338 T s = scalar::sin(angle);
339 T c = scalar::cos(angle);
351 inline MatrixT<T>& MatrixT<T>::translation(MatrixT& out,
const vector3& v)
362 inline MatrixT<T>& MatrixT<T>::scaling(MatrixT& out,
const vector3& v)
374 inline MatrixT<T>& MatrixT<T>::perspectiveFovLH(MatrixT& out, T fovy, T aspect, T zNear, T zFar)
376 float y = T(1) / scalar::tan(T(0.5) * fovy);
377 float x = y / aspect;
382 0.0f, 0.0f, zFar / (zFar - zNear), 1.0f,
383 0.0f, 0.0f, -zFar * zNear / (zFar - zNear), 0.0f);
388 inline MatrixT<T>& MatrixT<T>::perspectiveFovRH(MatrixT& out, T fovy, T aspect, T zNear, T zFar)
390 float y = T(1) / scalar::tan(T(0.5) * fovy);
391 float x = y / aspect;
396 0.0f, 0.0f, zFar / (zFar - zNear), -1.0f,
397 0.0f, 0.0f, zFar * zNear / (zFar - zNear), 0.0f);
402 inline MatrixT<T>& MatrixT<T>::perspectiveOffCenterLH(MatrixT& out, T left, T right, T bottom, T top, T znearPlane, T zfarPlane)
405 2 * znearPlane / (right - left), 0, 0, 0,
406 0, 2 * znearPlane / (top - bottom), 0, 0,
407 (left + right) / (left - right), (top + bottom) / (bottom - top), zfarPlane / (zfarPlane - znearPlane), 1,
408 0, 0, znearPlane * zfarPlane / (znearPlane - zfarPlane), 0);
413 inline MatrixT<T>& MatrixT<T>::perspectiveOffCenterRH(MatrixT& out, T left, T right, T bottom, T top, T znearPlane, T zfarPlane)
416 2 * znearPlane / (right - left), 0, 0, 0,
417 0, 2 * znearPlane / (top - bottom), 0, 0,
418 (left + right) / (right - left), (top + bottom) / (top - bottom), zfarPlane / (znearPlane - zfarPlane), -1,
419 0, 0, znearPlane * zfarPlane / (znearPlane - zfarPlane), 0);
424 inline MatrixT<T>& MatrixT<T>::orthoLH(MatrixT& out, T w, T h, T zNear, T zFar)
429 0, 0, T(1) / (zFar - zNear), 0,
430 0, 0, zNear / (zNear - zFar), 1);
435 inline MatrixT<T>& MatrixT<T>::inverse(MatrixT& out,
const MatrixT& mat)
437 T a9 = mat.m31 * mat.m42 - mat.m32 * mat.m41;
438 T aa = mat.m31 * mat.m43 - mat.m33 * mat.m41;
439 T a8 = mat.m31 * mat.m44 - mat.m34 * mat.m41;
441 T a2 = mat.m32 * mat.m43 - mat.m33 * mat.m42;
442 T a5 = mat.m32 * mat.m44 - mat.m34 * mat.m42;
443 T a3 = mat.m33 * mat.m44 - mat.m34 * mat.m43;
445 T t1 = mat.m22 * a3 - mat.m23 * a5 + mat.m24 * a2;
446 T t2 = mat.m21 * a3 - mat.m23 * a8 + mat.m24 * aa;
447 T t3 = mat.m21 * a5 - mat.m22 * a8 + mat.m24 * a9;
448 T t4 = mat.m21 * a2 - mat.m22 * aa + mat.m23 * a9;
450 T det = mat.m11 * t1 - mat.m12 * t2 + mat.m13 * t3 - mat.m14 * t4;
452 if (scalar::abs(det) < T(0.0001))
460 T a0 = mat.m21 * mat.m32 - mat.m22 * mat.m31;
461 T af = mat.m21 * mat.m42 - mat.m22 * mat.m41;
462 T ac = mat.m22 * mat.m33 - mat.m23 * mat.m32;
463 T a6 = mat.m22 * mat.m43 - mat.m23 * mat.m42;
465 T ab = mat.m23 * mat.m34 - mat.m24 * mat.m33;
466 T a7 = mat.m23 * mat.m44 - mat.m24 * mat.m43;
467 T ae = mat.m21 * mat.m34 - mat.m24 * mat.m31;
468 T ad = mat.m21 * mat.m44 - mat.m24 * mat.m41;
470 T b0 = mat.m22 * mat.m34 - mat.m24 * mat.m32;
471 T b1 = mat.m21 * mat.m43 - mat.m23 * mat.m41;
472 T b2 = mat.m21 * mat.m33 - mat.m23 * mat.m31;
473 T b3 = mat.m22 * mat.m44 - mat.m24 * mat.m42;
477 -det * (mat.m12 * a3 - mat.m13 * a5 + mat.m14 * a2),
478 det * (mat.m12 * a7 - mat.m13 * b3 + mat.m14 * a6),
479 -det * (mat.m12 * ab - mat.m13 * b0 + mat.m14 * ac),
482 det * (mat.m11 * a3 - mat.m13 * a8 + mat.m14 * aa),
483 -det * (mat.m11 * a7 - mat.m13 * ad + mat.m14 * b1),
484 det * (mat.m11 * ab - mat.m13 * ae + mat.m14 * b2),
487 -det * (mat.m11 * a5 - mat.m12 * a8 + mat.m14 * a9),
488 det * (mat.m11 * b3 - mat.m12 * ad + mat.m14 * af),
489 -det * (mat.m11 * b0 - mat.m12 * ae + mat.m14 * a0),
492 det * (mat.m11 * a2 - mat.m12 * aa + mat.m13 * a9),
493 -det * (mat.m11 * a6 - mat.m12 * b1 + mat.m13 * af),
494 det * (mat.m11 * ac - mat.m12 * b2 + mat.m13 * a0));
499 inline MatrixT<T>& MatrixT<T>::transpose(MatrixT& out,
const MatrixT& mat)
501 vector4* rows = (vector4*)mat.ml;
503 rows[0].x, rows[1].x, rows[2].x, rows[3].x,
504 rows[0].y, rows[1].y, rows[2].y, rows[3].y,
505 rows[0].z, rows[1].z, rows[2].z, rows[3].z,
506 rows[0].w, rows[1].w, rows[2].w, rows[3].w);
511 inline VectorT3<T>& MatrixT<T>::transformVec3(VectorT3<T>& out,
const vector3& in,
const MatrixT& mat)
514 in.x * mat.m11 + in.y * mat.m21 + in.z * mat.m31 + mat.m41,
515 in.x * mat.m12 + in.y * mat.m22 + in.z * mat.m32 + mat.m42,
516 in.x * mat.m13 + in.y * mat.m23 + in.z * mat.m33 + mat.m43);
521 inline VectorT4<T>& MatrixT<T>::transformVec4(VectorT4<T>& out,
const vector4& in,
const MatrixT& mat)
524 in.x * mat.m11 + in.y * mat.m21 + in.z * mat.m31 + in.w * mat.m41,
525 in.x * mat.m12 + in.y * mat.m22 + in.z * mat.m32 + in.w * mat.m42,
526 in.x * mat.m13 + in.y * mat.m23 + in.z * mat.m33 + in.w * mat.m43,
527 in.x * mat.m14 + in.y * mat.m24 + in.z * mat.m34 + in.w * mat.m44);
532 typedef MatrixT<float> Matrix;
533 typedef MatrixT<double> MatrixD;
–oxgl-end–!
Definition: Actor.h:14