Merge pull request #1555 from JanMatas/softBody-opengl-rendering

Implemented support for softbodies in OpenGL renderer
This commit is contained in:
erwincoumans
2018-02-18 10:48:46 -08:00
committed by GitHub
3 changed files with 55 additions and 2 deletions

View File

@@ -6,12 +6,11 @@
#include "../CommonInterfaces/CommonRenderInterface.h" #include "../CommonInterfaces/CommonRenderInterface.h"
#include "Bullet3Common/b3Scalar.h" #include "Bullet3Common/b3Scalar.h"
#include "CollisionShape2TriangleMesh.h" #include "CollisionShape2TriangleMesh.h"
#include "BulletSoftBody/btSoftBodyHelpers.h"
#include "../OpenGLWindow/ShapeData.h" #include "../OpenGLWindow/ShapeData.h"
#include "../OpenGLWindow/SimpleCamera.h" #include "../OpenGLWindow/SimpleCamera.h"
#include "../OpenGLWindow/GLInstanceGraphicsShape.h"
#define BT_LINE_BATCH_SIZE 512 #define BT_LINE_BATCH_SIZE 512
@@ -422,6 +421,14 @@ void OpenGLGuiHelper::createCollisionShapeGraphicsObject(btCollisionShape* colli
//if (collisionShape->getShapeType()==BOX_SHAPE_PROXYTYPE) //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) if (collisionShape->getShapeType()==MULTI_SPHERE_SHAPE_PROXYTYPE)
{ {
btMultiSphereShape* ms = (btMultiSphereShape*) collisionShape; btMultiSphereShape* ms = (btMultiSphereShape*) collisionShape;
@@ -916,6 +923,14 @@ void OpenGLGuiHelper::syncPhysicsToGraphics(const btDiscreteDynamicsWorld* rbWor
{ {
//B3_PROFILE("writeSingleInstanceTransformToCPU"); //B3_PROFILE("writeSingleInstanceTransformToCPU");
btCollisionObject* colObj = rbWorld->getCollisionObjectArray()[i]; btCollisionObject* colObj = rbWorld->getCollisionObjectArray()[i];
btCollisionShape* collisionShape = colObj->getCollisionShape();
if (collisionShape->getShapeType()==SOFTBODY_SHAPE_PROXYTYPE && collisionShape->getUserIndex() >=0) {
btAlignedObjectArray<GLInstanceVertex> gfxVertices;
btAlignedObjectArray<int> indices;
computeSoftBodyVertices(collisionShape, gfxVertices, indices);
m_data->m_glApp->m_renderer->updateShape(collisionShape->getUserIndex(), &gfxVertices[0].xyzw[0]);
continue;
}
btVector3 pos = colObj->getWorldTransform().getOrigin(); btVector3 pos = colObj->getWorldTransform().getOrigin();
btQuaternion orn = colObj->getWorldTransform().getRotation(); btQuaternion orn = colObj->getWorldTransform().getRotation();
int index = colObj->getUserIndex(); int index = colObj->getUserIndex();
@@ -1271,3 +1286,33 @@ void OpenGLGuiHelper::dumpFramesToVideo(const char* mp4FileName)
m_data->m_glApp->dumpFramesToVideo(mp4FileName); m_data->m_glApp->dumpFramesToVideo(mp4FileName);
} }
} }
void OpenGLGuiHelper::computeSoftBodyVertices(btCollisionShape* collisionShape,
btAlignedObjectArray<GLInstanceVertex>& gfxVertices,
btAlignedObjectArray<int>& 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);
}
}
}

View File

@@ -5,6 +5,7 @@
class btCollisionShape; class btCollisionShape;
class btTransform; class btTransform;
#include "LinearMath/btAlignedObjectArray.h" #include "LinearMath/btAlignedObjectArray.h"
#include "../OpenGLWindow/GLInstanceGraphicsShape.h"
struct OpenGLGuiHelper : public GUIHelperInterface struct OpenGLGuiHelper : public GUIHelperInterface
{ {
@@ -99,6 +100,10 @@ struct OpenGLGuiHelper : public GUIHelperInterface
virtual void dumpFramesToVideo(const char* mp4FileName); virtual void dumpFramesToVideo(const char* mp4FileName);
int createCheckeredTexture(int r,int g, int b); int createCheckeredTexture(int r,int g, int b);
void computeSoftBodyVertices(btCollisionShape* collisionShape,
btAlignedObjectArray<GLInstanceVertex>& gfxVertices,
btAlignedObjectArray<int>& indices);
}; };
#endif //OPENGL_GUI_HELPER_H #endif //OPENGL_GUI_HELPER_H

View File

@@ -5884,7 +5884,10 @@ bool PhysicsServerCommandProcessor::processLoadSoftBodyCommand(const struct Shar
psb->scale(btVector3(scale,scale,scale)); psb->scale(btVector3(scale,scale,scale));
psb->setTotalMass(mass,true); psb->setTotalMass(mass,true);
psb->getCollisionShape()->setMargin(collisionMargin); psb->getCollisionShape()->setMargin(collisionMargin);
psb->getCollisionShape()->setUserPointer(psb);
m_data->m_dynamicsWorld->addSoftBody(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(); int bodyUniqueId = m_data->m_bodyHandles.allocHandle();
InternalBodyHandle* bodyHandle = m_data->m_bodyHandles.getHandle(bodyUniqueId); InternalBodyHandle* bodyHandle = m_data->m_bodyHandles.getHandle(bodyUniqueId);
bodyHandle->m_softBody = psb; bodyHandle->m_softBody = psb;