mirror of https://github.com/bjornbytes/lovr.git
Merge a0484bb0f2
into aa79822edb
This commit is contained in:
commit
78c8838332
|
@ -1 +1 @@
|
||||||
Subproject commit 69906e40f571bcbdda00fe2e6f504c3487f93d56
|
Subproject commit 67d92ac4239d79a8ec44e6dea8c4eda9ae514628
|
|
@ -577,3 +577,13 @@ int luax_readmesh(lua_State* L, int index, float** vertices, uint32_t* vertexCou
|
||||||
|
|
||||||
return luaL_argerror(L, index, "table, Mesh, or Model");
|
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(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);
|
||||||
|
}
|
||||||
|
lua_pop(L, n);
|
||||||
|
}
|
|
@ -130,6 +130,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_readcolor(lua_State* L, int index, float color[4]);
|
||||||
void luax_optcolor(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);
|
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
|
// Module helpers
|
||||||
|
|
||||||
|
|
|
@ -251,6 +251,8 @@ void luax_checkdatatuples(lua_State* L, int index, int start, uint32_t count, co
|
||||||
const DataField* field = &format->fields[f];
|
const DataField* field = &format->fields[f];
|
||||||
if (lua_isuserdata(L, -1)) {
|
if (lua_isuserdata(L, -1)) {
|
||||||
luax_checkfieldv(L, -1, field->type, data + field->offset);
|
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 {
|
} else {
|
||||||
while (n < (int) typeComponents[field->type]) {
|
while (n < (int) typeComponents[field->type]) {
|
||||||
lua_rawgeti(L, -n - 1, subindex + n);
|
lua_rawgeti(L, -n - 1, subindex + n);
|
||||||
|
|
|
@ -33,12 +33,18 @@ extern const luaL_Reg lovrMat4[];
|
||||||
|
|
||||||
static LOVR_THREAD_LOCAL Pool* pool;
|
static LOVR_THREAD_LOCAL Pool* pool;
|
||||||
|
|
||||||
static struct { const char* name; lua_CFunction constructor, indexer; const luaL_Reg* api; int metaref; } lovrVectorInfo[] = {
|
static struct {
|
||||||
[V_VEC2] = { "vec2", l_lovrMathVec2, l_lovrVec2__metaindex, lovrVec2, LUA_REFNIL },
|
const char* name;
|
||||||
[V_VEC3] = { "vec3", l_lovrMathVec3, l_lovrVec3__metaindex, lovrVec3, LUA_REFNIL },
|
lua_CFunction constructor, indexer;
|
||||||
[V_VEC4] = { "vec4", l_lovrMathVec4, l_lovrVec4__metaindex, lovrVec4, LUA_REFNIL },
|
const luaL_Reg* api;
|
||||||
[V_QUAT] = { "quat", l_lovrMathQuat, l_lovrQuat__metaindex, lovrQuat, LUA_REFNIL },
|
int metaref;
|
||||||
[V_MAT4] = { "mat4", l_lovrMathMat4, l_lovrMat4__metaindex, lovrMat4, LUA_REFNIL }
|
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) {
|
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) {
|
float* luax_checkvector(lua_State* L, int index, VectorType type, const char* expected) {
|
||||||
VectorType t;
|
VectorType t;
|
||||||
float* p = luax_tovector(L, index, &t);
|
float* p;
|
||||||
if (!p || t != type) luax_typeerror(L, index, expected ? expected : lovrVectorInfo[type].name);
|
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;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,8 +52,11 @@ int luax_readvec2(lua_State* L, int index, vec2 v, const char* expected) {
|
||||||
v[0] = luax_tofloat(L, index++);
|
v[0] = luax_tofloat(L, index++);
|
||||||
v[1] = luax_optfloat(L, index++, v[0]);
|
v[1] = luax_optfloat(L, index++, v[0]);
|
||||||
return index;
|
return index;
|
||||||
|
case LUA_TTABLE:
|
||||||
|
luax_readobjarr(L, index, 2, v, "vec2");
|
||||||
|
return index + 1;
|
||||||
default:
|
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;
|
return index + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,8 +72,11 @@ int luax_readvec3(lua_State* L, int index, vec3 v, const char* expected) {
|
||||||
v[1] = luax_optfloat(L, index++, v[0]);
|
v[1] = luax_optfloat(L, index++, v[0]);
|
||||||
v[2] = luax_optfloat(L, index++, v[0]);
|
v[2] = luax_optfloat(L, index++, v[0]);
|
||||||
return index;
|
return index;
|
||||||
|
case LUA_TTABLE:
|
||||||
|
luax_readobjarr(L, index, 3, v, "vec3");
|
||||||
|
return index + 1;
|
||||||
default:
|
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;
|
return index + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,13 +93,17 @@ int luax_readvec4(lua_State* L, int index, vec4 v, const char* expected) {
|
||||||
v[2] = luax_optfloat(L, index++, v[0]);
|
v[2] = luax_optfloat(L, index++, v[0]);
|
||||||
v[3] = luax_optfloat(L, index++, v[0]);
|
v[3] = luax_optfloat(L, index++, v[0]);
|
||||||
return index;
|
return index;
|
||||||
|
case LUA_TTABLE:
|
||||||
|
luax_readobjarr(L, index, 4, v, "vec4");
|
||||||
|
return index + 1;
|
||||||
default:
|
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;
|
return index + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int luax_readscale(lua_State* L, int index, vec3 v, int components, 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)) {
|
switch (lua_type(L, index)) {
|
||||||
case LUA_TNIL:
|
case LUA_TNIL:
|
||||||
case LUA_TNONE:
|
case LUA_TNONE:
|
||||||
|
@ -112,6 +122,17 @@ int luax_readscale(lua_State* L, int index, vec3 v, int components, const char*
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return index;
|
return index;
|
||||||
|
case LUA_TTABLE:
|
||||||
|
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: {
|
default: {
|
||||||
VectorType type;
|
VectorType type;
|
||||||
float* u = luax_tovector(L, index++, &type);
|
float* u = luax_tovector(L, index++, &type);
|
||||||
|
@ -122,7 +143,7 @@ int luax_readscale(lua_State* L, int index, vec3 v, int components, const char*
|
||||||
} else if (type == V_VEC3) {
|
} else if (type == V_VEC3) {
|
||||||
vec3_init(v, u);
|
vec3_init(v, u);
|
||||||
} else {
|
} else {
|
||||||
return luax_typeerror(L, index, "vec2, vec3, or number");
|
return luax_typeerror(L, index, "vec2, vec3, table or number");
|
||||||
}
|
}
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
@ -143,8 +164,11 @@ int luax_readquat(lua_State* L, int index, quat q, const char* expected) {
|
||||||
az = luax_optfloat(L, index++, 0.f);
|
az = luax_optfloat(L, index++, 0.f);
|
||||||
quat_fromAngleAxis(q, angle, ax, ay, az);
|
quat_fromAngleAxis(q, angle, ax, ay, az);
|
||||||
return index;
|
return index;
|
||||||
|
case LUA_TTABLE:
|
||||||
|
luax_readobjarr(L, index, 4, q, "quat");
|
||||||
|
return index + 1;
|
||||||
default:
|
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;
|
return index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -167,11 +191,18 @@ int luax_readmat4(lua_State* L, int index, mat4 m, int scaleComponents) {
|
||||||
}
|
}
|
||||||
} // Fall through
|
} // Fall through
|
||||||
|
|
||||||
|
case LUA_TTABLE:
|
||||||
|
if (lua_istable(L, index) && luax_len(L, index) >= 16) {
|
||||||
|
luax_readobjarr(L, index, 16, m, "mat4");
|
||||||
|
return index + 1;
|
||||||
|
}
|
||||||
|
// Fall through
|
||||||
|
|
||||||
case LUA_TNUMBER: {
|
case LUA_TNUMBER: {
|
||||||
float S[3];
|
float S[3];
|
||||||
float R[4];
|
float R[4];
|
||||||
mat4_identity(m);
|
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_readscale(L, index, S, scaleComponents, NULL);
|
||||||
index = luax_readquat(L, index, R, NULL);
|
index = luax_readquat(L, index, R, NULL);
|
||||||
mat4_rotateQuat(m, R);
|
mat4_rotateQuat(m, R);
|
||||||
|
|
Loading…
Reference in New Issue