From 6062f3a48818af755ebea1781d5e475d3537af1d Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 17 Jun 2024 22:19:21 +0300 Subject: [PATCH] add mat4.mul --- src/logic/scripting/lua/libmat4.cpp | 32 +++++++++++++++++++++++++ src/logic/scripting/lua/lua_util.hpp | 36 ++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/logic/scripting/lua/libmat4.cpp b/src/logic/scripting/lua/libmat4.cpp index 1a2e477a..ae70f581 100644 --- a/src/logic/scripting/lua/libmat4.cpp +++ b/src/logic/scripting/lua/libmat4.cpp @@ -7,6 +7,37 @@ static int l_idt(lua::State* L) { return lua::pushmat4(L, glm::mat4(1.0f)); } +static int l_mul(lua::State* L) { + uint argc = lua::gettop(L); + if (argc < 2 || argc > 3) { + throw std::runtime_error("invalid arguments number (2 or 3 expected)"); + } + auto matrix1 = lua::tomat4(L, 1); + uint len2 = lua::objlen(L, 2); + if (len2 < 3) { + throw std::runtime_error("argument #2: vec3 or vec4 expected"); + } + switch (argc) { + case 2: { + if (len2 == 4) { + return lua::pushvec4(L, matrix1 * lua::tovec4(L, 2)); + } else if (len2 == 3) { + return lua::pushvec3(L, matrix1 * glm::vec4(lua::tovec3(L, 2), 1.0f)); + } + return lua::pushmat4(L, matrix1 * lua::tomat4(L, 2)); + } + case 3: { + if (len2 == 4) { + return lua::setvec4(L, 3, matrix1 * lua::tovec4(L, 2)); + } else if (len2 == 3) { + return lua::setvec3(L, 3, matrix1 * glm::vec4(lua::tovec3(L, 2), 1.0f)); + } + return lua::setmat4(L, 3, matrix1 * lua::tomat4(L, 2)); + } + } + return 0; +} + /// Overloads: /// mat4.(vec: float[3]) -> float[16] - creates transform matrix /// mat4.(matrix: float[16], vec: float[3]) -> float[16] - creates transformed copy of matrix @@ -83,6 +114,7 @@ static int l_tostring(lua::State* L) { const luaL_Reg mat4lib [] = { {"idt", lua::wrap}, + {"mul", lua::wrap}, {"scale", lua::wrap>}, {"rotate", lua::wrap}, {"translate", lua::wrap>}, diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index 79b649c3..c98af803 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -221,6 +221,24 @@ namespace lua { } return 1; } + /// @brief pushes vector table to the stack and updates it with glm vec4 + inline int setvec4(lua::State* L, int idx, glm::vec4 vec) { + pushvalue(L, idx); + for (uint i = 0; i < 4; i++) { + pushnumber(L, vec[i]); + rawseti(L, i+1); + } + return 1; + } + /// @brief pushes vector table to the stack and updates it with glm vec3 + inline int setvec3(lua::State* L, int idx, glm::vec3 vec) { + pushvalue(L, idx); + for (uint i = 0; i < 3; i++) { + pushnumber(L, vec[i]); + rawseti(L, i+1); + } + return 1; + } inline int pushcfunction(lua::State* L, lua_CFunction func) { lua_pushcfunction(L, func); return 1; @@ -331,7 +349,6 @@ namespace lua { pop(L); return glm::vec2(x, y); } - inline glm::vec3 tovec3(lua::State* L, int idx) { pushvalue(L, idx); if (!istable(L, idx) || objlen(L, idx) < 3) { @@ -346,7 +363,22 @@ namespace lua { pop(L); return glm::vec3(x, y, z); } - + inline glm::vec4 tovec4(lua::State* L, int idx) { + pushvalue(L, idx); + if (!istable(L, idx) || objlen(L, idx) < 4) { + throw std::runtime_error("value must be an array of four numbers"); + } + rawgeti(L, 1); + auto x = tonumber(L, -1); pop(L); + rawgeti(L, 2); + auto y = tonumber(L, -1); pop(L); + rawgeti(L, 3); + auto z = tonumber(L, -1); pop(L); + rawgeti(L, 4); + auto w = tonumber(L, -1); pop(L); + pop(L); + return glm::vec4(x, y, z, w); + } inline glm::mat4 tomat4(lua::State* L, int idx) { pushvalue(L, idx); if (!istable(L, idx) || objlen(L, idx) < 16) {