From 82214eb99fd7a31a71a74266f9c5938fe0dfe908 Mon Sep 17 00:00:00 2001 From: Jan Matas Date: Thu, 8 Feb 2018 13:31:40 +0000 Subject: [PATCH] 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;