From 82214eb99fd7a31a71a74266f9c5938fe0dfe908 Mon Sep 17 00:00:00 2001 From: Jan Matas Date: Thu, 8 Feb 2018 13:31:40 +0000 Subject: [PATCH 1/3] Implemented support for softbodies in OpenGL renderer --- examples/ExampleBrowser/OpenGLGuiHelper.cpp | 49 ++++++++++++++++++- examples/ExampleBrowser/OpenGLGuiHelper.h | 5 ++ .../PhysicsServerCommandProcessor.cpp | 3 ++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/examples/ExampleBrowser/OpenGLGuiHelper.cpp b/examples/ExampleBrowser/OpenGLGuiHelper.cpp index 97d618aa7..1c7daff79 100644 --- a/examples/ExampleBrowser/OpenGLGuiHelper.cpp +++ b/examples/ExampleBrowser/OpenGLGuiHelper.cpp @@ -6,12 +6,11 @@ #include "../CommonInterfaces/CommonRenderInterface.h" #include "Bullet3Common/b3Scalar.h" #include "CollisionShape2TriangleMesh.h" +#include "BulletSoftBody/btSoftBodyHelpers.h" #include "../OpenGLWindow/ShapeData.h" #include "../OpenGLWindow/SimpleCamera.h" -#include "../OpenGLWindow/GLInstanceGraphicsShape.h" - #define BT_LINE_BATCH_SIZE 512 @@ -422,6 +421,14 @@ void OpenGLGuiHelper::createCollisionShapeGraphicsObject(btCollisionShape* colli //if (collisionShape->getShapeType()==BOX_SHAPE_PROXYTYPE) { } + if (collisionShape->getShapeType() == SOFTBODY_SHAPE_PROXYTYPE) + { + computeSoftBodyVertices(collisionShape, gfxVertices, indices); + int shapeId = registerGraphicsShape(&gfxVertices[0].xyzw[0], gfxVertices.size(), &indices[0], indices.size(), B3_GL_TRIANGLES, + m_data->m_checkedTexture); + b3Assert(shapeId >= 0); + collisionShape->setUserIndex(shapeId); + } if (collisionShape->getShapeType()==MULTI_SPHERE_SHAPE_PROXYTYPE) { btMultiSphereShape* ms = (btMultiSphereShape*) collisionShape; @@ -916,6 +923,14 @@ void OpenGLGuiHelper::syncPhysicsToGraphics(const btDiscreteDynamicsWorld* rbWor { //B3_PROFILE("writeSingleInstanceTransformToCPU"); btCollisionObject* colObj = rbWorld->getCollisionObjectArray()[i]; + btCollisionShape* collisionShape = colObj->getCollisionShape(); + if (collisionShape->getShapeType()==SOFTBODY_SHAPE_PROXYTYPE && collisionShape->getUserIndex() >=0) { + btAlignedObjectArray gfxVertices; + btAlignedObjectArray indices; + computeSoftBodyVertices(collisionShape, gfxVertices, indices); + m_data->m_glApp->m_renderer->updateShape(collisionShape->getUserIndex(), &gfxVertices[0].xyzw[0]); + continue; + } btVector3 pos = colObj->getWorldTransform().getOrigin(); btQuaternion orn = colObj->getWorldTransform().getRotation(); int index = colObj->getUserIndex(); @@ -1271,3 +1286,33 @@ void OpenGLGuiHelper::dumpFramesToVideo(const char* mp4FileName) m_data->m_glApp->dumpFramesToVideo(mp4FileName); } } + +void OpenGLGuiHelper::computeSoftBodyVertices(btCollisionShape* collisionShape, + btAlignedObjectArray& gfxVertices, + btAlignedObjectArray& indices) +{ + b3Assert(collisionShape->getUserPointer()); + btSoftBody* psb = (btSoftBody*)collisionShape->getUserPointer(); + gfxVertices.resize(psb->m_faces.size() * 3); + int i, j, k; + for (i = 0; i < psb->m_faces.size(); i++) // Foreach face + { + for (k = 0; k < 3; k++) // Foreach vertex on a face + { + int currentIndex = i * 3 + k; + for (int j = 0; j < 3; j++) + { + gfxVertices[currentIndex].xyzw[j] = psb->m_faces[i].m_n[k]->m_x[j]; + } + for (int j = 0; j < 3; j++) + { + gfxVertices[currentIndex].normal[j] = psb->m_faces[i].m_n[k]->m_n[j]; + } + for (int j = 0; j < 2; j++) + { + gfxVertices[currentIndex].uv[j] = 0.5; //we don't have UV info... + } + indices.push_back(currentIndex); + } + } +} diff --git a/examples/ExampleBrowser/OpenGLGuiHelper.h b/examples/ExampleBrowser/OpenGLGuiHelper.h index 52a522e0e..a2705fb38 100644 --- a/examples/ExampleBrowser/OpenGLGuiHelper.h +++ b/examples/ExampleBrowser/OpenGLGuiHelper.h @@ -5,6 +5,7 @@ class btCollisionShape; class btTransform; #include "LinearMath/btAlignedObjectArray.h" +#include "../OpenGLWindow/GLInstanceGraphicsShape.h" struct OpenGLGuiHelper : public GUIHelperInterface { @@ -99,6 +100,10 @@ struct OpenGLGuiHelper : public GUIHelperInterface virtual void dumpFramesToVideo(const char* mp4FileName); int createCheckeredTexture(int r,int g, int b); + + void computeSoftBodyVertices(btCollisionShape* collisionShape, + btAlignedObjectArray& gfxVertices, + btAlignedObjectArray& indices); }; #endif //OPENGL_GUI_HELPER_H diff --git a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp index a0471ff1d..7995ef1dd 100644 --- a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp +++ b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp @@ -5862,7 +5862,10 @@ bool PhysicsServerCommandProcessor::processLoadSoftBodyCommand(const struct Shar psb->scale(btVector3(scale,scale,scale)); psb->setTotalMass(mass,true); psb->getCollisionShape()->setMargin(collisionMargin); + psb->getCollisionShape()->setUserPointer(psb); m_data->m_dynamicsWorld->addSoftBody(psb); + m_data->m_guiHelper->createCollisionShapeGraphicsObject(psb->getCollisionShape()); + m_data->m_guiHelper->autogenerateGraphicsObjects(this->m_data->m_dynamicsWorld); int bodyUniqueId = m_data->m_bodyHandles.allocHandle(); InternalBodyHandle* bodyHandle = m_data->m_bodyHandles.getHandle(bodyUniqueId); bodyHandle->m_softBody = psb; From d2d987a5ed2bb8b3cd6d914b8db790958eeafe88 Mon Sep 17 00:00:00 2001 From: Jan Matas Date: Thu, 8 Feb 2018 13:49:36 +0000 Subject: [PATCH 2/3] Create a new pointer in btCollisionShape instead of just using userPtr. --- examples/ExampleBrowser/OpenGLGuiHelper.cpp | 4 ++-- .../SharedMemory/PhysicsServerCommandProcessor.cpp | 2 +- .../CollisionShapes/btCollisionShape.h | 13 +++++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/examples/ExampleBrowser/OpenGLGuiHelper.cpp b/examples/ExampleBrowser/OpenGLGuiHelper.cpp index 1c7daff79..388708259 100644 --- a/examples/ExampleBrowser/OpenGLGuiHelper.cpp +++ b/examples/ExampleBrowser/OpenGLGuiHelper.cpp @@ -1291,8 +1291,8 @@ void OpenGLGuiHelper::computeSoftBodyVertices(btCollisionShape* collisionShape, btAlignedObjectArray& gfxVertices, btAlignedObjectArray& indices) { - b3Assert(collisionShape->getUserPointer()); - btSoftBody* psb = (btSoftBody*)collisionShape->getUserPointer(); + b3Assert(collisionShape->getBodyPointer()); + btSoftBody* psb = (btSoftBody*)collisionShape->getBodyPointer(); gfxVertices.resize(psb->m_faces.size() * 3); int i, j, k; for (i = 0; i < psb->m_faces.size(); i++) // Foreach face diff --git a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp index 7995ef1dd..086fdfd05 100644 --- a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp +++ b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp @@ -5862,7 +5862,7 @@ bool PhysicsServerCommandProcessor::processLoadSoftBodyCommand(const struct Shar psb->scale(btVector3(scale,scale,scale)); psb->setTotalMass(mass,true); psb->getCollisionShape()->setMargin(collisionMargin); - psb->getCollisionShape()->setUserPointer(psb); + psb->getCollisionShape()->setBodyPointer(psb); m_data->m_dynamicsWorld->addSoftBody(psb); m_data->m_guiHelper->createCollisionShapeGraphicsObject(psb->getCollisionShape()); m_data->m_guiHelper->autogenerateGraphicsObjects(this->m_data->m_dynamicsWorld); diff --git a/src/BulletCollision/CollisionShapes/btCollisionShape.h b/src/BulletCollision/CollisionShapes/btCollisionShape.h index 6c4916fbd..299dff038 100644 --- a/src/BulletCollision/CollisionShapes/btCollisionShape.h +++ b/src/BulletCollision/CollisionShapes/btCollisionShape.h @@ -30,6 +30,7 @@ protected: int m_shapeType; void* m_userPointer; int m_userIndex; + void* m_bodyPointer; public: @@ -131,6 +132,18 @@ public: { return m_userPointer; } + + /// optional pointer to body the shape represents + void setBodyPointer(void* bodyPointer) + { + m_bodyPointer = bodyPointer; + } + + void* getBodyPointer() const + { + return m_bodyPointer; + } + void setUserIndex(int index) { m_userIndex = index; From 817fb40fb53f4225c3fb2879fc01f6faff1f25a4 Mon Sep 17 00:00:00 2001 From: Jan Matas Date: Mon, 12 Feb 2018 11:26:39 +0000 Subject: [PATCH 3/3] Revert "Create a new pointer in btCollisionShape instead of just using userPtr." This reverts commit d2d987a5ed2bb8b3cd6d914b8db790958eeafe88. After discussion, we decided to use userPtr. --- examples/ExampleBrowser/OpenGLGuiHelper.cpp | 4 ++-- .../SharedMemory/PhysicsServerCommandProcessor.cpp | 2 +- .../CollisionShapes/btCollisionShape.h | 13 ------------- 3 files changed, 3 insertions(+), 16 deletions(-) diff --git a/examples/ExampleBrowser/OpenGLGuiHelper.cpp b/examples/ExampleBrowser/OpenGLGuiHelper.cpp index 388708259..1c7daff79 100644 --- a/examples/ExampleBrowser/OpenGLGuiHelper.cpp +++ b/examples/ExampleBrowser/OpenGLGuiHelper.cpp @@ -1291,8 +1291,8 @@ void OpenGLGuiHelper::computeSoftBodyVertices(btCollisionShape* collisionShape, btAlignedObjectArray& gfxVertices, btAlignedObjectArray& indices) { - b3Assert(collisionShape->getBodyPointer()); - btSoftBody* psb = (btSoftBody*)collisionShape->getBodyPointer(); + b3Assert(collisionShape->getUserPointer()); + btSoftBody* psb = (btSoftBody*)collisionShape->getUserPointer(); gfxVertices.resize(psb->m_faces.size() * 3); int i, j, k; for (i = 0; i < psb->m_faces.size(); i++) // Foreach face diff --git a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp index 086fdfd05..7995ef1dd 100644 --- a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp +++ b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp @@ -5862,7 +5862,7 @@ bool PhysicsServerCommandProcessor::processLoadSoftBodyCommand(const struct Shar psb->scale(btVector3(scale,scale,scale)); psb->setTotalMass(mass,true); psb->getCollisionShape()->setMargin(collisionMargin); - psb->getCollisionShape()->setBodyPointer(psb); + psb->getCollisionShape()->setUserPointer(psb); m_data->m_dynamicsWorld->addSoftBody(psb); m_data->m_guiHelper->createCollisionShapeGraphicsObject(psb->getCollisionShape()); m_data->m_guiHelper->autogenerateGraphicsObjects(this->m_data->m_dynamicsWorld); diff --git a/src/BulletCollision/CollisionShapes/btCollisionShape.h b/src/BulletCollision/CollisionShapes/btCollisionShape.h index 299dff038..6c4916fbd 100644 --- a/src/BulletCollision/CollisionShapes/btCollisionShape.h +++ b/src/BulletCollision/CollisionShapes/btCollisionShape.h @@ -30,7 +30,6 @@ protected: int m_shapeType; void* m_userPointer; int m_userIndex; - void* m_bodyPointer; public: @@ -132,18 +131,6 @@ public: { return m_userPointer; } - - /// optional pointer to body the shape represents - void setBodyPointer(void* bodyPointer) - { - m_bodyPointer = bodyPointer; - } - - void* getBodyPointer() const - { - return m_bodyPointer; - } - void setUserIndex(int index) { m_userIndex = index;