diff --git a/src/lol/math/matrix.h b/src/lol/math/matrix.h index e58fe235..426c21a0 100644 --- a/src/lol/math/matrix.h +++ b/src/lol/math/matrix.h @@ -548,6 +548,37 @@ static inline mat_t &operator *=(mat_t &a, return a = a * b; } +/* + * Matrix-matrix outer product (Kronecker product) + */ + +template +static inline mat_t +outer(mat_t const &a, mat_t const &b) +{ + /* FIXME: could this be optimised somehow? */ + mat_t ret; + for (int i1 = 0; i1 < COLS1; ++i1) + for (int j1 = 0; j1 < ROWS1; ++j1) + for (int i2 = 0; i2 < COLS2; ++i2) + for (int j2 = 0; j2 < ROWS2; ++j2) + ret[i1 * COLS2 + i2][j1 * ROWS2 + j2] + = a[i1][j1] * b[i2][j2]; + return ret; +} + +/* + * Vector-vector outer product + */ + +template +static inline mat_t outer(vec_t const &a, + vec_t const &b) +{ + return *reinterpret_cast const *>(&a) + * *reinterpret_cast const *>(&b); +} + #if !LOL_FEATURE_CXX11_CONSTEXPR #undef constexpr #endif diff --git a/src/math/matrix.cpp b/src/math/matrix.cpp index 9135c136..4730f108 100644 --- a/src/math/matrix.cpp +++ b/src/math/matrix.cpp @@ -17,17 +17,6 @@ namespace lol { -template<> mat3 mat3::scale(float x) -{ - mat3 ret(1.0f); - - ret[0][0] = x; - ret[1][1] = x; - ret[2][2] = x; - - return ret; -} - template<> mat3 mat3::scale(float x, float y, float z) { mat3 ret(1.0f); @@ -39,6 +28,11 @@ template<> mat3 mat3::scale(float x, float y, float z) return ret; } +template<> mat3 mat3::scale(float x) +{ + return scale(x, x, x); +} + template<> mat3 mat3::scale(vec3 v) { return scale(v.x, v.y, v.z); @@ -198,7 +192,10 @@ template<> mat4 mat4::frustum(float left, float right, float bottom, return ret; } -//Returns a standard perspective matrix +/* + * Return a standard perspective matrix + */ + template<> mat4 mat4::perspective(float fov_y, float width, float height, float near, float far) { @@ -208,9 +205,14 @@ template<> mat4 mat4::perspective(float fov_y, float width, return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far); } -//Returns a perspective matrix with the camera location shifted to be on the near plane +/* + * Return a perspective matrix with the camera location shifted to be on + * the near plane + */ + template<> mat4 mat4::shifted_perspective(float fov_y, float screen_size, - float screen_ratio_yx, float near, float far) + float screen_ratio_yx, + float near, float far) { float tan_y = tanf(radians(fov_y) * .5f); ASSERT(tan_y > 0.000001f);