diff --git a/src/modules/graphics/opengl/Mesh.cpp b/src/modules/graphics/opengl/Mesh.cpp index f9a7afc18..ff4fc934b 100644 --- a/src/modules/graphics/opengl/Mesh.cpp +++ b/src/modules/graphics/opengl/Mesh.cpp @@ -76,6 +76,17 @@ void Mesh::setVertices(const std::vector &verts) memcpy(vbo_mapper.get(), &verts[0], size); } +const Vertex *Mesh::getVertices() const +{ + if (vbo) + { + VertexBuffer::Bind vbo_bind(*vbo); + return (Vertex *) vbo->map(); + } + + return nullptr; +} + void Mesh::setVertex(size_t index, const Vertex &v) { if (index >= vertex_count) diff --git a/src/modules/graphics/opengl/Mesh.h b/src/modules/graphics/opengl/Mesh.h index 8d30f5e20..45d21ce53 100644 --- a/src/modules/graphics/opengl/Mesh.h +++ b/src/modules/graphics/opengl/Mesh.h @@ -71,6 +71,11 @@ public: **/ void setVertices(const std::vector &verts); + /** + * Gets all of the vertices in the Mesh as an array. + **/ + const Vertex *getVertices() const; + /** * Sets an individual vertex in the Mesh. * @param index The index into the list of vertices to use. diff --git a/src/modules/graphics/opengl/wrap_Mesh.cpp b/src/modules/graphics/opengl/wrap_Mesh.cpp index 4419995b8..624eecbf3 100644 --- a/src/modules/graphics/opengl/wrap_Mesh.cpp +++ b/src/modules/graphics/opengl/wrap_Mesh.cpp @@ -133,6 +133,52 @@ int w_Mesh_setVertices(lua_State *L) return 0; } +int w_Mesh_getVertices(lua_State *L) +{ + Mesh *t = luax_checkmesh(L, 1); + + const Vertex *vertices = t->getVertices(); + + size_t count = t->getVertexCount(); + lua_createtable(L, count, 0); + + for (size_t i = 0; i < count; i++) + { + // Create vertex table. + lua_createtable(L, 8, 0); + + lua_pushnumber(L, vertices[i].x); + lua_rawseti(L, -2, 1); + + lua_pushnumber(L, vertices[i].y); + lua_rawseti(L, -2, 2); + + lua_pushnumber(L, vertices[i].s); + lua_rawseti(L, -2, 3); + + lua_pushnumber(L, vertices[i].t); + lua_rawseti(L, -2, 4); + + lua_pushnumber(L, vertices[i].r); + lua_rawseti(L, -2, 5); + + lua_pushnumber(L, vertices[i].g); + lua_rawseti(L, -2, 6); + + lua_pushnumber(L, vertices[i].b); + lua_rawseti(L, -2, 7); + + lua_pushnumber(L, vertices[i].a); + lua_rawseti(L, -2, 8); + + // Insert vertex table into vertices table. + lua_rawseti(L, -2, i + 1); + } + + // Return vertices table. + return 1; +} + int w_Mesh_getVertexCount(lua_State *L) { Mesh *t = luax_checkmesh(L, 1); @@ -258,6 +304,7 @@ static const luaL_Reg functions[] = { "setVertex", w_Mesh_setVertex }, { "getVertex", w_Mesh_getVertex }, { "setVertices", w_Mesh_setVertices }, + { "getVertices", w_Mesh_getVertices }, { "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 fd584b1ce..1204bbcea 100644 --- a/src/modules/graphics/opengl/wrap_Mesh.h +++ b/src/modules/graphics/opengl/wrap_Mesh.h @@ -37,6 +37,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_getVertices(lua_State *L); int w_Mesh_getVertexCount(lua_State *L); int w_Mesh_setVertexMap(lua_State *L); int w_Mesh_getVertexMap(lua_State *L);