From 117f7e3061c3cb3d4d95f85a6e562730e482918a Mon Sep 17 00:00:00 2001 From: xiejiangzhi Date: Sun, 4 Feb 2024 13:17:54 +0800 Subject: [PATCH 01/12] Support table vector --- src/api/l_math_vectors.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/api/l_math_vectors.c b/src/api/l_math_vectors.c index 4e1afaa6..b67acb81 100644 --- a/src/api/l_math_vectors.c +++ b/src/api/l_math_vectors.c @@ -42,6 +42,15 @@ static const uint32_t* swizzles[5] = { // Helpers +inline void luax_readobjarr(lua_State* L, int index, size_t n, float* out, const char* name) { + lovrCheck(lua_objlen(L, index) >= n, "length of %s table must >= %i", name, n); + for (int i = 0; i < 4; i++) { + lua_rawgeti(L, index, i + 1); + out[i] = lua_tonumber(L, -1); + lua_pop(L, 1); + } +} + int luax_readvec2(lua_State* L, int index, vec2 v, const char* expected) { switch (lua_type(L, index)) { case LUA_TNIL: @@ -52,6 +61,10 @@ int luax_readvec2(lua_State* L, int index, vec2 v, const char* expected) { v[0] = luax_tofloat(L, index++); v[1] = luax_optfloat(L, index++, v[0]); return index; + case LUA_TTABLE: + luax_readobjarr(L, index, 2, v, "vec2"); + index++; + return index; default: vec2_init(v, luax_checkvector(L, index, V_VEC2, expected ? expected : "vec2 or number")); return index + 1; @@ -69,6 +82,10 @@ int luax_readvec3(lua_State* L, int index, vec3 v, const char* expected) { v[1] = luax_optfloat(L, index++, v[0]); v[2] = luax_optfloat(L, index++, v[0]); return index; + case LUA_TTABLE: + luax_readobjarr(L, index, 3, v, "vec3"); + index++; + return index; default: vec3_init(v, luax_checkvector(L, index, V_VEC3, expected ? expected : "vec3 or number")); return index + 1; @@ -87,6 +104,10 @@ int luax_readvec4(lua_State* L, int index, vec4 v, const char* expected) { v[2] = luax_optfloat(L, index++, v[0]); v[3] = luax_optfloat(L, index++, v[0]); return index; + case LUA_TTABLE: + luax_readobjarr(L, index, 4, v, "vec4"); + index++; + return index; default: vec4_init(v, luax_checkvector(L, index, V_VEC4, expected ? expected : "vec4 or number")); return index + 1; @@ -112,6 +133,10 @@ int luax_readscale(lua_State* L, int index, vec3 v, int components, const char* } } return index; + case LUA_TTABLE: + luax_readobjarr(L, index, 3, v, "scale"); + index++; + return index; default: { VectorType type; float* u = luax_tovector(L, index++, &type); @@ -143,6 +168,12 @@ int luax_readquat(lua_State* L, int index, quat q, const char* expected) { az = luax_optfloat(L, index++, 0.f); quat_fromAngleAxis(q, angle, ax, ay, az); return index; + case LUA_TTABLE: + float v[4]; + luax_readobjarr(L, index, 4, v, "quat"); + quat_fromAngleAxis(q, v[0], v[1], v[2], v[3]); + index++; + return index; default: quat_init(q, luax_checkvector(L, index++, V_QUAT, expected ? expected : "quat or number")); return index; @@ -167,6 +198,14 @@ int luax_readmat4(lua_State* L, int index, mat4 m, int scaleComponents) { } } // Fall through + case LUA_TTABLE: + if (lua_type(L, index) == LUA_TTABLE && lua_objlen(L, index) >= 16) { + luax_readobjarr(L, index, 16, m, "mat4"); + index++; + return index; + } + // Fall through + case LUA_TNUMBER: { float S[3]; float R[4]; From fa230dffff7f75d7814f08c36dd517b053b53ffc Mon Sep 17 00:00:00 2001 From: xiejiangzhi Date: Sun, 4 Feb 2024 14:06:34 +0800 Subject: [PATCH 02/12] Fix table array read for luax_readobjarr --- src/api/l_math_vectors.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/api/l_math_vectors.c b/src/api/l_math_vectors.c index b67acb81..455399eb 100644 --- a/src/api/l_math_vectors.c +++ b/src/api/l_math_vectors.c @@ -44,7 +44,7 @@ static const uint32_t* swizzles[5] = { inline void luax_readobjarr(lua_State* L, int index, size_t n, float* out, const char* name) { lovrCheck(lua_objlen(L, index) >= n, "length of %s table must >= %i", name, n); - for (int i = 0; i < 4; i++) { + for (int i = 0; i < n; i++) { lua_rawgeti(L, index, i + 1); out[i] = lua_tonumber(L, -1); lua_pop(L, 1); @@ -63,8 +63,7 @@ int luax_readvec2(lua_State* L, int index, vec2 v, const char* expected) { return index; case LUA_TTABLE: luax_readobjarr(L, index, 2, v, "vec2"); - index++; - return index; + return index + 1; default: vec2_init(v, luax_checkvector(L, index, V_VEC2, expected ? expected : "vec2 or number")); return index + 1; @@ -84,8 +83,7 @@ int luax_readvec3(lua_State* L, int index, vec3 v, const char* expected) { return index; case LUA_TTABLE: luax_readobjarr(L, index, 3, v, "vec3"); - index++; - return index; + return index + 1; default: vec3_init(v, luax_checkvector(L, index, V_VEC3, expected ? expected : "vec3 or number")); return index + 1; @@ -106,8 +104,7 @@ int luax_readvec4(lua_State* L, int index, vec4 v, const char* expected) { return index; case LUA_TTABLE: luax_readobjarr(L, index, 4, v, "vec4"); - index++; - return index; + return index + 1; default: vec4_init(v, luax_checkvector(L, index, V_VEC4, expected ? expected : "vec4 or number")); return index + 1; @@ -135,8 +132,7 @@ int luax_readscale(lua_State* L, int index, vec3 v, int components, const char* return index; case LUA_TTABLE: luax_readobjarr(L, index, 3, v, "scale"); - index++; - return index; + return index + 1; default: { VectorType type; float* u = luax_tovector(L, index++, &type); @@ -172,8 +168,7 @@ int luax_readquat(lua_State* L, int index, quat q, const char* expected) { float v[4]; luax_readobjarr(L, index, 4, v, "quat"); quat_fromAngleAxis(q, v[0], v[1], v[2], v[3]); - index++; - return index; + return index + 1; default: quat_init(q, luax_checkvector(L, index++, V_QUAT, expected ? expected : "quat or number")); return index; From fddd8e44e478d42fca89d5039abf4f67c583509e Mon Sep 17 00:00:00 2001 From: xiejiangzhi Date: Mon, 5 Feb 2024 09:56:17 +0800 Subject: [PATCH 03/12] Read table values as raw values for luax_readquat --- src/api/l_math_vectors.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/api/l_math_vectors.c b/src/api/l_math_vectors.c index 455399eb..1137e531 100644 --- a/src/api/l_math_vectors.c +++ b/src/api/l_math_vectors.c @@ -151,23 +151,22 @@ int luax_readscale(lua_State* L, int index, vec3 v, int components, const char* } int luax_readquat(lua_State* L, int index, quat q, const char* expected) { - float angle, ax, ay, az; + float v[4]; switch (lua_type(L, index)) { case LUA_TNIL: case LUA_TNONE: quat_identity(q); return ++index; case LUA_TNUMBER: - angle = luax_optfloat(L, index++, 0.f); - ax = luax_optfloat(L, index++, 0.f); - ay = luax_optfloat(L, index++, 1.f); - az = luax_optfloat(L, index++, 0.f); - quat_fromAngleAxis(q, angle, ax, ay, az); + v[0] = luax_optfloat(L, index++, 0.f); + v[1] = luax_optfloat(L, index++, 0.f); + v[2] = luax_optfloat(L, index++, 1.f); + v[3] = luax_optfloat(L, index++, 0.f); + quat_fromAngleAxis(q, v[0], v[1], v[2], v[3]); return index; case LUA_TTABLE: - float v[4]; luax_readobjarr(L, index, 4, v, "quat"); - quat_fromAngleAxis(q, v[0], v[1], v[2], v[3]); + quat_init(q, v); return index + 1; default: quat_init(q, luax_checkvector(L, index++, V_QUAT, expected ? expected : "quat or number")); From 9761ee6aaf69636d1a5aab5a9d11855866464ddd Mon Sep 17 00:00:00 2001 From: xiejiangzhi Date: Mon, 5 Feb 2024 10:02:42 +0800 Subject: [PATCH 04/12] Add luax_readobjarr to api.h --- src/api/api.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/api/api.h b/src/api/api.h index 327d7aba..7c5493ee 100644 --- a/src/api/api.h +++ b/src/api/api.h @@ -180,6 +180,7 @@ struct ColoredString* luax_checkcoloredstrings(lua_State* L, int index, uint32_t float* luax_tovector(lua_State* L, int index, VectorType* type); float* luax_checkvector(lua_State* L, int index, VectorType type, const char* expected); float* luax_newtempvector(lua_State* L, VectorType type); +inline void luax_readobjarr(lua_State* L, int index, size_t n, float* out, const char* name); int luax_readvec2(lua_State* L, int index, float* v, const char* expected); int luax_readvec3(lua_State* L, int index, float* v, const char* expected); int luax_readvec4(lua_State* L, int index, float* v, const char* expected); From 9f42eaf2df26e22bdbd8642fd2a112ca9848d57d Mon Sep 17 00:00:00 2001 From: xiejiangzhi Date: Mon, 5 Feb 2024 11:08:19 +0800 Subject: [PATCH 05/12] Move luax_readobjarr definition to api.c --- src/api/api.c | 9 +++++++++ src/api/api.h | 2 +- src/api/l_math_vectors.c | 12 +----------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index a6d0067c..043bc6bd 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -577,3 +577,12 @@ int luax_readmesh(lua_State* L, int index, float** vertices, uint32_t* vertexCou return luaL_argerror(L, index, "table, Mesh, or Model"); } + +void luax_readobjarr(lua_State* L, int index, int n, float* out, const char* name) { + lovrCheck(lua_objlen(L, index) >= n, "length of %s table must >= %i", name, n); + for (int i = 0; i < n; i++) { + lua_rawgeti(L, index, i + 1); + out[i] = lua_tonumber(L, -1); + } + lua_pop(L, n); +} \ No newline at end of file diff --git a/src/api/api.h b/src/api/api.h index 7c5493ee..73f091d9 100644 --- a/src/api/api.h +++ b/src/api/api.h @@ -129,6 +129,7 @@ uint32_t _luax_optu32(lua_State* L, int index, uint32_t fallback); void luax_readcolor(lua_State* L, int index, float color[4]); void luax_optcolor(lua_State* L, int index, float color[4]); int luax_readmesh(lua_State* L, int index, float** vertices, uint32_t* vertexCount, uint32_t** indices, uint32_t* indexCount, bool* shouldFree); +void luax_readobjarr(lua_State* L, int index, int n, float* out, const char* name); // Module helpers @@ -180,7 +181,6 @@ struct ColoredString* luax_checkcoloredstrings(lua_State* L, int index, uint32_t float* luax_tovector(lua_State* L, int index, VectorType* type); float* luax_checkvector(lua_State* L, int index, VectorType type, const char* expected); float* luax_newtempvector(lua_State* L, VectorType type); -inline void luax_readobjarr(lua_State* L, int index, size_t n, float* out, const char* name); int luax_readvec2(lua_State* L, int index, float* v, const char* expected); int luax_readvec3(lua_State* L, int index, float* v, const char* expected); int luax_readvec4(lua_State* L, int index, float* v, const char* expected); diff --git a/src/api/l_math_vectors.c b/src/api/l_math_vectors.c index 1137e531..ab536996 100644 --- a/src/api/l_math_vectors.c +++ b/src/api/l_math_vectors.c @@ -42,15 +42,6 @@ static const uint32_t* swizzles[5] = { // Helpers -inline void luax_readobjarr(lua_State* L, int index, size_t n, float* out, const char* name) { - lovrCheck(lua_objlen(L, index) >= n, "length of %s table must >= %i", name, n); - for (int i = 0; i < n; i++) { - lua_rawgeti(L, index, i + 1); - out[i] = lua_tonumber(L, -1); - lua_pop(L, 1); - } -} - int luax_readvec2(lua_State* L, int index, vec2 v, const char* expected) { switch (lua_type(L, index)) { case LUA_TNIL: @@ -165,8 +156,7 @@ int luax_readquat(lua_State* L, int index, quat q, const char* expected) { quat_fromAngleAxis(q, v[0], v[1], v[2], v[3]); return index; case LUA_TTABLE: - luax_readobjarr(L, index, 4, v, "quat"); - quat_init(q, v); + luax_readobjarr(L, index, 4, q, "quat"); return index + 1; default: quat_init(q, luax_checkvector(L, index++, V_QUAT, expected ? expected : "quat or number")); From 9e3336ad6097aabe1cb8a3bd7e8fc43854db5131 Mon Sep 17 00:00:00 2001 From: xiejiangzhi Date: Mon, 5 Feb 2024 11:28:03 +0800 Subject: [PATCH 06/12] Rollback change of luax_readquat vars names --- src/api/l_math_vectors.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/api/l_math_vectors.c b/src/api/l_math_vectors.c index ab536996..02727065 100644 --- a/src/api/l_math_vectors.c +++ b/src/api/l_math_vectors.c @@ -142,18 +142,18 @@ int luax_readscale(lua_State* L, int index, vec3 v, int components, const char* } int luax_readquat(lua_State* L, int index, quat q, const char* expected) { - float v[4]; + float angle, ax, ay, az; switch (lua_type(L, index)) { case LUA_TNIL: case LUA_TNONE: quat_identity(q); return ++index; case LUA_TNUMBER: - v[0] = luax_optfloat(L, index++, 0.f); - v[1] = luax_optfloat(L, index++, 0.f); - v[2] = luax_optfloat(L, index++, 1.f); - v[3] = luax_optfloat(L, index++, 0.f); - quat_fromAngleAxis(q, v[0], v[1], v[2], v[3]); + angle = luax_optfloat(L, index++, 0.f); + ax = luax_optfloat(L, index++, 0.f); + ay = luax_optfloat(L, index++, 1.f); + az = luax_optfloat(L, index++, 0.f); + quat_fromAngleAxis(q, angle, ax, ay, az); return index; case LUA_TTABLE: luax_readobjarr(L, index, 4, q, "quat"); @@ -185,8 +185,7 @@ int luax_readmat4(lua_State* L, int index, mat4 m, int scaleComponents) { case LUA_TTABLE: if (lua_type(L, index) == LUA_TTABLE && lua_objlen(L, index) >= 16) { luax_readobjarr(L, index, 16, m, "mat4"); - index++; - return index; + return index + 1; } // Fall through From 1a321d004b49c858727a3031bd9057dacfd21bf3 Mon Sep 17 00:00:00 2001 From: xiejiangzhi Date: Tue, 6 Feb 2024 09:21:24 +0800 Subject: [PATCH 07/12] Fix negative index for luax_readobjarr --- src/api/api.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/api/api.c b/src/api/api.c index 043bc6bd..9f0170d8 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -579,7 +579,8 @@ int luax_readmesh(lua_State* L, int index, float** vertices, uint32_t* vertexCou } void luax_readobjarr(lua_State* L, int index, int n, float* out, const char* name) { - lovrCheck(lua_objlen(L, index) >= n, "length of %s table must >= %i", name, n); + lovrCheck(luax_len(L, index) >= n, "length of %s table must >= %i", name, n); + if (index < 0) index = lua_gettop(L) + index + 1; for (int i = 0; i < n; i++) { lua_rawgeti(L, index, i + 1); out[i] = lua_tonumber(L, -1); From cd5b95efce4d2b0ea963faa82e23275df814ffb1 Mon Sep 17 00:00:00 2001 From: xiejiangzhi Date: Wed, 7 Feb 2024 11:43:20 +0800 Subject: [PATCH 08/12] Support table vector for buffer:setData --- src/api/l_graphics_buffer.c | 2 ++ src/api/l_math_vectors.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/api/l_graphics_buffer.c b/src/api/l_graphics_buffer.c index 9c299121..2bd276f3 100644 --- a/src/api/l_graphics_buffer.c +++ b/src/api/l_graphics_buffer.c @@ -248,6 +248,8 @@ void luax_checkdatatuples(lua_State* L, int index, int start, uint32_t count, co const DataField* field = &format->fields[f]; if (lua_isuserdata(L, -1)) { luax_checkfieldv(L, -1, field->type, data + field->offset); + } else if (lua_istable(L, -1)) { + luax_checkfieldt(L, -1, field->type, data + field->offset); } else { while (n < (int) typeComponents[field->type]) { lua_rawgeti(L, -n - 1, subindex + n); diff --git a/src/api/l_math_vectors.c b/src/api/l_math_vectors.c index 02727065..d0505263 100644 --- a/src/api/l_math_vectors.c +++ b/src/api/l_math_vectors.c @@ -183,7 +183,7 @@ int luax_readmat4(lua_State* L, int index, mat4 m, int scaleComponents) { } // Fall through case LUA_TTABLE: - if (lua_type(L, index) == LUA_TTABLE && lua_objlen(L, index) >= 16) { + if (lua_istable(L, index) && luax_len(L, index) >= 16) { luax_readobjarr(L, index, 16, m, "mat4"); return index + 1; } From f980c9ab310797c63f4b3a46ca44d3eb98598afa Mon Sep 17 00:00:00 2001 From: xiejiangzhi Date: Thu, 8 Feb 2024 09:36:25 +0800 Subject: [PATCH 09/12] Fix table vector for luax_readscale with different components --- src/api/l_math_vectors.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/api/l_math_vectors.c b/src/api/l_math_vectors.c index d0505263..caa67d57 100644 --- a/src/api/l_math_vectors.c +++ b/src/api/l_math_vectors.c @@ -56,7 +56,7 @@ int luax_readvec2(lua_State* L, int index, vec2 v, const char* expected) { luax_readobjarr(L, index, 2, v, "vec2"); return index + 1; default: - vec2_init(v, luax_checkvector(L, index, V_VEC2, expected ? expected : "vec2 or number")); + vec2_init(v, luax_checkvector(L, index, V_VEC2, expected ? expected : "vec2, table or number")); return index + 1; } } @@ -76,7 +76,7 @@ int luax_readvec3(lua_State* L, int index, vec3 v, const char* expected) { luax_readobjarr(L, index, 3, v, "vec3"); return index + 1; default: - vec3_init(v, luax_checkvector(L, index, V_VEC3, expected ? expected : "vec3 or number")); + vec3_init(v, luax_checkvector(L, index, V_VEC3, expected ? expected : "vec3, table or number")); return index + 1; } } @@ -97,7 +97,7 @@ int luax_readvec4(lua_State* L, int index, vec4 v, const char* expected) { luax_readobjarr(L, index, 4, v, "vec4"); return index + 1; default: - vec4_init(v, luax_checkvector(L, index, V_VEC4, expected ? expected : "vec4 or number")); + vec4_init(v, luax_checkvector(L, index, V_VEC4, expected ? expected : "vec4, table or number")); return index + 1; } } @@ -122,7 +122,7 @@ int luax_readscale(lua_State* L, int index, vec3 v, int components, const char* } return index; case LUA_TTABLE: - luax_readobjarr(L, index, 3, v, "scale"); + luax_readobjarr(L, index, components, v, "scale"); return index + 1; default: { VectorType type; @@ -134,7 +134,7 @@ int luax_readscale(lua_State* L, int index, vec3 v, int components, const char* } else if (type == V_VEC3) { vec3_init(v, u); } else { - return luax_typeerror(L, index, "vec2, vec3, or number"); + return luax_typeerror(L, index, "vec2, vec3, table or number"); } return index; } @@ -159,7 +159,7 @@ int luax_readquat(lua_State* L, int index, quat q, const char* expected) { luax_readobjarr(L, index, 4, q, "quat"); return index + 1; default: - quat_init(q, luax_checkvector(L, index++, V_QUAT, expected ? expected : "quat or number")); + quat_init(q, luax_checkvector(L, index++, V_QUAT, expected ? expected : "quat, table or number")); return index; } } @@ -193,7 +193,7 @@ int luax_readmat4(lua_State* L, int index, mat4 m, int scaleComponents) { float S[3]; float R[4]; mat4_identity(m); - index = luax_readvec3(L, index, m + 12, "mat4, vec3, or number"); + index = luax_readvec3(L, index, m + 12, "mat4, vec3, table or number"); index = luax_readscale(L, index, S, scaleComponents, NULL); index = luax_readquat(L, index, R, NULL); mat4_rotateQuat(m, R); From d1e7d136f0b2f66b632564b475553e9b1177aef2 Mon Sep 17 00:00:00 2001 From: xiejiangzhi Date: Sat, 17 Feb 2024 12:23:48 +0800 Subject: [PATCH 10/12] Fix luax_readscale with lua table --- src/api/l_math_vectors.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/api/l_math_vectors.c b/src/api/l_math_vectors.c index caa67d57..b9f53289 100644 --- a/src/api/l_math_vectors.c +++ b/src/api/l_math_vectors.c @@ -122,7 +122,15 @@ int luax_readscale(lua_State* L, int index, vec3 v, int components, const char* } return index; case LUA_TTABLE: - luax_readobjarr(L, index, components, v, "scale"); + int tlen = luax_len(L, index); + if (tlen >= 3) { + luax_readobjarr(L, index, 3, v, "scale"); + } else if (tlen == 2) { + luax_readobjarr(L, index, 2, v, "scale"); + v[2] = 1.f; + } else { + return luax_typeerror(L, index, "table length must >= 2"); + } return index + 1; default: { VectorType type; From 4687f752c7d39c221337292c736a061aaa29ab4e Mon Sep 17 00:00:00 2001 From: xiejiangzhi Date: Mon, 19 Feb 2024 11:14:21 +0800 Subject: [PATCH 11/12] Fix compile error for luax_readscale --- plugins/lovr-http | 2 +- src/api/l_math_vectors.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/lovr-http b/plugins/lovr-http index 69906e40..67d92ac4 160000 --- a/plugins/lovr-http +++ b/plugins/lovr-http @@ -1 +1 @@ -Subproject commit 69906e40f571bcbdda00fe2e6f504c3487f93d56 +Subproject commit 67d92ac4239d79a8ec44e6dea8c4eda9ae514628 diff --git a/src/api/l_math_vectors.c b/src/api/l_math_vectors.c index b9f53289..c2e72a66 100644 --- a/src/api/l_math_vectors.c +++ b/src/api/l_math_vectors.c @@ -103,6 +103,7 @@ int luax_readvec4(lua_State* L, int index, vec4 v, const char* expected) { } int luax_readscale(lua_State* L, int index, vec3 v, int components, const char* expected) { + int tlen; switch (lua_type(L, index)) { case LUA_TNIL: case LUA_TNONE: @@ -122,7 +123,7 @@ int luax_readscale(lua_State* L, int index, vec3 v, int components, const char* } return index; case LUA_TTABLE: - int tlen = luax_len(L, index); + tlen = luax_len(L, index); if (tlen >= 3) { luax_readobjarr(L, index, 3, v, "scale"); } else if (tlen == 2) { From a0484bb0f27519c884ef1ea9ddab00300bc15f48 Mon Sep 17 00:00:00 2001 From: xiejiangzhi Date: Wed, 21 Feb 2024 20:27:05 +0800 Subject: [PATCH 12/12] Support table for luax_checkvector --- src/api/l_math.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/api/l_math.c b/src/api/l_math.c index 460eb03d..6b1f5b94 100644 --- a/src/api/l_math.c +++ b/src/api/l_math.c @@ -33,12 +33,18 @@ extern const luaL_Reg lovrMat4[]; static LOVR_THREAD_LOCAL Pool* pool; -static struct { const char* name; lua_CFunction constructor, indexer; const luaL_Reg* api; int metaref; } lovrVectorInfo[] = { - [V_VEC2] = { "vec2", l_lovrMathVec2, l_lovrVec2__metaindex, lovrVec2, LUA_REFNIL }, - [V_VEC3] = { "vec3", l_lovrMathVec3, l_lovrVec3__metaindex, lovrVec3, LUA_REFNIL }, - [V_VEC4] = { "vec4", l_lovrMathVec4, l_lovrVec4__metaindex, lovrVec4, LUA_REFNIL }, - [V_QUAT] = { "quat", l_lovrMathQuat, l_lovrQuat__metaindex, lovrQuat, LUA_REFNIL }, - [V_MAT4] = { "mat4", l_lovrMathMat4, l_lovrMat4__metaindex, lovrMat4, LUA_REFNIL } +static struct { + const char* name; + lua_CFunction constructor, indexer; + const luaL_Reg* api; + int metaref; + int components; +} lovrVectorInfo[] = { + [V_VEC2] = { "vec2", l_lovrMathVec2, l_lovrVec2__metaindex, lovrVec2, LUA_REFNIL, 2 }, + [V_VEC3] = { "vec3", l_lovrMathVec3, l_lovrVec3__metaindex, lovrVec3, LUA_REFNIL, 3 }, + [V_VEC4] = { "vec4", l_lovrMathVec4, l_lovrVec4__metaindex, lovrVec4, LUA_REFNIL, 4 }, + [V_QUAT] = { "quat", l_lovrMathQuat, l_lovrQuat__metaindex, lovrQuat, LUA_REFNIL, 4 }, + [V_MAT4] = { "mat4", l_lovrMathMat4, l_lovrMat4__metaindex, lovrMat4, LUA_REFNIL, 16 } }; static void luax_destroypool(void) { @@ -70,8 +76,18 @@ float* luax_tovector(lua_State* L, int index, VectorType* type) { float* luax_checkvector(lua_State* L, int index, VectorType type, const char* expected) { VectorType t; - float* p = luax_tovector(L, index, &t); - if (!p || t != type) luax_typeerror(L, index, expected ? expected : lovrVectorInfo[type].name); + float* p; + if (lua_istable(L, index)) { + lovrPoolAllocate(pool, type, &p); + luax_readobjarr(L, index, lovrVectorInfo[type].components, p, lovrVectorInfo[type].name); + return p; + } else { + p = luax_tovector(L, index, &t); + } + + if (!p || t != type) { + luax_typeerror(L, index, expected ? expected : lovrVectorInfo[type].name); + } return p; }