|
@@ -440,8 +440,10 @@ int w_newShader(lua_State *L)
|
|
|
|
|
|
int w_newMesh(lua_State *L)
|
|
|
{
|
|
|
- // Check first argument: mandatory table of vertices.
|
|
|
- luaL_checktype(L, 1, LUA_TTABLE);
|
|
|
+ // Check first argument: table of vertices or number of vertices.
|
|
|
+ int ttype = lua_type(L, 1);
|
|
|
+ if (ttype != LUA_TTABLE && ttype != LUA_TNUMBER)
|
|
|
+ luaL_argerror(L, 1, "table or number expected");
|
|
|
|
|
|
// Second argument: optional texture.
|
|
|
Texture *tex = nullptr;
|
|
@@ -456,52 +458,60 @@ int w_newMesh(lua_State *L)
|
|
|
if (str && !Mesh::getConstant(str, mode))
|
|
|
return luaL_error(L, "Invalid mesh draw mode: %s", str);
|
|
|
|
|
|
- size_t vertex_count = lua_objlen(L, 1);
|
|
|
- std::vector<Vertex> vertices;
|
|
|
- vertices.reserve(vertex_count);
|
|
|
-
|
|
|
- bool use_colors = false;
|
|
|
+ Mesh *t = nullptr;
|
|
|
|
|
|
- // Get the vertices from the table.
|
|
|
- for (size_t i = 1; i <= vertex_count; i++)
|
|
|
+ if (ttype == LUA_TTABLE)
|
|
|
{
|
|
|
- lua_rawgeti(L, 1, i);
|
|
|
+ size_t vertex_count = lua_objlen(L, 1);
|
|
|
+ std::vector<Vertex> vertices;
|
|
|
+ vertices.reserve(vertex_count);
|
|
|
|
|
|
- if (lua_type(L, -1) != LUA_TTABLE)
|
|
|
- return luax_typerror(L, 1, "table of tables");
|
|
|
+ bool use_colors = false;
|
|
|
|
|
|
- for (int j = 1; j <= 8; j++)
|
|
|
- lua_rawgeti(L, -j, j);
|
|
|
+ // Get the vertices from the table.
|
|
|
+ for (size_t i = 1; i <= vertex_count; i++)
|
|
|
+ {
|
|
|
+ lua_rawgeti(L, 1, i);
|
|
|
|
|
|
- Vertex v;
|
|
|
+ if (lua_type(L, -1) != LUA_TTABLE)
|
|
|
+ return luax_typerror(L, 1, "table of tables");
|
|
|
|
|
|
- v.x = (float) luaL_checknumber(L, -8);
|
|
|
- v.y = (float) luaL_checknumber(L, -7);
|
|
|
+ for (int j = 1; j <= 8; j++)
|
|
|
+ lua_rawgeti(L, -j, j);
|
|
|
|
|
|
- v.s = (float) luaL_checknumber(L, -6);
|
|
|
- v.t = (float) luaL_checknumber(L, -5);
|
|
|
+ Vertex v;
|
|
|
|
|
|
- v.r = (unsigned char) luaL_optinteger(L, -4, 255);
|
|
|
- v.g = (unsigned char) luaL_optinteger(L, -3, 255);
|
|
|
- v.b = (unsigned char) luaL_optinteger(L, -2, 255);
|
|
|
- v.a = (unsigned char) luaL_optinteger(L, -1, 255);
|
|
|
+ v.x = (float) luaL_checknumber(L, -8);
|
|
|
+ v.y = (float) luaL_checknumber(L, -7);
|
|
|
|
|
|
- // Enable per-vertex coloring if any color is not the default.
|
|
|
- if (!use_colors && (v.r != 255 || v.g != 255 || v.b != 255 || v.a != 255))
|
|
|
- use_colors = true;
|
|
|
+ v.s = (float) luaL_checknumber(L, -6);
|
|
|
+ v.t = (float) luaL_checknumber(L, -5);
|
|
|
|
|
|
- lua_pop(L, 9);
|
|
|
- vertices.push_back(v);
|
|
|
- }
|
|
|
+ v.r = (unsigned char) luaL_optinteger(L, -4, 255);
|
|
|
+ v.g = (unsigned char) luaL_optinteger(L, -3, 255);
|
|
|
+ v.b = (unsigned char) luaL_optinteger(L, -2, 255);
|
|
|
+ v.a = (unsigned char) luaL_optinteger(L, -1, 255);
|
|
|
|
|
|
- Mesh *t = nullptr;
|
|
|
- EXCEPT_GUARD(t = instance->newMesh(vertices, mode);)
|
|
|
+ // Enable per-vertex coloring if any color is not the default.
|
|
|
+ if (!use_colors && (v.r != 255 || v.g != 255 || v.b != 255 || v.a != 255))
|
|
|
+ use_colors = true;
|
|
|
+
|
|
|
+ lua_pop(L, 9);
|
|
|
+ vertices.push_back(v);
|
|
|
+ }
|
|
|
+
|
|
|
+ EXCEPT_GUARD(t = instance->newMesh(vertices, mode);)
|
|
|
+ t->setVertexColors(use_colors);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ int count = luaL_checkint(L, 1);
|
|
|
+ EXCEPT_GUARD(t = instance->newMesh(count, mode);)
|
|
|
+ }
|
|
|
|
|
|
if (tex)
|
|
|
t->setTexture(tex);
|
|
|
|
|
|
- t->setVertexColors(use_colors);
|
|
|
-
|
|
|
luax_pushtype(L, "Mesh", GRAPHICS_MESH_T, t);
|
|
|
return 1;
|
|
|
}
|