From 1e95e489cba359b80dd4566bf579a419b323ba13 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Thu, 14 Jul 2016 11:43:33 -0700 Subject: [PATCH] make mesh around btConvexShape, copy to model::Mesh --- .../physics/src/CollisionRenderMeshCache.cpp | 59 +++++++++++++++++-- .../physics/src/CollisionRenderMeshCache.h | 2 +- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/libraries/physics/src/CollisionRenderMeshCache.cpp b/libraries/physics/src/CollisionRenderMeshCache.cpp index e563c05e90..c7ce892e52 100644 --- a/libraries/physics/src/CollisionRenderMeshCache.cpp +++ b/libraries/physics/src/CollisionRenderMeshCache.cpp @@ -12,28 +12,77 @@ #include "CollisionRenderMeshCache.h" #include -//#include -//#include "ShapeFactory.h" #include +#include +#include // for MAX_HULL_POINTS + +float verts[3 * MAX_HULL_POINTS]; + +void copyHullToMesh(const btShapeHull& hull, model::MeshPointer mesh) { + if ((bool)mesh) { + const uint32_t* hullIndices = hull.getIndexPointer(); + int32_t numIndices = hull.numIndices(); + assert(numIndices <= 6 * MAX_HULL_POINTS); + + { // new part + model::Mesh::Part part; + part._startIndex = mesh->getIndexBuffer().getNumElements(); + part._numIndices = (model::Index)numIndices; + part._baseVertex = mesh->getVertexBuffer().getNumElements(); + + gpu::BufferView::Size numBytes = sizeof(model::Mesh::Part); + const gpu::Byte* data = reinterpret_cast(&part); + mesh->getPartBuffer()._buffer->append(numBytes, data); + } + + { // new vertices + const btVector3* hullVertices = hull.getVertexPointer(); + int32_t numVertices = hull.numVertices(); + assert(numVertices <= MAX_HULL_POINTS); + for (int32_t i = 0; i < numVertices; ++i) { + float* data = verts + 3 * i; + data[0] = hullVertices[i].getX(); + data[1] = hullVertices[i].getY(); + data[2] = hullVertices[i].getZ(); + } + + gpu::BufferView::Size numBytes = sizeof(float) * (3 * numVertices); + const gpu::Byte* data = reinterpret_cast(verts); + mesh->getVertexBuffer()._buffer->append(numBytes, data); + } + + { // new indices + gpu::BufferView::Size numBytes = (gpu::BufferView::Size)(sizeof(uint32_t) * hull.numIndices()); + const gpu::Byte* data = reinterpret_cast(hullIndices); + mesh->getIndexBuffer()._buffer->append(numBytes, data); + } + } +} model::MeshPointer createMeshFromShape(const btCollisionShape* shape) { if (!shape) { return std::make_shared(); } + model::MeshPointer mesh = std::make_shared(); int32_t shapeType = shape->getShapeType(); if (shapeType == (int32_t)COMPOUND_SHAPE_PROXYTYPE) { + const btScalar MARGIN = 0.0f; const btCompoundShape* compoundShape = static_cast(shape); int32_t numSubShapes = compoundShape->getNumChildShapes(); for (int i = 0; i < numSubShapes; ++i) { const btCollisionShape* childShape = compoundShape->getChildShape(i); - std::cout << "adebug " << i << " " << (void*)(childShape) << std::endl; // adebug + if (childShape->isConvex()) { + const btConvexShape* convexShape = static_cast(childShape); + btShapeHull shapeHull(convexShape); + shapeHull.buildHull(MARGIN); + copyHullToMesh(shapeHull, mesh); + } } } else if (shape->isConvex()) { - std::cout << "adebug " << (void*)(shape)<< std::endl; // adebug } - return std::make_shared(); + return mesh; } CollisionRenderMeshCache::CollisionRenderMeshCache() { diff --git a/libraries/physics/src/CollisionRenderMeshCache.h b/libraries/physics/src/CollisionRenderMeshCache.h index 03083048d8..05da3750c3 100644 --- a/libraries/physics/src/CollisionRenderMeshCache.h +++ b/libraries/physics/src/CollisionRenderMeshCache.h @@ -31,7 +31,7 @@ namespace std { class CollisionRenderMeshCache { public: - using Key = btCollisionShape const *; + using Key = const btCollisionShape*; CollisionRenderMeshCache(); ~CollisionRenderMeshCache();