From 4d0c95c7890570540ddc01c01cbd4d696a0e4e06 Mon Sep 17 00:00:00 2001 From: bjorn Date: Thu, 26 Dec 2024 18:23:12 -0800 Subject: [PATCH] Fix newConvexShape with Mesh; --- src/modules/graphics/graphics.c | 28 +++++++++++++++------------- test/lovr/physics.lua | 10 ++++++++++ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/modules/graphics/graphics.c b/src/modules/graphics/graphics.c index aec95e4e4..1bc2758f2 100644 --- a/src/modules/graphics/graphics.c +++ b/src/modules/graphics/graphics.c @@ -4701,21 +4701,23 @@ bool lovrMeshGetTriangles(Mesh* mesh, float** vertices, uint32_t** indices, uint position = (float*) ((char*) position + format->stride); } - if (mesh->indexCount > 0) { - *indexCount = mesh->indexCount; - *indices = lovrMalloc(*indexCount * sizeof(uint32_t)); - if (mesh->indexBuffer->info.format->type == TYPE_U16 || mesh->indexBuffer->info.format->type == TYPE_INDEX16) { - for (uint32_t i = 0; i < mesh->indexCount; i++) { - (*indices)[i] = (uint32_t) ((uint16_t*) mesh->indices)[i]; + if (indices) { + if (mesh->indexCount > 0) { + *indexCount = mesh->indexCount; + *indices = lovrMalloc(*indexCount * sizeof(uint32_t)); + if (mesh->indexBuffer->info.format->type == TYPE_U16 || mesh->indexBuffer->info.format->type == TYPE_INDEX16) { + for (uint32_t i = 0; i < mesh->indexCount; i++) { + (*indices)[i] = (uint32_t) ((uint16_t*) mesh->indices)[i]; + } + } else { + memcpy(*indices, mesh->indices, mesh->indexCount * sizeof(uint32_t)); } } else { - memcpy(*indices, mesh->indices, mesh->indexCount * sizeof(uint32_t)); - } - } else { - *indexCount = format->length; - *indices = lovrMalloc(*indexCount * sizeof(uint32_t)); - for (uint32_t i = 0; i < format->length; i++) { - (*indices)[i] = i; + *indexCount = format->length; + *indices = lovrMalloc(*indexCount * sizeof(uint32_t)); + for (uint32_t i = 0; i < format->length; i++) { + (*indices)[i] = i; + } } } diff --git a/test/lovr/physics.lua b/test/lovr/physics.lua index aba4e018d..c52f17bff 100644 --- a/test/lovr/physics.lua +++ b/test/lovr/physics.lua @@ -87,6 +87,16 @@ group('physics', function() test('ConvexShape', function() shape = lovr.physics.newConvexShape({ 1, 1, 0, -1, 1, 0, 0, 0, 0, 0, 0, 1 }) expect(shape:getPointCount()).to.equal(4) + + mesh = lovr.graphics.newMesh({ + { 1, 1, 0 }, + { -1, 1, 0 }, + { 0, 0, 0 }, + { 0, 0, 1 } + }) + mesh:setIndices({ 1, 2, 3, 1, 3, 4, 1, 2, 4, 2, 3, 4 }) + shape = lovr.physics.newConvexShape(mesh) + expect(shape:getPointCount()).to.equal(4) end) group('MeshShape', function()