diff --git a/src/logic/scripting/lua/libmat4.cpp b/src/logic/scripting/lua/libmat4.cpp index 414c5551..1a2e477a 100644 --- a/src/logic/scripting/lua/libmat4.cpp +++ b/src/logic/scripting/lua/libmat4.cpp @@ -7,27 +7,61 @@ static int l_idt(lua::State* L) { return lua::pushmat4(L, glm::mat4(1.0f)); } -/// @brief mat4.scale(matrix=idt: array[16], scale: array[3]) -> array[16] -/// Modifies matrix -static int l_scale(lua::State* L) { +/// Overloads: +/// mat4.(vec: float[3]) -> float[16] - creates transform matrix +/// mat4.(matrix: float[16], vec: float[3]) -> float[16] - creates transformed copy of matrix +/// mat4.(matrix: float[16], vec: float[3], dst: float[16]) -> sets dst to transformed version of matrix +template +inline int l_transform_func(lua::State* L) { uint argc = lua::gettop(L); switch (argc) { case 1: { - auto scale = lua::tovec3(L, 1); - return lua::pushmat4(L, glm::scale(glm::mat4(1.0f), scale)); + auto vec = lua::tovec3(L, 1); + return lua::pushmat4(L, func(glm::mat4(1.0f), vec)); } case 2: { auto matrix = lua::tomat4(L, 1); - auto scale = lua::tovec3(L, 2); - return lua::pushmat4(L, glm::scale(matrix, scale)); + auto vec = lua::tovec3(L, 2); + return lua::pushmat4(L, func(matrix, vec)); } case 3: { auto matrix = lua::tomat4(L, 1); - auto scale = lua::tovec3(L, 2); - return lua::setmat4(L, 3, glm::scale(matrix, scale)); + auto vec = lua::tovec3(L, 2); + return lua::setmat4(L, 3, func(matrix, vec)); } default: { - throw std::runtime_error("invalid number of arguments (1 or 2 expected)"); + throw std::runtime_error("invalid arguments number (1, 2 or 3 expected)"); + } + } + return 0; +} + +/// Overloads: +/// mat4.rotate(vec: float[3], angle: float) -> float[16] - creates rotation matrix +/// mat4.rotate(matrix: float[16], vec: float[3], angle: float) -> float[16] - creates rotated copy of matrix +/// mat4.rotate(matrix: float[16], vec: float[3], angle: float, dst: float[16]) -> sets dst to rotated version of matrix +inline int l_rotate(lua::State* L) { + uint argc = lua::gettop(L); + switch (argc) { + case 2: { + auto vec = lua::tovec3(L, 1); + auto angle = static_cast(lua::tonumber(L, 2)); + return lua::pushmat4(L, glm::rotate(glm::mat4(1.0f), angle, vec)); + } + case 3: { + auto matrix = lua::tomat4(L, 1); + auto vec = lua::tovec3(L, 2); + auto angle = static_cast(lua::tonumber(L, 3)); + return lua::pushmat4(L, glm::rotate(matrix, angle, vec)); + } + case 4: { + auto matrix = lua::tomat4(L, 1); + auto vec = lua::tovec3(L, 2); + auto angle = static_cast(lua::tonumber(L, 3)); + return lua::setmat4(L, 3, glm::rotate(matrix, angle, vec)); + } + default: { + throw std::runtime_error("invalid arguments number (2, 3 or 4 expected)"); } } return 0; @@ -49,7 +83,9 @@ static int l_tostring(lua::State* L) { const luaL_Reg mat4lib [] = { {"idt", lua::wrap}, - {"scale", lua::wrap}, + {"scale", lua::wrap>}, + {"rotate", lua::wrap}, + {"translate", lua::wrap>}, {"tostring", lua::wrap}, {NULL, NULL} };