Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getVertices to ConvexShape #826

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/api/l_physics_shapes.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "api.h"
#include "physics/physics.h"
#include "data/image.h"
#include "data/blob.h"
#include "core/maf.h"
#include "util.h"
#include <stdlib.h>
Expand Down Expand Up @@ -562,6 +563,21 @@ static int l_lovrConvexShapeGetFace(lua_State* L) {
return 1;
}

static int l_lovrConvexShapeGetVertices(lua_State* L) {
ConvexShape* convex = luax_checktype(L, 1, ConvexShape);
uint32_t icount, maxFaceVcount;
lovrConvexShapeGetIndicesCount(convex, &icount, &maxFaceVcount);
// pos + normal + UV
uint32_t size = icount * 8 * sizeof(float);
uint16_t* vertices = lovrMalloc(size);
luax_assert(L, lovrConvexShapeGetVertices(convex, vertices, maxFaceVcount));

Blob* blob = lovrBlobCreate(vertices, size, "ConvexIndices");
luax_pushtype(L, Blob, blob);
lovrRelease(blob, lovrBlobDestroy);
return 1;
}

static int l_lovrConvexShapeGetScale(lua_State* L) {
ConvexShape* convex = luax_checktype(L, 1, ConvexShape);
float scale = lovrConvexShapeGetScale(convex);
Expand All @@ -575,6 +591,7 @@ const luaL_Reg lovrConvexShape[] = {
{ "getPoint", l_lovrConvexShapeGetPoint },
{ "getFaceCount", l_lovrConvexShapeGetFaceCount },
{ "getFace", l_lovrConvexShapeGetFace },
{ "getVertices", l_lovrConvexShapeGetVertices },
{ "getScale", l_lovrConvexShapeGetScale },
{ NULL, NULL }
};
Expand Down
59 changes: 59 additions & 0 deletions src/modules/physics/physics.c
Original file line number Diff line number Diff line change
Expand Up @@ -2320,6 +2320,65 @@ uint32_t lovrConvexShapeGetFace(ConvexShape* shape, uint32_t index, uint32_t* po
return JPH_ConvexHullShape_GetFaceVertices(hull, index, capacity, pointIndices);
}

bool lovrConvexShapeGetIndicesCount(ConvexShape* shape, uint32_t* icount, uint32_t* maxFaceVcount) {
const JPH_ConvexHullShape* hull = (const JPH_ConvexHullShape*) JPH_DecoratedShape_GetInnerShape((const JPH_DecoratedShape*) shape->handle);
*icount = 0;
*maxFaceVcount = 0;
for (uint32_t i = 0; i < JPH_ConvexHullShape_GetNumFaces(hull); i++) {
uint32_t n = JPH_ConvexHullShape_GetNumVerticesInFace(hull, i);
*maxFaceVcount = MAX(*maxFaceVcount, n);
*icount = *icount + (n - 2) * 3;
}
return true;
}

static void setVertice(float* vertice, float pos[3], float normal[3]) {
vertice[0] = pos[0];
vertice[1] = pos[1];
vertice[2] = pos[2];
vertice[3] = normal[0];
vertice[4] = normal[1];
vertice[5] = normal[2];
vertice[6] = 0.f;
vertice[7] = 0.f;
}

static void calcTriangleNormal(float p1[3], float p2[3], float p3[3], float outNormal[3]) {
float tmp[3];
vec3_init(outNormal, p2);
vec3_sub(outNormal, p1);
vec3_init(tmp, p3);
vec3_sub(tmp, p1);
vec3_cross(outNormal, tmp);
vec3_normalize(outNormal);
}

bool lovrConvexShapeGetVertices(ConvexShape* shape, float* vertices, uint32_t maxFaceVcount) {
const JPH_ConvexHullShape* hull = (const JPH_ConvexHullShape*) JPH_DecoratedShape_GetInnerShape((const JPH_DecoratedShape*) shape->handle);
uint32_t* fvs = lovrMalloc(maxFaceVcount * sizeof(uint32_t));
uint32_t idx = 0;
JPH_Vec3 v;
for (uint32_t i = 0; i < JPH_ConvexHullShape_GetNumFaces(hull); i++) {
int n = JPH_ConvexHullShape_GetFaceVertices(hull, i, maxFaceVcount, fvs);
float p0[3], p1[3], p2[3], normal[3];
JPH_ConvexHullShape_GetPoint(hull, fvs[0], &v);
vec3_fromJolt(p0, &v);
for (int i = 2; i < n; i++) {
JPH_ConvexHullShape_GetPoint(hull, fvs[i - 1], &v);
vec3_fromJolt(p1, &v);
JPH_ConvexHullShape_GetPoint(hull, fvs[i], &v);
vec3_fromJolt(p2, &v);
calcTriangleNormal(p0, p1, p2, normal);
setVertice(vertices + idx, p0, normal);
setVertice(vertices + idx + 8, p1, normal);
setVertice(vertices + idx + 16, p2, normal);
idx += 24;
}
}
lovrFree(fvs);
return true;
}

float lovrConvexShapeGetScale(ConvexShape* shape) {
JPH_Vec3 v;
JPH_ScaledShape_GetScale((JPH_ScaledShape*) shape->handle, &v);
Expand Down
2 changes: 2 additions & 0 deletions src/modules/physics/physics.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ uint32_t lovrConvexShapeGetPointCount(ConvexShape* shape);
bool lovrConvexShapeGetPoint(ConvexShape* shape, uint32_t index, float point[3]);
uint32_t lovrConvexShapeGetFaceCount(ConvexShape* shape);
uint32_t lovrConvexShapeGetFace(ConvexShape* shape, uint32_t index, uint32_t* pointIndices, uint32_t capacity);
bool lovrConvexShapeGetIndicesCount(ConvexShape* shape, uint32_t* icount, uint32_t* maxFaceVcount);
bool lovrConvexShapeGetVertices(ConvexShape* shape, float* vertices, uint32_t maxFaceVcount);
float lovrConvexShapeGetScale(ConvexShape* shape);

MeshShape* lovrMeshShapeCreate(uint32_t vertexCount, float vertices[], uint32_t indexCount, uint32_t indices[], float scale);
Expand Down
1 change: 1 addition & 0 deletions test/lovr/physics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ group('physics', function()
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)
expect(shape:getVertices():getSize()).to.equal(12 * 8 * 4)
end
end)

Expand Down
Loading