diff --git a/glm/detail/constants.hpp b/glm/detail/constants.hpp index 04f70a0..c10c32e 100644 --- a/glm/detail/constants.hpp +++ b/glm/detail/constants.hpp @@ -11,7 +11,9 @@ #include "setup.hpp" -namespace glm{ + + +namespace glm { // // class constants diff --git a/glm/detail/functional.hpp b/glm/detail/functional.hpp index c68d251..2e549e7 100644 --- a/glm/detail/functional.hpp +++ b/glm/detail/functional.hpp @@ -9,11 +9,11 @@ #ifndef GLM_FUNCTIONAL_HPP_20211116111721 #define GLM_FUNCTIONAL_HPP_20211116111721 -#include "../vec.hpp" +#include "../vector.hpp" -namespace glm{ +namespace glm { diff --git a/glm/detail/functions.hpp b/glm/detail/functions.hpp index 6970c31..317f19f 100644 --- a/glm/detail/functions.hpp +++ b/glm/detail/functions.hpp @@ -28,7 +28,19 @@ matrixCompMult -namespace glm{ +namespace glm { + +template +GLM_API T min(const T& x, const T& y) +{ + return x < y ? x : y; +} + +template +GLM_API T max(const T& x, const T& y) +{ + return x < y ? y : x; +} template GLM_API T abs(const T& value) @@ -57,13 +69,13 @@ GLM_API T exp(const T& value) template GLM_API T exp2(const T& value) { - static_cast(std::exp2(static_cast(value))); + return static_cast(std::exp2(static_cast(value))); } template GLM_API T floor(const T& value) { - static_cast(std::floor(static_cast(value))); + return static_cast(std::floor(static_cast(value))); } // 取小数部分 @@ -73,24 +85,12 @@ GLM_API T fract(const T& value) return value - floor(value); } -template -GLM_API T max(const T& x, const T& y) -{ - return x < y ? y : x; -} - template GLM_API T mid(const T& x, const T& y) { return mix(x, y, constants::half); } -template -GLM_API T min(const T& x, const T& y) -{ - return x < y ? x : y; -} - template GLM_API T mix(const T& x, const T& y, A a) { @@ -117,9 +117,9 @@ GLM_API T pow(const T& x, const T& y) } template -GLM_API int round(const T& value) +GLM_API T round(const T& value) { - static_cast(std::round(static_cast(value))); + return value + (value < T(0) ? T(-0.5) : (0.5)); } template @@ -223,7 +223,7 @@ GLM_API vec pow(const vec& x, const vec& y) } template -GLM_API int round(const vec& v) +GLM_API vec round(const vec& v) { return compute(round, v); } diff --git a/glm/detail/logical.hpp b/glm/detail/logical.hpp index d775be3..d74ba80 100644 --- a/glm/detail/logical.hpp +++ b/glm/detail/logical.hpp @@ -9,13 +9,13 @@ #ifndef GLM_LOGICAL_HPP_20211119132746 #define GLM_LOGICAL_HPP_20211119132746 -#include "../vec.hpp" -#include "functions.hpp" #include "functional.hpp" +#include "functions.hpp" +#include "type_traits.hpp" -namespace glm{ +namespace glm { // // Logical operation diff --git a/glm/detail/setup.hpp b/glm/detail/setup.hpp index e140044..b6d6020 100644 --- a/glm/detail/setup.hpp +++ b/glm/detail/setup.hpp @@ -73,7 +73,7 @@ -namespace glm{ +namespace glm { typedef size_t length_t; diff --git a/glm/detail/type_traits.hpp b/glm/detail/type_traits.hpp index 093efbf..200c01c 100644 --- a/glm/detail/type_traits.hpp +++ b/glm/detail/type_traits.hpp @@ -13,7 +13,7 @@ -namespace glm{ +namespace glm { template diff --git a/glm/geometric.hpp b/glm/geometric.hpp index 8bc17e1..d3ddad2 100644 --- a/glm/geometric.hpp +++ b/glm/geometric.hpp @@ -12,7 +12,8 @@ #include "detail/functions.hpp" #include "detail/functional.hpp" #include "trigonometric.hpp" -#include "vec.hpp" + + namespace glm{ diff --git a/glm/glm.hpp b/glm/glm.hpp index 0013543..77c5359 100644 --- a/glm/glm.hpp +++ b/glm/glm.hpp @@ -10,8 +10,8 @@ #define GLM_HPP_20211115201400 #include "detail/setup.hpp" -#include "detail/functions.hpp" #include "detail/functional.hpp" +#include "detail/functions.hpp" #include "detail/logical.hpp" #include "detail/type_traits.hpp" @@ -19,12 +19,20 @@ #include "vec3.hpp" #include "vec4.hpp" -#include "mat2.hpp" -#include "mat3.hpp" -#include "mat4.hpp" +#include "mat2x2.hpp" +#include "mat2x3.hpp" +#include "mat2x4.hpp" +#include "mat3x2.hpp" +#include "mat3x3.hpp" +#include "mat3x4.hpp" +#include "mat4x2.hpp" +#include "mat4x3.hpp" +#include "mat4x4.hpp" #include "geometric.hpp" #include "trigonometric.hpp" +#include "gtc/matrix_determinant.hpp" +#include "gtc/matrix_inverse.hpp" #include "gtc/operator.hpp" #include "gtx/io.hpp" #include "gtx/string_cast.hpp" diff --git a/glm/gtc/matrix_determinant.hpp b/glm/gtc/matrix_determinant.hpp new file mode 100644 index 0000000..87e86e7 --- /dev/null +++ b/glm/gtc/matrix_determinant.hpp @@ -0,0 +1,66 @@ +/* + Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) + + glm.matrix_determinant.hpp + + 2022-02-13 23:01:45 + +*/ +#ifndef GLM_MATRIX_DETERMINANT_HPP_20220213230145 +#define GLM_MATRIX_DETERMINANT_HPP_20220213230145 + +#include "../matrix.hpp" + + + +namespace glm { + +// +// bool mat2_determinant( in mat2, out T ) +// + +template +T determinant(const mat<2, 2, T>& m) +{ + return m[0][0] * m[1][1] - m[0][1] * m[1][0]; +} + +// +// bool mat3_determinant( in mat3, out T ) +// + +template +GLM_API T determinant(const mat<3, 3, T>& m) +{ + return + + m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]) + - m[1][0] * (m[0][1] * m[2][2] - m[2][1] * m[0][2]) + + m[2][0] * (m[0][1] * m[1][2] - m[1][1] * m[0][2]); +} + +// +// bool mat4_determinant( in mat4, out T ) +// + +template +GLM_API T determinant(const mat<4, 4, T>& m) +{ + T a0 = m[0][0] * m[1][1] - m[0][1] * m[1][0]; + T a1 = m[0][0] * m[1][2] - m[0][2] * m[1][0]; + T a2 = m[0][0] * m[1][3] - m[0][3] * m[1][0]; + T a3 = m[0][1] * m[1][2] - m[0][2] * m[1][1]; + T a4 = m[0][1] * m[1][3] - m[0][3] * m[1][1]; + T a5 = m[0][2] * m[1][3] - m[0][3] * m[1][2]; + T b0 = m[2][0] * m[3][1] - m[2][1] * m[3][0]; + T b1 = m[2][0] * m[3][2] - m[2][2] * m[3][0]; + T b2 = m[2][0] * m[3][3] - m[2][3] * m[3][0]; + T b3 = m[2][1] * m[3][2] - m[2][2] * m[3][1]; + T b4 = m[2][1] * m[3][3] - m[2][3] * m[3][1]; + T b5 = m[2][2] * m[3][3] - m[2][3] * m[3][2]; + + return a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0; +} + +}// end namespace glm + +#endif// GLM_MATRIX_DETERMINANT_HPP_20220213230145 diff --git a/glm/gtc/matrix_inverse.hpp b/glm/gtc/matrix_inverse.hpp new file mode 100644 index 0000000..3df26d9 --- /dev/null +++ b/glm/gtc/matrix_inverse.hpp @@ -0,0 +1,128 @@ +/* + Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) + + matrix_inverse.hpp + + 2022-02-13 23:00:38 + +*/ +#ifndef GLM_MATRIX_INVERSE_HPP_20220213230038 +#define GLM_MATRIX_INVERSE_HPP_20220213230038 + +#include "matrix_determinant.hpp" + + + +namespace glm { + +// +// mat2 inverse( in mat2 ) +// + +template +GLM_API mat<2, 2, T> inverse(const mat<2, 2, T>& m) +{ + T d = mat2_determinant(m); + + if (abs(d) < constants::epsilon) { + return m; + } + + d = constants::one / d; + + mat<2, 2, T> product; + product[0][0] = + m[1][1] * d; + product[0][1] = - m[0][1] * d; + product[1][0] = - m[1][0] * d; + product[1][1] = + m[0][0] * d; + + return product; +} + + +// +// mat3 inverse( in mat3 ) +// + +template +GLM_API mat<3, 3, T> inverse(const mat<3, 3, T>& m) +{ + T d = determinant(m); + + if (abs(d) < constants::epsilon) { + return m; + } + + d = constants::one / d; + + mat<3, 3, T> product; + product[0][0] = +(m[1][1] * m[2][2] - m[2][1] * m[1][2]) * d; + product[0][1] = -(m[0][1] * m[2][2] - m[2][1] * m[0][2]) * d; + product[0][2] = +(m[0][1] * m[1][2] - m[1][1] * m[0][2]) * d; + product[1][0] = -(m[1][0] * m[2][2] - m[2][0] * m[1][2]) * d; + product[1][1] = +(m[0][0] * m[2][2] - m[2][0] * m[0][2]) * d; + product[1][2] = -(m[0][0] * m[1][2] - m[1][0] * m[0][2]) * d; + product[2][0] = +(m[1][0] * m[2][1] - m[2][0] * m[1][1]) * d; + product[2][1] = -(m[0][0] * m[2][1] - m[2][0] * m[0][1]) * d; + product[2][2] = +(m[0][0] * m[1][1] - m[1][0] * m[0][1]) * d; + + return product; +} + +// +// mat4 inverse( in mat4 ) +// + +template +GLM_API mat<4, 4, T> inverse(const mat<4, 4, T>& m) +{ + T a0 = m[0][0] * m[1][1] - m[0][1] * m[1][0]; + T a1 = m[0][0] * m[1][2] - m[0][2] * m[1][0]; + T a2 = m[0][0] * m[1][3] - m[0][3] * m[1][0]; + T a3 = m[0][1] * m[1][2] - m[0][2] * m[1][1]; + T a4 = m[0][1] * m[1][3] - m[0][3] * m[1][1]; + T a5 = m[0][2] * m[1][3] - m[0][3] * m[1][2]; + T b0 = m[2][0] * m[3][1] - m[2][1] * m[3][0]; + T b1 = m[2][0] * m[3][2] - m[2][2] * m[3][0]; + T b2 = m[2][0] * m[3][3] - m[2][3] * m[3][0]; + T b3 = m[2][1] * m[3][2] - m[2][2] * m[3][1]; + T b4 = m[2][1] * m[3][3] - m[2][3] * m[3][1]; + T b5 = m[2][2] * m[3][3] - m[2][3] * m[3][2]; + + // calculate the determinant. + T d = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0; + + if (abs(d) < constants::epsilon) { + return m; + } + + d = constants::one / d; + + mat<4, 4, T> product; + product[0][0] = (+ m[1][1] * b5 - m[1][2] * b4 + m[1][3] * b3) * d; + product[0][1] = (- m[0][1] * b5 + m[0][2] * b4 - m[0][3] * b3) * d; + product[0][2] = (+ m[3][1] * a5 - m[3][2] * a4 + m[3][3] * a3) * d; + product[0][3] = (- m[2][1] * a5 + m[2][2] * a4 - m[2][3] * a3) * d; + + product[1][0] = (- m[1][0] * b5 + m[1][2] * b2 - m[1][3] * b1) * d; + product[1][1] = (+ m[0][0] * b5 - m[0][2] * b2 + m[0][3] * b1) * d; + product[1][2] = (- m[3][0] * a5 + m[3][2] * a2 - m[3][3] * a1) * d; + product[1][3] = (+ m[2][0] * a5 - m[2][2] * a2 + m[2][3] * a1) * d; + + product[2][0] = (+ m[1][0] * b4 - m[1][1] * b2 + m[1][3] * b0) * d; + product[2][1] = (- m[0][0] * b4 + m[0][1] * b2 - m[0][3] * b0) * d; + product[2][2] = (+ m[3][0] * a4 - m[3][1] * a2 + m[3][3] * a0) * d; + product[2][3] = (- m[2][0] * a4 + m[2][1] * a2 - m[2][3] * a0) * d; + + product[3][0] = (- m[1][0] * b3 + m[1][1] * b1 - m[1][2] * b0) * d; + product[3][1] = (+ m[0][0] * b3 - m[0][1] * b1 + m[0][2] * b0) * d; + product[3][2] = (- m[3][0] * a3 + m[3][1] * a1 - m[3][2] * a0) * d; + product[3][3] = (+ m[2][0] * a3 - m[2][1] * a1 + m[2][2] * a0) * d; + + return product; +} + + +}// end namespace glm + +#endif// GLM_MATRIX_INVERSE_HPP_20220213230038 diff --git a/glm/gtc/matrix_transform.hpp b/glm/gtc/matrix_transform.hpp index 576586c..a4aede5 100644 --- a/glm/gtc/matrix_transform.hpp +++ b/glm/gtc/matrix_transform.hpp @@ -9,8 +9,7 @@ #ifndef GLM_MATRIX_TRANSFORM_HPP_20211113214455 #define GLM_MATRIX_TRANSFORM_HPP_20211113214455 -#include "../vec.hpp" -#include "../mat.hpp" +#include "../matrix.hpp" #include "../detail/type_traits.hpp" #include "../trigonometric.hpp" @@ -172,7 +171,7 @@ class matrix_transform : public constants template GLM_API genType identity() { - return type_traits::identity(); + return type_traits::identity(); } // diff --git a/glm/gtc/type_ptr.hpp b/glm/gtc/type_ptr.hpp index c651521..1564d17 100644 --- a/glm/gtc/type_ptr.hpp +++ b/glm/gtc/type_ptr.hpp @@ -9,7 +9,7 @@ #ifndef GLM_TYPE_PTR_HPP_20211116004441 #define GLM_TYPE_PTR_HPP_20211116004441 -#include "../mat4.hpp" +#include "../matrix.hpp" diff --git a/glm/gtx/intersect.hpp b/glm/gtx/intersect.hpp new file mode 100644 index 0000000..0b55923 --- /dev/null +++ b/glm/gtx/intersect.hpp @@ -0,0 +1,204 @@ +/* + Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) + + intersect.hpp + + 2022-01-27 01:40:02 + +*/ +#ifndef GLM_INTERSECT_HPP_20220127014002 +#define GLM_INTERSECT_HPP_20220127014002 + +#include "../vector.hpp" + +namespace glm{ + +// +// bool intersectRayPlane( in rayPos, in rayDir, in planeOrg, in planeNormal, out distance ) +// + +template +GLM_API bool intersectRayPlane( + const vec<3, T>& rayPos, + const vec<3, T>& rayDir, + const vec<3, T>& planeOrg, + const vec<3, T>& planeNormal, + T& intersectionDistance +) +{ + T d = glm::dot(rayDir, planeNormal); + + // if dir and planeNormal are not perpendicular + if (abs(d) > constants::epsilon) { + T dist = dot(planeOrg - rayPos, planeNormal) / d; + // allow only intersections + if (dist > constants::zero) { + intersectionDistance = dist; + return true; + } + } + + return false; +} + +// +// bool intersectRayTriangle( in rayPos, in rayDir, in v1, in v2, in v3, out coord2, out distance ) +// + +template +GLM_API bool intersectRayTriangle( + const vec<3, T>& rayPos, // ray position + const vec<3, T>& rayDir, // ray direction + const vec<3, T>& v1, // triangle + const vec<3, T>& v2, + const vec<3, T>& v3, + vec<2, T>& coord, // out coord + T& intersectionDistance // out distance +) +{ + vec<3, T> v21 = v2 - v1; + vec<3, T> v31 = v3 - v1; + + // p + vec<3, T> p = cross(rayDir, v31); + + // determinant + T det = dot(v21, p); + + // keep det > 0, modify dir accordingly + vec<3, T> dir; + if (det > 0) { + dir = rayPos - v1; + } + else { + dir = v1 - rayPos; + det = -det; + } + + // determinant 接近 0 时,表示射线与平面几乎平行 + if (det < constants::epsilon) { + return false; + } + + // 计算 u, 保证 u <= 1 + coord.x = dot(dir, p); + if (coord.x < T(0) || coord.x > det) { + return false; + } + + // q + vec<3, T> q = cross(dir, v21); + + // 计算 v, 保证 u + v <= 1 + coord.y = dot(rayDir, q); + if (coord.y < T(0) || coord.x + coord.y > det) { + return false; + } + + // 计算距离 + intersectionDistance = dot(v31, q); + + // 计算纹理坐标差值 + float inv_det = 1.0f / det; + intersectionDistance *= inv_det; + if (intersectionDistance < constants::epsilon) { + return false; + } + + coord *= inv_det; + + return true; +} + +// +// bool intersectRayTriangle( in rayPos, in rayDir, in v1, in v2, in v3, out coord3, out distance ) +// + +template +GLM_API bool intersectRayTriangle( + const vec<3, T>& rayPos, // ray position + const vec<3, T>& rayDir, // ray direction + const vec<3, T>& v1, // triangle + const vec<3, T>& v2, + const vec<3, T>& v3, + vec<3, T>& coord, // out coord + T& intersectionDistance // out distance +) +{ + vec<2, T> uv; + if (intersectRayTriangle(rayPos, rayDir, v1, v2, v3, uv, intersectionDistance)) { + coord.x = constants::one - uv.x - uv.y; + coord.y = uv.y; + coord.z = uv.x; + return true; + } + return false; +} + +// +// bool intersectRaySphere( in rayPos, in rayDir, in sphereCenter, in sphereRadiusSquered, out distance ) +// + +template +GLM_API bool intersectRaySphere( + const vec<3, T>& rayPos, const vec<3, T>& rayDir, // ray + const vec<3, T>& sphereCenter, T sphereRadiusSquered, // sphere + T& intersectionDistance) // out distance +{ + vec<3, T> dir = sphereCenter - rayPos; + + T a = dot(dir, rayDir); + T d = dot(dir, dir) - a * a; + if (d > sphereRadiusSquered) { + return false; + } + d = sqrt(sphereRadiusSquered - d); + intersectionDistance = a > d + constants::epsilon ? a - d : a + d; + return intersectionDistance > constants::epsilon; +} + +// +// bool intersectRaySphere( in rayPos, in rayDir, in sphereCenter, in sphereRadius, out position, out normal ) +// + +template +GLM_API bool intersectRaySphere( + const vec<3, T>& rayPos, const vec<3, T>& rayDir, // ray + const vec<3, T>& sphereCenter, T sphereRadius, // sphere + vec<3, T>& intersectionPosition, // out position + vec<3, T>& intersectionNormal // out normal +) +{ + T dist; + if (intersectRaySphere(rayPos, rayDir, sphereCenter, sphereRadius * sphereRadius, dist)) { + intersectionPosition = rayPos + rayDir * dist; + intersectionNormal = (intersectionPosition - sphereCenter) / sphereRadius; + return true; + } + return false; +} + +// +// bool intersectRaySphere( in rayPos, in rayDir, in sphereCenter, in sphereRadius, out distance, out position, out normal ) +// + +template +GLM_API bool intersectRaySphere( + const vec<3, T>& rayPos, const vec<3, T>& rayDir, // ray + const vec<3, T>& sphereCenter, T sphereRadius, // sphere + T& intersectionDistance, // out distance + vec<3, T>& intersectionPosition, // out position + vec<3, T>& intersectionNormal // out normal +) +{ + if (intersectRaySphere(rayPos, rayDir, sphereCenter, sphereRadius * sphereRadius, intersectionDistance)) { + intersectionPosition = rayPos + rayDir * intersectionDistance; + intersectionNormal = (intersectionPosition - sphereCenter) / sphereRadius; + return true; + } + return false; +} + +}// end namespace glm + +#endif// GLM_INTERSECT_HPP_20220127014002 diff --git a/glm/gtx/io.hpp b/glm/gtx/io.hpp index ece49d9..ee72c6c 100644 --- a/glm/gtx/io.hpp +++ b/glm/gtx/io.hpp @@ -10,52 +10,60 @@ #define GLM_IO_HPP_20211115201242 #include - -#include "../vec.hpp" -#include "../mat.hpp" +#include "../matrix.hpp" namespace glm{ template -std::ostream& operator <<(std::ostream& os, const vec& m) +std::ostream& operator<<(std::ostream& out, const vec& m) { - os << "{ "; + out << "[ "; for(size_t i=0; i < N; ++i){ - os << std::fixed; - os.width(6); - os.precision(3); - os << m[i]; + out << std::fixed; + out.width(6); + out.precision(3); + out << m[i]; if(i != N - 1){ - os << ", "; + out << ", "; } } - os << " }"; + out << " ]"; - return os; + return out; } template -std::ostream& operator<<(std::ostream& os, const mat<4, 4, T>& m) +std::ostream& operator<<(std::ostream& out, const mat<4, 4, T>& m) { - os << "{" << std::endl; + out << std::endl; for(size_t x = 0; x < m.COLS; ++x){ - //os << "\t"; + if(x == 0) + out << "[["; + else + out << " ["; for(size_t y = 0; y < m.ROWS; ++y){ - os << std::fixed; - os.width(8); - os.precision(3); - os << m[y][x]; - if(y * m.COLS + x < m.length() - 1){ - os << ", "; + out << std::fixed; + out.width(8); + out.precision(3); + out << m[y][x]; + + if(y != m.ROWS - 1) { + out << ","; + } + else { + if(x != m.COLS - 1) { + out << ']' << std::endl; + } + else { + out << "]]" << std::endl; + } } } - os << std::endl; } - os << "}"; - return os; + return out; } }// end namespace glm diff --git a/glm/gtx/normal.hpp b/glm/gtx/normal.hpp index 3816c3c..d0fce77 100644 --- a/glm/gtx/normal.hpp +++ b/glm/gtx/normal.hpp @@ -9,7 +9,7 @@ #ifndef GLM_NORMAL_HPP_20211231171936 #define GLM_NORMAL_HPP_20211231171936 -#include "../vec.hpp" +#include "../vector.hpp" namespace glm{ diff --git a/glm/gtx/rotate_vector.hpp b/glm/gtx/rotate_vector.hpp index a4092fd..666ac6b 100644 --- a/glm/gtx/rotate_vector.hpp +++ b/glm/gtx/rotate_vector.hpp @@ -9,7 +9,7 @@ #ifndef GLM_ROTATE_VECTOR_HPP_20211115232106 #define GLM_ROTATE_VECTOR_HPP_20211115232106 -#include "../vec.hpp" +#include "../vector.hpp" @@ -37,7 +37,7 @@ vec<2, T> rotate( const vec<2, T>& v, T angle ) template vec<3, T> rotate( const vec<3, T>& v, T angle, const vec<3, T>& axis ) { - return rotate(angle, axis) * vec<4, T>(v, constant::one); + return rotate(angle, axis) * vec<4, T>(v, constants::one); } template diff --git a/glm/mat2.hpp b/glm/mat2.hpp deleted file mode 100644 index 04ff50b..0000000 --- a/glm/mat2.hpp +++ /dev/null @@ -1,449 +0,0 @@ -/* - Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) - - mat2.hpp - - 2020-03-31 13:14:12 - -*/ -#ifndef GLM_MAT2_HPP_20200331131412 -#define GLM_MAT2_HPP_20200331131412 - -#include "mat.hpp" -#include "gtc/matrix_transform.hpp" - - - -namespace glm{ - - -// -// void mat2x3_add( in mat2x3, in T, out mat2x3 ) -// - -template -inline void mat2x3_add(const T* m, T scalar, T* dest) -{ - dest[0] = m[0] + scalar; - dest[1] = m[1] + scalar; - dest[2] = m[2] + scalar; - dest[3] = m[3] + scalar; - dest[4] = m[4] + scalar; - dest[5] = m[5] + scalar; -} - -// -// void mat2x3_add( in mat2x3, in mat2x3, out mat2x3 ) -// - -template -inline void mat2x3_add(const T* m1, const T* m2, T* dest) -{ - dest[0] = m1[0] + m2[0]; - dest[1] = m1[1] + m2[1]; - dest[2] = m1[2] + m2[2]; - dest[3] = m1[3] + m2[3]; - dest[4] = m1[4] + m2[4]; - dest[5] = m1[5] + m2[5]; -} - -// -// void mat2x3_sub( in mat2x3, in mat2x3, out mat2x3 ) -// - -template -inline void mat2x3_sub(const T* m1, const T* m2, T* dest) -{ - dest[0] = m1[0] - m2[0]; - dest[1] = m1[1] - m2[1]; - dest[2] = m1[2] - m2[2]; - dest[3] = m1[3] - m2[3]; - dest[4] = m1[4] - m2[4]; - dest[5] = m1[5] - m2[5]; -} - -// -// void mat2x3_mul( in mat2x3, in T, out mat2x3 ) -// - -template -inline void mat2x3_mul(const T* m, T scalar, T* dest) -{ - dest[0] = m[0] * scalar; - dest[1] = m[1] * scalar; - dest[2] = m[2] * scalar; - dest[3] = m[3] * scalar; - dest[4] = m[4] * scalar; - dest[5] = m[5] * scalar; -} - -// -// void mat2x3_mul( in mat2x3, in mat2x3, out mat2x3 ) -// - -template -GLM_API void mat2x3_mul(const T* m1, const T* m2, T* dest) -{ - T product[6]; - - product[0] = m1[0] * m2[0] + m1[1] * m2[2]; - product[1] = m1[0] * m2[1] + m1[1] * m2[3]; - - product[2] = m1[2] * m2[0] + m1[3] * m2[2]; - product[3] = m1[2] * m2[1] + m1[3] * m2[3]; - - product[4] = m1[4] * m2[0] + m1[5] * m2[2] + m2[4]; - product[5] = m1[4] * m2[1] + m1[5] * m2[3] + m2[5]; - - memcpy(dest, product, sizeof(product)); -} - -// -// bool mat2x3_determinant( in mat2x3, out T ) -// -template -bool mat2x3_determinant(const T* m, T& d) -{ - d = m[0] * m[3] - m[1] * m[2]; - - if (abs(d) < constants::epsilon) { - return false; - } - - d = constants::one / d; - - return true; -} - -// -// bool mat2x3_inverse( in mat2x3, out mat2x3 ) -// - -template -bool mat2x3_inverse(const T* m, T* dest) -{ - T product[6]; - T d; - - if (mat2x3_determinant(m, d) == false) { - return false; - } - - product[0] = m[3] * d; - product[1] = -m[1] * d; - product[2] = -m[2] * d; - product[3] = m[0] * d; - product[4] = -m[4] * product[0] - m[5] * product[2]; - product[5] = -m[4] * product[1] - m[5] * product[3]; - - memcpy(dest, product, sizeof(product)); - - return true; -} - -// -// void mat2x3_transform( in mat2x3, in vec2, out vec2 ) -// - -template -GLM_API void mat2x3_transform(const T* m, const T* v, T* dest) -{ - T product[2]; - - product[0] = m[0] * v[0] + m[2] * v[1] + m[4]; - product[1] = m[1] * v[0] + m[3] * v[1] + m[5]; - - memcpy(dest, product, sizeof(product)); -} - -// -// mat2x3 -// - -template -class mat<2, 3, T> -{ -public: - GLM_CLASS_TYPEDEF(T); - - typedef mat<2, 3, T> this_type; - typedef vec<2, T> row_type; - - enum{ - COLS = 2, - ROWS = 3, - ELEMENTS = 6 - }; - -public: - row_type m[3]; - -public: - mat() - { - } - - mat(value_type scalar) - { - const T zero = constants::zero; - m[0] = vec<2, T>(scalar, zero); - m[1] = vec<2, T>(zero, scalar); - m[2] = vec<2, T>(zero, zero); - } - - mat(const row_type& a, const row_type& b, const row_type& c) - { - m[0] = a; - m[1] = b; - m[2] = c; - } - - mat( - value_type m00, value_type m01, - value_type m10, value_type m11, - value_type m20, value_type m21) - { - m[0] = row_type(m00, m01); - m[1] = row_type(m10, m11); - m[2] = row_type(m20, m21); - } - - mat(const this_type& m) - { - memcpy(data(), m.data(), sizeof(value_type) * ELEMENTS); - } - - length_t length()const - { - return ELEMENTS; - } - - pointer data() - { - return &m[0][0]; - } - - const_pointer data()const - { - return &m[0][0]; - } - - row_type& operator[](size_t i) - { - return m[i]; - } - - const row_type& operator[](size_t i)const - { - return m[i]; - } - - reference operator()(size_t x, size_t y) - { - return m[y][x]; - } - - const_reference operator()(size_t x, size_t y)const - { - return m[y][x]; - } - - // - // ext - // - - this_type& translate(value_type x, value_type y) - { - m[2][0] += x; - m[2][1] += y; - return *this; - } - - this_type& scale(value_type x) - { - return this->scale(x, x); - } - - this_type& scale(value_type x, value_type y) - { - m[0][0] *= x; m[0][1] *= y; - m[1][0] *= x; m[1][1] *= y; - m[2][0] *= x; m[2][1] *= y; - - return *this; - } - - this_type& rotate(value_type angle) - { - const value_type zero = constants::zero; - value_type cosine = cos(angle); - value_type sine = sin(angle); - - value_type mat[6] = { - cosine, sine, - -sine, cosine, - zero, zero - }; - - mat2x3_mul(data(), mat, data()); - - return *this; - } -}; - -// -// operator -// - -template -GLM_API mat<2, 3, T> operator +(const mat<2, 3, T>& m, T n) -{ - mat<2, 3, T> product; - mat2x3_add(m.data(), n, product.data()); - return product; -} - -template -GLM_API mat<2, 3, T> operator +(const mat<2, 3, T>& m1, const mat<2, 3, T>& m2) -{ - mat<2, 3, T> product; - mat2x3_add(m1.data(), m2.data(), product.data()); - return product; -} - -template -GLM_API mat<2, 3, T> operator -(const mat<2, 3, T>& m, T n) -{ - mat<2, 3, T> product; - mat2x3_mul(m.data(), -n, product.data()); - return product; -} - -template -GLM_API mat<2, 3, T> operator -(const mat<2, 3, T>& m1, const mat<2, 3, T>& m2) -{ - mat<2, 3, T> product; - mat2x3_sub(m1.data(), m2.data(), product.data()); - return product; -} - -template -GLM_API mat<2, 3, T> operator *(const mat<2, 3, T>& m, T n) -{ - mat<2, 3, T> product; - mat2x3_mul(m.data(), n, product.data()); - return product; -} - -template -GLM_API mat<2, 3, T> operator *(const mat<2, 3, T>& m1, const mat<2, 3, T>& m2) -{ - mat<2, 3, T> product; - mat2x3_mul(m1.data(), m2.data(), product.data()); - return product; -} - -template -GLM_API mat<2, 3, T> operator /(const mat<2, 3, T>& m, T n) -{ - mat<2, 3, T> product; - mat2x3_mul(m.data(), constants::one / n, product.data()); - return product; -} - -// transform -template -GLM_API vec<2, T> operator *(const mat<2, 3, T>& m, const vec<2, T>& v) -{ - vec<2, T> product; - mat2x3_transform(m.data(), v.data(), product.data()); - return product; -} - -// -// mat2x3 transpose( in mat2x3 ) -// - -template -GLM_API mat<2, 3, T> transpose(const mat<2, 3, T>& m) -{ - mat<2, 3, T> product; - mat2x3_transpose_copy(m.data(), product.data()); - return product; -} - -// -// mat2x3 inverse( in mat2x3 ) -// - -template -GLM_API mat<2, 3, T> inverse(const mat<2, 3, T>& m) -{ - mat<2, 3, T> product; - mat2x3_inverse(m.data(), product.data()); - return product; -} - - - -// -// extension -// - -template -GLM_API mat<2, 3, T> rotate2x3(T angle) -{ - T cosine = cos(angle); - T sine = sin(angle); - return mat<2, 3, T>(cosine, -sine, sine, cosine, constants::zero, constants::zero); -} - -template -GLM_API mat<2, 3, T> translate(const mat<2, 3, T>& m, const vec<2, T>& v) -{ - return mat<2, 3, T>(m[0], m[1], m[2] + v); -} - -template -GLM_API mat<2, 3, T> rotate(const mat<2, 3, T>& m, T angle) -{ - mat<2, 3, T> product; - - mat2x3_mul(m.data(), rotate2x3(angle).data(), product.data()); - - return product; -} - -template -GLM_API mat<2, 3, T> scale(const mat<2, 3, T>& m, const vec<2, T>& v) -{ - return mat<2, 3, T>(m[0] * v, m[1] * v, m[2] * v); -} - -}// end namespace glm - -#endif// GLM_MAT2_HPP_20200331131412 - -/* - -void ortho(Transform3x2 dst, float l, float r, float b, float t) -{ - dst[0][0] = 2/(r-l); - dst[0][1] = 0; - dst[0][2] = -(r+l)/(r-l); - dst[1][0] = 0; - dst[1][1] = 2/(t-b); - dst[1][2] = -(t+b)/(t-b); -} - -void inverse_ortho(Transform3x2 dst, float l, float r, float b, float t) -{ - dst[0][0] = (r-l)/2; - dst[0][1] = 0; - dst[0][2] = (r+l)/2; - dst[1][0] = 0; - dst[1][1] = (t-b)/2; - dst[1][2] = (t+b)/2; -} - -*/ \ No newline at end of file diff --git a/glm/mat2x2.hpp b/glm/mat2x2.hpp new file mode 100644 index 0000000..8be083f --- /dev/null +++ b/glm/mat2x2.hpp @@ -0,0 +1,327 @@ +/* + Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) + + mat2x2.hpp + + 2022-02-15 15:29:35 + +*/ +#ifndef GLM_MAT2X2_HPP20220215152935 +#define GLM_MAT2X2_HPP20220215152935 + +#include "matrix.hpp" + + + +namespace glm { + +// +// class mat<2, 2, T> +// + +template +class mat<2, 2, T> +{ +public: + GLM_CLASS_TYPEDEF(T); + + typedef mat<2, 2, T> this_type; + typedef vec<2, T> col_type; + typedef vec<2, T> row_type; + + enum { + ROWS = 2, + COLS = 2, + ELEMENTS = 4, + }; + +public: + col_type m[COLS]; + +public: + mat() + { + } + + mat(value_type scalar) + { + const T zero = constants::zero; + m[0] = col_type(scalar, zero); + m[1] = col_type(zero, scalar); + } + + mat(const col_type& v0, const col_type& v1) + { + m[0] = v0; + m[1] = v1; + } + + mat(value_type m00, value_type m01, + value_type m10, value_type m11) + { + m[0] = col_type(m00, m01); + m[1] = col_type(m10, m11); + } + + mat(const mat<2, 2, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + mat(const mat<2, 3, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + mat(const mat<2, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + mat(const mat<3, 2, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + mat(const mat<3, 3, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + mat(const mat<3, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + mat(const mat<4, 2, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + mat(const mat<4, 3, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + mat(const mat<4, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + length_t length()const + { + return COLS; + } + + size_t elements()const + { + return ELEMENTS; + } + + pointer data() + { + return &m[0][0]; + } + + const_pointer data()const + { + return &m[0][0]; + } + + col_type& operator[](size_t i) + { + return m[i]; + } + + const col_type& operator[](size_t i)const + { + return m[i]; + } +}; + +// +// functions +// + +// mat2 = mul( in mat2, in mat2 ) +template +GLM_API mat<2, 2, T> mat2_mul(const mat<2, 2, T>& m1, const mat<2, 2, T>& m2) +{ + mat<2, 2, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1]; + + return product; +} + +// mat3x2 = mul( in mat2, in mat3x2 ) +template +GLM_API mat<3, 2, T> mat2_mul(const mat<2, 2, T>& m1, const mat<3, 2, T>& m2) +{ + mat<3, 2, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1]; + + return product; +} + +// mat4x2 = mul( in mat2, in mat4x2 ) +template +GLM_API mat<4, 2, T> mat2_mul(const mat<2, 2, T>& m1, const mat<4, 2, T>& m2) +{ + mat<4, 2, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1]; + product[3][0] = m1[0][0] * m2[3][0] + m1[1][0] * m2[3][1]; + product[3][1] = m1[0][1] * m2[3][0] + m1[1][1] * m2[3][1]; + + return product; +} + +// mat2 = transpose( in mat2 ) +template +GLM_API mat<2, 2, T> transpose(const mat<2, 2, T>& m) +{ + return mat2( + m[0][0], m[1][0], + m[0][1], m[1][1]); +} + +// vec2 = transform( in vec2, in mat2 ) +template +GLM_API vec<2, T> transform(const vec<2, T>& v, const mat<2, 2, T>& m) +{ + return vec<2, T>( + v[0] * m[0][0] + v[1] * m[0][1], + v[0] * m[1][0] + v[1] * m[1][1]); +} + +// vec2 = transform ( in mat2, in vec2 ) +template +GLM_API vec<2, T> transform(const mat<2, 2, T>& m, const vec<2, T>& v) +{ + return vec<2, T>( + m[0][0] * v[0] + m[1][0] * v[1], + m[0][1] * v[0] + m[1][1] * v[1]); +} + +// +// operators +// + +// mat2 = mat2 + scalar +template +GLM_API mat<2, 2, T> operator+(const mat<2, 2, T>& m, T scalar) +{ + return mat<2, 2, T>(m[0] + scalar, m[1] + scalar); +} + +// mat2 = mat2 - scalar +template +GLM_API mat<2, 2, T> operator-(const mat<2, 2, T>& m, T scalar) +{ + return mat<2, 2, T>(m[0] - scalar, m[1] - scalar); +} + +// mat2 = mat2 * scalar +template +GLM_API mat<2, 2, T> operator*(const mat<2, 2, T>& m, T scalar) +{ + return mat<2, 2, T>(m[0] * scalar, m[1] * scalar); +} + +// mat2 = mat2 / scalar +template +GLM_API mat<2, 2, T> operator/(const mat<2, 2, T>& m, T scalar) +{ + return mat<2, 2, T>(m[0] / scalar, m[1] / scalar); +} + +// mat2 = scalar + mat2 +template +GLM_API mat<2, 2, T> operator+(T scalar, const mat<2, 2, T>& m) +{ + return mat<2, 2, T>(scalar + m[0], scalar + m[1]); +} + +// mat2 = scalar - mat2 +template +GLM_API mat<2, 2, T> operator-(T scalar, const mat<2, 2, T>& m) +{ + return mat<2, 2, T>(scalar - m[0], scalar - m[1]); +} + +// mat2 = scalar * mat2 +template +GLM_API mat<2, 2, T> operator*(T scalar, const mat<2, 2, T>& m) +{ + return mat<2, 2, T>(scalar * m[0], scalar * m[1]); +} + +// mat2 = scalar / mat2 +template +GLM_API mat<2, 2, T> operator/(T scalar, const mat<2, 2, T>& m) +{ + return mat<2, 2, T>(scalar / m[0], scalar / m[1]); +} + +// mat2 = mat2 x mat2 +template +GLM_API mat<2, 2, T> operator*(const mat<2, 2, T>& m1, const mat<2, 2, T>& m2) +{ + return mat2_mul(m1, m2); +} + +// mat3x2 = mat2 x mat3x2 +template +GLM_API mat<3, 2, T> operator*(const mat<2, 2, T>& m1, const mat<3, 2, T>& m2) +{ + return mat2_mul(m1, m2); +} + +// mat4x2 = mat2 x mat4x2 +template +GLM_API mat<4, 2, T> operator*(const mat<2, 2, T>& m1, const mat<4, 2, T>& m2) +{ + return mat2_mul(m1, m2); +} + +// vec2 = vec2 x mat2 +template +GLM_API vec<2, T> operator*(const vec<2, T>& v, const mat<2, 2, T>& m) +{ + return transform(v, m); +} + +// vec2 = mat2 x vec2 +template +GLM_API vec<2, T> operator*(const mat<2, 2, T>& v, const vec<2, T>& m) +{ + return transform(m, v); +} + +}// end namespace glm + +#endif// GLM_MAT2X2_HPP20220215152935 diff --git a/glm/mat2x3.hpp b/glm/mat2x3.hpp new file mode 100644 index 0000000..9011542 --- /dev/null +++ b/glm/mat2x3.hpp @@ -0,0 +1,343 @@ +/* + Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) + + mat2x3.hpp + + 2022-02-15 15:29:35 + +*/ +#ifndef GLM_MAT2X3_HPP20220215152935 +#define GLM_MAT2X3_HPP20220215152935 + +#include "matrix.hpp" + + + +namespace glm { + +// +// class mat<2, 3, T> +// + +template +class mat<2, 3, T> +{ +public: + GLM_CLASS_TYPEDEF(T); + + typedef mat<2, 3, T> this_type; + typedef vec<3, T> col_type; + typedef vec<2, T> row_type; + + enum { + ROWS = 2, + COLS = 3, + ELEMENTS = 6, + }; + +public: + col_type m[COLS]; + +public: + mat() + { + } + + mat(value_type scalar) + { + const T zero = constants::zero; + m[0] = col_type(scalar, zero, zero); + m[1] = col_type(zero, scalar, zero); + } + + mat(const col_type& v0, const col_type& v1) + { + m[0] = v0; + m[1] = v1; + } + + mat(value_type m00, value_type m01, value_type m02, + value_type m10, value_type m11, value_type m12) + { + m[0] = col_type(m00, m01, m02); + m[1] = col_type(m10, m11, m12); + } + + mat(const mat<2, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + } + + mat(const mat<2, 3, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + mat(const mat<2, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + mat(const mat<3, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + } + + mat(const mat<3, 3, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + mat(const mat<3, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + mat(const mat<4, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + } + + mat(const mat<4, 3, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + mat(const mat<4, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + length_t length()const + { + return COLS; + } + + size_t elements()const + { + return ELEMENTS; + } + + pointer data() + { + return &m[0][0]; + } + + const_pointer data()const + { + return &m[0][0]; + } + + col_type& operator[](size_t i) + { + return m[i]; + } + + const col_type& operator[](size_t i)const + { + return m[i]; + } +}; + +// +// functions +// + +// mat2x3 = mul( in mat2x3, in mat2 ) +template +GLM_API mat<2, 3, T> mat2x3_mul(const mat<2, 3, T>& m1, const mat<2, 2, T>& m2) +{ + mat<2, 3, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1]; + + return product; +} + +// mat3 = mul( in mat2x3, in mat3x2 ) +template +GLM_API mat<3, 3, T> mat2x3_mul(const mat<2, 3, T>& m1, const mat<3, 2, T>& m2) +{ + mat<3, 3, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1]; + product[2][2] = m1[0][2] * m2[2][0] + m1[1][2] * m2[2][1]; + + return product; +} + +// mat4x3 = mul( in mat2x3, in mat4x2 ) +template +GLM_API mat<4, 3, T> mat2x3_mul(const mat<2, 3, T>& m1, const mat<4, 2, T>& m2) +{ + mat<4, 3, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1]; + product[2][2] = m1[0][2] * m2[2][0] + m1[1][2] * m2[2][1]; + product[3][0] = m1[0][0] * m2[3][0] + m1[1][0] * m2[3][1]; + product[3][1] = m1[0][1] * m2[3][0] + m1[1][1] * m2[3][1]; + product[3][2] = m1[0][2] * m2[3][0] + m1[1][2] * m2[3][1]; + + return product; +} + +// mat3x2 = transpose( in mat2x3 ) +template +GLM_API mat<3, 2, T> transpose(const mat<2, 3, T>& m) +{ + return mat3x2( + m[0][0], m[1][0], m[2][0], + m[0][1], m[1][1], m[2][1]); +} + +// vec3 = transform( in vec3, in mat2x3 ) +template +GLM_API vec<3, T> transform(const vec<3, T>& v, const mat<2, 3, T>& m) +{ + return vec<2, T>( + v[0] * m[0][0] + v[1] * m[0][1] + v[2] * m[0][2], + v[0] * m[1][0] + v[1] * m[1][1] + v[2] * m[1][2]); +} + +// vec2 = transform ( in mat2x3, in vec2 ) +template +GLM_API vec<2, T> transform(const mat<2, 3, T>& m, const vec<2, T>& v) +{ + return vec<2, T>( + m[0][0] * v[0] + m[1][0] * v[1], + m[0][1] * v[0] + m[1][1] * v[1], + m[0][2] * v[0] + m[1][2] * v[1]); +} + +// +// operators +// + +// mat2x3 = mat2x3 + scalar +template +GLM_API mat<2, 3, T> operator+(const mat<2, 3, T>& m, T scalar) +{ + return mat<2, 3, T>(m[0] + scalar, m[1] + scalar); +} + +// mat2x3 = mat2x3 - scalar +template +GLM_API mat<2, 3, T> operator-(const mat<2, 3, T>& m, T scalar) +{ + return mat<2, 3, T>(m[0] - scalar, m[1] - scalar); +} + +// mat2x3 = mat2x3 * scalar +template +GLM_API mat<2, 3, T> operator*(const mat<2, 3, T>& m, T scalar) +{ + return mat<2, 3, T>(m[0] * scalar, m[1] * scalar); +} + +// mat2x3 = mat2x3 / scalar +template +GLM_API mat<2, 3, T> operator/(const mat<2, 3, T>& m, T scalar) +{ + return mat<2, 3, T>(m[0] / scalar, m[1] / scalar); +} + +// mat2x3 = scalar + mat2x3 +template +GLM_API mat<2, 3, T> operator+(T scalar, const mat<2, 3, T>& m) +{ + return mat<2, 3, T>(scalar + m[0], scalar + m[1]); +} + +// mat2x3 = scalar - mat2x3 +template +GLM_API mat<2, 3, T> operator-(T scalar, const mat<2, 3, T>& m) +{ + return mat<2, 3, T>(scalar - m[0], scalar - m[1]); +} + +// mat2x3 = scalar * mat2x3 +template +GLM_API mat<2, 3, T> operator*(T scalar, const mat<2, 3, T>& m) +{ + return mat<2, 3, T>(scalar * m[0], scalar * m[1]); +} + +// mat2x3 = scalar / mat2x3 +template +GLM_API mat<2, 3, T> operator/(T scalar, const mat<2, 3, T>& m) +{ + return mat<2, 3, T>(scalar / m[0], scalar / m[1]); +} + +// mat2x3 = mat2x3 x mat2 +template +GLM_API mat<2, 3, T> operator*(const mat<2, 3, T>& m1, const mat<2, 2, T>& m2) +{ + return mat2x3_mul(m1, m2); +} + +// mat3 = mat2x3 x mat3x2 +template +GLM_API mat<3, 3, T> operator*(const mat<2, 3, T>& m1, const mat<3, 2, T>& m2) +{ + return mat2x3_mul(m1, m2); +} + +// mat4x3 = mat2x3 x mat4x2 +template +GLM_API mat<4, 3, T> operator*(const mat<2, 3, T>& m1, const mat<4, 2, T>& m2) +{ + return mat2x3_mul(m1, m2); +} + +// vec3 = vec3 x mat2x3 +template +GLM_API vec<3, T> operator*(const vec<3, T>& v, const mat<2, 3, T>& m) +{ + return transform(v, m); +} + +// vec2 = mat2x3 x vec2 +template +GLM_API vec<2, T> operator*(const mat<2, 3, T>& v, const vec<2, T>& m) +{ + return transform(m, v); +} + +}// end namespace glm + +#endif// GLM_MAT2X3_HPP20220215152935 diff --git a/glm/mat2x4.hpp b/glm/mat2x4.hpp new file mode 100644 index 0000000..399cdff --- /dev/null +++ b/glm/mat2x4.hpp @@ -0,0 +1,359 @@ +/* + Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) + + mat2x4.hpp + + 2022-02-15 15:29:35 + +*/ +#ifndef GLM_MAT2X4_HPP20220215152935 +#define GLM_MAT2X4_HPP20220215152935 + +#include "matrix.hpp" + + + +namespace glm { + +// +// class mat<2, 4, T> +// + +template +class mat<2, 4, T> +{ +public: + GLM_CLASS_TYPEDEF(T); + + typedef mat<2, 4, T> this_type; + typedef vec<4, T> col_type; + typedef vec<2, T> row_type; + + enum { + ROWS = 2, + COLS = 4, + ELEMENTS = 8, + }; + +public: + col_type m[COLS]; + +public: + mat() + { + } + + mat(value_type scalar) + { + const T zero = constants::zero; + m[0] = col_type(scalar, zero, zero, zero); + m[1] = col_type(zero, scalar, zero, zero); + } + + mat(const col_type& v0, const col_type& v1) + { + m[0] = v0; + m[1] = v1; + } + + mat(value_type m00, value_type m01, value_type m02, value_type m03, + value_type m10, value_type m11, value_type m12, value_type m13) + { + m[0] = col_type(m00, m01, m02, m03); + m[1] = col_type(m10, m11, m12, m13); + } + + mat(const mat<2, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero, zero); + m[1] = col_type(other.m[1], zero, zero); + } + + mat(const mat<2, 3, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + } + + mat(const mat<2, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + mat(const mat<3, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero, zero); + m[1] = col_type(other.m[1], zero, zero); + } + + mat(const mat<3, 3, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + } + + mat(const mat<3, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + mat(const mat<4, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero, zero); + m[1] = col_type(other.m[1], zero, zero); + } + + mat(const mat<4, 3, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + } + + mat(const mat<4, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + } + + length_t length()const + { + return COLS; + } + + size_t elements()const + { + return ELEMENTS; + } + + pointer data() + { + return &m[0][0]; + } + + const_pointer data()const + { + return &m[0][0]; + } + + col_type& operator[](size_t i) + { + return m[i]; + } + + const col_type& operator[](size_t i)const + { + return m[i]; + } +}; + +// +// functions +// + +// mat2x4 = mul( in mat2x4, in mat2 ) +template +GLM_API mat<2, 4, T> mat2x4_mul(const mat<2, 4, T>& m1, const mat<2, 2, T>& m2) +{ + mat<2, 4, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1]; + product[0][3] = m1[0][3] * m2[0][0] + m1[1][3] * m2[0][1]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1]; + product[1][3] = m1[0][3] * m2[1][0] + m1[1][3] * m2[1][1]; + + return product; +} + +// mat3x4 = mul( in mat2x4, in mat3x2 ) +template +GLM_API mat<3, 4, T> mat2x4_mul(const mat<2, 4, T>& m1, const mat<3, 2, T>& m2) +{ + mat<3, 4, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1]; + product[0][3] = m1[0][3] * m2[0][0] + m1[1][3] * m2[0][1]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1]; + product[1][3] = m1[0][3] * m2[1][0] + m1[1][3] * m2[1][1]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1]; + product[2][2] = m1[0][2] * m2[2][0] + m1[1][2] * m2[2][1]; + product[2][3] = m1[0][3] * m2[2][0] + m1[1][3] * m2[2][1]; + + return product; +} + +// mat4 = mul( in mat2x4, in mat4x2 ) +template +GLM_API mat<4, 4, T> mat2x4_mul(const mat<2, 4, T>& m1, const mat<4, 2, T>& m2) +{ + mat<4, 4, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1]; + product[0][3] = m1[0][3] * m2[0][0] + m1[1][3] * m2[0][1]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1]; + product[1][3] = m1[0][3] * m2[1][0] + m1[1][3] * m2[1][1]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1]; + product[2][2] = m1[0][2] * m2[2][0] + m1[1][2] * m2[2][1]; + product[2][3] = m1[0][3] * m2[2][0] + m1[1][3] * m2[2][1]; + product[3][0] = m1[0][0] * m2[3][0] + m1[1][0] * m2[3][1]; + product[3][1] = m1[0][1] * m2[3][0] + m1[1][1] * m2[3][1]; + product[3][2] = m1[0][2] * m2[3][0] + m1[1][2] * m2[3][1]; + product[3][3] = m1[0][3] * m2[3][0] + m1[1][3] * m2[3][1]; + + return product; +} + +// mat4x2 = transpose( in mat2x4 ) +template +GLM_API mat<4, 2, T> transpose(const mat<2, 4, T>& m) +{ + return mat4x2( + m[0][0], m[1][0], m[2][0], m[3][0], + m[0][1], m[1][1], m[2][1], m[3][1]); +} + +// vec4 = transform( in vec4, in mat2x4 ) +template +GLM_API vec<4, T> transform(const vec<4, T>& v, const mat<2, 4, T>& m) +{ + return vec<2, T>( + v[0] * m[0][0] + v[1] * m[0][1] + v[2] * m[0][2] + v[3] * m[0][3], + v[0] * m[1][0] + v[1] * m[1][1] + v[2] * m[1][2] + v[3] * m[1][3]); +} + +// vec2 = transform ( in mat2x4, in vec2 ) +template +GLM_API vec<2, T> transform(const mat<2, 4, T>& m, const vec<2, T>& v) +{ + return vec<2, T>( + m[0][0] * v[0] + m[1][0] * v[1], + m[0][1] * v[0] + m[1][1] * v[1], + m[0][2] * v[0] + m[1][2] * v[1], + m[0][3] * v[0] + m[1][3] * v[1]); +} + +// +// operators +// + +// mat2x4 = mat2x4 + scalar +template +GLM_API mat<2, 4, T> operator+(const mat<2, 4, T>& m, T scalar) +{ + return mat<2, 4, T>(m[0] + scalar, m[1] + scalar); +} + +// mat2x4 = mat2x4 - scalar +template +GLM_API mat<2, 4, T> operator-(const mat<2, 4, T>& m, T scalar) +{ + return mat<2, 4, T>(m[0] - scalar, m[1] - scalar); +} + +// mat2x4 = mat2x4 * scalar +template +GLM_API mat<2, 4, T> operator*(const mat<2, 4, T>& m, T scalar) +{ + return mat<2, 4, T>(m[0] * scalar, m[1] * scalar); +} + +// mat2x4 = mat2x4 / scalar +template +GLM_API mat<2, 4, T> operator/(const mat<2, 4, T>& m, T scalar) +{ + return mat<2, 4, T>(m[0] / scalar, m[1] / scalar); +} + +// mat2x4 = scalar + mat2x4 +template +GLM_API mat<2, 4, T> operator+(T scalar, const mat<2, 4, T>& m) +{ + return mat<2, 4, T>(scalar + m[0], scalar + m[1]); +} + +// mat2x4 = scalar - mat2x4 +template +GLM_API mat<2, 4, T> operator-(T scalar, const mat<2, 4, T>& m) +{ + return mat<2, 4, T>(scalar - m[0], scalar - m[1]); +} + +// mat2x4 = scalar * mat2x4 +template +GLM_API mat<2, 4, T> operator*(T scalar, const mat<2, 4, T>& m) +{ + return mat<2, 4, T>(scalar * m[0], scalar * m[1]); +} + +// mat2x4 = scalar / mat2x4 +template +GLM_API mat<2, 4, T> operator/(T scalar, const mat<2, 4, T>& m) +{ + return mat<2, 4, T>(scalar / m[0], scalar / m[1]); +} + +// mat2x4 = mat2x4 x mat2 +template +GLM_API mat<2, 4, T> operator*(const mat<2, 4, T>& m1, const mat<2, 2, T>& m2) +{ + return mat2x4_mul(m1, m2); +} + +// mat3x4 = mat2x4 x mat3x2 +template +GLM_API mat<3, 4, T> operator*(const mat<2, 4, T>& m1, const mat<3, 2, T>& m2) +{ + return mat2x4_mul(m1, m2); +} + +// mat4 = mat2x4 x mat4x2 +template +GLM_API mat<4, 4, T> operator*(const mat<2, 4, T>& m1, const mat<4, 2, T>& m2) +{ + return mat2x4_mul(m1, m2); +} + +// vec4 = vec4 x mat2x4 +template +GLM_API vec<4, T> operator*(const vec<4, T>& v, const mat<2, 4, T>& m) +{ + return transform(v, m); +} + +// vec2 = mat2x4 x vec2 +template +GLM_API vec<2, T> operator*(const mat<2, 4, T>& v, const vec<2, T>& m) +{ + return transform(m, v); +} + +}// end namespace glm + +#endif// GLM_MAT2X4_HPP20220215152935 diff --git a/glm/mat3x2.hpp b/glm/mat3x2.hpp new file mode 100644 index 0000000..f39a4c5 --- /dev/null +++ b/glm/mat3x2.hpp @@ -0,0 +1,348 @@ +/* + Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) + + mat3x2.hpp + + 2022-02-15 15:29:35 + +*/ +#ifndef GLM_MAT3X2_HPP20220215152935 +#define GLM_MAT3X2_HPP20220215152935 + +#include "matrix.hpp" + + + +namespace glm { + +// +// class mat<3, 2, T> +// + +template +class mat<3, 2, T> +{ +public: + GLM_CLASS_TYPEDEF(T); + + typedef mat<3, 2, T> this_type; + typedef vec<2, T> col_type; + typedef vec<3, T> row_type; + + enum { + ROWS = 3, + COLS = 2, + ELEMENTS = 6, + }; + +public: + col_type m[COLS]; + +public: + mat() + { + } + + mat(value_type scalar) + { + const T zero = constants::zero; + m[0] = col_type(scalar, zero); + m[1] = col_type(zero, scalar); + m[2] = col_type(zero, zero); + } + + mat(const col_type& v0, const col_type& v1, const col_type& v2) + { + m[0] = v0; + m[1] = v1; + m[2] = v2; + } + + mat(value_type m00, value_type m01, + value_type m10, value_type m11, + value_type m20, value_type m21) + { + m[0] = col_type(m00, m01); + m[1] = col_type(m10, m11); + m[2] = col_type(m20, m21); + } + + mat(const mat<2, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = col_type(zero, zero); + } + + mat(const mat<2, 3, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = col_type(zero, zero); + } + + mat(const mat<2, 4, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = col_type(zero, zero); + } + + mat(const mat<3, 2, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + } + + mat(const mat<3, 3, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + } + + mat(const mat<3, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + } + + mat(const mat<4, 2, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + } + + mat(const mat<4, 3, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + } + + mat(const mat<4, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + } + + length_t length()const + { + return COLS; + } + + size_t elements()const + { + return ELEMENTS; + } + + pointer data() + { + return &m[0][0]; + } + + const_pointer data()const + { + return &m[0][0]; + } + + col_type& operator[](size_t i) + { + return m[i]; + } + + const col_type& operator[](size_t i)const + { + return m[i]; + } +}; + +// +// functions +// + +// mat2 = mul( in mat3x2, in mat2x3 ) +template +GLM_API mat<2, 2, T> mat3x2_mul(const mat<3, 2, T>& m1, const mat<2, 3, T>& m2) +{ + mat<2, 2, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2]; + + return product; +} + +// mat3x2 = mul( in mat3x2, in mat3 ) +template +GLM_API mat<3, 2, T> mat3x2_mul(const mat<3, 2, T>& m1, const mat<3, 3, T>& m2) +{ + mat<3, 2, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1] + m1[2][0] * m2[2][2]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1] + m1[2][1] * m2[2][2]; + + return product; +} + +// mat4x2 = mul( in mat3x2, in mat4x3 ) +template +GLM_API mat<4, 2, T> mat3x2_mul(const mat<3, 2, T>& m1, const mat<4, 3, T>& m2) +{ + mat<4, 2, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1] + m1[2][0] * m2[2][2]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1] + m1[2][1] * m2[2][2]; + product[3][0] = m1[0][0] * m2[3][0] + m1[1][0] * m2[3][1] + m1[2][0] * m2[3][2]; + product[3][1] = m1[0][1] * m2[3][0] + m1[1][1] * m2[3][1] + m1[2][1] * m2[3][2]; + + return product; +} + +// mat2x3 = transpose( in mat3x2 ) +template +GLM_API mat<2, 3, T> transpose(const mat<3, 2, T>& m) +{ + return mat2x3( + m[0][0], m[1][0], + m[0][1], m[1][1], + m[0][2], m[1][2]); +} + +// vec2 = transform( in vec2, in mat3x2 ) +template +GLM_API vec<2, T> transform(const vec<2, T>& v, const mat<3, 2, T>& m) +{ + return vec<3, T>( + v[0] * m[0][0] + v[1] * m[0][1], + v[0] * m[1][0] + v[1] * m[1][1], + v[0] * m[2][0] + v[1] * m[2][1]); +} + +// vec3 = transform ( in mat3x2, in vec3 ) +template +GLM_API vec<3, T> transform(const mat<3, 2, T>& m, const vec<3, T>& v) +{ + return vec<3, T>( + m[0][0] * v[0] + m[1][0] * v[1] + m[2][0] * v[2], + m[0][1] * v[0] + m[1][1] * v[1] + m[2][1] * v[2]); +} + +// +// operators +// + +// mat3x2 = mat3x2 + scalar +template +GLM_API mat<3, 2, T> operator+(const mat<3, 2, T>& m, T scalar) +{ + return mat<3, 2, T>(m[0] + scalar, m[1] + scalar, m[2] + scalar); +} + +// mat3x2 = mat3x2 - scalar +template +GLM_API mat<3, 2, T> operator-(const mat<3, 2, T>& m, T scalar) +{ + return mat<3, 2, T>(m[0] - scalar, m[1] - scalar, m[2] - scalar); +} + +// mat3x2 = mat3x2 * scalar +template +GLM_API mat<3, 2, T> operator*(const mat<3, 2, T>& m, T scalar) +{ + return mat<3, 2, T>(m[0] * scalar, m[1] * scalar, m[2] * scalar); +} + +// mat3x2 = mat3x2 / scalar +template +GLM_API mat<3, 2, T> operator/(const mat<3, 2, T>& m, T scalar) +{ + return mat<3, 2, T>(m[0] / scalar, m[1] / scalar, m[2] / scalar); +} + +// mat3x2 = scalar + mat3x2 +template +GLM_API mat<3, 2, T> operator+(T scalar, const mat<3, 2, T>& m) +{ + return mat<3, 2, T>(scalar + m[0], scalar + m[1], scalar + m[2]); +} + +// mat3x2 = scalar - mat3x2 +template +GLM_API mat<3, 2, T> operator-(T scalar, const mat<3, 2, T>& m) +{ + return mat<3, 2, T>(scalar - m[0], scalar - m[1], scalar - m[2]); +} + +// mat3x2 = scalar * mat3x2 +template +GLM_API mat<3, 2, T> operator*(T scalar, const mat<3, 2, T>& m) +{ + return mat<3, 2, T>(scalar * m[0], scalar * m[1], scalar * m[2]); +} + +// mat3x2 = scalar / mat3x2 +template +GLM_API mat<3, 2, T> operator/(T scalar, const mat<3, 2, T>& m) +{ + return mat<3, 2, T>(scalar / m[0], scalar / m[1], scalar / m[2]); +} + +// mat2 = mat3x2 x mat2x3 +template +GLM_API mat<2, 2, T> operator*(const mat<3, 2, T>& m1, const mat<2, 3, T>& m2) +{ + return mat3x2_mul(m1, m2); +} + +// mat3x2 = mat3x2 x mat3 +template +GLM_API mat<3, 2, T> operator*(const mat<3, 2, T>& m1, const mat<3, 3, T>& m2) +{ + return mat3x2_mul(m1, m2); +} + +// mat4x2 = mat3x2 x mat4x3 +template +GLM_API mat<4, 2, T> operator*(const mat<3, 2, T>& m1, const mat<4, 3, T>& m2) +{ + return mat3x2_mul(m1, m2); +} + +// vec2 = vec2 x mat3x2 +template +GLM_API vec<2, T> operator*(const vec<2, T>& v, const mat<3, 2, T>& m) +{ + return transform(v, m); +} + +// vec3 = mat3x2 x vec3 +template +GLM_API vec<3, T> operator*(const mat<3, 2, T>& v, const vec<3, T>& m) +{ + return transform(m, v); +} + +}// end namespace glm + +#endif// GLM_MAT3X2_HPP20220215152935 diff --git a/glm/mat3x3.hpp b/glm/mat3x3.hpp new file mode 100644 index 0000000..6185c65 --- /dev/null +++ b/glm/mat3x3.hpp @@ -0,0 +1,362 @@ +/* + Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) + + mat3x3.hpp + + 2022-02-15 15:29:35 + +*/ +#ifndef GLM_MAT3X3_HPP20220215152935 +#define GLM_MAT3X3_HPP20220215152935 + +#include "matrix.hpp" + + + +namespace glm { + +// +// class mat<3, 3, T> +// + +template +class mat<3, 3, T> +{ +public: + GLM_CLASS_TYPEDEF(T); + + typedef mat<3, 3, T> this_type; + typedef vec<3, T> col_type; + typedef vec<3, T> row_type; + + enum { + ROWS = 3, + COLS = 3, + ELEMENTS = 9, + }; + +public: + col_type m[COLS]; + +public: + mat() + { + } + + mat(value_type scalar) + { + const T zero = constants::zero; + m[0] = col_type(scalar, zero, zero); + m[1] = col_type(zero, scalar, zero); + m[2] = col_type(zero, zero, scalar); + } + + mat(const col_type& v0, const col_type& v1, const col_type& v2) + { + m[0] = v0; + m[1] = v1; + m[2] = v2; + } + + mat(value_type m00, value_type m01, value_type m02, + value_type m10, value_type m11, value_type m12, + value_type m20, value_type m21, value_type m22) + { + m[0] = col_type(m00, m01, m02); + m[1] = col_type(m10, m11, m12); + m[2] = col_type(m20, m21, m22); + } + + mat(const mat<2, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + m[2] = col_type(zero, zero, one); + } + + mat(const mat<2, 3, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = col_type(zero, zero, one); + } + + mat(const mat<2, 4, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = col_type(zero, zero, one); + } + + mat(const mat<3, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + m[2] = col_type(other.m[2], one); + } + + mat(const mat<3, 3, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + } + + mat(const mat<3, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + } + + mat(const mat<4, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + m[2] = col_type(other.m[2], one); + } + + mat(const mat<4, 3, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + } + + mat(const mat<4, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + } + + length_t length()const + { + return COLS; + } + + size_t elements()const + { + return ELEMENTS; + } + + pointer data() + { + return &m[0][0]; + } + + const_pointer data()const + { + return &m[0][0]; + } + + col_type& operator[](size_t i) + { + return m[i]; + } + + const col_type& operator[](size_t i)const + { + return m[i]; + } +}; + +// +// functions +// + +// mat2x3 = mul( in mat3, in mat2x3 ) +template +GLM_API mat<2, 3, T> mat3_mul(const mat<3, 3, T>& m1, const mat<2, 3, T>& m2) +{ + mat<2, 3, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1] + m1[2][2] * m2[0][2]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1] + m1[2][2] * m2[1][2]; + + return product; +} + +// mat3 = mul( in mat3, in mat3 ) +template +GLM_API mat<3, 3, T> mat3_mul(const mat<3, 3, T>& m1, const mat<3, 3, T>& m2) +{ + mat<3, 3, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1] + m1[2][2] * m2[0][2]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1] + m1[2][2] * m2[1][2]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1] + m1[2][0] * m2[2][2]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1] + m1[2][1] * m2[2][2]; + product[2][2] = m1[0][2] * m2[2][0] + m1[1][2] * m2[2][1] + m1[2][2] * m2[2][2]; + + return product; +} + +// mat4x3 = mul( in mat3, in mat4x3 ) +template +GLM_API mat<4, 3, T> mat3_mul(const mat<3, 3, T>& m1, const mat<4, 3, T>& m2) +{ + mat<4, 3, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1] + m1[2][2] * m2[0][2]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1] + m1[2][2] * m2[1][2]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1] + m1[2][0] * m2[2][2]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1] + m1[2][1] * m2[2][2]; + product[2][2] = m1[0][2] * m2[2][0] + m1[1][2] * m2[2][1] + m1[2][2] * m2[2][2]; + product[3][0] = m1[0][0] * m2[3][0] + m1[1][0] * m2[3][1] + m1[2][0] * m2[3][2]; + product[3][1] = m1[0][1] * m2[3][0] + m1[1][1] * m2[3][1] + m1[2][1] * m2[3][2]; + product[3][2] = m1[0][2] * m2[3][0] + m1[1][2] * m2[3][1] + m1[2][2] * m2[3][2]; + + return product; +} + +// mat3 = transpose( in mat3 ) +template +GLM_API mat<3, 3, T> transpose(const mat<3, 3, T>& m) +{ + return mat3( + m[0][0], m[1][0], m[2][0], + m[0][1], m[1][1], m[2][1], + m[0][2], m[1][2], m[2][2]); +} + +// vec3 = transform( in vec3, in mat3 ) +template +GLM_API vec<3, T> transform(const vec<3, T>& v, const mat<3, 3, T>& m) +{ + return vec<3, T>( + v[0] * m[0][0] + v[1] * m[0][1] + v[2] * m[0][2], + v[0] * m[1][0] + v[1] * m[1][1] + v[2] * m[1][2], + v[0] * m[2][0] + v[1] * m[2][1] + v[2] * m[2][2]); +} + +// vec3 = transform ( in mat3, in vec3 ) +template +GLM_API vec<3, T> transform(const mat<3, 3, T>& m, const vec<3, T>& v) +{ + return vec<3, T>( + m[0][0] * v[0] + m[1][0] * v[1] + m[2][0] * v[2], + m[0][1] * v[0] + m[1][1] * v[1] + m[2][1] * v[2], + m[0][2] * v[0] + m[1][2] * v[1] + m[2][2] * v[2]); +} + +// +// operators +// + +// mat3 = mat3 + scalar +template +GLM_API mat<3, 3, T> operator+(const mat<3, 3, T>& m, T scalar) +{ + return mat<3, 3, T>(m[0] + scalar, m[1] + scalar, m[2] + scalar); +} + +// mat3 = mat3 - scalar +template +GLM_API mat<3, 3, T> operator-(const mat<3, 3, T>& m, T scalar) +{ + return mat<3, 3, T>(m[0] - scalar, m[1] - scalar, m[2] - scalar); +} + +// mat3 = mat3 * scalar +template +GLM_API mat<3, 3, T> operator*(const mat<3, 3, T>& m, T scalar) +{ + return mat<3, 3, T>(m[0] * scalar, m[1] * scalar, m[2] * scalar); +} + +// mat3 = mat3 / scalar +template +GLM_API mat<3, 3, T> operator/(const mat<3, 3, T>& m, T scalar) +{ + return mat<3, 3, T>(m[0] / scalar, m[1] / scalar, m[2] / scalar); +} + +// mat3 = scalar + mat3 +template +GLM_API mat<3, 3, T> operator+(T scalar, const mat<3, 3, T>& m) +{ + return mat<3, 3, T>(scalar + m[0], scalar + m[1], scalar + m[2]); +} + +// mat3 = scalar - mat3 +template +GLM_API mat<3, 3, T> operator-(T scalar, const mat<3, 3, T>& m) +{ + return mat<3, 3, T>(scalar - m[0], scalar - m[1], scalar - m[2]); +} + +// mat3 = scalar * mat3 +template +GLM_API mat<3, 3, T> operator*(T scalar, const mat<3, 3, T>& m) +{ + return mat<3, 3, T>(scalar * m[0], scalar * m[1], scalar * m[2]); +} + +// mat3 = scalar / mat3 +template +GLM_API mat<3, 3, T> operator/(T scalar, const mat<3, 3, T>& m) +{ + return mat<3, 3, T>(scalar / m[0], scalar / m[1], scalar / m[2]); +} + +// mat2x3 = mat3 x mat2x3 +template +GLM_API mat<2, 3, T> operator*(const mat<3, 3, T>& m1, const mat<2, 3, T>& m2) +{ + return mat3_mul(m1, m2); +} + +// mat3 = mat3 x mat3 +template +GLM_API mat<3, 3, T> operator*(const mat<3, 3, T>& m1, const mat<3, 3, T>& m2) +{ + return mat3_mul(m1, m2); +} + +// mat4x3 = mat3 x mat4x3 +template +GLM_API mat<4, 3, T> operator*(const mat<3, 3, T>& m1, const mat<4, 3, T>& m2) +{ + return mat3_mul(m1, m2); +} + +// vec3 = vec3 x mat3 +template +GLM_API vec<3, T> operator*(const vec<3, T>& v, const mat<3, 3, T>& m) +{ + return transform(v, m); +} + +// vec3 = mat3 x vec3 +template +GLM_API vec<3, T> operator*(const mat<3, 3, T>& v, const vec<3, T>& m) +{ + return transform(m, v); +} + +}// end namespace glm + +#endif// GLM_MAT3X3_HPP20220215152935 diff --git a/glm/mat3x4.hpp b/glm/mat3x4.hpp new file mode 100644 index 0000000..f587911 --- /dev/null +++ b/glm/mat3x4.hpp @@ -0,0 +1,376 @@ +/* + Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) + + mat3x4.hpp + + 2022-02-15 15:29:35 + +*/ +#ifndef GLM_MAT3X4_HPP20220215152935 +#define GLM_MAT3X4_HPP20220215152935 + +#include "matrix.hpp" + + + +namespace glm { + +// +// class mat<3, 4, T> +// + +template +class mat<3, 4, T> +{ +public: + GLM_CLASS_TYPEDEF(T); + + typedef mat<3, 4, T> this_type; + typedef vec<4, T> col_type; + typedef vec<3, T> row_type; + + enum { + ROWS = 3, + COLS = 4, + ELEMENTS = 12, + }; + +public: + col_type m[COLS]; + +public: + mat() + { + } + + mat(value_type scalar) + { + const T zero = constants::zero; + m[0] = col_type(scalar, zero, zero, zero); + m[1] = col_type(zero, scalar, zero, zero); + m[2] = col_type(zero, zero, scalar, zero); + } + + mat(const col_type& v0, const col_type& v1, const col_type& v2) + { + m[0] = v0; + m[1] = v1; + m[2] = v2; + } + + mat(value_type m00, value_type m01, value_type m02, value_type m03, + value_type m10, value_type m11, value_type m12, value_type m13, + value_type m20, value_type m21, value_type m22, value_type m23) + { + m[0] = col_type(m00, m01, m02, m03); + m[1] = col_type(m10, m11, m12, m13); + m[2] = col_type(m20, m21, m22, m23); + } + + mat(const mat<2, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero, zero); + m[1] = col_type(other.m[1], zero, zero); + m[2] = col_type(zero, zero, one, zero); + } + + mat(const mat<2, 3, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + m[2] = col_type(zero, zero, one, zero); + } + + mat(const mat<2, 4, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = col_type(zero, zero, one, zero); + } + + mat(const mat<3, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero, zero); + m[1] = col_type(other.m[1], zero, zero); + m[2] = col_type(other.m[2], one, zero); + } + + mat(const mat<3, 3, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + m[2] = col_type(other.m[2], zero); + } + + mat(const mat<3, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + } + + mat(const mat<4, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero, zero); + m[1] = col_type(other.m[1], zero, zero); + m[2] = col_type(other.m[2], one, zero); + } + + mat(const mat<4, 3, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + m[2] = col_type(other.m[2], zero); + } + + mat(const mat<4, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + } + + length_t length()const + { + return COLS; + } + + size_t elements()const + { + return ELEMENTS; + } + + pointer data() + { + return &m[0][0]; + } + + const_pointer data()const + { + return &m[0][0]; + } + + col_type& operator[](size_t i) + { + return m[i]; + } + + const col_type& operator[](size_t i)const + { + return m[i]; + } +}; + +// +// functions +// + +// mat2x4 = mul( in mat3x4, in mat2x3 ) +template +GLM_API mat<2, 4, T> mat3x4_mul(const mat<3, 4, T>& m1, const mat<2, 3, T>& m2) +{ + mat<2, 4, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1] + m1[2][2] * m2[0][2]; + product[0][3] = m1[0][3] * m2[0][0] + m1[1][3] * m2[0][1] + m1[2][3] * m2[0][2]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1] + m1[2][2] * m2[1][2]; + product[1][3] = m1[0][3] * m2[1][0] + m1[1][3] * m2[1][1] + m1[2][3] * m2[1][2]; + + return product; +} + +// mat3x4 = mul( in mat3x4, in mat3 ) +template +GLM_API mat<3, 4, T> mat3x4_mul(const mat<3, 4, T>& m1, const mat<3, 3, T>& m2) +{ + mat<3, 4, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1] + m1[2][2] * m2[0][2]; + product[0][3] = m1[0][3] * m2[0][0] + m1[1][3] * m2[0][1] + m1[2][3] * m2[0][2]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1] + m1[2][2] * m2[1][2]; + product[1][3] = m1[0][3] * m2[1][0] + m1[1][3] * m2[1][1] + m1[2][3] * m2[1][2]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1] + m1[2][0] * m2[2][2]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1] + m1[2][1] * m2[2][2]; + product[2][2] = m1[0][2] * m2[2][0] + m1[1][2] * m2[2][1] + m1[2][2] * m2[2][2]; + product[2][3] = m1[0][3] * m2[2][0] + m1[1][3] * m2[2][1] + m1[2][3] * m2[2][2]; + + return product; +} + +// mat4 = mul( in mat3x4, in mat4x3 ) +template +GLM_API mat<4, 4, T> mat3x4_mul(const mat<3, 4, T>& m1, const mat<4, 3, T>& m2) +{ + mat<4, 4, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1] + m1[2][2] * m2[0][2]; + product[0][3] = m1[0][3] * m2[0][0] + m1[1][3] * m2[0][1] + m1[2][3] * m2[0][2]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1] + m1[2][2] * m2[1][2]; + product[1][3] = m1[0][3] * m2[1][0] + m1[1][3] * m2[1][1] + m1[2][3] * m2[1][2]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1] + m1[2][0] * m2[2][2]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1] + m1[2][1] * m2[2][2]; + product[2][2] = m1[0][2] * m2[2][0] + m1[1][2] * m2[2][1] + m1[2][2] * m2[2][2]; + product[2][3] = m1[0][3] * m2[2][0] + m1[1][3] * m2[2][1] + m1[2][3] * m2[2][2]; + product[3][0] = m1[0][0] * m2[3][0] + m1[1][0] * m2[3][1] + m1[2][0] * m2[3][2]; + product[3][1] = m1[0][1] * m2[3][0] + m1[1][1] * m2[3][1] + m1[2][1] * m2[3][2]; + product[3][2] = m1[0][2] * m2[3][0] + m1[1][2] * m2[3][1] + m1[2][2] * m2[3][2]; + product[3][3] = m1[0][3] * m2[3][0] + m1[1][3] * m2[3][1] + m1[2][3] * m2[3][2]; + + return product; +} + +// mat4x3 = transpose( in mat3x4 ) +template +GLM_API mat<4, 3, T> transpose(const mat<3, 4, T>& m) +{ + return mat4x3( + m[0][0], m[1][0], m[2][0], m[3][0], + m[0][1], m[1][1], m[2][1], m[3][1], + m[0][2], m[1][2], m[2][2], m[3][2]); +} + +// vec4 = transform( in vec4, in mat3x4 ) +template +GLM_API vec<4, T> transform(const vec<4, T>& v, const mat<3, 4, T>& m) +{ + return vec<3, T>( + v[0] * m[0][0] + v[1] * m[0][1] + v[2] * m[0][2] + v[3] * m[0][3], + v[0] * m[1][0] + v[1] * m[1][1] + v[2] * m[1][2] + v[3] * m[1][3], + v[0] * m[2][0] + v[1] * m[2][1] + v[2] * m[2][2] + v[3] * m[2][3]); +} + +// vec3 = transform ( in mat3x4, in vec3 ) +template +GLM_API vec<3, T> transform(const mat<3, 4, T>& m, const vec<3, T>& v) +{ + return vec<3, T>( + m[0][0] * v[0] + m[1][0] * v[1] + m[2][0] * v[2], + m[0][1] * v[0] + m[1][1] * v[1] + m[2][1] * v[2], + m[0][2] * v[0] + m[1][2] * v[1] + m[2][2] * v[2], + m[0][3] * v[0] + m[1][3] * v[1] + m[2][3] * v[2]); +} + +// +// operators +// + +// mat3x4 = mat3x4 + scalar +template +GLM_API mat<3, 4, T> operator+(const mat<3, 4, T>& m, T scalar) +{ + return mat<3, 4, T>(m[0] + scalar, m[1] + scalar, m[2] + scalar); +} + +// mat3x4 = mat3x4 - scalar +template +GLM_API mat<3, 4, T> operator-(const mat<3, 4, T>& m, T scalar) +{ + return mat<3, 4, T>(m[0] - scalar, m[1] - scalar, m[2] - scalar); +} + +// mat3x4 = mat3x4 * scalar +template +GLM_API mat<3, 4, T> operator*(const mat<3, 4, T>& m, T scalar) +{ + return mat<3, 4, T>(m[0] * scalar, m[1] * scalar, m[2] * scalar); +} + +// mat3x4 = mat3x4 / scalar +template +GLM_API mat<3, 4, T> operator/(const mat<3, 4, T>& m, T scalar) +{ + return mat<3, 4, T>(m[0] / scalar, m[1] / scalar, m[2] / scalar); +} + +// mat3x4 = scalar + mat3x4 +template +GLM_API mat<3, 4, T> operator+(T scalar, const mat<3, 4, T>& m) +{ + return mat<3, 4, T>(scalar + m[0], scalar + m[1], scalar + m[2]); +} + +// mat3x4 = scalar - mat3x4 +template +GLM_API mat<3, 4, T> operator-(T scalar, const mat<3, 4, T>& m) +{ + return mat<3, 4, T>(scalar - m[0], scalar - m[1], scalar - m[2]); +} + +// mat3x4 = scalar * mat3x4 +template +GLM_API mat<3, 4, T> operator*(T scalar, const mat<3, 4, T>& m) +{ + return mat<3, 4, T>(scalar * m[0], scalar * m[1], scalar * m[2]); +} + +// mat3x4 = scalar / mat3x4 +template +GLM_API mat<3, 4, T> operator/(T scalar, const mat<3, 4, T>& m) +{ + return mat<3, 4, T>(scalar / m[0], scalar / m[1], scalar / m[2]); +} + +// mat2x4 = mat3x4 x mat2x3 +template +GLM_API mat<2, 4, T> operator*(const mat<3, 4, T>& m1, const mat<2, 3, T>& m2) +{ + return mat3x4_mul(m1, m2); +} + +// mat3x4 = mat3x4 x mat3 +template +GLM_API mat<3, 4, T> operator*(const mat<3, 4, T>& m1, const mat<3, 3, T>& m2) +{ + return mat3x4_mul(m1, m2); +} + +// mat4 = mat3x4 x mat4x3 +template +GLM_API mat<4, 4, T> operator*(const mat<3, 4, T>& m1, const mat<4, 3, T>& m2) +{ + return mat3x4_mul(m1, m2); +} + +// vec4 = vec4 x mat3x4 +template +GLM_API vec<4, T> operator*(const vec<4, T>& v, const mat<3, 4, T>& m) +{ + return transform(v, m); +} + +// vec3 = mat3x4 x vec3 +template +GLM_API vec<3, T> operator*(const mat<3, 4, T>& v, const vec<3, T>& m) +{ + return transform(m, v); +} + +}// end namespace glm + +#endif// GLM_MAT3X4_HPP20220215152935 diff --git a/glm/mat4.hpp b/glm/mat4.hpp deleted file mode 100644 index fd92e77..0000000 --- a/glm/mat4.hpp +++ /dev/null @@ -1,543 +0,0 @@ -/* - Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) - - mat4.hpp - - 2018-04-15 19:45:04 - -*/ -#ifndef GLM_MAT4_HPP_20180415194504 -#define GLM_MAT4_HPP_20180415194504 - -#include "mat.hpp" -#include "simd/mat4.hpp" - - - -namespace glm{ - -// -// void mat4_add( in mat4, in T, out mat4 ) -// - -template -GLM_API void mat4_add(const T* m, T scalar, T* dest) -{ - dest[0] = m[0] + scalar; - dest[1] = m[1] + scalar; - dest[2] = m[2] + scalar; - dest[3] = m[3] + scalar; - dest[4] = m[4] + scalar; - dest[5] = m[5] + scalar; - dest[6] = m[6] + scalar; - dest[7] = m[7] + scalar; - dest[8] = m[8] + scalar; - dest[9] = m[9] + scalar; - dest[10] = m[10] + scalar; - dest[11] = m[11] + scalar; - dest[12] = m[12] + scalar; - dest[13] = m[13] + scalar; - dest[14] = m[14] + scalar; - dest[15] = m[15] + scalar; -} - -// -// void mat4_add( in mat4, in mat4, out mat4 ) -// - -template -GLM_API void mat4_add(const T* m1, const T* m2, T* dest) -{ - dest[0] = m1[0] + m2[0]; - dest[1] = m1[1] + m2[1]; - dest[2] = m1[2] + m2[2]; - dest[3] = m1[3] + m2[3]; - dest[4] = m1[4] + m2[4]; - dest[5] = m1[5] + m2[5]; - dest[6] = m1[6] + m2[6]; - dest[7] = m1[7] + m2[7]; - dest[8] = m1[8] + m2[8]; - dest[9] = m1[9] + m2[9]; - dest[10] = m1[10] + m2[10]; - dest[11] = m1[11] + m2[11]; - dest[12] = m1[12] + m2[12]; - dest[13] = m1[13] + m2[13]; - dest[14] = m1[14] + m2[14]; - dest[15] = m1[15] + m2[15]; -} - -// -// void mat4_sub( in mat4, in mat4, out mat4 ) -// - -template -GLM_API void mat4_sub(const T* m1, const T* m2, T* dest) -{ - dest[0] = m1[0] - m2[0]; - dest[1] = m1[1] - m2[1]; - dest[2] = m1[2] - m2[2]; - dest[3] = m1[3] - m2[3]; - dest[4] = m1[4] - m2[4]; - dest[5] = m1[5] - m2[5]; - dest[6] = m1[6] - m2[6]; - dest[7] = m1[7] - m2[7]; - dest[8] = m1[8] - m2[8]; - dest[9] = m1[9] - m2[9]; - dest[10] = m1[10] - m2[10]; - dest[11] = m1[11] - m2[11]; - dest[12] = m1[12] - m2[12]; - dest[13] = m1[13] - m2[13]; - dest[14] = m1[14] - m2[14]; - dest[15] = m1[15] - m2[15]; -} - -// -// void mat4_mul( in mat4, in T, out mat4 ) -// - -template -GLM_API void mat4_mul(const T* m, T scalar, T* dest) -{ - dest[0] = m[0] * scalar; - dest[1] = m[1] * scalar; - dest[2] = m[2] * scalar; - dest[3] = m[3] * scalar; - dest[4] = m[4] * scalar; - dest[5] = m[5] * scalar; - dest[6] = m[6] * scalar; - dest[7] = m[7] * scalar; - dest[8] = m[8] * scalar; - dest[9] = m[9] * scalar; - dest[10] = m[10] * scalar; - dest[11] = m[11] * scalar; - dest[12] = m[12] * scalar; - dest[13] = m[13] * scalar; - dest[14] = m[14] * scalar; - dest[15] = m[15] * scalar; -} - -// -// void mat4_mul( in mat4, in mat4, out mat4 ) -// - -template -GLM_API void mat4_mul(const T* m1, const T* m2, T* dest) -{ - T product[16]; - - product[0] = m1[0] * m2[0] + m1[4] * m2[1] + m1[8] * m2[2] + m1[12] * m2[3]; - product[1] = m1[1] * m2[0] + m1[5] * m2[1] + m1[9] * m2[2] + m1[13] * m2[3]; - product[2] = m1[2] * m2[0] + m1[6] * m2[1] + m1[10] * m2[2] + m1[14] * m2[3]; - product[3] = m1[3] * m2[0] + m1[7] * m2[1] + m1[11] * m2[2] + m1[15] * m2[3]; - - product[4] = m1[0] * m2[4] + m1[4] * m2[5] + m1[8] * m2[6] + m1[12] * m2[7]; - product[5] = m1[1] * m2[4] + m1[5] * m2[5] + m1[9] * m2[6] + m1[13] * m2[7]; - product[6] = m1[2] * m2[4] + m1[6] * m2[5] + m1[10] * m2[6] + m1[14] * m2[7]; - product[7] = m1[3] * m2[4] + m1[7] * m2[5] + m1[11] * m2[6] + m1[15] * m2[7]; - - product[8] = m1[0] * m2[8] + m1[4] * m2[9] + m1[8] * m2[10] + m1[12] * m2[11]; - product[9] = m1[1] * m2[8] + m1[5] * m2[9] + m1[9] * m2[10] + m1[13] * m2[11]; - product[10] = m1[2] * m2[8] + m1[6] * m2[9] + m1[10] * m2[10] + m1[14] * m2[11]; - product[11] = m1[3] * m2[8] + m1[7] * m2[9] + m1[11] * m2[10] + m1[15] * m2[11]; - - product[12] = m1[0] * m2[12] + m1[4] * m2[13] + m1[8] * m2[14] + m1[12] * m2[15]; - product[13] = m1[1] * m2[12] + m1[5] * m2[13] + m1[9] * m2[14] + m1[13] * m2[15]; - product[14] = m1[2] * m2[12] + m1[6] * m2[13] + m1[10] * m2[14] + m1[14] * m2[15]; - product[15] = m1[3] * m2[12] + m1[7] * m2[13] + m1[11] * m2[14] + m1[15] * m2[15]; - - memcpy(dest, product, sizeof(product)); -} - -// -// void mat4_transpose( inout mat4 ) -// - -template -GLM_API void mat4_transpose(T* m) -{ - std::swap(m[1], m[4]); - std::swap(m[2], m[8]); - std::swap(m[3], m[12]); - std::swap(m[6], m[9]); - std::swap(m[7], m[13]); - std::swap(m[11], m[14]); -} - -// -// void mat4_transpose( in mat4, out mat4 ) -// - -template -GLM_API void mat4_transpose(const T* m, T* dest) -{ - T product[16] = { - m[0], m[4], m[8], m[12], - m[1], m[5], m[9], m[13], - m[2], m[6], m[10], m[14], - m[3], m[7], m[11], m[15] - }; - - memcpy(dest, product, sizeof(product)); -} - -// -// void mat4_transpose_copy( in mat4, out mat4 ) -// - -template -GLM_API void mat4_transpose_copy(const T* m, T* dest) -{ - assert(m != dest); - dest[0] = m[0]; dest[1] = m[4]; dest[2] = m[8]; dest[3] = m[12]; - dest[4] = m[1]; dest[5] = m[5]; dest[6] = m[9]; dest[7] = m[13]; - dest[8] = m[2]; dest[9] = m[6]; dest[10] = m[10]; dest[11] = m[14]; - dest[12] = m[3]; dest[13] = m[7]; dest[14] = m[11]; dest[15] = m[15]; -} - -// -// bool mat4_determinant( in mat4, out T ) -// - -template -GLM_API bool mat4_determinant(const T* m, T& d) -{ - T a0 = m[0] * m[5] - m[1] * m[4]; - T a1 = m[0] * m[6] - m[2] * m[4]; - T a2 = m[0] * m[7] - m[3] * m[4]; - T a3 = m[1] * m[6] - m[2] * m[5]; - T a4 = m[1] * m[7] - m[3] * m[5]; - T a5 = m[2] * m[7] - m[3] * m[6]; - T b0 = m[8] * m[13] - m[9] * m[12]; - T b1 = m[8] * m[14] - m[10] * m[12]; - T b2 = m[8] * m[15] - m[11] * m[12]; - T b3 = m[9] * m[14] - m[10] * m[13]; - T b4 = m[9] * m[15] - m[11] * m[13]; - T b5 = m[10] * m[15] - m[11] * m[14]; - - // Calculate the determinant. - d = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0; - - if (abs(d) < constants::epsilon) { - return false; - } - - d = constants::one / d; - - return true; -} - -// -// bool mat4_inverse( in mat4, out mat4 ) -// - -template -GLM_API bool mat4_inverse(const T* m, T* dest) -{ - T product[16]; - T d; - - T a0 = m[0] * m[5] - m[1] * m[4]; - T a1 = m[0] * m[6] - m[2] * m[4]; - T a2 = m[0] * m[7] - m[3] * m[4]; - T a3 = m[1] * m[6] - m[2] * m[5]; - T a4 = m[1] * m[7] - m[3] * m[5]; - T a5 = m[2] * m[7] - m[3] * m[6]; - T b0 = m[8] * m[13] - m[9] * m[12]; - T b1 = m[8] * m[14] - m[10] * m[12]; - T b2 = m[8] * m[15] - m[11] * m[12]; - T b3 = m[9] * m[14] - m[10] * m[13]; - T b4 = m[9] * m[15] - m[11] * m[13]; - T b5 = m[10] * m[15] - m[11] * m[14]; - - // Calculate the determinant. - d = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0; - - if (abs(d) < constants::epsilon) { - return false; - } - - d = constants::one / d; - - if (mat4_determinant(m, d) == false) { - return false; - } - - product[0] = m[5] * b5 - m[6] * b4 + m[7] * b3; - product[1] = -m[1] * b5 + m[2] * b4 - m[3] * b3; - product[2] = m[13] * a5 - m[14] * a4 + m[15] * a3; - product[3] = -m[9] * a5 + m[10] * a4 - m[11] * a3; - - product[4] = -m[4] * b5 + m[6] * b2 - m[7] * b1; - product[5] = m[0] * b5 - m[2] * b2 + m[3] * b1; - product[6] = -m[12] * a5 + m[14] * a2 - m[15] * a1; - product[7] = m[8] * a5 - m[10] * a2 + m[11] * a1; - - product[8] = m[4] * b4 - m[5] * b2 + m[7] * b0; - product[9] = -m[0] * b4 + m[1] * b2 - m[3] * b0; - product[10] = m[12] * a4 - m[13] * a2 + m[15] * a0; - product[11] = -m[8] * a4 + m[9] * a2 - m[11] * a0; - - product[12] = -m[4] * b3 + m[5] * b1 - m[6] * b0; - product[13] = m[0] * b3 - m[1] * b1 + m[2] * b0; - product[14] = -m[12] * a3 + m[13] * a1 - m[14] * a0; - product[15] = m[8] * a3 - m[9] * a1 + m[10] * a0; - - mat4_mul(product, d, dest); - - return true; -} - -// -// void mat4_transform( in mat4, in vec4, out vec4 ) -// - -template -GLM_API void mat4_transform(const T* m, const T* v, T* dest) -{ - T product[4]; - - product[0] = v[0] * m[0] + v[1] * m[4] + v[2] * m[8] + v[3] * m[12]; - product[1] = v[0] * m[1] + v[1] * m[5] + v[2] * m[9] + v[3] * m[13]; - product[2] = v[0] * m[2] + v[1] * m[6] + v[2] * m[10] + v[3] * m[14]; - product[3] = v[0] * m[3] + v[1] * m[7] + v[2] * m[11] + v[3] * m[15]; - - memcpy(dest, product, sizeof(product)); -} - -// -// class mat<4, 4, T> -// - -template -class mat<4, 4, T> : public constants -{ -public: - GLM_CLASS_TYPEDEF(T); - - typedef mat<4, 4, T> this_type; - typedef vec<4, T> row_type; - - enum{ - COLS = 4, - ROWS = 4, - ELEMENTS = 16 - }; - -public: - row_type m[ROWS]; - -public: - mat() - { - } - - mat(value_type scalar) - { - const T zero = constants::zero; - m[0] = row_type(scalar, zero, zero, zero); - m[1] = row_type(zero, scalar, zero, zero); - m[2] = row_type(zero, zero, scalar, zero); - m[3] = row_type(zero, zero, zero, scalar); - } - - mat(const row_type& a, const row_type& b, const row_type& c, const row_type& d) - { - m[0] = a; - m[1] = b; - m[2] = c; - m[3] = d; - } - - mat( - value_type m00, value_type m01, value_type m02, value_type m03, - value_type m10, value_type m11, value_type m12, value_type m13, - value_type m20, value_type m21, value_type m22, value_type m23, - value_type m30, value_type m31, value_type m32, value_type m33) - { - m[0] = row_type(m00, m01, m02, m03); - m[1] = row_type(m10, m11, m12, m13); - m[2] = row_type(m20, m21, m22, m23); - m[3] = row_type(m30, m31, m32, m33); - } - - mat(const mat<3, 3, T>& other) - { - const T zero = constants::zero; - const T one = constants::zero; - - m[0] = row_type(other[0], zero); - m[1] = row_type(other[1], zero); - m[2] = row_type(other[2], zero); - m[3] = row_type(zero, zero, zero, one); - } - - mat(const mat<4, 4, T>& other) - { - memcpy(data(), other.data(), sizeof(T) * ELEMENTS); - } - - length_t length()const - { - return ELEMENTS; - } - - pointer data() - { - return &m[0][0]; - } - - const_pointer data()const - { - return &m[0][0]; - } - - row_type& operator[](size_t i) - { - return m[i]; - } - - const row_type& operator[](size_t i)const - { - return m[i]; - } - - reference operator()(size_t x, size_t y) - { - return m[y][x]; - } - - const_reference operator()(size_t x, size_t y)const - { - return m[y][x]; - } -}; - -// -// operator -// - -template -GLM_API mat<4, 4, T> operator +(const mat<4, 4, T>& m, T n) -{ - mat<4, 4, T> product; - mat4_add(m.data(), n, product.data()); - return product; -} - -template -GLM_API mat<4, 4, T> operator +(const mat<4, 4, T>& m1, const mat<4, 4, T>& m2) -{ - mat<4, 4, T> product; - mat4_add(m1.data(), m2.data(), product.data()); - return product; -} - -template -GLM_API mat<4, 4, T> operator -(const mat<4, 4, T>& m, T n) -{ - mat<4, 4, T> product; - mat4_mul(m.data(), -n, product.data()); - return product; -} - -template -GLM_API mat<4, 4, T> operator -(const mat<4, 4, T>& m1, const mat<4, 4, T>& m2) -{ - mat<4, 4, T> product; - mat4_sub(m1.data(), m2.data(), product.data()); - return product; -} - -template -GLM_API mat<4, 4, T> operator *(const mat<4, 4, T>& m, T n) -{ - mat<4, 4, T> product; - mat4_mul(m.data(), n, product.data()); - return product; -} - -template -GLM_API mat<4, 4, T> operator *(const mat<4, 4, T>& m1, const mat<4, 4, T>& m2) -{ - mat<4, 4, T> product; - mat4_mul(m1.data(), m2.data(), product.data()); - return product; -} - -template -GLM_API mat<4, 4, T> operator /(const mat<4, 4, T>& m, T n) -{ - mat<4, 4, T> product; - mat4_mul(m.data(), constants::one / n, product.data()); - return product; -} - -// transform -template -GLM_API vec<4, T> operator *(const mat<4, 4, T>& m, const vec<4, T>& v) -{ - vec<4, T> product; - mat4_transform(m.data(), v.data(), product.data()); - return product; -} - -// -// mat4 transpose( in mat4 ) -// - -template -GLM_API mat<4, 4, T> transpose(const mat<4, 4, T>& m) -{ - mat<4, 4, T> product; - mat4_transpose_copy(m.data(), product.data()); - return product; -} - -// -// T determinant( in mat4 ) -// - -template -GLM_API T determinant(const mat<4, 4, T>& m) -{ - T d; - if (mat4_determinant(m.data(), d)) { - return d; - } - else { - return T(); - } -} - -// -// mat4 inverse( in mat4 ) -// - -template -GLM_API mat<4, 4, T> inverse(const mat<4, 4, T>& m) -{ - mat<4, 4, T> product; - mat4_inverse(m.data(), product.data()); - return product; -} - - - -// -// extension -// - -// 获得摄像机位置 -template -GLM_API glm::vec<4, T> viewPosition(const glm::mat<4, 4, T>& m) -{ - glm::vec<4, T> product; - product.x = -(m[0][0] * m[3][0] + m[0][1] * m[3][1] + m[0][2] * m[3][2]); - product.y = -(m[1][0] * m[3][0] + m[1][1] * m[3][1] + m[0][2] * m[3][2]); - product.z = -(m[2][0] * m[3][0] + m[2][1] * m[3][1] + m[2][2] * m[3][2]); - return product; -} - -}// end namespace glm - -#endif// GLM_MAT4_HPP_20180415194504 diff --git a/glm/mat4x2.hpp b/glm/mat4x2.hpp new file mode 100644 index 0000000..b464283 --- /dev/null +++ b/glm/mat4x2.hpp @@ -0,0 +1,369 @@ +/* + Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) + + mat4x2.hpp + + 2022-02-15 15:29:35 + +*/ +#ifndef GLM_MAT4X2_HPP20220215152935 +#define GLM_MAT4X2_HPP20220215152935 + +#include "matrix.hpp" + + + +namespace glm { + +// +// class mat<4, 2, T> +// + +template +class mat<4, 2, T> +{ +public: + GLM_CLASS_TYPEDEF(T); + + typedef mat<4, 2, T> this_type; + typedef vec<2, T> col_type; + typedef vec<4, T> row_type; + + enum { + ROWS = 4, + COLS = 2, + ELEMENTS = 8, + }; + +public: + col_type m[COLS]; + +public: + mat() + { + } + + mat(value_type scalar) + { + const T zero = constants::zero; + m[0] = col_type(scalar, zero); + m[1] = col_type(zero, scalar); + m[2] = col_type(zero, zero); + m[3] = col_type(zero, zero); + } + + mat(const col_type& v0, const col_type& v1, const col_type& v2, const col_type& v3) + { + m[0] = v0; + m[1] = v1; + m[2] = v2; + m[3] = v3; + } + + mat(value_type m00, value_type m01, + value_type m10, value_type m11, + value_type m20, value_type m21, + value_type m30, value_type m31) + { + m[0] = col_type(m00, m01); + m[1] = col_type(m10, m11); + m[2] = col_type(m20, m21); + m[3] = col_type(m30, m31); + } + + mat(const mat<2, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = col_type(zero, zero); + m[3] = col_type(zero, zero); + } + + mat(const mat<2, 3, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = col_type(zero, zero); + m[3] = col_type(zero, zero); + } + + mat(const mat<2, 4, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = col_type(zero, zero); + m[3] = col_type(zero, zero); + } + + mat(const mat<3, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + m[3] = col_type(zero, zero); + } + + mat(const mat<3, 3, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + m[3] = col_type(zero, zero); + } + + mat(const mat<3, 4, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + m[3] = col_type(zero, zero); + } + + mat(const mat<4, 2, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + m[3] = other.m[3]; + } + + mat(const mat<4, 3, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + m[3] = other.m[3]; + } + + mat(const mat<4, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + m[3] = other.m[3]; + } + + length_t length()const + { + return COLS; + } + + size_t elements()const + { + return ELEMENTS; + } + + pointer data() + { + return &m[0][0]; + } + + const_pointer data()const + { + return &m[0][0]; + } + + col_type& operator[](size_t i) + { + return m[i]; + } + + const col_type& operator[](size_t i)const + { + return m[i]; + } +}; + +// +// functions +// + +// mat2 = mul( in mat4x2, in mat2x4 ) +template +GLM_API mat<2, 2, T> mat4x2_mul(const mat<4, 2, T>& m1, const mat<2, 4, T>& m2) +{ + mat<2, 2, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2] + m1[3][0] * m2[0][3]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2] + m1[3][1] * m2[0][3]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2] + m1[3][0] * m2[1][3]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2] + m1[3][1] * m2[1][3]; + + return product; +} + +// mat3x2 = mul( in mat4x2, in mat3x4 ) +template +GLM_API mat<3, 2, T> mat4x2_mul(const mat<4, 2, T>& m1, const mat<3, 4, T>& m2) +{ + mat<3, 2, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2] + m1[3][0] * m2[0][3]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2] + m1[3][1] * m2[0][3]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2] + m1[3][0] * m2[1][3]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2] + m1[3][1] * m2[1][3]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1] + m1[2][0] * m2[2][2] + m1[3][0] * m2[2][3]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1] + m1[2][1] * m2[2][2] + m1[3][1] * m2[2][3]; + + return product; +} + +// mat4x2 = mul( in mat4x2, in mat4 ) +template +GLM_API mat<4, 2, T> mat4x2_mul(const mat<4, 2, T>& m1, const mat<4, 4, T>& m2) +{ + mat<4, 2, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2] + m1[3][0] * m2[0][3]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2] + m1[3][1] * m2[0][3]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2] + m1[3][0] * m2[1][3]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2] + m1[3][1] * m2[1][3]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1] + m1[2][0] * m2[2][2] + m1[3][0] * m2[2][3]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1] + m1[2][1] * m2[2][2] + m1[3][1] * m2[2][3]; + product[3][0] = m1[0][0] * m2[3][0] + m1[1][0] * m2[3][1] + m1[2][0] * m2[3][2] + m1[3][0] * m2[3][3]; + product[3][1] = m1[0][1] * m2[3][0] + m1[1][1] * m2[3][1] + m1[2][1] * m2[3][2] + m1[3][1] * m2[3][3]; + + return product; +} + +// mat2x4 = transpose( in mat4x2 ) +template +GLM_API mat<2, 4, T> transpose(const mat<4, 2, T>& m) +{ + return mat2x4( + m[0][0], m[1][0], + m[0][1], m[1][1], + m[0][2], m[1][2], + m[0][3], m[1][3]); +} + +// vec2 = transform( in vec2, in mat4x2 ) +template +GLM_API vec<2, T> transform(const vec<2, T>& v, const mat<4, 2, T>& m) +{ + return vec<4, T>( + v[0] * m[0][0] + v[1] * m[0][1], + v[0] * m[1][0] + v[1] * m[1][1], + v[0] * m[2][0] + v[1] * m[2][1], + v[0] * m[3][0] + v[1] * m[3][1]); +} + +// vec4 = transform ( in mat4x2, in vec4 ) +template +GLM_API vec<4, T> transform(const mat<4, 2, T>& m, const vec<4, T>& v) +{ + return vec<4, T>( + m[0][0] * v[0] + m[1][0] * v[1] + m[2][0] * v[2] + m[3][0] * v[3], + m[0][1] * v[0] + m[1][1] * v[1] + m[2][1] * v[2] + m[3][1] * v[3]); +} + +// +// operators +// + +// mat4x2 = mat4x2 + scalar +template +GLM_API mat<4, 2, T> operator+(const mat<4, 2, T>& m, T scalar) +{ + return mat<4, 2, T>(m[0] + scalar, m[1] + scalar, m[2] + scalar, m[3] + scalar); +} + +// mat4x2 = mat4x2 - scalar +template +GLM_API mat<4, 2, T> operator-(const mat<4, 2, T>& m, T scalar) +{ + return mat<4, 2, T>(m[0] - scalar, m[1] - scalar, m[2] - scalar, m[3] - scalar); +} + +// mat4x2 = mat4x2 * scalar +template +GLM_API mat<4, 2, T> operator*(const mat<4, 2, T>& m, T scalar) +{ + return mat<4, 2, T>(m[0] * scalar, m[1] * scalar, m[2] * scalar, m[3] * scalar); +} + +// mat4x2 = mat4x2 / scalar +template +GLM_API mat<4, 2, T> operator/(const mat<4, 2, T>& m, T scalar) +{ + return mat<4, 2, T>(m[0] / scalar, m[1] / scalar, m[2] / scalar, m[3] / scalar); +} + +// mat4x2 = scalar + mat4x2 +template +GLM_API mat<4, 2, T> operator+(T scalar, const mat<4, 2, T>& m) +{ + return mat<4, 2, T>(scalar + m[0], scalar + m[1], scalar + m[2], scalar + m[3]); +} + +// mat4x2 = scalar - mat4x2 +template +GLM_API mat<4, 2, T> operator-(T scalar, const mat<4, 2, T>& m) +{ + return mat<4, 2, T>(scalar - m[0], scalar - m[1], scalar - m[2], scalar - m[3]); +} + +// mat4x2 = scalar * mat4x2 +template +GLM_API mat<4, 2, T> operator*(T scalar, const mat<4, 2, T>& m) +{ + return mat<4, 2, T>(scalar * m[0], scalar * m[1], scalar * m[2], scalar * m[3]); +} + +// mat4x2 = scalar / mat4x2 +template +GLM_API mat<4, 2, T> operator/(T scalar, const mat<4, 2, T>& m) +{ + return mat<4, 2, T>(scalar / m[0], scalar / m[1], scalar / m[2], scalar / m[3]); +} + +// mat2 = mat4x2 x mat2x4 +template +GLM_API mat<2, 2, T> operator*(const mat<4, 2, T>& m1, const mat<2, 4, T>& m2) +{ + return mat4x2_mul(m1, m2); +} + +// mat3x2 = mat4x2 x mat3x4 +template +GLM_API mat<3, 2, T> operator*(const mat<4, 2, T>& m1, const mat<3, 4, T>& m2) +{ + return mat4x2_mul(m1, m2); +} + +// mat4x2 = mat4x2 x mat4 +template +GLM_API mat<4, 2, T> operator*(const mat<4, 2, T>& m1, const mat<4, 4, T>& m2) +{ + return mat4x2_mul(m1, m2); +} + +// vec2 = vec2 x mat4x2 +template +GLM_API vec<2, T> operator*(const vec<2, T>& v, const mat<4, 2, T>& m) +{ + return transform(v, m); +} + +// vec4 = mat4x2 x vec4 +template +GLM_API vec<4, T> operator*(const mat<4, 2, T>& v, const vec<4, T>& m) +{ + return transform(m, v); +} + +}// end namespace glm + +#endif// GLM_MAT4X2_HPP20220215152935 diff --git a/glm/mat4x3.hpp b/glm/mat4x3.hpp new file mode 100644 index 0000000..fe6f842 --- /dev/null +++ b/glm/mat4x3.hpp @@ -0,0 +1,381 @@ +/* + Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) + + mat4x3.hpp + + 2022-02-15 15:29:35 + +*/ +#ifndef GLM_MAT4X3_HPP20220215152935 +#define GLM_MAT4X3_HPP20220215152935 + +#include "matrix.hpp" + + + +namespace glm { + +// +// class mat<4, 3, T> +// + +template +class mat<4, 3, T> +{ +public: + GLM_CLASS_TYPEDEF(T); + + typedef mat<4, 3, T> this_type; + typedef vec<3, T> col_type; + typedef vec<4, T> row_type; + + enum { + ROWS = 4, + COLS = 3, + ELEMENTS = 12, + }; + +public: + col_type m[COLS]; + +public: + mat() + { + } + + mat(value_type scalar) + { + const T zero = constants::zero; + m[0] = col_type(scalar, zero, zero); + m[1] = col_type(zero, scalar, zero); + m[2] = col_type(zero, zero, scalar); + m[3] = col_type(zero, zero, zero); + } + + mat(const col_type& v0, const col_type& v1, const col_type& v2, const col_type& v3) + { + m[0] = v0; + m[1] = v1; + m[2] = v2; + m[3] = v3; + } + + mat(value_type m00, value_type m01, value_type m02, + value_type m10, value_type m11, value_type m12, + value_type m20, value_type m21, value_type m22, + value_type m30, value_type m31, value_type m32) + { + m[0] = col_type(m00, m01, m02); + m[1] = col_type(m10, m11, m12); + m[2] = col_type(m20, m21, m22); + m[3] = col_type(m30, m31, m32); + } + + mat(const mat<2, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + m[2] = col_type(zero, zero, one); + m[3] = col_type(zero, zero, zero); + } + + mat(const mat<2, 3, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = col_type(zero, zero, one); + m[3] = col_type(zero, zero, zero); + } + + mat(const mat<2, 4, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = col_type(zero, zero, one); + m[3] = col_type(zero, zero, zero); + } + + mat(const mat<3, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + m[2] = col_type(other.m[2], one); + m[3] = col_type(zero, zero, zero); + } + + mat(const mat<3, 3, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + m[3] = col_type(zero, zero, zero); + } + + mat(const mat<3, 4, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + m[3] = col_type(zero, zero, zero); + } + + mat(const mat<4, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + m[2] = col_type(other.m[2], one); + m[3] = col_type(other.m[3], zero); + } + + mat(const mat<4, 3, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + m[3] = other.m[3]; + } + + mat(const mat<4, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + m[3] = other.m[3]; + } + + length_t length()const + { + return COLS; + } + + size_t elements()const + { + return ELEMENTS; + } + + pointer data() + { + return &m[0][0]; + } + + const_pointer data()const + { + return &m[0][0]; + } + + col_type& operator[](size_t i) + { + return m[i]; + } + + const col_type& operator[](size_t i)const + { + return m[i]; + } +}; + +// +// functions +// + +// mat2x3 = mul( in mat4x3, in mat2x4 ) +template +GLM_API mat<2, 3, T> mat4x3_mul(const mat<4, 3, T>& m1, const mat<2, 4, T>& m2) +{ + mat<2, 3, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2] + m1[3][0] * m2[0][3]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2] + m1[3][1] * m2[0][3]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1] + m1[2][2] * m2[0][2] + m1[3][2] * m2[0][3]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2] + m1[3][0] * m2[1][3]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2] + m1[3][1] * m2[1][3]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1] + m1[2][2] * m2[1][2] + m1[3][2] * m2[1][3]; + + return product; +} + +// mat3 = mul( in mat4x3, in mat3x4 ) +template +GLM_API mat<3, 3, T> mat4x3_mul(const mat<4, 3, T>& m1, const mat<3, 4, T>& m2) +{ + mat<3, 3, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2] + m1[3][0] * m2[0][3]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2] + m1[3][1] * m2[0][3]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1] + m1[2][2] * m2[0][2] + m1[3][2] * m2[0][3]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2] + m1[3][0] * m2[1][3]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2] + m1[3][1] * m2[1][3]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1] + m1[2][2] * m2[1][2] + m1[3][2] * m2[1][3]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1] + m1[2][0] * m2[2][2] + m1[3][0] * m2[2][3]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1] + m1[2][1] * m2[2][2] + m1[3][1] * m2[2][3]; + product[2][2] = m1[0][2] * m2[2][0] + m1[1][2] * m2[2][1] + m1[2][2] * m2[2][2] + m1[3][2] * m2[2][3]; + + return product; +} + +// mat4x3 = mul( in mat4x3, in mat4 ) +template +GLM_API mat<4, 3, T> mat4x3_mul(const mat<4, 3, T>& m1, const mat<4, 4, T>& m2) +{ + mat<4, 3, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2] + m1[3][0] * m2[0][3]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2] + m1[3][1] * m2[0][3]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1] + m1[2][2] * m2[0][2] + m1[3][2] * m2[0][3]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2] + m1[3][0] * m2[1][3]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2] + m1[3][1] * m2[1][3]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1] + m1[2][2] * m2[1][2] + m1[3][2] * m2[1][3]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1] + m1[2][0] * m2[2][2] + m1[3][0] * m2[2][3]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1] + m1[2][1] * m2[2][2] + m1[3][1] * m2[2][3]; + product[2][2] = m1[0][2] * m2[2][0] + m1[1][2] * m2[2][1] + m1[2][2] * m2[2][2] + m1[3][2] * m2[2][3]; + product[3][0] = m1[0][0] * m2[3][0] + m1[1][0] * m2[3][1] + m1[2][0] * m2[3][2] + m1[3][0] * m2[3][3]; + product[3][1] = m1[0][1] * m2[3][0] + m1[1][1] * m2[3][1] + m1[2][1] * m2[3][2] + m1[3][1] * m2[3][3]; + product[3][2] = m1[0][2] * m2[3][0] + m1[1][2] * m2[3][1] + m1[2][2] * m2[3][2] + m1[3][2] * m2[3][3]; + + return product; +} + +// mat3x4 = transpose( in mat4x3 ) +template +GLM_API mat<3, 4, T> transpose(const mat<4, 3, T>& m) +{ + return mat3x4( + m[0][0], m[1][0], m[2][0], + m[0][1], m[1][1], m[2][1], + m[0][2], m[1][2], m[2][2], + m[0][3], m[1][3], m[2][3]); +} + +// vec3 = transform( in vec3, in mat4x3 ) +template +GLM_API vec<3, T> transform(const vec<3, T>& v, const mat<4, 3, T>& m) +{ + return vec<4, T>( + v[0] * m[0][0] + v[1] * m[0][1] + v[2] * m[0][2], + v[0] * m[1][0] + v[1] * m[1][1] + v[2] * m[1][2], + v[0] * m[2][0] + v[1] * m[2][1] + v[2] * m[2][2], + v[0] * m[3][0] + v[1] * m[3][1] + v[2] * m[3][2]); +} + +// vec4 = transform ( in mat4x3, in vec4 ) +template +GLM_API vec<4, T> transform(const mat<4, 3, T>& m, const vec<4, T>& v) +{ + return vec<4, T>( + m[0][0] * v[0] + m[1][0] * v[1] + m[2][0] * v[2] + m[3][0] * v[3], + m[0][1] * v[0] + m[1][1] * v[1] + m[2][1] * v[2] + m[3][1] * v[3], + m[0][2] * v[0] + m[1][2] * v[1] + m[2][2] * v[2] + m[3][2] * v[3]); +} + +// +// operators +// + +// mat4x3 = mat4x3 + scalar +template +GLM_API mat<4, 3, T> operator+(const mat<4, 3, T>& m, T scalar) +{ + return mat<4, 3, T>(m[0] + scalar, m[1] + scalar, m[2] + scalar, m[3] + scalar); +} + +// mat4x3 = mat4x3 - scalar +template +GLM_API mat<4, 3, T> operator-(const mat<4, 3, T>& m, T scalar) +{ + return mat<4, 3, T>(m[0] - scalar, m[1] - scalar, m[2] - scalar, m[3] - scalar); +} + +// mat4x3 = mat4x3 * scalar +template +GLM_API mat<4, 3, T> operator*(const mat<4, 3, T>& m, T scalar) +{ + return mat<4, 3, T>(m[0] * scalar, m[1] * scalar, m[2] * scalar, m[3] * scalar); +} + +// mat4x3 = mat4x3 / scalar +template +GLM_API mat<4, 3, T> operator/(const mat<4, 3, T>& m, T scalar) +{ + return mat<4, 3, T>(m[0] / scalar, m[1] / scalar, m[2] / scalar, m[3] / scalar); +} + +// mat4x3 = scalar + mat4x3 +template +GLM_API mat<4, 3, T> operator+(T scalar, const mat<4, 3, T>& m) +{ + return mat<4, 3, T>(scalar + m[0], scalar + m[1], scalar + m[2], scalar + m[3]); +} + +// mat4x3 = scalar - mat4x3 +template +GLM_API mat<4, 3, T> operator-(T scalar, const mat<4, 3, T>& m) +{ + return mat<4, 3, T>(scalar - m[0], scalar - m[1], scalar - m[2], scalar - m[3]); +} + +// mat4x3 = scalar * mat4x3 +template +GLM_API mat<4, 3, T> operator*(T scalar, const mat<4, 3, T>& m) +{ + return mat<4, 3, T>(scalar * m[0], scalar * m[1], scalar * m[2], scalar * m[3]); +} + +// mat4x3 = scalar / mat4x3 +template +GLM_API mat<4, 3, T> operator/(T scalar, const mat<4, 3, T>& m) +{ + return mat<4, 3, T>(scalar / m[0], scalar / m[1], scalar / m[2], scalar / m[3]); +} + +// mat2x3 = mat4x3 x mat2x4 +template +GLM_API mat<2, 3, T> operator*(const mat<4, 3, T>& m1, const mat<2, 4, T>& m2) +{ + return mat4x3_mul(m1, m2); +} + +// mat3 = mat4x3 x mat3x4 +template +GLM_API mat<3, 3, T> operator*(const mat<4, 3, T>& m1, const mat<3, 4, T>& m2) +{ + return mat4x3_mul(m1, m2); +} + +// mat4x3 = mat4x3 x mat4 +template +GLM_API mat<4, 3, T> operator*(const mat<4, 3, T>& m1, const mat<4, 4, T>& m2) +{ + return mat4x3_mul(m1, m2); +} + +// vec3 = vec3 x mat4x3 +template +GLM_API vec<3, T> operator*(const vec<3, T>& v, const mat<4, 3, T>& m) +{ + return transform(v, m); +} + +// vec4 = mat4x3 x vec4 +template +GLM_API vec<4, T> operator*(const mat<4, 3, T>& v, const vec<4, T>& m) +{ + return transform(m, v); +} + +}// end namespace glm + +#endif// GLM_MAT4X3_HPP20220215152935 diff --git a/glm/mat4x4.hpp b/glm/mat4x4.hpp new file mode 100644 index 0000000..7d586b8 --- /dev/null +++ b/glm/mat4x4.hpp @@ -0,0 +1,393 @@ +/* + Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) + + mat4x4.hpp + + 2022-02-15 15:29:35 + +*/ +#ifndef GLM_MAT4X4_HPP20220215152935 +#define GLM_MAT4X4_HPP20220215152935 + +#include "matrix.hpp" + + + +namespace glm { + +// +// class mat<4, 4, T> +// + +template +class mat<4, 4, T> +{ +public: + GLM_CLASS_TYPEDEF(T); + + typedef mat<4, 4, T> this_type; + typedef vec<4, T> col_type; + typedef vec<4, T> row_type; + + enum { + ROWS = 4, + COLS = 4, + ELEMENTS = 16, + }; + +public: + col_type m[COLS]; + +public: + mat() + { + } + + mat(value_type scalar) + { + const T zero = constants::zero; + m[0] = col_type(scalar, zero, zero, zero); + m[1] = col_type(zero, scalar, zero, zero); + m[2] = col_type(zero, zero, scalar, zero); + m[3] = col_type(zero, zero, zero, scalar); + } + + mat(const col_type& v0, const col_type& v1, const col_type& v2, const col_type& v3) + { + m[0] = v0; + m[1] = v1; + m[2] = v2; + m[3] = v3; + } + + mat(value_type m00, value_type m01, value_type m02, value_type m03, + value_type m10, value_type m11, value_type m12, value_type m13, + value_type m20, value_type m21, value_type m22, value_type m23, + value_type m30, value_type m31, value_type m32, value_type m33) + { + m[0] = col_type(m00, m01, m02, m03); + m[1] = col_type(m10, m11, m12, m13); + m[2] = col_type(m20, m21, m22, m23); + m[3] = col_type(m30, m31, m32, m33); + } + + mat(const mat<2, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero, zero); + m[1] = col_type(other.m[1], zero, zero); + m[2] = col_type(zero, zero, one, zero); + m[3] = col_type(zero, zero, zero, one); + } + + mat(const mat<2, 3, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + m[2] = col_type(zero, zero, one, zero); + m[3] = col_type(zero, zero, zero, one); + } + + mat(const mat<2, 4, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = col_type(zero, zero, one, zero); + m[3] = col_type(zero, zero, zero, one); + } + + mat(const mat<3, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero, zero); + m[1] = col_type(other.m[1], zero, zero); + m[2] = col_type(other.m[2], one, zero); + m[3] = col_type(zero, zero, zero, one); + } + + mat(const mat<3, 3, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + m[2] = col_type(other.m[2], zero); + m[3] = col_type(zero, zero, zero, one); + } + + mat(const mat<3, 4, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + m[3] = col_type(zero, zero, zero, one); + } + + mat(const mat<4, 2, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero, zero); + m[1] = col_type(other.m[1], zero, zero); + m[2] = col_type(other.m[2], one, zero); + m[3] = col_type(other.m[3], zero, one); + } + + mat(const mat<4, 3, T>& other) + { + const T zero = constants::zero; + const T one = constants::one; + m[0] = col_type(other.m[0], zero); + m[1] = col_type(other.m[1], zero); + m[2] = col_type(other.m[2], zero); + m[3] = col_type(other.m[3], one); + } + + mat(const mat<4, 4, T>& other) + { + m[0] = other.m[0]; + m[1] = other.m[1]; + m[2] = other.m[2]; + m[3] = other.m[3]; + } + + length_t length()const + { + return COLS; + } + + size_t elements()const + { + return ELEMENTS; + } + + pointer data() + { + return &m[0][0]; + } + + const_pointer data()const + { + return &m[0][0]; + } + + col_type& operator[](size_t i) + { + return m[i]; + } + + const col_type& operator[](size_t i)const + { + return m[i]; + } +}; + +// +// functions +// + +// mat2x4 = mul( in mat4, in mat2x4 ) +template +GLM_API mat<2, 4, T> mat4_mul(const mat<4, 4, T>& m1, const mat<2, 4, T>& m2) +{ + mat<2, 4, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2] + m1[3][0] * m2[0][3]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2] + m1[3][1] * m2[0][3]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1] + m1[2][2] * m2[0][2] + m1[3][2] * m2[0][3]; + product[0][3] = m1[0][3] * m2[0][0] + m1[1][3] * m2[0][1] + m1[2][3] * m2[0][2] + m1[3][3] * m2[0][3]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2] + m1[3][0] * m2[1][3]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2] + m1[3][1] * m2[1][3]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1] + m1[2][2] * m2[1][2] + m1[3][2] * m2[1][3]; + product[1][3] = m1[0][3] * m2[1][0] + m1[1][3] * m2[1][1] + m1[2][3] * m2[1][2] + m1[3][3] * m2[1][3]; + + return product; +} + +// mat3x4 = mul( in mat4, in mat3x4 ) +template +GLM_API mat<3, 4, T> mat4_mul(const mat<4, 4, T>& m1, const mat<3, 4, T>& m2) +{ + mat<3, 4, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2] + m1[3][0] * m2[0][3]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2] + m1[3][1] * m2[0][3]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1] + m1[2][2] * m2[0][2] + m1[3][2] * m2[0][3]; + product[0][3] = m1[0][3] * m2[0][0] + m1[1][3] * m2[0][1] + m1[2][3] * m2[0][2] + m1[3][3] * m2[0][3]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2] + m1[3][0] * m2[1][3]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2] + m1[3][1] * m2[1][3]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1] + m1[2][2] * m2[1][2] + m1[3][2] * m2[1][3]; + product[1][3] = m1[0][3] * m2[1][0] + m1[1][3] * m2[1][1] + m1[2][3] * m2[1][2] + m1[3][3] * m2[1][3]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1] + m1[2][0] * m2[2][2] + m1[3][0] * m2[2][3]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1] + m1[2][1] * m2[2][2] + m1[3][1] * m2[2][3]; + product[2][2] = m1[0][2] * m2[2][0] + m1[1][2] * m2[2][1] + m1[2][2] * m2[2][2] + m1[3][2] * m2[2][3]; + product[2][3] = m1[0][3] * m2[2][0] + m1[1][3] * m2[2][1] + m1[2][3] * m2[2][2] + m1[3][3] * m2[2][3]; + + return product; +} + +// mat4 = mul( in mat4, in mat4 ) +template +GLM_API mat<4, 4, T> mat4_mul(const mat<4, 4, T>& m1, const mat<4, 4, T>& m2) +{ + mat<4, 4, T> product; + + product[0][0] = m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1] + m1[2][0] * m2[0][2] + m1[3][0] * m2[0][3]; + product[0][1] = m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1] + m1[2][1] * m2[0][2] + m1[3][1] * m2[0][3]; + product[0][2] = m1[0][2] * m2[0][0] + m1[1][2] * m2[0][1] + m1[2][2] * m2[0][2] + m1[3][2] * m2[0][3]; + product[0][3] = m1[0][3] * m2[0][0] + m1[1][3] * m2[0][1] + m1[2][3] * m2[0][2] + m1[3][3] * m2[0][3]; + product[1][0] = m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1] + m1[2][0] * m2[1][2] + m1[3][0] * m2[1][3]; + product[1][1] = m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1] + m1[2][1] * m2[1][2] + m1[3][1] * m2[1][3]; + product[1][2] = m1[0][2] * m2[1][0] + m1[1][2] * m2[1][1] + m1[2][2] * m2[1][2] + m1[3][2] * m2[1][3]; + product[1][3] = m1[0][3] * m2[1][0] + m1[1][3] * m2[1][1] + m1[2][3] * m2[1][2] + m1[3][3] * m2[1][3]; + product[2][0] = m1[0][0] * m2[2][0] + m1[1][0] * m2[2][1] + m1[2][0] * m2[2][2] + m1[3][0] * m2[2][3]; + product[2][1] = m1[0][1] * m2[2][0] + m1[1][1] * m2[2][1] + m1[2][1] * m2[2][2] + m1[3][1] * m2[2][3]; + product[2][2] = m1[0][2] * m2[2][0] + m1[1][2] * m2[2][1] + m1[2][2] * m2[2][2] + m1[3][2] * m2[2][3]; + product[2][3] = m1[0][3] * m2[2][0] + m1[1][3] * m2[2][1] + m1[2][3] * m2[2][2] + m1[3][3] * m2[2][3]; + product[3][0] = m1[0][0] * m2[3][0] + m1[1][0] * m2[3][1] + m1[2][0] * m2[3][2] + m1[3][0] * m2[3][3]; + product[3][1] = m1[0][1] * m2[3][0] + m1[1][1] * m2[3][1] + m1[2][1] * m2[3][2] + m1[3][1] * m2[3][3]; + product[3][2] = m1[0][2] * m2[3][0] + m1[1][2] * m2[3][1] + m1[2][2] * m2[3][2] + m1[3][2] * m2[3][3]; + product[3][3] = m1[0][3] * m2[3][0] + m1[1][3] * m2[3][1] + m1[2][3] * m2[3][2] + m1[3][3] * m2[3][3]; + + return product; +} + +// mat4 = transpose( in mat4 ) +template +GLM_API mat<4, 4, T> transpose(const mat<4, 4, T>& m) +{ + return mat4( + m[0][0], m[1][0], m[2][0], m[3][0], + m[0][1], m[1][1], m[2][1], m[3][1], + m[0][2], m[1][2], m[2][2], m[3][2], + m[0][3], m[1][3], m[2][3], m[3][3]); +} + +// vec4 = transform( in vec4, in mat4 ) +template +GLM_API vec<4, T> transform(const vec<4, T>& v, const mat<4, 4, T>& m) +{ + return vec<4, T>( + v[0] * m[0][0] + v[1] * m[0][1] + v[2] * m[0][2] + v[3] * m[0][3], + v[0] * m[1][0] + v[1] * m[1][1] + v[2] * m[1][2] + v[3] * m[1][3], + v[0] * m[2][0] + v[1] * m[2][1] + v[2] * m[2][2] + v[3] * m[2][3], + v[0] * m[3][0] + v[1] * m[3][1] + v[2] * m[3][2] + v[3] * m[3][3]); +} + +// vec4 = transform ( in mat4, in vec4 ) +template +GLM_API vec<4, T> transform(const mat<4, 4, T>& m, const vec<4, T>& v) +{ + return vec<4, T>( + m[0][0] * v[0] + m[1][0] * v[1] + m[2][0] * v[2] + m[3][0] * v[3], + m[0][1] * v[0] + m[1][1] * v[1] + m[2][1] * v[2] + m[3][1] * v[3], + m[0][2] * v[0] + m[1][2] * v[1] + m[2][2] * v[2] + m[3][2] * v[3], + m[0][3] * v[0] + m[1][3] * v[1] + m[2][3] * v[2] + m[3][3] * v[3]); +} + +// +// operators +// + +// mat4 = mat4 + scalar +template +GLM_API mat<4, 4, T> operator+(const mat<4, 4, T>& m, T scalar) +{ + return mat<4, 4, T>(m[0] + scalar, m[1] + scalar, m[2] + scalar, m[3] + scalar); +} + +// mat4 = mat4 - scalar +template +GLM_API mat<4, 4, T> operator-(const mat<4, 4, T>& m, T scalar) +{ + return mat<4, 4, T>(m[0] - scalar, m[1] - scalar, m[2] - scalar, m[3] - scalar); +} + +// mat4 = mat4 * scalar +template +GLM_API mat<4, 4, T> operator*(const mat<4, 4, T>& m, T scalar) +{ + return mat<4, 4, T>(m[0] * scalar, m[1] * scalar, m[2] * scalar, m[3] * scalar); +} + +// mat4 = mat4 / scalar +template +GLM_API mat<4, 4, T> operator/(const mat<4, 4, T>& m, T scalar) +{ + return mat<4, 4, T>(m[0] / scalar, m[1] / scalar, m[2] / scalar, m[3] / scalar); +} + +// mat4 = scalar + mat4 +template +GLM_API mat<4, 4, T> operator+(T scalar, const mat<4, 4, T>& m) +{ + return mat<4, 4, T>(scalar + m[0], scalar + m[1], scalar + m[2], scalar + m[3]); +} + +// mat4 = scalar - mat4 +template +GLM_API mat<4, 4, T> operator-(T scalar, const mat<4, 4, T>& m) +{ + return mat<4, 4, T>(scalar - m[0], scalar - m[1], scalar - m[2], scalar - m[3]); +} + +// mat4 = scalar * mat4 +template +GLM_API mat<4, 4, T> operator*(T scalar, const mat<4, 4, T>& m) +{ + return mat<4, 4, T>(scalar * m[0], scalar * m[1], scalar * m[2], scalar * m[3]); +} + +// mat4 = scalar / mat4 +template +GLM_API mat<4, 4, T> operator/(T scalar, const mat<4, 4, T>& m) +{ + return mat<4, 4, T>(scalar / m[0], scalar / m[1], scalar / m[2], scalar / m[3]); +} + +// mat2x4 = mat4 x mat2x4 +template +GLM_API mat<2, 4, T> operator*(const mat<4, 4, T>& m1, const mat<2, 4, T>& m2) +{ + return mat4_mul(m1, m2); +} + +// mat3x4 = mat4 x mat3x4 +template +GLM_API mat<3, 4, T> operator*(const mat<4, 4, T>& m1, const mat<3, 4, T>& m2) +{ + return mat4_mul(m1, m2); +} + +// mat4 = mat4 x mat4 +template +GLM_API mat<4, 4, T> operator*(const mat<4, 4, T>& m1, const mat<4, 4, T>& m2) +{ + return mat4_mul(m1, m2); +} + +// vec4 = vec4 x mat4 +template +GLM_API vec<4, T> operator*(const vec<4, T>& v, const mat<4, 4, T>& m) +{ + return transform(v, m); +} + +// vec4 = mat4 x vec4 +template +GLM_API vec<4, T> operator*(const mat<4, 4, T>& v, const vec<4, T>& m) +{ + return transform(m, v); +} + +}// end namespace glm + +#endif// GLM_MAT4X4_HPP20220215152935 diff --git a/glm/mat.hpp b/glm/matrix.hpp similarity index 91% rename from glm/mat.hpp rename to glm/matrix.hpp index 9370b36..5eb08fd 100644 --- a/glm/mat.hpp +++ b/glm/matrix.hpp @@ -1,75 +1,79 @@ -/* - Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) - - matrix.hpp - - 2020-10-27 02:05:52 - -*/ -#ifndef GLM_MATRIX_HPP_20201027020552 -#define GLM_MATRIX_HPP_20201027020552 - -#include "vec.hpp" - - - -namespace glm{ - -template -class mat -{ -public: - GLM_CLASS_TYPEDEF(T); - - typedef mat this_type; - typedef vec row_type; - - enum{ - COLUMNS = CX, - ROWS = CY, - ELEMENTS = CX * CY, - }; - -public: - row_type m[ROWS]; - -public: - length_t length()const - { - return ELEMENTS; - } - - pointer data() - { - return &m[0][0]; - } - - const_pointer data()const - { - return &m[0][0]; - } - - row_type& operator[](size_t i) - { - return m[i]; - } - - const row_type& operator[](size_t i)const - { - return m[i]; - } - - reference operator()(size_t x, size_t y) - { - return m[y][x]; - } - - const_reference operator()(size_t x, size_t y)const - { - return m[y][x]; - } -}; - -}// end namespace glm - -#endif// GLM_MATRIX_HPP_20201027020552 +/* + Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) + + matrix.hpp + + 2020-10-27 02:05:52 + +*/ +#ifndef GLM_MATRIX_HPP_20201027020552 +#define GLM_MATRIX_HPP_20201027020552 + +#include "vector.hpp" + + + +namespace glm{ + +// +// mat +// + +template +class mat +{ +public: + GLM_CLASS_TYPEDEF(T); + + typedef mat this_type; + typedef vec row_type; + + enum{ + COLUMNS = CX, + ROWS = CY, + ELEMENTS = CX * CY, + }; + +public: + row_type m[ROWS]; + +public: + length_t length()const + { + return ELEMENTS; + } + + pointer data() + { + return &m[0][0]; + } + + const_pointer data()const + { + return &m[0][0]; + } + + row_type& operator[](size_t i) + { + return m[i]; + } + + const row_type& operator[](size_t i)const + { + return m[i]; + } + + reference operator()(size_t x, size_t y) + { + return m[y][x]; + } + + const_reference operator()(size_t x, size_t y)const + { + return m[y][x]; + } +}; + +}// end namespace glm + +#endif// GLM_MATRIX_HPP_20201027020552 diff --git a/glm/vec.hpp b/glm/vec.hpp deleted file mode 100644 index 06087bd..0000000 --- a/glm/vec.hpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) - - vec.hpp - - 2021-10-19 14:21:37 - -*/ -#ifndef GLM_VEC_HPP_20211019142137 -#define GLM_VEC_HPP_20211019142137 - -#include "detail/constants.hpp" - - - -namespace glm{ - -template -class vec -{ -public: - GLM_CLASS_TYPEDEF(T); - - typedef vec this_type; - - enum { ELEMENTS = N }; - -public: - value_type m[ELEMENTS]; - -public: - length_t length()const { return ELEMENTS; } - - reference operator[](size_t i) { return m[i]; } - const_reference operator[](size_t i)const { return m[i]; } - - pointer data() { return m; } - const_pointer data()const { return m; } -}; - -}// end namespace glm - -#endif// GLM_VEC_HPP_20211019142137 diff --git a/glm/vec2.hpp b/glm/vec2.hpp index 25504cd..b567984 100644 --- a/glm/vec2.hpp +++ b/glm/vec2.hpp @@ -9,7 +9,7 @@ #ifndef GLM_VEC2_HPP_20211019154130 #define GLM_VEC2_HPP_20211019154130 -#include "vec.hpp" +#include "vector.hpp" diff --git a/glm/vec3.hpp b/glm/vec3.hpp index bc07eaa..e68723e 100644 --- a/glm/vec3.hpp +++ b/glm/vec3.hpp @@ -9,7 +9,7 @@ #ifndef GLM_VEC3_HPP_20211019154138 #define GLM_VEC3_HPP_20211019154138 -#include "vec.hpp" +#include "vector.hpp" diff --git a/glm/vec4.hpp b/glm/vec4.hpp index f8fae07..8dd3a48 100644 --- a/glm/vec4.hpp +++ b/glm/vec4.hpp @@ -9,7 +9,7 @@ #ifndef GLM_VEC4_HPP_20211019154150 #define GLM_VEC4_HPP_20211019154150 -#include "vec.hpp" +#include "vector.hpp" #include "simd/vec4.hpp" diff --git a/glm/vector.hpp b/glm/vector.hpp new file mode 100644 index 0000000..4258050 --- /dev/null +++ b/glm/vector.hpp @@ -0,0 +1,60 @@ +/* + Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) + + vector.hpp + + 2021-10-19 14:21:37 + +*/ +#ifndef GLM_VECTOR_HPP_20211019142137 +#define GLM_VECTOR_HPP_20211019142137 + +#include "detail/constants.hpp" + + + +namespace glm{ + +template +class vec +{ +public: + GLM_CLASS_TYPEDEF(T); + + typedef vec this_type; + + enum { ELEMENTS = N }; + +public: + value_type m[ELEMENTS]; + +public: + length_t length()const + { + return ELEMENTS; + } + + reference operator[](size_t i) + { + return m[i]; + } + + const_reference operator[](size_t i)const + { + return m[i]; + } + + pointer data() + { + return m; + } + + const_pointer data()const + { + return m; + } +}; + +}// end namespace glm + +#endif// GLM_VECTOR_HPP_20211019142137