add quat Lua library

This commit is contained in:
MihailRis
2024-07-22 19:05:27 +03:00
parent b5e7b63a9d
commit e0cb57a10a
10 changed files with 152 additions and 27 deletions
+1
View File
@@ -31,6 +31,7 @@ extern const luaL_Reg jsonlib [];
extern const luaL_Reg mat4lib [];
extern const luaL_Reg packlib [];
extern const luaL_Reg playerlib [];
extern const luaL_Reg quatlib []; // quat.cpp
extern const luaL_Reg timelib [];
extern const luaL_Reg tomllib [];
extern const luaL_Reg vec2lib []; // vecn.cpp
+50
View File
@@ -0,0 +1,50 @@
#include "api_lua.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/ext/matrix_transform.hpp>
#include <glm/gtx/quaternion.hpp>
static int l_from_mat4(lua::State* L) {
uint argc = lua::check_argc(L, 1, 2);
auto matrix = lua::tomat4(L, 1);
if (argc == 1) {
return lua::pushquat(L, glm::toQuat(matrix));
} else {
return lua::setquat(L, 2, glm::toQuat(matrix));
}
}
static int l_slerp(lua::State* L) {
uint argc = lua::check_argc(L, 3, 4);
auto quat1 = lua::toquat(L, 1);
auto quat2 = lua::toquat(L, 2);
float t = lua::tonumber(L, 3);
if (argc == 3) {
return lua::pushquat(L, glm::slerp(quat1, quat2, t));
} else {
return lua::setquat(L, 4, glm::slerp(quat1, quat2, t));
}
}
static int l_tostring(lua::State* L) {
lua::check_argc(L, 1);
auto quat = lua::toquat(L, 1);
std::stringstream ss;
ss << "quat" << "{";
for (int i = 0; i < 4; i++) {
if (i > 0) {
ss << ", ";
}
ss << quat[i];
}
ss << "}";
return lua::pushstring(L, ss.str());
}
const luaL_Reg quatlib [] = {
{"from_mat4", lua::wrap<l_from_mat4>},
{"slerp", lua::wrap<l_slerp>},
{"tostring", lua::wrap<l_tostring>},
{NULL, NULL}
};
+1
View File
@@ -40,6 +40,7 @@ static void create_libs(lua::State* L) {
openlib(L, "mat4", mat4lib);
openlib(L, "pack", packlib);
openlib(L, "player", playerlib);
openlib(L, "quat", quatlib);
openlib(L, "time", timelib);
openlib(L, "toml", tomllib);
openlib(L, "vec2", vec2lib);
+8 -1
View File
@@ -196,7 +196,14 @@ namespace lua {
}
return 1;
}
inline int setquat(lua::State* L, int idx, glm::quat quat) {
pushvalue(L, idx);
for (int i = 0; i < 4; i++) {
pushnumber(L, quat[i]);
rawseti(L, i+1);
}
return 1;
}
inline int pushmat4(lua::State* L, glm::mat4 matrix) {
createtable(L, 16, 0);
for (uint y = 0; y < 4; y++) {