add mat4 library

This commit is contained in:
MihailRis 2024-06-17 10:12:33 +03:00
parent 0debe32da4
commit c7a843bc63
4 changed files with 94 additions and 0 deletions

View File

@ -17,6 +17,7 @@ extern const luaL_Reg inputlib [];
extern const luaL_Reg inventorylib [];
extern const luaL_Reg itemlib [];
extern const luaL_Reg jsonlib [];
extern const luaL_Reg mat4lib [];
extern const luaL_Reg packlib [];
extern const luaL_Reg playerlib [];
extern const luaL_Reg timelib [];

View File

@ -0,0 +1,49 @@
#include "api_lua.hpp"
#include <sstream>
#include <glm/ext/matrix_transform.hpp>
static int l_idt(lua::State* L) {
return lua::pushmat4(L, glm::mat4(1.0f));
}
static int l_scale(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));
}
case 2: {
auto matrix = lua::tomat4(L, 1);
auto scale = lua::tovec3(L, 2);
return lua::pushmat4(L, glm::scale(matrix, scale));
}
default: {
throw std::runtime_error("invalid number of arguments (1 or 2 expected)");
}
}
return 0;
}
static int l_tostring(lua::State* L) {
auto matrix = lua::tomat4(L, 1);
std::stringstream ss;
ss << "mat4 {\n";
for (uint y = 0; y < 4; y++) {
for (uint x = 0; x < 4; x++) {
ss << "\t" << matrix[y][x];
}
ss << "\n";
}
ss << "}";
return lua::pushstring(L, ss.str());
}
const luaL_Reg mat4lib [] = {
{"idt", lua::wrap<l_idt>},
{"scale", lua::wrap<l_scale>},
{"tostring", lua::wrap<l_tostring>},
{NULL, NULL}
};

View File

@ -37,6 +37,7 @@ static void create_libs(lua::State* L) {
openlib(L, "inventory", inventorylib);
openlib(L, "item", itemlib);
openlib(L, "json", jsonlib);
openlib(L, "mat4", mat4lib);
openlib(L, "pack", packlib);
openlib(L, "player", playerlib);
openlib(L, "time", timelib);

View File

@ -195,6 +195,17 @@ namespace lua {
rawseti(L, 4);
return 1;
}
inline int pushmat4(lua::State* L, glm::mat4 matrix) {
createtable(L, 16, 0);
for (uint y = 0; y < 4; y++) {
for (uint x = 0; x < 4; x++) {
uint i = y * 4 + x;
pushnumber(L, matrix[y][x]);
rawseti(L, i+1);
}
}
return 1;
}
inline int pushcfunction(lua::State* L, lua_CFunction func) {
lua_pushcfunction(L, func);
return 1;
@ -310,6 +321,38 @@ namespace lua {
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) {
throw std::runtime_error("value must be an array of three 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);
pop(L);
return glm::vec3(x, y, z);
}
inline glm::mat4 tomat4(lua::State* L, int idx) {
pushvalue(L, idx);
if (!istable(L, idx) || objlen(L, idx) < 16) {
throw std::runtime_error("value must be an array of 16 numbers");
}
glm::mat4 matrix;
for (uint y = 0; y < 4; y++) {
for (uint x = 0; x < 4; x++) {
uint i = y * 4 + x;
rawgeti(L, i+1);
matrix[y][x] = static_cast<float>(tonumber(L, -1));
pop(L);
}
}
return matrix;
}
inline glm::vec4 tocolor(lua::State* L, int idx) {
pushvalue(L, idx);
if (!istable(L, -1) || objlen(L, idx) < 4) {