add mat4.decompose

This commit is contained in:
MihailRis
2024-06-21 19:43:45 +03:00
parent 5977c9c241
commit 66dd54b548
3 changed files with 82 additions and 1 deletions
+47
View File
@@ -1,7 +1,11 @@
#include "api_lua.hpp"
#include <sstream>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/ext/matrix_transform.hpp>
#include <glm/gtx/matrix_decompose.hpp>
#include <glm/gtx/quaternion.hpp>
/// 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<l_inverse>},
{"transpose", lua::wrap<l_transpose>},
{"determinant", lua::wrap<l_determinant>},
{"decompose", lua::wrap<l_decompose>},
{"tostring", lua::wrap<l_tostring>},
{NULL, NULL}
};