int MeshBinder::setVertexArray(lua_State *L) { Binder binder(L); GMesh *mesh = static_cast<GMesh*>(binder.getInstance("Mesh", 1)); std::vector<float> vertices; int order=mesh->is3d()?3:2; if (lua_type(L, 2) == LUA_TTABLE) { int n = lua_objlen(L, 2); n = (n / order) * order; vertices.resize(n); for (int i = 0; i < n; ++i) { lua_rawgeti(L, 2, i + 1); vertices[i] = luaL_checknumber(L, -1); lua_pop(L, 1); } } else { int n = lua_gettop(L) - 1; n = (n / order) * order; vertices.resize(n); for (int i = 0; i < n; ++i) vertices[i] = luaL_checknumber(L, i + 2); } mesh->setVertexArray(&vertices[0], vertices.size()); return 0; }
int MeshBinder::setVertices(lua_State *L) { Binder binder(L); GMesh *mesh = static_cast<GMesh*>(binder.getInstance("Mesh", 1)); bool is3d=mesh->is3d(); int order=is3d?4:3; if (lua_type(L, 2) == LUA_TTABLE) { int n = lua_objlen(L, 2); for (int k = 0; k < n/order; ++k) { lua_rawgeti(L, 2, k * order + 1); int i = luaL_checkinteger(L, -1) - 1; lua_pop(L, 1); lua_rawgeti(L, 2, k * order + 2); float x = luaL_checknumber(L, -1); lua_pop(L, 1); lua_rawgeti(L, 2, k * order + 3); float y = luaL_checknumber(L, -1); lua_pop(L, 1); float z=0; if (is3d) { lua_rawgeti(L, 2, k * order + 4); z = luaL_checknumber(L, -1); lua_pop(L, 1); } mesh->setVertex(i, x, y, z); } } else { int n = lua_gettop(L) - 1; for (int k = 0; k < n/order; ++k) { int i = luaL_checkinteger(L, k * order + 2) - 1; float x = luaL_checknumber(L, k * order + 3); float y = luaL_checknumber(L, k * order + 4); float z=0; if (is3d) z= luaL_checknumber(L, k * order + 5); mesh->setVertex(i, x, y, z); } } return 0; }
int MeshBinder::getVertex(lua_State *L) { Binder binder(L); GMesh *mesh = static_cast<GMesh*>(binder.getInstance("Mesh", 1)); int i = luaL_checkinteger(L, 2) - 1; if (i < 0 || i >= mesh->getVertexArraySize()) return luaL_error(L, "The supplied index is out of bounds."); int order=mesh->is3d()?3:2; float x, y, z; mesh->getVertex(i, &x, &y, &z); lua_pushnumber(L, x); lua_pushnumber(L, y); if (order==3) lua_pushnumber(L, z); return order; }