diff --git a/src/modules/graphics/opengl/Mesh.cpp b/src/modules/graphics/opengl/Mesh.cpp index deb54e9b4..92be5bf78 100644 --- a/src/modules/graphics/opengl/Mesh.cpp +++ b/src/modules/graphics/opengl/Mesh.cpp @@ -31,12 +31,12 @@ namespace opengl { Mesh::Mesh(const std::vector &verts, Mesh::DrawMode mode) - : vbo(0) + : vbo(nullptr) , vertex_count(0) - , ibo(0) + , ibo(nullptr) , element_count(0) , draw_mode(mode) - , image(0) + , image(nullptr) , colors_enabled(false) { setVertices(verts); @@ -58,7 +58,7 @@ void Mesh::setVertices(const std::vector &verts) if (vbo && size > vbo->getSize()) { delete vbo; - vbo = 0; + vbo = nullptr; } if (!vbo) @@ -84,7 +84,7 @@ void Mesh::setVertex(size_t index, const Vertex &v) VertexBuffer::Bind vbo_bind(*vbo); // We unmap the vertex buffer in Mesh::draw. This lets us coalesce the - // buffer transfer calls. + // buffer transfer calls into just one. Vertex *vertices = (Vertex *) vbo->map(); vertices[index] = v; } @@ -106,7 +106,7 @@ size_t Mesh::getVertexCount() const return vertex_count; } -void Mesh::setVertexMap(const std::vector &map) +void Mesh::setVertexMap(const std::vector &map) { for (size_t i = 0; i < map.size(); i++) { @@ -114,12 +114,12 @@ void Mesh::setVertexMap(const std::vector &map) throw love::Exception("Invalid vertex map value: %d", map[i]); } - size_t size = sizeof(uint16) * map.size(); + size_t size = sizeof(uint32) * map.size(); if (ibo && size > ibo->getSize()) { delete ibo; - ibo = 0; + ibo = nullptr; } if (!ibo) @@ -140,14 +140,14 @@ void Mesh::setVertexMap(const std::vector &map) } } -const uint16 *Mesh::getVertexMap() const +const uint32 *Mesh::getVertexMap() const { if (ibo && element_count > 0) { VertexBuffer::Bind ibo_bind(*ibo); // We unmap the buffer in Mesh::draw and Mesh::setVertexMap. - return (uint16 *) ibo->map(); + return (uint32 *) ibo->map(); } return 0; @@ -173,7 +173,7 @@ void Mesh::setImage() if (image) image->release(); - image = 0; + image = nullptr; } Image *Mesh::getImage() const @@ -249,7 +249,7 @@ void Mesh::draw(float x, float y, float angle, float sx, float sy, float ox, flo ibo->unmap(); // Use the custom vertex map to draw the vertices. - glDrawElements(mode, element_count, GL_UNSIGNED_SHORT, ibo->getPointer(0)); + glDrawElements(mode, element_count, GL_UNSIGNED_INT, ibo->getPointer(0)); } else { diff --git a/src/modules/graphics/opengl/Mesh.h b/src/modules/graphics/opengl/Mesh.h index 4515bd2c4..8d30f5e20 100644 --- a/src/modules/graphics/opengl/Mesh.h +++ b/src/modules/graphics/opengl/Mesh.h @@ -90,14 +90,14 @@ public: * A 0-element vector is equivalent to the default vertex map: * {0, 1, 2, 3, 4, ...} **/ - void setVertexMap(const std::vector &map); + void setVertexMap(const std::vector &map); /** * Gets a pointer to the vertex map array. The pointer is only valid until * the next function call in the graphics module. * May return null if the vertex map is empty. **/ - const uint16 *getVertexMap() const; + const uint32 *getVertexMap() const; /** * Gets the total number of elements in the vertex map array. diff --git a/src/modules/graphics/opengl/wrap_Graphics.cpp b/src/modules/graphics/opengl/wrap_Graphics.cpp index fa2c9bda8..e6e954b01 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.cpp +++ b/src/modules/graphics/opengl/wrap_Graphics.cpp @@ -461,7 +461,7 @@ int w_newMesh(lua_State *L) lua_rawgeti(L, 1, i); if (lua_type(L, -1) != LUA_TTABLE) - return luax_typerror(L, 2, "table of tables"); + return luax_typerror(L, 1, "table of tables"); for (int j = 1; j <= 8; j++) lua_rawgeti(L, -j, j); diff --git a/src/modules/graphics/opengl/wrap_Mesh.cpp b/src/modules/graphics/opengl/wrap_Mesh.cpp index 41ebf0055..1c662f954 100644 --- a/src/modules/graphics/opengl/wrap_Mesh.cpp +++ b/src/modules/graphics/opengl/wrap_Mesh.cpp @@ -93,6 +93,46 @@ int w_Mesh_getVertex(lua_State *L) return 8; } +int w_Mesh_setVertices(lua_State *L) +{ + Mesh *t = luax_checkmesh(L, 1); + + size_t vertex_count = lua_objlen(L, 2); + std::vector vertices; + vertices.reserve(vertex_count); + + // Get the vertices from the table. + for (size_t i = 1; i <= vertex_count; i++) + { + lua_rawgeti(L, 2, i); + + if (lua_type(L, -1) != LUA_TTABLE) + return luax_typerror(L, 2, "table of tables"); + + for (int j = 1; j <= 8; j++) + lua_rawgeti(L, -j, j); + + Vertex v; + + v.x = (float) luaL_checknumber(L, -8); + v.y = (float) luaL_checknumber(L, -7); + + v.s = (float) luaL_checknumber(L, -6); + v.t = (float) luaL_checknumber(L, -5); + + 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); + + lua_pop(L, 9); + vertices.push_back(v); + } + + EXCEPT_GUARD(t->setVertices(vertices);) + return 0; +} + int w_Mesh_getVertexCount(lua_State *L) { Mesh *t = luax_checkmesh(L, 1); @@ -107,7 +147,7 @@ int w_Mesh_setVertexMap(lua_State *L) bool is_table = lua_istable(L, 2); int nargs = is_table ? lua_objlen(L, 2) : lua_gettop(L) - 1; - std::vector vertexmap; + std::vector vertexmap; vertexmap.reserve(nargs); for (int i = 0; i < nargs; i++) @@ -115,11 +155,11 @@ int w_Mesh_setVertexMap(lua_State *L) if (is_table) { lua_rawgeti(L, 2, i + 1); - vertexmap.push_back(uint16(luaL_checkinteger(L, -1) - 1)); + vertexmap.push_back(uint32(luaL_checkinteger(L, -1) - 1)); lua_pop(L, 1); } else - vertexmap.push_back(uint16(luaL_checkinteger(L, i + 2) - 1)); + vertexmap.push_back(uint32(luaL_checkinteger(L, i + 2) - 1)); } EXCEPT_GUARD(t->setVertexMap(vertexmap);) @@ -130,7 +170,7 @@ int w_Mesh_getVertexMap(lua_State *L) { Mesh *t = luax_checkmesh(L, 1); - const uint16 *vertex_map = t->getVertexMap(); + const uint32 *vertex_map = t->getVertexMap(); size_t elements = t->getVertexMapCount(); lua_createtable(L, elements, 0); @@ -216,6 +256,7 @@ static const luaL_Reg functions[] = { { "setVertex", w_Mesh_setVertex }, { "getVertex", w_Mesh_getVertex }, + { "setVertices", w_Mesh_setVertices }, { "getVertexCount", w_Mesh_getVertexCount }, { "setVertexMap", w_Mesh_setVertexMap }, { "getVertexMap", w_Mesh_getVertexMap }, diff --git a/src/modules/graphics/opengl/wrap_Mesh.h b/src/modules/graphics/opengl/wrap_Mesh.h index 7fa112a31..fd584b1ce 100644 --- a/src/modules/graphics/opengl/wrap_Mesh.h +++ b/src/modules/graphics/opengl/wrap_Mesh.h @@ -36,6 +36,7 @@ Mesh *luax_checkmesh(lua_State *L, int idx); int w_Mesh_setVertex(lua_State *L); int w_Mesh_getVertex(lua_State *L); +int w_Mesh_setVertices(lua_State *L); int w_Mesh_getVertexCount(lua_State *L); int w_Mesh_setVertexMap(lua_State *L); int w_Mesh_getVertexMap(lua_State *L);