From 3b8d485fe9073b9b439a5f9c0df4941338eee9fa Mon Sep 17 00:00:00 2001 From: bjorn Date: Tue, 14 Jan 2020 00:22:35 -0800 Subject: [PATCH] Mesh:setVertices(Blob); --- src/api/l_graphics_mesh.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/api/l_graphics_mesh.c b/src/api/l_graphics_mesh.c index 76252f0c..81955c6c 100644 --- a/src/api/l_graphics_mesh.c +++ b/src/api/l_graphics_mesh.c @@ -271,19 +271,30 @@ static int l_lovrMeshSetVertexAttribute(lua_State* L) { static int l_lovrMeshSetVertices(lua_State* L) { Mesh* mesh = luax_checktype(L, 1, Mesh); - uint32_t capacity = lovrMeshGetVertexCount(mesh); - luaL_checktype(L, 2, LUA_TTABLE); - uint32_t sourceSize = luax_len(L, 2); - uint32_t start = luaL_optinteger(L, 3, 1) - 1; - uint32_t count = luaL_optinteger(L, 4, sourceSize); - lovrAssert(start + count <= capacity, "Overflow in Mesh:setVertices: Mesh can only hold %d vertices", capacity); - lovrAssert(count <= sourceSize, "Cannot set %d vertices on Mesh: source only has %d vertices", count, sourceSize); if (!mesh->vertexBuffer || mesh->attributeCount == 0 || mesh->attributes[0].buffer != mesh->vertexBuffer) { - lovrThrow("Mesh does not have a vertex buffer"); + lovrThrow("Mesh:setVertices does not work when the Mesh does not have a vertex buffer"); } + uint32_t capacity = lovrMeshGetVertexCount(mesh); + uint32_t start = luaL_optinteger(L, 3, 1) - 1; + uint32_t count = luaL_optinteger(L, 4, capacity - start); size_t stride = mesh->attributes[0].stride; + + Blob* blob = luax_totype(L, 2, Blob); + if (blob) { + count = MIN(count, blob->size / stride); + lovrAssert(start + count <= capacity, "Overflow in Mesh:setVertices: Mesh can only hold %d vertices", capacity); + void* data = lovrBufferMap(mesh->vertexBuffer, start * stride); + memcpy(data, blob->data, count * stride); + lovrBufferFlush(mesh->vertexBuffer, start * stride, count * stride); + return 0; + } + + luaL_checktype(L, 2, LUA_TTABLE); + count = MIN(count, lua_objlen(L, 2)); + lovrAssert(start + count <= capacity, "Overflow in Mesh:setVertices: Mesh can only hold %d vertices", capacity); + AttributeData data = { .raw = lovrBufferMap(mesh->vertexBuffer, start * stride) }; for (uint32_t i = 0; i < count; i++) {