refactor libvecn

This commit is contained in:
MihailRis 2024-06-25 18:37:57 +03:00
parent c7db73e25b
commit 0c4b978466

View File

@ -24,10 +24,10 @@ static int l_binop(lua::State* L) {
}
}
template<int n>
static int l_normalize(lua::State* L) {
template<int n, glm::vec<n, float>(*func)(const glm::vec<n, float>&)>
static int l_unaryop(lua::State* L) {
uint argc = lua::gettop(L);
auto vec = glm::normalize(lua::tovec<n>(L, 1));
auto vec = func(lua::tovec<n>(L, 1));
switch (argc) {
case 1:
lua::createtable(L, n, 0);
@ -44,13 +44,13 @@ static int l_normalize(lua::State* L) {
}
}
template<int n>
static int l_len(lua::State* L) {
template<int n, float(*func)(const glm::vec<n, float>&)>
static int l_scalar_op(lua::State* L) {
auto vec = lua::tovec<n>(L, 1);
if (lua::gettop(L) != 1) {
throw std::runtime_error("invalid arguments number (1 expected)");
}
return lua::pushnumber(L, glm::length(vec));
return lua::pushnumber(L, func(vec));
}
const luaL_Reg vec2lib [] = {
@ -58,8 +58,8 @@ const luaL_Reg vec2lib [] = {
{"sub", lua::wrap<l_binop<2, std::minus>>},
{"mul", lua::wrap<l_binop<2, std::multiplies>>},
{"div", lua::wrap<l_binop<2, std::divides>>},
{"normalize", lua::wrap<l_normalize<2>>},
{"length", lua::wrap<l_len<2>>},
{"normalize", lua::wrap<l_unaryop<2, glm::normalize>>},
{"length", lua::wrap<l_scalar_op<2, glm::length>>},
{NULL, NULL}
};
@ -68,8 +68,8 @@ const luaL_Reg vec3lib [] = {
{"sub", lua::wrap<l_binop<3, std::minus>>},
{"mul", lua::wrap<l_binop<3, std::multiplies>>},
{"div", lua::wrap<l_binop<3, std::divides>>},
{"normalize", lua::wrap<l_normalize<3>>},
{"length", lua::wrap<l_len<3>>},
{"normalize", lua::wrap<l_unaryop<3, glm::normalize>>},
{"length", lua::wrap<l_scalar_op<3, glm::length>>},
{NULL, NULL}
};
@ -78,7 +78,7 @@ const luaL_Reg vec4lib [] = {
{"sub", lua::wrap<l_binop<4, std::minus>>},
{"mul", lua::wrap<l_binop<4, std::multiplies>>},
{"div", lua::wrap<l_binop<4, std::divides>>},
{"normalize", lua::wrap<l_normalize<4>>},
{"length", lua::wrap<l_len<4>>},
{"normalize", lua::wrap<l_unaryop<4, glm::normalize>>},
{"length", lua::wrap<l_scalar_op<4, glm::length>>},
{NULL, NULL}
};