From 66dd54b548e102303ef49a9b717cee87b54d2e5b Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 21 Jun 2024 19:43:45 +0300 Subject: [PATCH] add mat4.decompose --- doc/en/scripting/builtins/libmat4.md | 19 ++++++++++- doc/ru/scripting/builtins/libmat4.md | 17 ++++++++++ src/logic/scripting/lua/libmat4.cpp | 47 ++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/doc/en/scripting/builtins/libmat4.md b/doc/en/scripting/builtins/libmat4.md index 6216dff20..a2f8b75a6 100644 --- a/doc/en/scripting/builtins/libmat4.md +++ b/doc/en/scripting/builtins/libmat4.md @@ -9,6 +9,7 @@ Most functions have several options for argument lists (overloads). Type conventions will be used on this page. - vector - an array of three or four numbers - vec3 - array of three numbers +- vec4 - array of four numbers - matrix - array of 16 numbers - matrix > [!WARNING] @@ -98,7 +99,23 @@ mat4.rotate(m: matrix, axis: vec3, angle: number) mat4.rotate(m: matrix, axis: vec3, angle: number, dst: matrix) ``` -## Translation to string - *mat4.tostring(...)* +## Decomposition - *mat4.decompose(...)* + +Decomposes the transformation matrix into its components. + +```lua +mat4.decompose(m: matrix) +-- returns a table: +{ + scale=vec3, + rotation=matrix, + translation=vec3, + skew=vec3, + perspective=vec4 +} +``` + +## Casting to string - *mat4.tostring(...)* ```lua -- returns a string representing the contents of the matrix diff --git a/doc/ru/scripting/builtins/libmat4.md b/doc/ru/scripting/builtins/libmat4.md index 30d027843..2a5b4534f 100644 --- a/doc/ru/scripting/builtins/libmat4.md +++ b/doc/ru/scripting/builtins/libmat4.md @@ -9,6 +9,7 @@ На данной странице будут использоваться условные обозначения типов. - vector - массив из трех или четырех чисел - vec3 - массив из трех чисел +- vec4 - массив из четырех чисел - matrix - массив из 16 чисел - матрица > [!WARNING] @@ -98,6 +99,22 @@ mat4.rotate(m: matrix, axis: vec3, angle: number) mat4.rotate(m: matrix, axis: vec3, angle: number, dst: matrix) ``` +## Декомпозиция - *mat4.decompose(...)* + +Раскладывает матрицу трансформации на составляющие. + +```lua +mat4.decompose(m: matrix) +-- возвращает таблицу: +{ + scale=vec3, + rotation=matrix, + translation=vec3, + skew=vec3, + perspective=vec4 +} +``` + ## Перевод в строку - *mat4.tostring(...)* ```lua diff --git a/src/logic/scripting/lua/libmat4.cpp b/src/logic/scripting/lua/libmat4.cpp index 00dd2e960..4e989545e 100644 --- a/src/logic/scripting/lua/libmat4.cpp +++ b/src/logic/scripting/lua/libmat4.cpp @@ -1,7 +1,11 @@ #include "api_lua.hpp" #include + +#define GLM_ENABLE_EXPERIMENTAL #include +#include +#include /// Overloads: /// mat4.idt() -> float[16] - creates identity matrix @@ -165,6 +169,48 @@ static int l_transpose(lua::State* L) { return 0; } +/// mat4.decompose(m: float[16]) -> { +/// scale=float[3], +/// rotation=float[16], +/// translation=float[3], +/// skew=float[3], +/// perspective=float[4] +/// } +static int l_decompose(lua::State* L) { + auto matrix = lua::tomat4(L, 1); + glm::vec3 scale; + glm::quat rotation; + glm::vec3 translation; + glm::vec3 skew; + glm::vec4 perspective; + glm::decompose( + matrix, + scale, + rotation, + translation, + skew, + perspective + ); + + lua::createtable(L, 0, 5); + + lua::pushvec3_arr(L, scale); + lua::setfield(L, "scale"); + + lua::pushmat4(L, glm::toMat4(rotation)); + lua::setfield(L, "rotation"); + + lua::pushvec3_arr(L, translation); + lua::setfield(L, "translation"); + + lua::pushvec3_arr(L, skew); + lua::setfield(L, "skew"); + + lua::pushvec4_arr(L, perspective); + lua::setfield(L, "perspective"); + return 1; +} + static int l_tostring(lua::State* L) { auto matrix = lua::tomat4(L, 1); bool multiline = lua::toboolean(L, 2); @@ -202,6 +248,7 @@ const luaL_Reg mat4lib [] = { {"inverse", lua::wrap}, {"transpose", lua::wrap}, {"determinant", lua::wrap}, + {"decompose", lua::wrap}, {"tostring", lua::wrap}, {NULL, NULL} };