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
+43
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) {