Merge branch 'main' into entities

This commit is contained in:
MihailRis 2024-06-25 18:47:15 +03:00
commit 9fc8f7342f
5 changed files with 111 additions and 14 deletions

View File

@ -23,6 +23,9 @@ extern const luaL_Reg packlib [];
extern const luaL_Reg playerlib [];
extern const luaL_Reg timelib [];
extern const luaL_Reg tomllib [];
extern const luaL_Reg vec2lib [];
extern const luaL_Reg vec3lib [];
extern const luaL_Reg vec4lib [];
extern const luaL_Reg worldlib [];
// Lua Overrides

View File

@ -60,9 +60,9 @@ static int l_mul(lua::State* L) {
}
case 3: {
if (len2 == 4) {
return lua::setvec4(L, 3, matrix1 * lua::tovec4(L, 2));
return lua::setvec(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::setvec(L, 3, matrix1 * glm::vec4(lua::tovec3(L, 2), 1.0f));
}
return lua::setmat4(L, 3, matrix1 * lua::tomat4(L, 2));
}

View File

@ -0,0 +1,84 @@
#include "api_lua.hpp"
#include <glm/glm.hpp>
template<int n, template<class> class Op>
static int l_binop(lua::State* L) {
uint argc = lua::gettop(L);
auto a = lua::tovec<n>(L, 1);
auto b = lua::tovec<n>(L, 2);
Op op;
switch (argc) {
case 2:
lua::createtable(L, n, 0);
for (uint i = 0; i < n; i++) {
lua::pushnumber(L, op(a[i], b[i]));
lua::rawseti(L, i+1);
}
return 1;
case 3:
return lua::setvec(L, 3, op(a, b));
default: {
throw std::runtime_error("invalid arguments number (2 or 3 expected)");
}
}
}
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 = func(lua::tovec<n>(L, 1));
switch (argc) {
case 1:
lua::createtable(L, n, 0);
for (uint i = 0; i < n; i++) {
lua::pushnumber(L, vec[i]);
lua::rawseti(L, i+1);
}
return 1;
case 3:
return lua::setvec(L, 3, vec);
default: {
throw std::runtime_error("invalid arguments number (1 or 2 expected)");
}
}
}
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, func(vec));
}
const luaL_Reg vec2lib [] = {
{"add", lua::wrap<l_binop<2, std::plus>>},
{"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_unaryop<2, glm::normalize>>},
{"length", lua::wrap<l_scalar_op<2, glm::length>>},
{NULL, NULL}
};
const luaL_Reg vec3lib [] = {
{"add", lua::wrap<l_binop<3, std::plus>>},
{"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_unaryop<3, glm::normalize>>},
{"length", lua::wrap<l_scalar_op<3, glm::length>>},
{NULL, NULL}
};
const luaL_Reg vec4lib [] = {
{"add", lua::wrap<l_binop<4, std::plus>>},
{"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_unaryop<4, glm::normalize>>},
{"length", lua::wrap<l_scalar_op<4, glm::length>>},
{NULL, NULL}
};

View File

@ -43,6 +43,9 @@ static void create_libs(lua::State* L) {
openlib(L, "player", playerlib);
openlib(L, "time", timelib);
openlib(L, "toml", tomllib);
openlib(L, "vec2", vec2lib);
openlib(L, "vec3", vec3lib);
openlib(L, "vec4", vec4lib);
openlib(L, "world", worldlib);
addfunc(L, "print", lua::wrap<l_print>);

View File

@ -221,19 +221,10 @@ 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) {
template<int n>
inline int setvec(lua::State* L, int idx, glm::vec<n, float> 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++) {
for (int i = 0; i < n; i++) {
pushnumber(L, vec[i]);
rawseti(L, i+1);
}
@ -337,6 +328,22 @@ namespace lua {
setglobal(L, name);
}
template<int n>
inline glm::vec<n, float> tovec(lua::State* L, int idx) {
pushvalue(L, idx);
if (!istable(L, idx) || objlen(L, idx) < n) {
throw std::runtime_error("value must be an array of "+std::to_string(n)+" numbers");
}
glm::vec<n, float> vec;
for (int i = 0; i < n; i++) {
rawgeti(L, 1);
vec[i] = tonumber(L, -1);
pop(L);
}
pop(L);
return vec;
}
inline glm::vec2 tovec2(lua::State* L, int idx) {
pushvalue(L, idx);
if (!istable(L, idx) || objlen(L, idx) < 2) {