Skip to content

Commit

Permalink
Fix newConvexShape with Mesh;
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornbytes committed Dec 27, 2024
1 parent 380614a commit 4d0c95c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/modules/graphics/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/lovr/physics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 4d0c95c

Please sign in to comment.