118 lines
3.5 KiB
C
118 lines
3.5 KiB
C
/**
|
|
* Copyright (c) 2026 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#pragma once
|
|
#include "script/module/modulebase.h"
|
|
#include "modulevec3.h"
|
|
#include "modulevec4.h"
|
|
|
|
static void luaMat4Push(lua_State *L, mat4 m) {
|
|
mat4 *u = (mat4 *)lua_newuserdata(L, sizeof(mat4));
|
|
glm_mat4_copy(m, *u);
|
|
luaL_getmetatable(L, "mat4_mt");
|
|
lua_setmetatable(L, -2);
|
|
}
|
|
|
|
static void luaMat4Check(lua_State *L, int idx, mat4 out) {
|
|
mat4 *m = (mat4 *)luaL_checkudata(L, idx, "mat4_mt");
|
|
glm_mat4_copy(*m, out);
|
|
}
|
|
|
|
static int mat4Index(lua_State *L) {
|
|
lua_getmetatable(L, 1);
|
|
lua_getfield(L, -1, luaL_checkstring(L, 2));
|
|
return 1;
|
|
}
|
|
|
|
static int mat4OpMul(lua_State *L) {
|
|
mat4 *a = (mat4 *)luaL_checkudata(L, 1, "mat4_mt");
|
|
mat4 *b = (mat4 *)luaL_checkudata(L, 2, "mat4_mt");
|
|
mat4 r; glm_mat4_mul(*a, *b, r);
|
|
luaMat4Push(L, r); return 1;
|
|
}
|
|
|
|
static int mat4OpToString(lua_State *L) {
|
|
lua_pushstring(L, "mat4(...)"); return 1;
|
|
}
|
|
|
|
static int mat4Transpose(lua_State *L) {
|
|
mat4 *m = (mat4 *)luaL_checkudata(L, 1, "mat4_mt");
|
|
mat4 r; glm_mat4_transpose_to(*m, r);
|
|
luaMat4Push(L, r); return 1;
|
|
}
|
|
|
|
static int mat4Inverse(lua_State *L) {
|
|
mat4 *m = (mat4 *)luaL_checkudata(L, 1, "mat4_mt");
|
|
mat4 r; glm_mat4_inv(*m, r);
|
|
luaMat4Push(L, r); return 1;
|
|
}
|
|
|
|
static int mat4MulVec3(lua_State *L) {
|
|
mat4 *m = (mat4 *)luaL_checkudata(L, 1, "mat4_mt");
|
|
vec3 v; luaVec3Check(L, 2, v);
|
|
float_t w = lua_gettop(L) >= 3 ? (float_t)luaL_checknumber(L, 3) : 1.0f;
|
|
vec3 r; glm_mat4_mulv3(*m, v, w, r);
|
|
luaVec3Push(L, r); return 1;
|
|
}
|
|
|
|
static int mat4MulVec4(lua_State *L) {
|
|
mat4 *m = (mat4 *)luaL_checkudata(L, 1, "mat4_mt");
|
|
vec4 v; luaVec4Check(L, 2, v);
|
|
vec4 r; glm_mat4_mulv(*m, v, r);
|
|
luaVec4Push(L, r); return 1;
|
|
}
|
|
|
|
static int mat4Translate(lua_State *L) {
|
|
mat4 *m = (mat4 *)luaL_checkudata(L, 1, "mat4_mt");
|
|
vec3 v; luaVec3Check(L, 2, v);
|
|
mat4 r; glm_mat4_copy(*m, r);
|
|
glm_translate(r, v);
|
|
luaMat4Push(L, r); return 1;
|
|
}
|
|
|
|
static int mat4Scale(lua_State *L) {
|
|
mat4 *m = (mat4 *)luaL_checkudata(L, 1, "mat4_mt");
|
|
vec3 v; luaVec3Check(L, 2, v);
|
|
mat4 r; glm_mat4_copy(*m, r);
|
|
glm_scale(r, v);
|
|
luaMat4Push(L, r); return 1;
|
|
}
|
|
|
|
static int mat4Identity(lua_State *L) {
|
|
mat4 r; glm_mat4_identity(r);
|
|
luaMat4Push(L, r); return 1;
|
|
}
|
|
|
|
static int mat4Determinant(lua_State *L) {
|
|
mat4 *m = (mat4 *)luaL_checkudata(L, 1, "mat4_mt");
|
|
lua_pushnumber(L, (lua_Number)glm_mat4_det(*m)); return 1;
|
|
}
|
|
|
|
static int mat4Create(lua_State *L) {
|
|
mat4 m; glm_mat4_identity(m);
|
|
luaMat4Push(L, m); return 1;
|
|
}
|
|
|
|
static void moduleMathMat4(lua_State *L) {
|
|
if(!luaL_newmetatable(L, "mat4_mt")) { lua_pop(L, 1); return; }
|
|
|
|
lua_pushcfunction(L, mat4Index); lua_setfield(L, -2, "__index");
|
|
lua_pushcfunction(L, mat4OpMul); lua_setfield(L, -2, "__mul");
|
|
lua_pushcfunction(L, mat4OpToString); lua_setfield(L, -2, "__tostring");
|
|
lua_pushcfunction(L, mat4Transpose); lua_setfield(L, -2, "transpose");
|
|
lua_pushcfunction(L, mat4Inverse); lua_setfield(L, -2, "inverse");
|
|
lua_pushcfunction(L, mat4MulVec3); lua_setfield(L, -2, "mulVec3");
|
|
lua_pushcfunction(L, mat4MulVec4); lua_setfield(L, -2, "mulVec4");
|
|
lua_pushcfunction(L, mat4Translate); lua_setfield(L, -2, "translate");
|
|
lua_pushcfunction(L, mat4Scale); lua_setfield(L, -2, "scale");
|
|
lua_pushcfunction(L, mat4Identity); lua_setfield(L, -2, "identity");
|
|
lua_pushcfunction(L, mat4Determinant); lua_setfield(L, -2, "determinant");
|
|
lua_pop(L, 1);
|
|
|
|
lua_register(L, "mat4", mat4Create);
|
|
}
|