reorder files, in preparation for Bullet 3 -> Bullet 2 merge
This commit is contained in:
44
Demos3/GpuDemos/rigidbody/Bullet2FileDemo.cpp
Normal file
44
Demos3/GpuDemos/rigidbody/Bullet2FileDemo.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
|
||||
#include "Bullet2FileDemo.h"
|
||||
#include "BulletDataExtractor.h"
|
||||
#include "GpuRigidBodyDemoInternalData.h"
|
||||
#include "OpenGLWindow/GLInstancingRenderer.h"
|
||||
|
||||
Bullet2FileDemo::Bullet2FileDemo()
|
||||
{
|
||||
m_loader = 0;
|
||||
}
|
||||
Bullet2FileDemo::~Bullet2FileDemo()
|
||||
{
|
||||
delete m_loader;
|
||||
}
|
||||
|
||||
void Bullet2FileDemo::setupScene(const ConstructionInfo& ci)
|
||||
{
|
||||
b3Assert(ci.m_instancingRenderer);
|
||||
|
||||
const char* fileName="data/testFile.bullet";
|
||||
|
||||
FILE* f = 0;
|
||||
|
||||
const char* prefix[]={"./","../","../../","../../../","../../../../"};
|
||||
int numPrefixes = sizeof(prefix)/sizeof(const char*);
|
||||
char relativeFileName[1024];
|
||||
|
||||
for (int i=0;!f && i<numPrefixes;i++)
|
||||
{
|
||||
sprintf(relativeFileName,"%s%s",prefix[i],fileName);
|
||||
f = fopen(relativeFileName,"rb");
|
||||
}
|
||||
if (f)
|
||||
{
|
||||
fclose(f);
|
||||
createScene(*ci.m_instancingRenderer,*m_data->m_np,*m_data->m_rigidBodyPipeline,relativeFileName);
|
||||
}
|
||||
// m_loader = new b3BulletDataExtractor(*ci.m_instancingRenderer,*m_data->m_np,*m_data->m_rigidBodyPipeline);
|
||||
// m_loader->convertAllObjects(bulletFile);
|
||||
|
||||
b3Vector3 pos(-20,10,0);
|
||||
ci.m_instancingRenderer->setCameraTargetPosition(pos);
|
||||
ci.m_instancingRenderer->setCameraDistance(10);
|
||||
}
|
||||
32
Demos3/GpuDemos/rigidbody/Bullet2FileDemo.h
Normal file
32
Demos3/GpuDemos/rigidbody/Bullet2FileDemo.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef BULLET2_FILE_DEMO_H
|
||||
#define BULLET2_FILE_DEMO_H
|
||||
|
||||
|
||||
|
||||
#include "GpuRigidBodyDemo.h"
|
||||
|
||||
class Bullet2FileDemo : public GpuRigidBodyDemo
|
||||
{
|
||||
class b3BulletDataExtractor* m_loader;
|
||||
|
||||
public:
|
||||
|
||||
Bullet2FileDemo();
|
||||
virtual ~Bullet2FileDemo();
|
||||
virtual const char* getName()
|
||||
{
|
||||
return "Bullet2File";
|
||||
}
|
||||
|
||||
static GpuDemo* MyCreateFunc()
|
||||
{
|
||||
GpuDemo* demo = new Bullet2FileDemo;
|
||||
return demo;
|
||||
}
|
||||
|
||||
virtual void setupScene(const ConstructionInfo& ci);
|
||||
|
||||
};
|
||||
|
||||
#endif//BULLET2_FILE_DEMO_H
|
||||
|
||||
768
Demos3/GpuDemos/rigidbody/BulletDataExtractor.cpp
Normal file
768
Demos3/GpuDemos/rigidbody/BulletDataExtractor.cpp
Normal file
@@ -0,0 +1,768 @@
|
||||
int NUM_OBJECTS_X = 20;
|
||||
int NUM_OBJECTS_Y = 20;
|
||||
int NUM_OBJECTS_Z = 20;
|
||||
|
||||
|
||||
|
||||
float X_GAP = 2.3f;
|
||||
float Y_GAP = 2.f;
|
||||
float Z_GAP = 2.3f;
|
||||
|
||||
#include "BulletDataExtractor.h"
|
||||
#include "Bullet3Serialize/Bullet2FileLoader/b3BulletFile.h"
|
||||
bool keepStaticObjects = true;
|
||||
extern bool enableExperimentalCpuConcaveCollision;
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "OpenGLWindow/OpenGLInclude.h"
|
||||
|
||||
|
||||
#include "OpenGLWindow/GLInstancingRenderer.h"
|
||||
//#include "LinearMath/b3Quickprof.h"
|
||||
#include "Bullet3Common/b3Quaternion.h"
|
||||
#include "Bullet3Common/b3Matrix3x3.h"
|
||||
#include "Bullet3OpenCL/NarrowphaseCollision/b3ConvexUtility.h"
|
||||
#include "OpenGLWindow/ShapeData.h"
|
||||
#include "../../Wavefront/objLoader.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3GpuRigidBodyPipeline.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3GpuNarrowPhase.h"
|
||||
|
||||
///work-in-progress
|
||||
///This ReadBulletSample is kept as simple as possible without dependencies to the Bullet SDK.
|
||||
///It can be used to load .bullet data for other physics SDKs
|
||||
///For a more complete example how to load and convert Bullet data using the Bullet SDK check out
|
||||
///the Bullet/Demos/SerializeDemo and Bullet/Serialize/BulletWorldImporter
|
||||
|
||||
|
||||
//using namespace Bullet;
|
||||
|
||||
struct GraphicsVertex
|
||||
{
|
||||
float xyzw[4];
|
||||
float normal[3];
|
||||
float uv[2];
|
||||
};
|
||||
struct GraphicsShape
|
||||
{
|
||||
const float* m_vertices;
|
||||
int m_numvertices;
|
||||
const int* m_indices;
|
||||
int m_numIndices;
|
||||
float m_scaling[4];
|
||||
};
|
||||
|
||||
struct InstanceGroup
|
||||
{
|
||||
Bullet3SerializeBullet2::b3CollisionShapeData* m_shape;
|
||||
int m_collisionShapeIndex;
|
||||
|
||||
b3AlignedObjectArray<bParse::bStructHandle*> m_rigidBodies;
|
||||
};
|
||||
|
||||
|
||||
void createScene( GLInstancingRenderer& renderer,b3GpuNarrowPhase& np, b3GpuRigidBodyPipeline& rbWorld, const char* fileName)
|
||||
{
|
||||
//const char* fileName="../../bin/convex-trimesh.bullet";
|
||||
//const char* fileName="../../bin/1000 convex.bullet";
|
||||
//const char* fileName="../../bin/1000 stack.bullet";
|
||||
//const char* fileName="../../bin/3000 fall.bullet";
|
||||
|
||||
|
||||
//const char* fileName="../../bin/testFile.bullet";
|
||||
|
||||
|
||||
|
||||
FILE* f = fopen(fileName,"rb");
|
||||
if (f)
|
||||
{
|
||||
fclose(f);
|
||||
|
||||
bool verboseDumpAllTypes = false;
|
||||
|
||||
bParse::b3BulletFile* bulletFile2 = new bParse::b3BulletFile(fileName);
|
||||
|
||||
bool ok = (bulletFile2->getFlags()& bParse::FD_OK)!=0;
|
||||
|
||||
if (ok)
|
||||
bulletFile2->parse(verboseDumpAllTypes);
|
||||
else
|
||||
{
|
||||
printf("Error loading file %s.\n",fileName);
|
||||
exit(0);
|
||||
}
|
||||
ok = (bulletFile2->getFlags()& bParse::FD_OK)!=0;
|
||||
|
||||
if (!(bulletFile2->getFlags() & bParse::FD_DOUBLE_PRECISION))
|
||||
{
|
||||
if (!ok)
|
||||
{
|
||||
printf("Error parsing file %s.\n",fileName);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (verboseDumpAllTypes)
|
||||
{
|
||||
bulletFile2->dumpChunks(bulletFile2->getFileDNA());
|
||||
}
|
||||
|
||||
|
||||
b3BulletDataExtractor extractor(renderer,np,rbWorld);
|
||||
|
||||
extractor.convertAllObjects(bulletFile2);
|
||||
delete bulletFile2;
|
||||
return;
|
||||
|
||||
} else
|
||||
{
|
||||
printf("Error: double precision .bullet files not supported in this demo\n");
|
||||
}
|
||||
|
||||
delete bulletFile2;
|
||||
} else
|
||||
{
|
||||
printf("Warning: cannot find file %s, using programmatically created scene instead.\n",fileName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
enum LocalBroadphaseNativeTypes
|
||||
{
|
||||
// polyhedral convex shapes
|
||||
BOX_SHAPE_PROXYTYPE,
|
||||
TRIANGLE_SHAPE_PROXYTYPE,
|
||||
TETRAHEDRAL_SHAPE_PROXYTYPE,
|
||||
CONVEX_TRIANGLEMESH_SHAPE_PROXYTYPE,
|
||||
CONVEX_HULL_SHAPE_PROXYTYPE,
|
||||
CONVEX_POINT_CLOUD_SHAPE_PROXYTYPE,
|
||||
CUSTOM_POLYHEDRAL_SHAPE_TYPE,
|
||||
//implicit convex shapes
|
||||
IMPLICIT_CONVEX_SHAPES_START_HERE,
|
||||
SPHERE_SHAPE_PROXYTYPE,
|
||||
MULTI_SPHERE_SHAPE_PROXYTYPE,
|
||||
CAPSULE_SHAPE_PROXYTYPE,
|
||||
CONE_SHAPE_PROXYTYPE,
|
||||
CONVEX_SHAPE_PROXYTYPE,
|
||||
CYLINDER_SHAPE_PROXYTYPE,
|
||||
UNIFORM_SCALING_SHAPE_PROXYTYPE,
|
||||
MINKOWSKI_SUM_SHAPE_PROXYTYPE,
|
||||
MINKOWSKI_DIFFERENCE_SHAPE_PROXYTYPE,
|
||||
BOX_2D_SHAPE_PROXYTYPE,
|
||||
CONVEX_2D_SHAPE_PROXYTYPE,
|
||||
CUSTOM_CONVEX_SHAPE_TYPE,
|
||||
//concave shapes
|
||||
CONCAVE_SHAPES_START_HERE,
|
||||
//keep all the convex shapetype below here, for the check IsConvexShape in broadphase proxy!
|
||||
TRIANGLE_MESH_SHAPE_PROXYTYPE,
|
||||
SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE,
|
||||
///used for demo integration FAST/Swift collision library and Bullet
|
||||
FAST_CONCAVE_MESH_PROXYTYPE,
|
||||
//terrain
|
||||
TERRAIN_SHAPE_PROXYTYPE,
|
||||
///Used for GIMPACT Trimesh integration
|
||||
GIMPACT_SHAPE_PROXYTYPE,
|
||||
///Multimaterial mesh
|
||||
MULTIMATERIAL_TRIANGLE_MESH_PROXYTYPE,
|
||||
|
||||
EMPTY_SHAPE_PROXYTYPE,
|
||||
STATIC_PLANE_PROXYTYPE,
|
||||
CUSTOM_CONCAVE_SHAPE_TYPE,
|
||||
CONCAVE_SHAPES_END_HERE,
|
||||
|
||||
COMPOUND_SHAPE_PROXYTYPE,
|
||||
|
||||
SOFTBODY_SHAPE_PROXYTYPE,
|
||||
HFFLUID_SHAPE_PROXYTYPE,
|
||||
HFFLUID_BUOYANT_CONVEX_SHAPE_PROXYTYPE,
|
||||
INVALID_SHAPE_PROXYTYPE,
|
||||
|
||||
MAX_BROADPHASE_COLLISION_TYPES
|
||||
|
||||
};
|
||||
|
||||
b3BulletDataExtractor::b3BulletDataExtractor(GLInstancingRenderer& renderer, b3GpuNarrowPhase& np, b3GpuRigidBodyPipeline& rbWorld)
|
||||
:m_renderer(renderer), m_np(np), m_rbPipeline(rbWorld)
|
||||
{
|
||||
}
|
||||
|
||||
b3BulletDataExtractor::~b3BulletDataExtractor()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
void b3BulletDataExtractor::convertAllObjects(bParse::b3BulletFile* bulletFile2)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i=0;i<bulletFile2->m_collisionShapes.size();i++)
|
||||
{
|
||||
Bullet3SerializeBullet2::b3CollisionShapeData* shapeData = (Bullet3SerializeBullet2::b3CollisionShapeData*)bulletFile2->m_collisionShapes[i];
|
||||
if (shapeData->m_name)
|
||||
printf("converting shape %s\n", shapeData->m_name);
|
||||
int shapeIndex = convertCollisionShape(shapeData);
|
||||
//valid conversion?
|
||||
if (shapeIndex>=0)
|
||||
{
|
||||
InstanceGroup* group = new InstanceGroup;
|
||||
group->m_shape = shapeData;
|
||||
group->m_collisionShapeIndex = shapeIndex;
|
||||
m_instanceGroups.push_back(group);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
for (i=0;i<bulletFile2->m_rigidBodies.size();i++)
|
||||
{
|
||||
|
||||
Bullet3SerializeBullet2::b3RigidBodyFloatData* colObjData = (Bullet3SerializeBullet2::b3RigidBodyFloatData*)bulletFile2->m_rigidBodies[i];
|
||||
Bullet3SerializeBullet2::b3CollisionShapeData* shapeData = (Bullet3SerializeBullet2::b3CollisionShapeData*)colObjData->m_collisionObjectData.m_collisionShape;
|
||||
for (int j=0;j<m_instanceGroups.size();j++)
|
||||
{
|
||||
if (m_instanceGroups[j]->m_shape == shapeData)
|
||||
{
|
||||
m_instanceGroups[j]->m_rigidBodies.push_back(bulletFile2->m_rigidBodies[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//now register all objects in order
|
||||
for (int i=0;i<m_instanceGroups.size();i++)
|
||||
{
|
||||
if (m_instanceGroups[i]->m_rigidBodies.size()>0)
|
||||
{
|
||||
|
||||
m_renderer.registerShape(m_graphicsShapes[i]->m_vertices,m_graphicsShapes[i]->m_numvertices,m_graphicsShapes[i]->m_indices,m_graphicsShapes[i]->m_numIndices);
|
||||
|
||||
for (int j=0;j<m_instanceGroups[i]->m_rigidBodies.size();j++)
|
||||
{
|
||||
Bullet3SerializeBullet2::b3RigidBodyFloatData* colObjData = (Bullet3SerializeBullet2::b3RigidBodyFloatData*)m_instanceGroups[i]->m_rigidBodies[j];
|
||||
|
||||
b3Matrix3x3 mat;
|
||||
mat.deSerializeFloat((const b3Matrix3x3FloatData&)colObjData->m_collisionObjectData.m_worldTransform.m_basis);
|
||||
b3Quaternion orn;
|
||||
mat.getRotation(orn);
|
||||
float quaternion[4] = {orn[0],orn[1],orn[2],orn[3]};
|
||||
float pos[4] = {colObjData->m_collisionObjectData.m_worldTransform.m_origin.m_floats[0],
|
||||
colObjData->m_collisionObjectData.m_worldTransform.m_origin.m_floats[1],
|
||||
colObjData->m_collisionObjectData.m_worldTransform.m_origin.m_floats[2],
|
||||
0.f};
|
||||
float color[4] = {0,0,0,1};
|
||||
float mass = 0.f;
|
||||
if (colObjData->m_inverseMass==0.f)
|
||||
{
|
||||
color[1] = 1;
|
||||
} else
|
||||
{
|
||||
mass = 1.f/colObjData->m_inverseMass;
|
||||
color[2] = 1;
|
||||
}
|
||||
if (keepStaticObjects || colObjData->m_inverseMass!=0.f)
|
||||
{
|
||||
|
||||
m_rbPipeline.registerPhysicsInstance(mass,pos,quaternion,m_instanceGroups[i]->m_collisionShapeIndex,0,true);
|
||||
m_renderer.registerGraphicsInstance(m_instanceGroups[i]->m_collisionShapeIndex,pos,quaternion,color,m_graphicsShapes[i]->m_scaling);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (i=0;i<bulletFile2->m_collisionObjects.size();i++)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
m_rbPipeline.writeAllInstancesToGpu();
|
||||
}
|
||||
|
||||
|
||||
|
||||
int b3BulletDataExtractor::convertCollisionShape( Bullet3SerializeBullet2::b3CollisionShapeData* shapeData )
|
||||
{
|
||||
int shapeIndex = -1;
|
||||
|
||||
switch (shapeData->m_shapeType)
|
||||
{
|
||||
case STATIC_PLANE_PROXYTYPE:
|
||||
{
|
||||
Bullet3SerializeBullet2::b3StaticPlaneShapeData* planeData = (Bullet3SerializeBullet2::b3StaticPlaneShapeData*)shapeData;
|
||||
shapeIndex = createPlaneShape(planeData->m_planeNormal,planeData->m_planeConstant, planeData->m_localScaling);
|
||||
break;
|
||||
}
|
||||
|
||||
case CYLINDER_SHAPE_PROXYTYPE:
|
||||
case CAPSULE_SHAPE_PROXYTYPE:
|
||||
case BOX_SHAPE_PROXYTYPE:
|
||||
case SPHERE_SHAPE_PROXYTYPE:
|
||||
case MULTI_SPHERE_SHAPE_PROXYTYPE:
|
||||
case CONVEX_HULL_SHAPE_PROXYTYPE:
|
||||
{
|
||||
Bullet3SerializeBullet2::b3ConvexInternalShapeData* bsd = (Bullet3SerializeBullet2::b3ConvexInternalShapeData*)shapeData;
|
||||
|
||||
switch (shapeData->m_shapeType)
|
||||
{
|
||||
case BOX_SHAPE_PROXYTYPE:
|
||||
{
|
||||
shapeIndex = createBoxShape(bsd->m_implicitShapeDimensions, bsd->m_localScaling,bsd->m_collisionMargin);
|
||||
break;
|
||||
}
|
||||
case SPHERE_SHAPE_PROXYTYPE:
|
||||
{
|
||||
shapeIndex = createSphereShape(bsd->m_implicitShapeDimensions.m_floats[0],bsd->m_localScaling, bsd->m_collisionMargin);
|
||||
break;
|
||||
}
|
||||
case CONVEX_HULL_SHAPE_PROXYTYPE:
|
||||
{
|
||||
Bullet3SerializeBullet2::b3ConvexHullShapeData* convexData = (Bullet3SerializeBullet2::b3ConvexHullShapeData*)bsd;
|
||||
int numPoints = convexData->m_numUnscaledPoints;
|
||||
b3Vector3 localScaling;
|
||||
localScaling.deSerializeFloat((b3Vector3FloatData&)bsd->m_localScaling);
|
||||
b3AlignedObjectArray<b3Vector3> tmpPoints;
|
||||
int i;
|
||||
if (convexData->m_unscaledPointsFloatPtr)
|
||||
{
|
||||
for ( i=0;i<numPoints;i++)
|
||||
{
|
||||
b3Vector3 pt = b3Vector3(convexData->m_unscaledPointsFloatPtr[i].m_floats[0],
|
||||
convexData->m_unscaledPointsFloatPtr[i].m_floats[1],
|
||||
convexData->m_unscaledPointsFloatPtr[i].m_floats[2]);//convexData->m_unscaledPointsFloatPtr[i].m_floats[3]);
|
||||
|
||||
tmpPoints.push_back(pt*localScaling);
|
||||
}
|
||||
}
|
||||
float unitScaling[4] = {1,1,1,1};
|
||||
|
||||
|
||||
int strideInBytes = sizeof(b3Vector3);
|
||||
strideInBytes = 4*sizeof(float);
|
||||
int noHeightField = 1;
|
||||
shapeIndex = m_np.registerConvexHullShape(&tmpPoints[0].m_floats[0],strideInBytes, numPoints,&unitScaling[0]);
|
||||
|
||||
printf("createConvexHull with %d vertices\n",numPoints);
|
||||
|
||||
GraphicsShape* gfxShape = createGraphicsShapeFromConvexHull(&tmpPoints[0],tmpPoints.size());
|
||||
m_graphicsShapes.push_back(gfxShape);
|
||||
|
||||
return shapeIndex;
|
||||
break;
|
||||
}
|
||||
#if 0
|
||||
case CAPSULE_SHAPE_PROXYTYPE:
|
||||
{
|
||||
b3CapsuleShapeData* capData = (b3CapsuleShapeData*)shapeData;
|
||||
switch (capData->m_upAxis)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
shape = createCapsuleShapeX(implicitShapeDimensions.getY(),2*implicitShapeDimensions.getX());
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
shape = createCapsuleShapeY(implicitShapeDimensions.getX(),2*implicitShapeDimensions.getY());
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
shape = createCapsuleShapeZ(implicitShapeDimensions.getX(),2*implicitShapeDimensions.getZ());
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
printf("error: wrong up axis for b3CapsuleShape\n");
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
break;
|
||||
}
|
||||
case CYLINDER_SHAPE_PROXYTYPE:
|
||||
{
|
||||
b3CylinderShapeData* cylData = (b3CylinderShapeData*) shapeData;
|
||||
b3Vector3 halfExtents = implicitShapeDimensions+margin;
|
||||
switch (cylData->m_upAxis)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
shape = createCylinderShapeX(halfExtents.getY(),halfExtents.getX());
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
shape = createCylinderShapeY(halfExtents.getX(),halfExtents.getY());
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
shape = createCylinderShapeZ(halfExtents.getX(),halfExtents.getZ());
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
printf("unknown Cylinder up axis\n");
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
case MULTI_SPHERE_SHAPE_PROXYTYPE:
|
||||
{
|
||||
b3MultiSphereShapeData* mss = (b3MultiSphereShapeData*)bsd;
|
||||
int numSpheres = mss->m_localPositionArraySize;
|
||||
int i;
|
||||
for ( i=0;i<numSpheres;i++)
|
||||
{
|
||||
tmpPos[i].deSerializeFloat(mss->m_localPositionArrayPtr[i].m_pos);
|
||||
radii[i] = mss->m_localPositionArrayPtr[i].m_radius;
|
||||
}
|
||||
shape = new b3MultiSphereShape(&tmpPos[0],&radii[0],numSpheres);
|
||||
break;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
default:
|
||||
{
|
||||
printf("error: cannot create shape type (%d)\n",shapeData->m_shapeType);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case TRIANGLE_MESH_SHAPE_PROXYTYPE:
|
||||
{
|
||||
Bullet3SerializeBullet2::b3TriangleMeshShapeData* trimesh = (Bullet3SerializeBullet2::b3TriangleMeshShapeData*)shapeData;
|
||||
printf("numparts = %d\n",trimesh->m_meshInterface.m_numMeshParts);
|
||||
if (trimesh->m_meshInterface.m_numMeshParts)
|
||||
{
|
||||
for (int i=0;i<trimesh->m_meshInterface.m_numMeshParts;i++)
|
||||
{
|
||||
Bullet3SerializeBullet2::b3MeshPartData& dat = trimesh->m_meshInterface.m_meshPartsPtr[i];
|
||||
printf("numtris = %d, numverts = %d\n", dat.m_numTriangles,dat.m_numVertices);//,dat.m_vertices3f,dat.m_3indices16
|
||||
printf("scaling = %f,%f,%f\n", trimesh->m_meshInterface.m_scaling.m_floats[0],trimesh->m_meshInterface.m_scaling.m_floats[1],trimesh->m_meshInterface.m_scaling.m_floats[2]);
|
||||
// dat.
|
||||
//dat.
|
||||
|
||||
}
|
||||
|
||||
///trimesh->m_meshInterface.m_meshPartsPtr
|
||||
//trimesh->m_meshInterface.m_scaling
|
||||
}
|
||||
//trimesh->m_meshInterface
|
||||
//b3TriangleIndexVertexArray* meshInterface = createMeshInterface(trimesh->m_meshInterface);
|
||||
|
||||
|
||||
//scaling
|
||||
//b3Vector3 scaling; scaling.deSerializeFloat(trimesh->m_meshInterface.m_scaling);
|
||||
//meshInterface->setScaling(scaling);
|
||||
|
||||
//printf("trimesh->m_collisionMargin=%f\n",trimesh->m_collisionMargin);
|
||||
break;
|
||||
}
|
||||
|
||||
#if 0
|
||||
case COMPOUND_SHAPE_PROXYTYPE:
|
||||
{
|
||||
b3CompoundShapeData* compoundData = (b3CompoundShapeData*)shapeData;
|
||||
b3CompoundShape* compoundShape = createCompoundShape();
|
||||
|
||||
|
||||
b3AlignedObjectArray<b3CollisionShape*> childShapes;
|
||||
for (int i=0;i<compoundData->m_numChildShapes;i++)
|
||||
{
|
||||
b3CollisionShape* childShape = convertCollisionShape(compoundData->m_childShapePtr[i].m_childShape);
|
||||
if (childShape)
|
||||
{
|
||||
b3Transform localTransform;
|
||||
localTransform.deSerializeFloat(compoundData->m_childShapePtr[i].m_transform);
|
||||
compoundShape->addChildShape(localTransform,childShape);
|
||||
} else
|
||||
{
|
||||
printf("error: couldn't create childShape for compoundShape\n");
|
||||
}
|
||||
|
||||
}
|
||||
shape = compoundShape;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case GIMPACT_SHAPE_PROXYTYPE:
|
||||
{
|
||||
b3GImpactMeshShapeData* gimpactData = (b3GImpactMeshShapeData*) shapeData;
|
||||
if (gimpactData->m_gimpactSubType == CONST_GIMPACT_TRIMESH_SHAPE)
|
||||
{
|
||||
b3TriangleIndexVertexArray* meshInterface = createMeshInterface(gimpactData->m_meshInterface);
|
||||
b3GImpactMeshShape* gimpactShape = createGimpactShape(meshInterface);
|
||||
b3Vector3 localScaling;
|
||||
localScaling.deSerializeFloat(gimpactData->m_localScaling);
|
||||
gimpactShape->setLocalScaling(localScaling);
|
||||
gimpactShape->setMargin(b3Scalar(gimpactData->m_collisionMargin));
|
||||
gimpactShape->updateBound();
|
||||
shape = gimpactShape;
|
||||
} else
|
||||
{
|
||||
printf("unsupported gimpact sub type\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SOFTBODY_SHAPE_PROXYTYPE:
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
default:
|
||||
{
|
||||
printf("unsupported shape type (%d)\n",shapeData->m_shapeType);
|
||||
}
|
||||
}
|
||||
|
||||
return shapeIndex;
|
||||
|
||||
}
|
||||
|
||||
int b3BulletDataExtractor::createBoxShape( const Bullet3SerializeBullet2::b3Vector3FloatData& halfDimensions, const Bullet3SerializeBullet2::b3Vector3FloatData& localScaling, float collisionMargin)
|
||||
{
|
||||
float cubeScaling[4] = {
|
||||
halfDimensions.m_floats[0]*localScaling.m_floats[0]+collisionMargin,
|
||||
halfDimensions.m_floats[1]*localScaling.m_floats[1]+collisionMargin,
|
||||
halfDimensions.m_floats[2]*localScaling.m_floats[2]+collisionMargin,
|
||||
1};
|
||||
int strideInBytes = sizeof(float)*9;
|
||||
int noHeightField = 1;
|
||||
int cubeCollisionShapeIndex = m_np.registerConvexHullShape(&cube_vertices[0],strideInBytes, sizeof(cube_vertices)/strideInBytes,&cubeScaling[0]);
|
||||
|
||||
{
|
||||
int numVertices = sizeof(cube_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(cube_indices)/sizeof(int);
|
||||
|
||||
GraphicsShape* gfxShape = new GraphicsShape;
|
||||
gfxShape->m_vertices = cube_vertices;
|
||||
gfxShape->m_numvertices = numVertices;
|
||||
gfxShape->m_indices = cube_indices;
|
||||
gfxShape->m_numIndices = numIndices;
|
||||
for (int i=0;i<4;i++)
|
||||
gfxShape->m_scaling[i] = cubeScaling[i];
|
||||
m_graphicsShapes.push_back(gfxShape);
|
||||
}
|
||||
|
||||
printf("createBoxShape with half extents %f,%f,%f\n",cubeScaling[0], cubeScaling[1],cubeScaling[2]);
|
||||
//halfDimensions * localScaling
|
||||
return cubeCollisionShapeIndex;
|
||||
}
|
||||
|
||||
int b3BulletDataExtractor::createSphereShape( float radius, const Bullet3SerializeBullet2::b3Vector3FloatData& localScaling, float collisionMargin)
|
||||
{
|
||||
printf("createSphereShape with radius %f\n",radius);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int b3BulletDataExtractor::createPlaneShape( const Bullet3SerializeBullet2::b3Vector3FloatData& planeNormal, float planeConstant, const Bullet3SerializeBullet2::b3Vector3FloatData& localScaling)
|
||||
{
|
||||
printf("createPlaneShape with normal %f,%f,%f and planeConstant\n",planeNormal.m_floats[0], planeNormal.m_floats[1],planeNormal.m_floats[2],planeConstant);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
GraphicsShape* b3BulletDataExtractor::createGraphicsShapeFromWavefrontObj(objLoader* obj)
|
||||
{
|
||||
b3AlignedObjectArray<GraphicsVertex>* vertices = new b3AlignedObjectArray<GraphicsVertex>;
|
||||
{
|
||||
// int numVertices = obj->vertexCount;
|
||||
// int numIndices = 0;
|
||||
b3AlignedObjectArray<int>* indicesPtr = new b3AlignedObjectArray<int>;
|
||||
/*
|
||||
for (int v=0;v<obj->vertexCount;v++)
|
||||
{
|
||||
vtx.xyzw[0] = obj->vertexList[v]->e[0];
|
||||
vtx.xyzw[1] = obj->vertexList[v]->e[1];
|
||||
vtx.xyzw[2] = obj->vertexList[v]->e[2];
|
||||
b3Vector3 n(vtx.xyzw[0],vtx.xyzw[1],vtx.xyzw[2]);
|
||||
if (n.length2()>B3_EPSILON)
|
||||
{
|
||||
n.normalize();
|
||||
vtx.normal[0] = n[0];
|
||||
vtx.normal[1] = n[1];
|
||||
vtx.normal[2] = n[2];
|
||||
|
||||
} else
|
||||
{
|
||||
vtx.normal[0] = 0; //todo
|
||||
vtx.normal[1] = 1;
|
||||
vtx.normal[2] = 0;
|
||||
}
|
||||
vtx.uv[0] = 0.5f;vtx.uv[1] = 0.5f; //todo
|
||||
vertices->push_back(vtx);
|
||||
}
|
||||
*/
|
||||
|
||||
for (int f=0;f<obj->faceCount;f++)
|
||||
{
|
||||
obj_face* face = obj->faceList[f];
|
||||
//b3Vector3 normal(face.m_plane[0],face.m_plane[1],face.m_plane[2]);
|
||||
if (face->vertex_count>=3)
|
||||
{
|
||||
b3Vector3 normal(0,1,0);
|
||||
int vtxBaseIndex = vertices->size();
|
||||
|
||||
if (face->vertex_count<=4)
|
||||
{
|
||||
indicesPtr->push_back(vtxBaseIndex);
|
||||
indicesPtr->push_back(vtxBaseIndex+1);
|
||||
indicesPtr->push_back(vtxBaseIndex+2);
|
||||
|
||||
GraphicsVertex vtx0;
|
||||
vtx0.xyzw[0] = obj->vertexList[face->vertex_index[0]]->e[0];
|
||||
vtx0.xyzw[1] = obj->vertexList[face->vertex_index[0]]->e[1];
|
||||
vtx0.xyzw[2] = obj->vertexList[face->vertex_index[0]]->e[2];
|
||||
vtx0.uv[0] = 0.5;
|
||||
vtx0.uv[1] = 0.5;
|
||||
|
||||
GraphicsVertex vtx1;
|
||||
vtx1.xyzw[0] = obj->vertexList[face->vertex_index[1]]->e[0];
|
||||
vtx1.xyzw[1] = obj->vertexList[face->vertex_index[1]]->e[1];
|
||||
vtx1.xyzw[2] = obj->vertexList[face->vertex_index[1]]->e[2];
|
||||
vtx1.uv[0] = 0.5;
|
||||
vtx1.uv[1] = 0.5;
|
||||
|
||||
GraphicsVertex vtx2;
|
||||
vtx2.xyzw[0] = obj->vertexList[face->vertex_index[2]]->e[0];
|
||||
vtx2.xyzw[1] = obj->vertexList[face->vertex_index[2]]->e[1];
|
||||
vtx2.xyzw[2] = obj->vertexList[face->vertex_index[2]]->e[2];
|
||||
vtx2.uv[0] = 0.5;
|
||||
vtx2.uv[1] = 0.5;
|
||||
|
||||
b3Vector3 v0(vtx0.xyzw[0],vtx0.xyzw[1],vtx0.xyzw[2]);
|
||||
b3Vector3 v1(vtx1.xyzw[0],vtx1.xyzw[1],vtx1.xyzw[2]);
|
||||
b3Vector3 v2(vtx2.xyzw[0],vtx2.xyzw[1],vtx2.xyzw[2]);
|
||||
|
||||
normal = (v1-v0).cross(v2-v0);
|
||||
normal.normalize();
|
||||
vtx0.normal[0] = normal[0];
|
||||
vtx0.normal[1] = normal[1];
|
||||
vtx0.normal[2] = normal[2];
|
||||
vtx1.normal[0] = normal[0];
|
||||
vtx1.normal[1] = normal[1];
|
||||
vtx1.normal[2] = normal[2];
|
||||
vtx2.normal[0] = normal[0];
|
||||
vtx2.normal[1] = normal[1];
|
||||
vtx2.normal[2] = normal[2];
|
||||
vertices->push_back(vtx0);
|
||||
vertices->push_back(vtx1);
|
||||
vertices->push_back(vtx2);
|
||||
}
|
||||
if (face->vertex_count==4)
|
||||
{
|
||||
|
||||
indicesPtr->push_back(vtxBaseIndex);
|
||||
indicesPtr->push_back(vtxBaseIndex+1);
|
||||
indicesPtr->push_back(vtxBaseIndex+2);
|
||||
indicesPtr->push_back(vtxBaseIndex+3);
|
||||
//
|
||||
GraphicsVertex vtx3;
|
||||
vtx3.xyzw[0] = obj->vertexList[face->vertex_index[3]]->e[0];
|
||||
vtx3.xyzw[1] = obj->vertexList[face->vertex_index[3]]->e[1];
|
||||
vtx3.xyzw[2] = obj->vertexList[face->vertex_index[3]]->e[2];
|
||||
vtx3.uv[0] = 0.5;
|
||||
vtx3.uv[1] = 0.5;
|
||||
|
||||
vtx3.normal[0] = normal[0];
|
||||
vtx3.normal[1] = normal[1];
|
||||
vtx3.normal[2] = normal[2];
|
||||
|
||||
vertices->push_back(vtx3);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
GraphicsShape* gfxShape = new GraphicsShape;
|
||||
gfxShape->m_vertices = &vertices->at(0).xyzw[0];
|
||||
gfxShape->m_numvertices = vertices->size();
|
||||
gfxShape->m_indices = &indicesPtr->at(0);
|
||||
gfxShape->m_numIndices = indicesPtr->size();
|
||||
for (int i=0;i<4;i++)
|
||||
gfxShape->m_scaling[i] = 1;//bake the scaling into the vertices
|
||||
return gfxShape;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
GraphicsShape* b3BulletDataExtractor::createGraphicsShapeFromConvexHull(const b3Vector3* tmpPoints, int numPoints)
|
||||
{
|
||||
b3ConvexUtility* utilPtr = new b3ConvexUtility();
|
||||
bool merge = true;
|
||||
utilPtr->initializePolyhedralFeatures(tmpPoints,numPoints,merge);
|
||||
|
||||
b3AlignedObjectArray<GraphicsVertex>* vertices = new b3AlignedObjectArray<GraphicsVertex>;
|
||||
{
|
||||
int numVertices = utilPtr->m_vertices.size();
|
||||
int numIndices = 0;
|
||||
b3AlignedObjectArray<int>* indicesPtr = new b3AlignedObjectArray<int>;
|
||||
for (int f=0;f<utilPtr->m_faces.size();f++)
|
||||
{
|
||||
const b3MyFace& face = utilPtr->m_faces[f];
|
||||
b3Vector3 normal(face.m_plane[0],face.m_plane[1],face.m_plane[2]);
|
||||
if (face.m_indices.size()>2)
|
||||
{
|
||||
|
||||
GraphicsVertex vtx;
|
||||
const b3Vector3& orgVertex = utilPtr->m_vertices[face.m_indices[0]];
|
||||
vtx.xyzw[0] = orgVertex[0];vtx.xyzw[1] = orgVertex[1];vtx.xyzw[2] = orgVertex[2];vtx.xyzw[3] = 0.f;
|
||||
vtx.normal[0] = normal[0];vtx.normal[1] = normal[1];vtx.normal[2] = normal[2];
|
||||
vtx.uv[0] = 0.5f;vtx.uv[1] = 0.5f;
|
||||
int newvtxindex0 = vertices->size();
|
||||
vertices->push_back(vtx);
|
||||
|
||||
for (int j=1;j<face.m_indices.size()-1;j++)
|
||||
{
|
||||
indicesPtr->push_back(newvtxindex0);
|
||||
{
|
||||
GraphicsVertex vtx;
|
||||
const b3Vector3& orgVertex = utilPtr->m_vertices[face.m_indices[j]];
|
||||
vtx.xyzw[0] = orgVertex[0];vtx.xyzw[1] = orgVertex[1];vtx.xyzw[2] = orgVertex[2];vtx.xyzw[3] = 0.f;
|
||||
vtx.normal[0] = normal[0];vtx.normal[1] = normal[1];vtx.normal[2] = normal[2];
|
||||
vtx.uv[0] = 0.5f;vtx.uv[1] = 0.5f;
|
||||
int newvtxindexj = vertices->size();
|
||||
vertices->push_back(vtx);
|
||||
indicesPtr->push_back(newvtxindexj);
|
||||
}
|
||||
|
||||
{
|
||||
GraphicsVertex vtx;
|
||||
const b3Vector3& orgVertex = utilPtr->m_vertices[face.m_indices[j+1]];
|
||||
vtx.xyzw[0] = orgVertex[0];vtx.xyzw[1] = orgVertex[1];vtx.xyzw[2] = orgVertex[2];vtx.xyzw[3] = 0.f;
|
||||
vtx.normal[0] = normal[0];vtx.normal[1] = normal[1];vtx.normal[2] = normal[2];
|
||||
vtx.uv[0] = 0.5f;vtx.uv[1] = 0.5f;
|
||||
int newvtxindexj1 = vertices->size();
|
||||
vertices->push_back(vtx);
|
||||
indicesPtr->push_back(newvtxindexj1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
GraphicsShape* gfxShape = new GraphicsShape;
|
||||
gfxShape->m_vertices = &vertices->at(0).xyzw[0];
|
||||
gfxShape->m_numvertices = vertices->size();
|
||||
gfxShape->m_indices = &indicesPtr->at(0);
|
||||
gfxShape->m_numIndices = indicesPtr->size();
|
||||
for (int i=0;i<4;i++)
|
||||
gfxShape->m_scaling[i] = 1;//bake the scaling into the vertices
|
||||
return gfxShape;
|
||||
}
|
||||
}
|
||||
53
Demos3/GpuDemos/rigidbody/BulletDataExtractor.h
Normal file
53
Demos3/GpuDemos/rigidbody/BulletDataExtractor.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#ifndef BULLET_DATA_EXTRACTOR_H
|
||||
#define BULLET_DATA_EXTRACTOR_H
|
||||
|
||||
|
||||
#include "Bullet3Serialize/Bullet2FileLoader/autogenerated/bullet2.h"
|
||||
#include "Bullet3Common/b3AlignedObjectArray.h"
|
||||
#include "Bullet3Common/b3Vector3.h"
|
||||
|
||||
class GLInstancingRenderer;
|
||||
class b3GpuRigidBodyPipeline;
|
||||
class b3GpuNarrowPhase;
|
||||
|
||||
void createScene(GLInstancingRenderer& renderer,b3GpuNarrowPhase& np, b3GpuRigidBodyPipeline& rbWorld, const char* fileName);
|
||||
|
||||
namespace bParse
|
||||
{
|
||||
class b3BulletFile;
|
||||
};
|
||||
|
||||
class b3BulletDataExtractor
|
||||
{
|
||||
GLInstancingRenderer& m_renderer;
|
||||
b3GpuNarrowPhase& m_np;
|
||||
b3GpuRigidBodyPipeline& m_rbPipeline;
|
||||
|
||||
public:
|
||||
|
||||
b3AlignedObjectArray<struct InstanceGroup*> m_instanceGroups;
|
||||
b3AlignedObjectArray<struct GraphicsShape*> m_graphicsShapes;
|
||||
|
||||
b3BulletDataExtractor(GLInstancingRenderer& renderer, b3GpuNarrowPhase& np, b3GpuRigidBodyPipeline& rbPipeline);
|
||||
|
||||
virtual ~b3BulletDataExtractor();
|
||||
|
||||
virtual void convertAllObjects(bParse::b3BulletFile* bulletFile);
|
||||
|
||||
//return -1 for invalid
|
||||
virtual int convertCollisionShape( Bullet3SerializeBullet2::b3CollisionShapeData* shapeData );
|
||||
|
||||
virtual int createPlaneShape( const Bullet3SerializeBullet2::b3Vector3FloatData& planeNormal, float planeConstant, const Bullet3SerializeBullet2::b3Vector3FloatData& localScaling);
|
||||
|
||||
virtual int createBoxShape( const Bullet3SerializeBullet2::b3Vector3FloatData& halfDimensions, const Bullet3SerializeBullet2::b3Vector3FloatData& localScaling, float collisionMargin);
|
||||
|
||||
virtual int createSphereShape( float radius, const Bullet3SerializeBullet2::b3Vector3FloatData& localScaling, float collisionMargin);
|
||||
|
||||
static GraphicsShape* createGraphicsShapeFromConvexHull(const b3Vector3* tmpPoints, int numPoints);
|
||||
static GraphicsShape* createGraphicsShapeFromWavefrontObj(class objLoader* obj);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //BULLET_DATA_EXTRACTOR_H
|
||||
757
Demos3/GpuDemos/rigidbody/ConcaveScene.cpp
Normal file
757
Demos3/GpuDemos/rigidbody/ConcaveScene.cpp
Normal file
@@ -0,0 +1,757 @@
|
||||
#include "ConcaveScene.h"
|
||||
#include "GpuRigidBodyDemo.h"
|
||||
#include "Bullet3Common/b3Quickprof.h"
|
||||
#include "OpenGLWindow/ShapeData.h"
|
||||
|
||||
#include "OpenGLWindow/GLInstancingRenderer.h"
|
||||
#include "Bullet3Common/b3Quaternion.h"
|
||||
#include "OpenGLWindow/b3gWindowInterface.h"
|
||||
#include "Bullet3OpenCL/BroadphaseCollision/b3GpuSapBroadphase.h"
|
||||
#include "../GpuDemoInternalData.h"
|
||||
#include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
|
||||
#include "OpenGLWindow/OpenGLInclude.h"
|
||||
#include "OpenGLWindow/GLInstanceRendererInternalData.h"
|
||||
#include "Bullet3OpenCL/ParallelPrimitives/b3LauncherCL.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3GpuRigidBodyPipeline.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3GpuNarrowPhase.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3Config.h"
|
||||
#include "GpuRigidBodyDemoInternalData.h"
|
||||
#include"../../Wavefront/objLoader.h"
|
||||
#include "Bullet3Common/b3Transform.h"
|
||||
|
||||
#include "OpenGLWindow/GLInstanceGraphicsShape.h"
|
||||
#define CONCAVE_GAPX 16
|
||||
#define CONCAVE_GAPY 8
|
||||
#define CONCAVE_GAPZ 16
|
||||
|
||||
|
||||
GLInstanceGraphicsShape* createGraphicsShapeFromWavefrontObj(objLoader* obj)
|
||||
{
|
||||
b3AlignedObjectArray<GLInstanceVertex>* vertices = new b3AlignedObjectArray<GLInstanceVertex>;
|
||||
{
|
||||
// int numVertices = obj->vertexCount;
|
||||
// int numIndices = 0;
|
||||
b3AlignedObjectArray<int>* indicesPtr = new b3AlignedObjectArray<int>;
|
||||
/*
|
||||
for (int v=0;v<obj->vertexCount;v++)
|
||||
{
|
||||
vtx.xyzw[0] = obj->vertexList[v]->e[0];
|
||||
vtx.xyzw[1] = obj->vertexList[v]->e[1];
|
||||
vtx.xyzw[2] = obj->vertexList[v]->e[2];
|
||||
b3Vector3 n(vtx.xyzw[0],vtx.xyzw[1],vtx.xyzw[2]);
|
||||
if (n.length2()>B3_EPSILON)
|
||||
{
|
||||
n.normalize();
|
||||
vtx.normal[0] = n[0];
|
||||
vtx.normal[1] = n[1];
|
||||
vtx.normal[2] = n[2];
|
||||
|
||||
} else
|
||||
{
|
||||
vtx.normal[0] = 0; //todo
|
||||
vtx.normal[1] = 1;
|
||||
vtx.normal[2] = 0;
|
||||
}
|
||||
vtx.uv[0] = 0.5f;vtx.uv[1] = 0.5f; //todo
|
||||
vertices->push_back(vtx);
|
||||
}
|
||||
*/
|
||||
|
||||
for (int f=0;f<obj->faceCount;f++)
|
||||
{
|
||||
obj_face* face = obj->faceList[f];
|
||||
//b3Vector3 normal(face.m_plane[0],face.m_plane[1],face.m_plane[2]);
|
||||
if (face->vertex_count>=3)
|
||||
{
|
||||
b3Vector3 normal(0,1,0);
|
||||
int vtxBaseIndex = vertices->size();
|
||||
|
||||
if (face->vertex_count<=4)
|
||||
{
|
||||
indicesPtr->push_back(vtxBaseIndex);
|
||||
indicesPtr->push_back(vtxBaseIndex+1);
|
||||
indicesPtr->push_back(vtxBaseIndex+2);
|
||||
|
||||
GLInstanceVertex vtx0;
|
||||
vtx0.xyzw[0] = obj->vertexList[face->vertex_index[0]]->e[0];
|
||||
vtx0.xyzw[1] = obj->vertexList[face->vertex_index[0]]->e[1];
|
||||
vtx0.xyzw[2] = obj->vertexList[face->vertex_index[0]]->e[2];
|
||||
vtx0.xyzw[3] = 0.f;//obj->vertexList[face->vertex_index[0]]->e[2];
|
||||
|
||||
vtx0.uv[0] = 0.5f;//obj->textureList[face->vertex_index[0]]->e[0];
|
||||
vtx0.uv[1] = 0.5f;//obj->textureList[face->vertex_index[0]]->e[1];
|
||||
|
||||
GLInstanceVertex vtx1;
|
||||
vtx1.xyzw[0] = obj->vertexList[face->vertex_index[1]]->e[0];
|
||||
vtx1.xyzw[1] = obj->vertexList[face->vertex_index[1]]->e[1];
|
||||
vtx1.xyzw[2] = obj->vertexList[face->vertex_index[1]]->e[2];
|
||||
vtx1.xyzw[3]= 0.f;
|
||||
vtx1.uv[0] = 0.5f;//obj->textureList[face->vertex_index[1]]->e[0];
|
||||
vtx1.uv[1] = 0.5f;//obj->textureList[face->vertex_index[1]]->e[1];
|
||||
|
||||
GLInstanceVertex vtx2;
|
||||
vtx2.xyzw[0] = obj->vertexList[face->vertex_index[2]]->e[0];
|
||||
vtx2.xyzw[1] = obj->vertexList[face->vertex_index[2]]->e[1];
|
||||
vtx2.xyzw[2] = obj->vertexList[face->vertex_index[2]]->e[2];
|
||||
vtx2.xyzw[3] = 0.f;
|
||||
vtx2.uv[0] = 0.5f;obj->textureList[face->vertex_index[2]]->e[0];
|
||||
vtx2.uv[1] = 0.5f;obj->textureList[face->vertex_index[2]]->e[1];
|
||||
|
||||
|
||||
b3Vector3 v0(vtx0.xyzw[0],vtx0.xyzw[1],vtx0.xyzw[2]);
|
||||
b3Vector3 v1(vtx1.xyzw[0],vtx1.xyzw[1],vtx1.xyzw[2]);
|
||||
b3Vector3 v2(vtx2.xyzw[0],vtx2.xyzw[1],vtx2.xyzw[2]);
|
||||
|
||||
normal = (v1-v0).cross(v2-v0);
|
||||
normal.normalize();
|
||||
vtx0.normal[0] = normal[0];
|
||||
vtx0.normal[1] = normal[1];
|
||||
vtx0.normal[2] = normal[2];
|
||||
vtx1.normal[0] = normal[0];
|
||||
vtx1.normal[1] = normal[1];
|
||||
vtx1.normal[2] = normal[2];
|
||||
vtx2.normal[0] = normal[0];
|
||||
vtx2.normal[1] = normal[1];
|
||||
vtx2.normal[2] = normal[2];
|
||||
vertices->push_back(vtx0);
|
||||
vertices->push_back(vtx1);
|
||||
vertices->push_back(vtx2);
|
||||
}
|
||||
if (face->vertex_count==4)
|
||||
{
|
||||
|
||||
indicesPtr->push_back(vtxBaseIndex);
|
||||
indicesPtr->push_back(vtxBaseIndex+1);
|
||||
indicesPtr->push_back(vtxBaseIndex+2);
|
||||
indicesPtr->push_back(vtxBaseIndex+3);
|
||||
//
|
||||
GLInstanceVertex vtx3;
|
||||
vtx3.xyzw[0] = obj->vertexList[face->vertex_index[3]]->e[0];
|
||||
vtx3.xyzw[1] = obj->vertexList[face->vertex_index[3]]->e[1];
|
||||
vtx3.xyzw[2] = obj->vertexList[face->vertex_index[3]]->e[2];
|
||||
vtx3.uv[0] = 0.5;
|
||||
vtx3.uv[1] = 0.5;
|
||||
|
||||
vtx3.normal[0] = normal[0];
|
||||
vtx3.normal[1] = normal[1];
|
||||
vtx3.normal[2] = normal[2];
|
||||
|
||||
vertices->push_back(vtx3);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
GLInstanceGraphicsShape* gfxShape = new GLInstanceGraphicsShape;
|
||||
gfxShape->m_vertices = vertices;
|
||||
gfxShape->m_numvertices = vertices->size();
|
||||
gfxShape->m_indices = indicesPtr;
|
||||
gfxShape->m_numIndices = indicesPtr->size();
|
||||
for (int i=0;i<4;i++)
|
||||
gfxShape->m_scaling[i] = 1;//bake the scaling into the vertices
|
||||
return gfxShape;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ConcaveScene::createConcaveMesh(const ConstructionInfo& ci, const char* fileName, const b3Vector3& shift, const b3Vector3& scaling)
|
||||
{
|
||||
objLoader* objData = new objLoader();
|
||||
|
||||
FILE* f = 0;
|
||||
|
||||
char relativeFileName[1024];
|
||||
{
|
||||
const char* prefix[]={"./","../","../../","../../../","../../../../"};
|
||||
int numPrefixes = sizeof(prefix)/sizeof(char*);
|
||||
|
||||
for (int i=0;i<numPrefixes;i++)
|
||||
{
|
||||
|
||||
sprintf(relativeFileName,"%s%s",prefix[i],fileName);
|
||||
f = fopen(relativeFileName,"r");
|
||||
if (f)
|
||||
{
|
||||
fclose(f);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (f)
|
||||
fclose(f);
|
||||
else
|
||||
return;
|
||||
|
||||
objData->load(relativeFileName);
|
||||
int index=10;
|
||||
|
||||
{
|
||||
GLInstanceGraphicsShape* shape = createGraphicsShapeFromWavefrontObj(objData);
|
||||
|
||||
|
||||
b3AlignedObjectArray<b3Vector3> verts;
|
||||
for (int i=0;i<shape->m_numvertices;i++)
|
||||
{
|
||||
for (int j=0;j<3;j++)
|
||||
shape->m_vertices->at(i).xyzw[j] += shift[j];
|
||||
|
||||
b3Vector3 vtx(shape->m_vertices->at(i).xyzw[0],
|
||||
shape->m_vertices->at(i).xyzw[1],
|
||||
shape->m_vertices->at(i).xyzw[2]);
|
||||
verts.push_back(vtx*scaling);
|
||||
}
|
||||
|
||||
int colIndex = m_data->m_np->registerConcaveMesh(&verts,shape->m_indices,b3Vector3(1,1,1));
|
||||
|
||||
{
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
int numVertices = sizeof(cube_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(cube_indices)/sizeof(int);
|
||||
//int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
//int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
|
||||
|
||||
int shapeId = ci.m_instancingRenderer->registerShape(&shape->m_vertices->at(0).xyzw[0], shape->m_numvertices, &shape->m_indices->at(0), shape->m_numIndices);
|
||||
b3Quaternion orn(0,0,0,1);
|
||||
|
||||
b3Vector4 color(0.3,0.3,1,1.f);//0.5);//1.f
|
||||
|
||||
|
||||
{
|
||||
float mass = 0.f;
|
||||
b3Vector3 position(0,0,0);
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index,false);
|
||||
index++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
delete objData;
|
||||
|
||||
}
|
||||
|
||||
void ConcaveScene::setupScene(const ConstructionInfo& ci)
|
||||
{
|
||||
|
||||
if (1)
|
||||
{
|
||||
|
||||
//char* fileName = "data/slopedPlane100.obj";
|
||||
//char* fileName = "data/plane100.obj";
|
||||
char* fileName = "data/plane100.obj";
|
||||
|
||||
//char* fileName = "data/teddy.obj";//"plane.obj";
|
||||
// char* fileName = "data/sponza_closed.obj";//"plane.obj";
|
||||
//char* fileName = "data/leoTest1.obj";
|
||||
// char* fileName = "data/samurai_monastry.obj";
|
||||
// char* fileName = "data/teddy2_VHACD_CHs.obj";
|
||||
|
||||
b3Vector3 shift1(0,-50,0);//0,230,80);//150,-100,-120);
|
||||
|
||||
b3Vector4 scaling(4,4,4,1);
|
||||
|
||||
// createConcaveMesh(ci,"data/plane100.obj",shift1,scaling);
|
||||
//createConcaveMesh(ci,"data/plane100.obj",shift,scaling);
|
||||
|
||||
b3Vector3 shift2(0,0,0);//0,230,80);//150,-100,-120);
|
||||
createConcaveMesh(ci,"data/teddy.obj",shift2,scaling);
|
||||
|
||||
b3Vector3 shift3(130,-150,-75);//0,230,80);//150,-100,-120);
|
||||
createConcaveMesh(ci,"data/leoTest1.obj",shift3,scaling);
|
||||
|
||||
|
||||
} else
|
||||
{
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
int numVertices = sizeof(cube_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(cube_indices)/sizeof(int);
|
||||
int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
int group=1;
|
||||
int mask=1;
|
||||
int index=0;
|
||||
{
|
||||
b3Vector4 scaling(400,0.001,400,1);
|
||||
int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
b3Vector3 position(0,-2,0);
|
||||
b3Quaternion orn(0,0,0,1);
|
||||
|
||||
b3Vector4 color(0,0,1,1);
|
||||
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(0.f,position,orn,colIndex,index,false);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
createDynamicObjects(ci);
|
||||
|
||||
m_data->m_rigidBodyPipeline->writeAllInstancesToGpu();
|
||||
|
||||
float camPos[4]={0,0,0,0};//65.5,4.5,65.5,0};
|
||||
//float camPos[4]={1,12.5,1.5,0};
|
||||
m_instancingRenderer->setCameraPitch(45);
|
||||
m_instancingRenderer->setCameraTargetPosition(camPos);
|
||||
m_instancingRenderer->setCameraDistance(25);
|
||||
|
||||
}
|
||||
|
||||
void ConcaveScene::createDynamicObjects(const ConstructionInfo& ci)
|
||||
{
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
int numVertices = sizeof(cube_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(cube_indices)/sizeof(int);
|
||||
//int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
int group=1;
|
||||
int mask=1;
|
||||
|
||||
|
||||
int index=0;
|
||||
|
||||
|
||||
if (1)
|
||||
{
|
||||
int curColor = 0;
|
||||
b3Vector4 colors[4] =
|
||||
{
|
||||
b3Vector4(1,1,1,1),
|
||||
b3Vector4(1,1,0.3,1),
|
||||
b3Vector4(0.3,1,1,1),
|
||||
b3Vector4(0.3,0.3,1,1),
|
||||
};
|
||||
|
||||
b3Vector4 scaling(1,1,1,1);
|
||||
int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
for (int i=0;i<ci.arraySizeX;i++)
|
||||
{
|
||||
for (int j=0;j<ci.arraySizeY;j++)
|
||||
{
|
||||
for (int k=0;k<ci.arraySizeZ;k++)
|
||||
{
|
||||
float mass = 1;
|
||||
|
||||
//b3Vector3 position(-2*ci.gapX+i*ci.gapX,25+j*ci.gapY,-2*ci.gapZ+k*ci.gapZ);
|
||||
b3Vector3 position(-(ci.arraySizeX/2)*CONCAVE_GAPX+i*CONCAVE_GAPX,150+j*CONCAVE_GAPY,-(ci.arraySizeZ/2)*CONCAVE_GAPZ+k*CONCAVE_GAPZ);
|
||||
b3Quaternion orn(0,0,0,1);
|
||||
|
||||
b3Vector4 color = colors[curColor];
|
||||
curColor++;
|
||||
curColor&=3;
|
||||
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index,false);
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ConcaveCompoundScene::setupScene(const ConstructionInfo& ci)
|
||||
{
|
||||
ConcaveScene::setupScene(ci);
|
||||
|
||||
float camPos[4]={0,50,0,0};//65.5,4.5,65.5,0};
|
||||
//float camPos[4]={1,12.5,1.5,0};
|
||||
m_instancingRenderer->setCameraPitch(45);
|
||||
m_instancingRenderer->setCameraTargetPosition(camPos);
|
||||
m_instancingRenderer->setCameraDistance(40);
|
||||
|
||||
|
||||
}
|
||||
|
||||
void ConcaveCompound2Scene::createDynamicObjects(const ConstructionInfo& ci)
|
||||
{
|
||||
objLoader* objData = new objLoader();
|
||||
|
||||
char* fileName = "data/teddy2_VHACD_CHs.obj";
|
||||
//char* fileName = "data/cube_offset.obj";
|
||||
|
||||
|
||||
b3Vector3 shift(0,0,0);//0,230,80);//150,-100,-120);
|
||||
b3Vector4 scaling(1,1,1,1);
|
||||
FILE* f = 0;
|
||||
|
||||
char relativeFileName[1024];
|
||||
{
|
||||
const char* prefix[]={"./","../","../../","../../../","../../../../"};
|
||||
int numPrefixes = sizeof(prefix)/sizeof(char*);
|
||||
|
||||
for (int i=0;i<numPrefixes;i++)
|
||||
{
|
||||
|
||||
sprintf(relativeFileName,"%s%s",prefix[i],fileName);
|
||||
f = fopen(relativeFileName,"r");
|
||||
if (f)
|
||||
{
|
||||
fclose(f);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (f)
|
||||
fclose(f);
|
||||
else
|
||||
return;
|
||||
|
||||
objData->load(relativeFileName);
|
||||
|
||||
if (objData->objectCount>0)
|
||||
{
|
||||
|
||||
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
|
||||
b3AlignedObjectArray<GLInstanceVertex> vertexArray;
|
||||
b3AlignedObjectArray<int> indexArray;
|
||||
|
||||
|
||||
|
||||
|
||||
//int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
int group=1;
|
||||
int mask=1;
|
||||
int index=0;
|
||||
int colIndex = 0;
|
||||
|
||||
b3AlignedObjectArray<GLInstanceVertex> vertices;
|
||||
int stride2 = sizeof(GLInstanceVertex);
|
||||
b3Assert(stride2 == strideInBytes);
|
||||
|
||||
{
|
||||
|
||||
|
||||
b3AlignedObjectArray<b3GpuChildShape> childShapes;
|
||||
|
||||
int numChildShapes = objData->objectCount;
|
||||
|
||||
for (int i=0;i<numChildShapes;i++)
|
||||
// int i=4;
|
||||
{
|
||||
|
||||
obj_object* object = objData->objectList[i];
|
||||
int numVertices = i==numChildShapes-1? objData->vertexCount-object->vertex_offset : objData->objectList[i+1]->vertex_offset - object->vertex_offset;
|
||||
int numFaces = i==numChildShapes-1? objData->faceCount - object->face_offset : objData->objectList[i+1]->face_offset-object->face_offset;
|
||||
|
||||
|
||||
|
||||
|
||||
//for now, only support polyhedral child shapes
|
||||
b3GpuChildShape child;
|
||||
|
||||
b3Vector3 pos(0,0,0);
|
||||
b3Quaternion orn(0,0,0,1);
|
||||
for (int v=0;v<4;v++)
|
||||
{
|
||||
child.m_childPosition[v] = pos[v];
|
||||
child.m_childOrientation[v] = orn[v];
|
||||
}
|
||||
|
||||
b3Transform tr;
|
||||
tr.setIdentity();
|
||||
tr.setOrigin(pos);
|
||||
tr.setRotation(orn);
|
||||
|
||||
int baseIndex = vertexArray.size();
|
||||
|
||||
for (int f=0;f<numFaces;f++)
|
||||
{
|
||||
obj_face* face = objData->faceList[object->face_offset+f];
|
||||
if (face->vertex_count==3)
|
||||
{
|
||||
for (int i=0;i<3;i++)
|
||||
{
|
||||
indexArray.push_back(face->vertex_index[i]);//-object->vertex_offset);
|
||||
}
|
||||
} else
|
||||
{
|
||||
b3Assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
b3Vector3 center(0,0,0);
|
||||
|
||||
b3AlignedObjectArray<GLInstanceVertex> tmpVertices;
|
||||
//add transformed graphics vertices and indices
|
||||
b3Vector3 myScaling(1,1,1);//50,50,50);//300,300,300);
|
||||
for (int v=0;v<numVertices;v++)
|
||||
{
|
||||
GLInstanceVertex vert;
|
||||
obj_vector* orgVert = objData->vertexList[object->vertex_offset+v];
|
||||
vert.uv[0] = 0.5f;
|
||||
vert.uv[1] = 0.5f;
|
||||
vert.normal[0]=0.f;
|
||||
vert.normal[1]=1.f;
|
||||
vert.normal[2]=0.f;
|
||||
b3Vector3 vertPos;
|
||||
vertPos[0] = orgVert->e[0]*myScaling[0];
|
||||
vertPos[1] = orgVert->e[1]*myScaling[1];
|
||||
vertPos[2] = orgVert->e[2]*myScaling[2];
|
||||
vertPos[3] =0.f;
|
||||
center+=vertPos;
|
||||
}
|
||||
|
||||
center/=numVertices;
|
||||
|
||||
for (int v=0;v<numVertices;v++)
|
||||
{
|
||||
GLInstanceVertex vert;
|
||||
obj_vector* orgVert = objData->vertexList[object->vertex_offset+v];
|
||||
vert.uv[0] = 0.5f;
|
||||
vert.uv[1] = 0.5f;
|
||||
vert.normal[0]=0.f;
|
||||
vert.normal[1]=1.f;
|
||||
vert.normal[2]=0.f;
|
||||
b3Vector3 vertPos;
|
||||
vertPos[0] = orgVert->e[0]*myScaling[0];
|
||||
vertPos[1] = orgVert->e[1]*myScaling[1];
|
||||
vertPos[2] = orgVert->e[2]*myScaling[2];
|
||||
vertPos[3] =0.f;
|
||||
// vertPos-=center;
|
||||
vert.xyzw[0] = vertPos[0];
|
||||
vert.xyzw[1] = vertPos[1];
|
||||
vert.xyzw[2] = vertPos[2];
|
||||
|
||||
tmpVertices.push_back(vert);
|
||||
b3Vector3 newPos = tr*vertPos;
|
||||
vert.xyzw[0] = newPos[0];
|
||||
vert.xyzw[1] = newPos[1];
|
||||
vert.xyzw[2] = newPos[2];
|
||||
vert.xyzw[3] = 0.f;
|
||||
vertexArray.push_back(vert);
|
||||
}
|
||||
|
||||
|
||||
int childColIndex = m_data->m_np->registerConvexHullShape(&tmpVertices[0].xyzw[0],strideInBytes,numVertices, scaling);
|
||||
child.m_shapeIndex = childColIndex;
|
||||
childShapes.push_back(child);
|
||||
colIndex = childColIndex;
|
||||
}
|
||||
colIndex= m_data->m_np->registerCompoundShape(&childShapes);
|
||||
|
||||
}
|
||||
|
||||
//int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
int shapeId = ci.m_instancingRenderer->registerShape(&vertexArray[0].xyzw[0],vertexArray.size(),&indexArray[0],indexArray.size());
|
||||
|
||||
b3Vector4 colors[4] =
|
||||
{
|
||||
b3Vector4(1,0,0,1),
|
||||
b3Vector4(0,1,0,1),
|
||||
b3Vector4(0,0,1,1),
|
||||
b3Vector4(0,1,1,1),
|
||||
};
|
||||
|
||||
int curColor = 0;
|
||||
for (int i=0;i<ci.arraySizeX;i++)
|
||||
{
|
||||
for (int j=0;j<ci.arraySizeY;j++)
|
||||
{
|
||||
for (int k=0;k<ci.arraySizeZ;k++)
|
||||
|
||||
{
|
||||
float mass = 1;//j==0? 0.f : 1.f;
|
||||
|
||||
//b3Vector3 position(i*10*ci.gapX,j*ci.gapY,k*10*ci.gapZ);
|
||||
b3Vector3 position(i*10*ci.gapX,10+j*ci.gapY,k*10*ci.gapZ);
|
||||
|
||||
// b3Quaternion orn(0,0,0,1);
|
||||
b3Quaternion orn(b3Vector3(0,0,1),1.8);
|
||||
|
||||
b3Vector4 color = colors[curColor];
|
||||
curColor++;
|
||||
curColor&=3;
|
||||
b3Vector4 scaling(1,1,1,1);
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index,false);
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
delete objData;
|
||||
}
|
||||
|
||||
void ConcaveCompoundScene::createDynamicObjects(const ConstructionInfo& ci)
|
||||
{
|
||||
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
int numVertices = sizeof(cube_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(cube_indices)/sizeof(int);
|
||||
|
||||
b3AlignedObjectArray<GLInstanceVertex> vertexArray;
|
||||
b3AlignedObjectArray<int> indexArray;
|
||||
|
||||
|
||||
//int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
int group=1;
|
||||
int mask=1;
|
||||
int index=0;
|
||||
float scaling[4] = {1,1,1,1};
|
||||
int colIndex = 0;
|
||||
|
||||
GLInstanceVertex* cubeVerts = (GLInstanceVertex*)&cube_vertices[0];
|
||||
int stride2 = sizeof(GLInstanceVertex);
|
||||
b3Assert(stride2 == strideInBytes);
|
||||
|
||||
{
|
||||
int childColIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
|
||||
|
||||
b3Vector3 childPositions[3] = {
|
||||
b3Vector3(0,-2,0),
|
||||
b3Vector3(0,0,0),
|
||||
b3Vector3(0,0,2)
|
||||
};
|
||||
|
||||
b3AlignedObjectArray<b3GpuChildShape> childShapes;
|
||||
int numChildShapes = 3;
|
||||
for (int i=0;i<numChildShapes;i++)
|
||||
{
|
||||
//for now, only support polyhedral child shapes
|
||||
b3GpuChildShape child;
|
||||
child.m_shapeIndex = childColIndex;
|
||||
b3Vector3 pos = childPositions[i];
|
||||
b3Quaternion orn(0,0,0,1);
|
||||
for (int v=0;v<4;v++)
|
||||
{
|
||||
child.m_childPosition[v] = pos[v];
|
||||
child.m_childOrientation[v] = orn[v];
|
||||
}
|
||||
childShapes.push_back(child);
|
||||
b3Transform tr;
|
||||
tr.setIdentity();
|
||||
tr.setOrigin(pos);
|
||||
tr.setRotation(orn);
|
||||
|
||||
int baseIndex = vertexArray.size();
|
||||
for (int j=0;j<numIndices;j++)
|
||||
indexArray.push_back(cube_indices[j]+baseIndex);
|
||||
|
||||
//add transformed graphics vertices and indices
|
||||
for (int v=0;v<numVertices;v++)
|
||||
{
|
||||
GLInstanceVertex vert = cubeVerts[v];
|
||||
b3Vector3 vertPos(vert.xyzw[0],vert.xyzw[1],vert.xyzw[2]);
|
||||
b3Vector3 newPos = tr*vertPos;
|
||||
vert.xyzw[0] = newPos[0];
|
||||
vert.xyzw[1] = newPos[1];
|
||||
vert.xyzw[2] = newPos[2];
|
||||
vert.xyzw[3] = 0.f;
|
||||
vertexArray.push_back(vert);
|
||||
}
|
||||
|
||||
}
|
||||
colIndex= m_data->m_np->registerCompoundShape(&childShapes);
|
||||
|
||||
}
|
||||
|
||||
//int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
int shapeId = ci.m_instancingRenderer->registerShape(&vertexArray[0].xyzw[0],vertexArray.size(),&indexArray[0],indexArray.size());
|
||||
|
||||
b3Vector4 colors[4] =
|
||||
{
|
||||
b3Vector4(1,0,0,1),
|
||||
b3Vector4(0,1,0,1),
|
||||
b3Vector4(0,0,1,1),
|
||||
b3Vector4(0,1,1,1),
|
||||
};
|
||||
|
||||
int curColor = 0;
|
||||
for (int i=0;i<ci.arraySizeX;i++)
|
||||
{
|
||||
for (int j=0;j<ci.arraySizeY;j++)
|
||||
{
|
||||
for (int k=0;k<ci.arraySizeZ;k++)
|
||||
|
||||
{
|
||||
float mass = 1;//j==0? 0.f : 1.f;
|
||||
|
||||
b3Vector3 position(i*ci.gapX,50+j*ci.gapY,k*ci.gapZ);
|
||||
//b3Quaternion orn(0,0,0,1);
|
||||
b3Quaternion orn(b3Vector3(1,0,0),0.7);
|
||||
|
||||
b3Vector4 color = colors[curColor];
|
||||
curColor++;
|
||||
curColor&=3;
|
||||
b3Vector4 scaling(1,1,1,1);
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index,false);
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ConcaveSphereScene::setupScene(const ConstructionInfo& ci)
|
||||
{
|
||||
ConcaveScene::setupScene(ci);
|
||||
|
||||
float camPos[4]={0,50,0,0};//65.5,4.5,65.5,0};
|
||||
//float camPos[4]={1,12.5,1.5,0};
|
||||
m_instancingRenderer->setCameraPitch(45);
|
||||
m_instancingRenderer->setCameraTargetPosition(camPos);
|
||||
m_instancingRenderer->setCameraDistance(40);
|
||||
|
||||
|
||||
}
|
||||
|
||||
void ConcaveSphereScene::createDynamicObjects(const ConstructionInfo& ci)
|
||||
{
|
||||
b3Vector4 colors[4] =
|
||||
{
|
||||
b3Vector4(1,0,0,1),
|
||||
b3Vector4(0,1,0,1),
|
||||
b3Vector4(0,1,1,1),
|
||||
b3Vector4(1,1,0,1),
|
||||
};
|
||||
|
||||
int index=0;
|
||||
int curColor = 0;
|
||||
float radius = 1;
|
||||
//int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
int colIndex = m_data->m_np->registerSphereShape(radius);//>registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
int prevGraphicsShapeIndex = registerGraphicsSphereShape(ci,radius,false);
|
||||
|
||||
for (int i=0;i<ci.arraySizeX;i++)
|
||||
{
|
||||
for (int j=0;j<ci.arraySizeY;j++)
|
||||
{
|
||||
for (int k=0;k<ci.arraySizeZ;k++)
|
||||
{
|
||||
float mass = 1.f;
|
||||
|
||||
|
||||
b3Vector3 position(-(ci.arraySizeX/2)*8+i*8,50+j*8,-(ci.arraySizeZ/2)*8+k*8);
|
||||
|
||||
//b3Vector3 position(0,-41,0);//0,0,0);//i*radius*3,-41+j*radius*3,k*radius*3);
|
||||
|
||||
b3Quaternion orn(0,0,0,1);
|
||||
|
||||
b3Vector4 color = colors[curColor];
|
||||
curColor++;
|
||||
curColor&=3;
|
||||
b3Vector4 scaling(radius,radius,radius,1);
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(prevGraphicsShapeIndex,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index,false);
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
101
Demos3/GpuDemos/rigidbody/ConcaveScene.h
Normal file
101
Demos3/GpuDemos/rigidbody/ConcaveScene.h
Normal file
@@ -0,0 +1,101 @@
|
||||
#ifndef CONCAVE_SCENE_H
|
||||
#define CONCAVE_SCENE_H
|
||||
|
||||
#include "GpuRigidBodyDemo.h"
|
||||
#include "Bullet3Common/b3Vector3.h"
|
||||
|
||||
class ConcaveScene : public GpuRigidBodyDemo
|
||||
{
|
||||
public:
|
||||
|
||||
ConcaveScene(){}
|
||||
virtual ~ConcaveScene(){}
|
||||
virtual const char* getName()
|
||||
{
|
||||
return "GRBConcave";
|
||||
}
|
||||
|
||||
static GpuDemo* MyCreateFunc()
|
||||
{
|
||||
GpuDemo* demo = new ConcaveScene;
|
||||
return demo;
|
||||
}
|
||||
|
||||
virtual void setupScene(const ConstructionInfo& ci);
|
||||
|
||||
virtual void createDynamicObjects(const ConstructionInfo& ci);
|
||||
|
||||
virtual void createConcaveMesh(const ConstructionInfo& ci, const char* fileName, const b3Vector3& shift, const b3Vector3& scaling);
|
||||
|
||||
};
|
||||
|
||||
|
||||
class ConcaveSphereScene : public ConcaveScene
|
||||
{
|
||||
public:
|
||||
|
||||
ConcaveSphereScene(){}
|
||||
virtual ~ConcaveSphereScene(){}
|
||||
virtual const char* getName()
|
||||
{
|
||||
return "ConcaveSphere";
|
||||
}
|
||||
|
||||
static GpuDemo* MyCreateFunc()
|
||||
{
|
||||
GpuDemo* demo = new ConcaveSphereScene;
|
||||
return demo;
|
||||
}
|
||||
|
||||
virtual void setupScene(const ConstructionInfo& ci);
|
||||
|
||||
virtual void createDynamicObjects(const ConstructionInfo& ci);
|
||||
|
||||
};
|
||||
|
||||
|
||||
class ConcaveCompoundScene : public ConcaveScene
|
||||
{
|
||||
public:
|
||||
|
||||
ConcaveCompoundScene(){}
|
||||
virtual ~ConcaveCompoundScene(){}
|
||||
virtual const char* getName()
|
||||
{
|
||||
return "GRBConcaveCompound";
|
||||
}
|
||||
|
||||
static GpuDemo* MyCreateFunc()
|
||||
{
|
||||
GpuDemo* demo = new ConcaveCompoundScene;
|
||||
return demo;
|
||||
}
|
||||
|
||||
virtual void setupScene(const ConstructionInfo& ci);
|
||||
|
||||
virtual void createDynamicObjects(const ConstructionInfo& ci);
|
||||
|
||||
};
|
||||
|
||||
|
||||
class ConcaveCompound2Scene : public ConcaveCompoundScene
|
||||
{
|
||||
public:
|
||||
|
||||
ConcaveCompound2Scene(){}
|
||||
virtual ~ConcaveCompound2Scene(){}
|
||||
virtual const char* getName()
|
||||
{
|
||||
return "GRBConcave2Compound";
|
||||
}
|
||||
|
||||
static GpuDemo* MyCreateFunc()
|
||||
{
|
||||
GpuDemo* demo = new ConcaveCompound2Scene;
|
||||
return demo;
|
||||
}
|
||||
virtual void createDynamicObjects(const ConstructionInfo& ci);
|
||||
};
|
||||
|
||||
|
||||
#endif //CONCAVE_SCENE_H
|
||||
254
Demos3/GpuDemos/rigidbody/GpuCompoundScene.cpp
Normal file
254
Demos3/GpuDemos/rigidbody/GpuCompoundScene.cpp
Normal file
@@ -0,0 +1,254 @@
|
||||
#include "GpuCompoundScene.h"
|
||||
#include "GpuRigidBodyDemo.h"
|
||||
#include "Bullet3Common/b3Quickprof.h"
|
||||
#include "OpenGLWindow/ShapeData.h"
|
||||
|
||||
#include "OpenGLWindow/GLInstancingRenderer.h"
|
||||
#include "Bullet3Common/b3Quaternion.h"
|
||||
#include "OpenGLWindow/b3gWindowInterface.h"
|
||||
#include "Bullet3OpenCL/BroadphaseCollision/b3GpuSapBroadphase.h"
|
||||
#include "../GpuDemoInternalData.h"
|
||||
#include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
|
||||
#include "OpenGLWindow/OpenGLInclude.h"
|
||||
#include "OpenGLWindow/GLInstanceRendererInternalData.h"
|
||||
#include "Bullet3OpenCL/ParallelPrimitives/b3LauncherCL.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3GpuRigidBodyPipeline.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3GpuNarrowPhase.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3Config.h"
|
||||
#include "GpuRigidBodyDemoInternalData.h"
|
||||
#include "Bullet3Common/b3Transform.h"
|
||||
|
||||
#include "OpenGLWindow/GLInstanceGraphicsShape.h"
|
||||
|
||||
|
||||
|
||||
void GpuCompoundScene::setupScene(const ConstructionInfo& ci)
|
||||
{
|
||||
|
||||
createStaticEnvironment(ci);
|
||||
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
int numVertices = sizeof(cube_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(cube_indices)/sizeof(int);
|
||||
float scaling[4] = {1,1,1,1};
|
||||
|
||||
GLInstanceVertex* cubeVerts = (GLInstanceVertex*)&cube_vertices[0];
|
||||
int stride2 = sizeof(GLInstanceVertex);
|
||||
b3Assert(stride2 == strideInBytes);
|
||||
int index=0;
|
||||
int colIndex = -1;
|
||||
b3AlignedObjectArray<GLInstanceVertex> vertexArray;
|
||||
b3AlignedObjectArray<int> indexArray;
|
||||
{
|
||||
int childColIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
|
||||
|
||||
b3Vector3 childPositions[3] = {
|
||||
b3Vector3(0,-2,0),
|
||||
b3Vector3(0,0,0),
|
||||
b3Vector3(0,0,2)
|
||||
};
|
||||
|
||||
|
||||
b3AlignedObjectArray<b3GpuChildShape> childShapes;
|
||||
int numChildShapes = 3;
|
||||
for (int i=0;i<numChildShapes;i++)
|
||||
{
|
||||
//for now, only support polyhedral child shapes
|
||||
b3GpuChildShape child;
|
||||
child.m_shapeIndex = childColIndex;
|
||||
b3Vector3 pos = childPositions[i];
|
||||
b3Quaternion orn(0,0,0,1);
|
||||
for (int v=0;v<4;v++)
|
||||
{
|
||||
child.m_childPosition[v] = pos[v];
|
||||
child.m_childOrientation[v] = orn[v];
|
||||
}
|
||||
childShapes.push_back(child);
|
||||
b3Transform tr;
|
||||
tr.setIdentity();
|
||||
tr.setOrigin(pos);
|
||||
tr.setRotation(orn);
|
||||
|
||||
int baseIndex = vertexArray.size();
|
||||
for (int j=0;j<numIndices;j++)
|
||||
indexArray.push_back(cube_indices[j]+baseIndex);
|
||||
|
||||
//add transformed graphics vertices and indices
|
||||
for (int v=0;v<numVertices;v++)
|
||||
{
|
||||
GLInstanceVertex vert = cubeVerts[v];
|
||||
b3Vector3 vertPos(vert.xyzw[0],vert.xyzw[1],vert.xyzw[2]);
|
||||
b3Vector3 newPos = tr*vertPos;
|
||||
vert.xyzw[0] = newPos[0];
|
||||
vert.xyzw[1] = newPos[1];
|
||||
vert.xyzw[2] = newPos[2];
|
||||
vert.xyzw[3] = 0.f;
|
||||
vertexArray.push_back(vert);
|
||||
}
|
||||
|
||||
}
|
||||
colIndex= m_data->m_np->registerCompoundShape(&childShapes);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
int shapeId = ci.m_instancingRenderer->registerShape(&vertexArray[0].xyzw[0],vertexArray.size(),&indexArray[0],indexArray.size());
|
||||
|
||||
b3Vector4 colors[4] =
|
||||
{
|
||||
b3Vector4(1,0,0,1),
|
||||
b3Vector4(0,1,0,1),
|
||||
b3Vector4(0,0,1,1),
|
||||
b3Vector4(0,1,1,1),
|
||||
};
|
||||
|
||||
int curColor = 0;
|
||||
for (int i=0;i<ci.arraySizeX;i++)
|
||||
{
|
||||
for (int j=0;j<ci.arraySizeY;j++)
|
||||
{
|
||||
for (int k=0;k<ci.arraySizeZ;k++)
|
||||
{
|
||||
float mass = 1;//j==0? 0.f : 1.f;
|
||||
|
||||
b3Vector3 position(i*ci.gapX,10+j*ci.gapY,k*ci.gapZ);
|
||||
//b3Quaternion orn(0,0,0,1);
|
||||
b3Quaternion orn(b3Vector3(1,0,0),0.7);
|
||||
|
||||
b3Vector4 color = colors[curColor];
|
||||
curColor++;
|
||||
curColor&=3;
|
||||
b3Vector4 scaling(1,1,1,1);
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index,false);
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_data->m_rigidBodyPipeline->writeAllInstancesToGpu();
|
||||
|
||||
|
||||
float camPos[4]={0,0,0};//65.5,4.5,65.5,0};
|
||||
//float camPos[4]={1,12.5,1.5,0};
|
||||
m_instancingRenderer->setCameraTargetPosition(camPos);
|
||||
m_instancingRenderer->setCameraDistance(20);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void GpuCompoundScene::createStaticEnvironment(const ConstructionInfo& ci)
|
||||
{
|
||||
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
|
||||
|
||||
|
||||
|
||||
//int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
int group=1;
|
||||
int mask=1;
|
||||
int index=0;
|
||||
int colIndex = 0;
|
||||
|
||||
{
|
||||
if (1)
|
||||
{
|
||||
float radius = 41;
|
||||
int prevGraphicsShapeIndex = -1;
|
||||
{
|
||||
|
||||
|
||||
|
||||
if (radius>=100)
|
||||
{
|
||||
int numVertices = sizeof(detailed_sphere_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(detailed_sphere_indices)/sizeof(int);
|
||||
prevGraphicsShapeIndex = ci.m_instancingRenderer->registerShape(&detailed_sphere_vertices[0],numVertices,detailed_sphere_indices,numIndices);
|
||||
} else
|
||||
{
|
||||
bool usePointSprites = false;
|
||||
if (usePointSprites)
|
||||
{
|
||||
int numVertices = sizeof(point_sphere_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(point_sphere_indices)/sizeof(int);
|
||||
prevGraphicsShapeIndex = ci.m_instancingRenderer->registerShape(&point_sphere_vertices[0],numVertices,point_sphere_indices,numIndices,B3_GL_POINTS);
|
||||
} else
|
||||
{
|
||||
if (radius>=10)
|
||||
{
|
||||
int numVertices = sizeof(medium_sphere_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(medium_sphere_indices)/sizeof(int);
|
||||
prevGraphicsShapeIndex = ci.m_instancingRenderer->registerShape(&medium_sphere_vertices[0],numVertices,medium_sphere_indices,numIndices);
|
||||
} else
|
||||
{
|
||||
int numVertices = sizeof(low_sphere_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(low_sphere_indices)/sizeof(int);
|
||||
prevGraphicsShapeIndex = ci.m_instancingRenderer->registerShape(&low_sphere_vertices[0],numVertices,low_sphere_indices,numIndices);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
b3Vector4 colors[4] =
|
||||
{
|
||||
b3Vector4(1,0,0,1),
|
||||
b3Vector4(0,1,0,1),
|
||||
b3Vector4(0,1,1,1),
|
||||
b3Vector4(1,1,0,1),
|
||||
};
|
||||
|
||||
int curColor = 1;
|
||||
|
||||
//int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
int colIndex = m_data->m_np->registerSphereShape(radius);//>registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
float mass = 0.f;
|
||||
|
||||
//b3Vector3 position((j&1)+i*2.2,1+j*2.,(j&1)+k*2.2);
|
||||
b3Vector3 position(0,-41,0);
|
||||
|
||||
|
||||
b3Quaternion orn(0,0,0,1);
|
||||
|
||||
b3Vector4 color = colors[curColor];
|
||||
curColor++;
|
||||
curColor&=3;
|
||||
b3Vector4 scaling(radius,radius,radius,1);
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(prevGraphicsShapeIndex,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index,false);
|
||||
|
||||
index++;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GpuCompoundPlaneScene::createStaticEnvironment(const ConstructionInfo& ci)
|
||||
{
|
||||
|
||||
int index=0;
|
||||
b3Vector3 normal(0,1,0);
|
||||
float constant=0.f;
|
||||
int colIndex = m_data->m_np->registerPlaneShape(normal,constant);//>registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
b3Vector3 position(0,0,0);
|
||||
b3Quaternion orn(0,0,0,1);
|
||||
// b3Quaternion orn(b3Vector3(1,0,0),0.3);
|
||||
b3Vector4 color(0,0,1,1);
|
||||
b3Vector4 scaling(100,0.01,100,1);
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
int numVertices = sizeof(cube_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(cube_indices)/sizeof(int);
|
||||
int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
|
||||
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(0.f,position,orn,colIndex,index,false);
|
||||
}
|
||||
50
Demos3/GpuDemos/rigidbody/GpuCompoundScene.h
Normal file
50
Demos3/GpuDemos/rigidbody/GpuCompoundScene.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef GPU_COMPOUND_SCENE_H
|
||||
#define GPU_COMPOUND_SCENE_H
|
||||
|
||||
#include "GpuRigidBodyDemo.h"
|
||||
|
||||
class GpuCompoundScene : public GpuRigidBodyDemo
|
||||
{
|
||||
public:
|
||||
|
||||
GpuCompoundScene(){}
|
||||
virtual ~GpuCompoundScene(){}
|
||||
virtual const char* getName()
|
||||
{
|
||||
return "GpuCompound";
|
||||
}
|
||||
|
||||
static GpuDemo* MyCreateFunc()
|
||||
{
|
||||
GpuDemo* demo = new GpuCompoundScene;
|
||||
return demo;
|
||||
}
|
||||
|
||||
virtual void setupScene(const ConstructionInfo& ci);
|
||||
|
||||
virtual void createStaticEnvironment(const ConstructionInfo& ci);
|
||||
|
||||
};
|
||||
|
||||
|
||||
class GpuCompoundPlaneScene : public GpuCompoundScene
|
||||
{
|
||||
public:
|
||||
|
||||
GpuCompoundPlaneScene(){}
|
||||
virtual ~GpuCompoundPlaneScene(){}
|
||||
virtual const char* getName()
|
||||
{
|
||||
return "GpuCompoundPlane";
|
||||
}
|
||||
|
||||
static GpuDemo* MyCreateFunc()
|
||||
{
|
||||
GpuDemo* demo = new GpuCompoundPlaneScene;
|
||||
return demo;
|
||||
}
|
||||
|
||||
virtual void createStaticEnvironment(const ConstructionInfo& ci);
|
||||
|
||||
};
|
||||
#endif //GPU_COMPOUND_SCENE_H
|
||||
176
Demos3/GpuDemos/rigidbody/GpuConvexScene.cpp
Normal file
176
Demos3/GpuDemos/rigidbody/GpuConvexScene.cpp
Normal file
@@ -0,0 +1,176 @@
|
||||
#include "GpuConvexScene.h"
|
||||
#include "GpuRigidBodyDemo.h"
|
||||
#include "Bullet3Common/b3Quickprof.h"
|
||||
#include "OpenGLWindow/ShapeData.h"
|
||||
|
||||
#include "OpenGLWindow/GLInstancingRenderer.h"
|
||||
#include "Bullet3Common/b3Quaternion.h"
|
||||
#include "OpenGLWindow/b3gWindowInterface.h"
|
||||
#include "Bullet3OpenCL/BroadphaseCollision/b3GpuSapBroadphase.h"
|
||||
#include "../GpuDemoInternalData.h"
|
||||
#include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
|
||||
#include "OpenGLWindow/OpenGLInclude.h"
|
||||
#include "OpenGLWindow/GLInstanceRendererInternalData.h"
|
||||
#include "Bullet3OpenCL/ParallelPrimitives/b3LauncherCL.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3GpuRigidBodyPipeline.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3GpuNarrowPhase.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3Config.h"
|
||||
#include "GpuRigidBodyDemoInternalData.h"
|
||||
#include "../gwenUserInterface.h"
|
||||
#include "Bullet3Dynamics/ConstraintSolver/b3Point2PointConstraint.h"
|
||||
|
||||
void GpuConvexScene::setupScene(const ConstructionInfo& ci)
|
||||
{
|
||||
|
||||
int index=0;
|
||||
createStaticEnvironment(ci);
|
||||
|
||||
index+=createDynamicsObjects(ci);
|
||||
|
||||
m_data->m_rigidBodyPipeline->writeAllInstancesToGpu();
|
||||
|
||||
|
||||
float camPos[4]={ci.arraySizeX,ci.arraySizeY/2,ci.arraySizeZ,0};
|
||||
//float camPos[4]={1,12.5,1.5,0};
|
||||
m_instancingRenderer->setCameraTargetPosition(camPos);
|
||||
m_instancingRenderer->setCameraDistance(40);
|
||||
|
||||
|
||||
char msg[1024];
|
||||
int numInstances = index;
|
||||
sprintf(msg,"Num objects = %d",numInstances);
|
||||
ci.m_gui->setStatusBarMessage(msg,true);
|
||||
}
|
||||
|
||||
int GpuConvexScene::createDynamicsObjects(const ConstructionInfo& ci)
|
||||
{
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
int numVertices = sizeof(barrel_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(barrel_indices)/sizeof(int);
|
||||
return createDynamicsObjects2(ci,barrel_vertices,numVertices,barrel_indices,numIndices);
|
||||
}
|
||||
|
||||
int GpuBoxPlaneScene::createDynamicsObjects(const ConstructionInfo& ci)
|
||||
{
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
int numVertices = sizeof(cube_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(cube_indices)/sizeof(int);
|
||||
return createDynamicsObjects2(ci,cube_vertices,numVertices,cube_indices,numIndices);
|
||||
}
|
||||
|
||||
|
||||
int GpuConvexScene::createDynamicsObjects2(const ConstructionInfo& ci, const float* vertices, int numVertices, const int* indices, int numIndices)
|
||||
{
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
|
||||
int shapeId = ci.m_instancingRenderer->registerShape(&vertices[0],numVertices,indices,numIndices);
|
||||
int group=1;
|
||||
int mask=1;
|
||||
int index=0;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
{
|
||||
b3Vector4 colors[4] =
|
||||
{
|
||||
b3Vector4(1,0,0,1),
|
||||
b3Vector4(0,1,0,1),
|
||||
b3Vector4(0,1,1,1),
|
||||
b3Vector4(1,1,0,1),
|
||||
};
|
||||
|
||||
int curColor = 0;
|
||||
float scaling[4] = {1,1,1,1};
|
||||
int prevBody = -1;
|
||||
int insta = 0;
|
||||
|
||||
int colIndex = m_data->m_np->registerConvexHullShape(&vertices[0],strideInBytes,numVertices, scaling);
|
||||
//int colIndex = m_data->m_np->registerSphereShape(1);
|
||||
for (int i=0;i<ci.arraySizeX;i++)
|
||||
{
|
||||
for (int j=0;j<ci.arraySizeY;j++)
|
||||
{
|
||||
for (int k=0;k<ci.arraySizeZ;k++)
|
||||
{
|
||||
float mass = 1.f;
|
||||
if (j==0)//ci.arraySizeY-1)
|
||||
{
|
||||
//mass=0.f;
|
||||
}
|
||||
//b3Vector3 position((j&1)+i*2.2,1+j*2.,(j&1)+k*2.2);
|
||||
b3Vector3 position(i*2.2,10+j*2.2,k*2.2);
|
||||
|
||||
b3Quaternion orn(0,0,0,1);
|
||||
|
||||
b3Vector4 color = colors[curColor];
|
||||
curColor++;
|
||||
curColor&=3;
|
||||
b3Vector4 scaling(1,1,1,1);
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index,false);
|
||||
|
||||
|
||||
if (prevBody>=0)
|
||||
{
|
||||
b3Point2PointConstraint* p2p = new b3Point2PointConstraint(pid,prevBody,b3Vector3(0,-1.1,0),b3Vector3(0,1.1,0));
|
||||
// m_data->m_rigidBodyPipeline->addConstraint(p2p);//,false);
|
||||
}
|
||||
prevBody = pid;
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
|
||||
void GpuConvexScene::createStaticEnvironment(const ConstructionInfo& ci)
|
||||
{
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
int numVertices = sizeof(cube_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(cube_indices)/sizeof(int);
|
||||
int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
int group=1;
|
||||
int mask=1;
|
||||
int index=0;
|
||||
|
||||
|
||||
{
|
||||
b3Vector4 scaling(400,1,400,1);
|
||||
int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
b3Vector3 position(0,0,0);
|
||||
b3Quaternion orn(0,0,0,1);
|
||||
|
||||
b3Vector4 color(0,0,1,1);
|
||||
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(0.f,position,orn,colIndex,index,false);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void GpuConvexPlaneScene::createStaticEnvironment(const ConstructionInfo& ci)
|
||||
{
|
||||
int index=0;
|
||||
b3Vector3 normal(0,1,0);
|
||||
float constant=0.f;
|
||||
int colIndex = m_data->m_np->registerPlaneShape(normal,constant);//>registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
b3Vector3 position(0,0,0);
|
||||
b3Quaternion orn(0,0,0,1);
|
||||
// b3Quaternion orn(b3Vector3(1,0,0),0.3);
|
||||
b3Vector4 color(0,0,1,1);
|
||||
b3Vector4 scaling(100,0.001,100,1);
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
int numVertices = sizeof(cube_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(cube_indices)/sizeof(int);
|
||||
int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
|
||||
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(0.f,position,orn,colIndex,index,false);
|
||||
|
||||
}
|
||||
78
Demos3/GpuDemos/rigidbody/GpuConvexScene.h
Normal file
78
Demos3/GpuDemos/rigidbody/GpuConvexScene.h
Normal file
@@ -0,0 +1,78 @@
|
||||
#ifndef GPU_CONVEX_SCENE_H
|
||||
#define GPU_CONVEX_SCENE_H
|
||||
|
||||
#include "GpuRigidBodyDemo.h"
|
||||
|
||||
class GpuConvexScene : public GpuRigidBodyDemo
|
||||
{
|
||||
public:
|
||||
|
||||
GpuConvexScene(){}
|
||||
virtual ~GpuConvexScene(){}
|
||||
virtual const char* getName()
|
||||
{
|
||||
return "GRBConvex";
|
||||
}
|
||||
|
||||
static GpuDemo* MyCreateFunc()
|
||||
{
|
||||
GpuDemo* demo = new GpuConvexScene;
|
||||
return demo;
|
||||
}
|
||||
|
||||
virtual void setupScene(const ConstructionInfo& ci);
|
||||
|
||||
virtual int createDynamicsObjects(const ConstructionInfo& ci);
|
||||
|
||||
virtual int createDynamicsObjects2(const ConstructionInfo& ci,const float* vertices, int numVertices, const int* indices,int numIndices);
|
||||
|
||||
virtual void createStaticEnvironment(const ConstructionInfo& ci);
|
||||
|
||||
};
|
||||
|
||||
|
||||
class GpuConvexPlaneScene : public GpuConvexScene
|
||||
{
|
||||
public:
|
||||
|
||||
GpuConvexPlaneScene(){}
|
||||
virtual ~GpuConvexPlaneScene(){}
|
||||
virtual const char* getName()
|
||||
{
|
||||
return "GRBConvexPlane";
|
||||
}
|
||||
|
||||
static GpuDemo* MyCreateFunc()
|
||||
{
|
||||
GpuDemo* demo = new GpuConvexPlaneScene;
|
||||
return demo;
|
||||
}
|
||||
|
||||
virtual void createStaticEnvironment(const ConstructionInfo& ci);
|
||||
|
||||
};
|
||||
|
||||
|
||||
class GpuBoxPlaneScene : public GpuConvexPlaneScene
|
||||
{
|
||||
public:
|
||||
|
||||
GpuBoxPlaneScene(){}
|
||||
virtual ~GpuBoxPlaneScene(){}
|
||||
virtual const char* getName()
|
||||
{
|
||||
return "GRBBoxPlane";
|
||||
}
|
||||
|
||||
static GpuDemo* MyCreateFunc()
|
||||
{
|
||||
GpuDemo* demo = new GpuBoxPlaneScene;
|
||||
return demo;
|
||||
}
|
||||
|
||||
virtual int createDynamicsObjects(const ConstructionInfo& ci);
|
||||
|
||||
|
||||
|
||||
};
|
||||
#endif //GPU_CONVEX_SCENE_H
|
||||
218
Demos3/GpuDemos/rigidbody/GpuRigidBodyDemo.cpp
Normal file
218
Demos3/GpuDemos/rigidbody/GpuRigidBodyDemo.cpp
Normal file
@@ -0,0 +1,218 @@
|
||||
#include "GpuRigidBodyDemo.h"
|
||||
#include "Bullet3Common/b3Quickprof.h"
|
||||
#include "OpenGLWindow/ShapeData.h"
|
||||
#include "OpenGLWindow/GLInstancingRenderer.h"
|
||||
#include "Bullet3Common/b3Quaternion.h"
|
||||
#include "OpenGLWindow/b3gWindowInterface.h"
|
||||
#include "Bullet3OpenCL/BroadphaseCollision/b3GpuSapBroadphase.h"
|
||||
#include "../GpuDemoInternalData.h"
|
||||
#include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
|
||||
#include "OpenGLWindow/OpenGLInclude.h"
|
||||
#include "OpenGLWindow/GLInstanceRendererInternalData.h"
|
||||
#include "Bullet3OpenCL/ParallelPrimitives/b3LauncherCL.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3GpuRigidBodyPipeline.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3GpuNarrowPhase.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3Config.h"
|
||||
#include "GpuRigidBodyDemoInternalData.h"
|
||||
#include "Bullet3Collision/BroadPhaseCollision/b3DynamicBvhBroadphase.h"
|
||||
|
||||
static b3KeyboardCallback oldCallback = 0;
|
||||
extern bool gReset;
|
||||
|
||||
#define MSTRINGIFY(A) #A
|
||||
|
||||
static const char* s_rigidBodyKernelString = MSTRINGIFY(
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float4 m_pos;
|
||||
float4 m_quat;
|
||||
float4 m_linVel;
|
||||
float4 m_angVel;
|
||||
unsigned int m_collidableIdx;
|
||||
float m_invMass;
|
||||
float m_restituitionCoeff;
|
||||
float m_frictionCoeff;
|
||||
} Body;
|
||||
|
||||
__kernel void
|
||||
copyTransformsToVBOKernel( __global Body* gBodies, __global float4* posOrnColor, const int numNodes)
|
||||
{
|
||||
int nodeID = get_global_id(0);
|
||||
if( nodeID < numNodes )
|
||||
{
|
||||
posOrnColor[nodeID] = (float4) (gBodies[nodeID].m_pos.xyz,1.0);
|
||||
posOrnColor[nodeID + numNodes] = gBodies[nodeID].m_quat;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
GpuRigidBodyDemo::GpuRigidBodyDemo()
|
||||
:m_instancingRenderer(0),
|
||||
m_window(0)
|
||||
{
|
||||
m_data = new GpuRigidBodyDemoInternalData;
|
||||
}
|
||||
GpuRigidBodyDemo::~GpuRigidBodyDemo()
|
||||
{
|
||||
|
||||
delete m_data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static void PairKeyboardCallback(int key, int state)
|
||||
{
|
||||
if (key=='R' && state)
|
||||
{
|
||||
gReset = true;
|
||||
}
|
||||
|
||||
//b3DefaultKeyboardCallback(key,state);
|
||||
oldCallback(key,state);
|
||||
}
|
||||
|
||||
void GpuRigidBodyDemo::setupScene(const ConstructionInfo& ci)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void GpuRigidBodyDemo::initPhysics(const ConstructionInfo& ci)
|
||||
{
|
||||
|
||||
if (ci.m_window)
|
||||
{
|
||||
m_window = ci.m_window;
|
||||
oldCallback = ci.m_window->getKeyboardCallback();
|
||||
ci.m_window->setKeyboardCallback(PairKeyboardCallback);
|
||||
|
||||
}
|
||||
|
||||
m_instancingRenderer = ci.m_instancingRenderer;
|
||||
|
||||
initCL(ci.preferredOpenCLDeviceIndex,ci.preferredOpenCLPlatformIndex);
|
||||
|
||||
if (m_clData->m_clContext)
|
||||
{
|
||||
int errNum=0;
|
||||
|
||||
cl_program rbProg=0;
|
||||
m_data->m_copyTransformsToVBOKernel = b3OpenCLUtils::compileCLKernelFromString(m_clData->m_clContext,m_clData->m_clDevice,s_rigidBodyKernelString,"copyTransformsToVBOKernel",&errNum,rbProg);
|
||||
|
||||
b3Config config;
|
||||
b3GpuNarrowPhase* np = new b3GpuNarrowPhase(m_clData->m_clContext,m_clData->m_clDevice,m_clData->m_clQueue,config);
|
||||
b3GpuSapBroadphase* bp = new b3GpuSapBroadphase(m_clData->m_clContext,m_clData->m_clDevice,m_clData->m_clQueue);
|
||||
m_data->m_np = np;
|
||||
m_data->m_bp = bp;
|
||||
b3DynamicBvhBroadphase* broadphaseDbvt = new b3DynamicBvhBroadphase(config.m_maxConvexBodies);
|
||||
|
||||
m_data->m_rigidBodyPipeline = new b3GpuRigidBodyPipeline(m_clData->m_clContext,m_clData->m_clDevice,m_clData->m_clQueue, np, bp,broadphaseDbvt);
|
||||
|
||||
|
||||
setupScene(ci);
|
||||
|
||||
np->writeAllBodiesToGpu();
|
||||
bp->writeAabbsToGpu();
|
||||
}
|
||||
|
||||
|
||||
m_instancingRenderer->writeTransforms();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void GpuRigidBodyDemo::exitPhysics()
|
||||
{
|
||||
delete m_data->m_instancePosOrnColor;
|
||||
delete m_data->m_rigidBodyPipeline;
|
||||
|
||||
m_window->setKeyboardCallback(oldCallback);
|
||||
|
||||
delete m_data->m_np;
|
||||
m_data->m_np = 0;
|
||||
delete m_data->m_bp;
|
||||
m_data->m_bp = 0;
|
||||
|
||||
exitCL();
|
||||
}
|
||||
|
||||
|
||||
void GpuRigidBodyDemo::renderScene()
|
||||
{
|
||||
m_instancingRenderer->RenderScene();
|
||||
}
|
||||
|
||||
void GpuRigidBodyDemo::clientMoveAndDisplay()
|
||||
{
|
||||
bool animate=true;
|
||||
int numObjects= m_data->m_rigidBodyPipeline->getNumBodies();
|
||||
//m_instancingRenderer->getInternalData()->m_totalNumInstances;
|
||||
b3Vector4* positions = 0;
|
||||
if (animate && numObjects)
|
||||
{
|
||||
B3_PROFILE("gl2cl");
|
||||
|
||||
if (!m_data->m_instancePosOrnColor)
|
||||
{
|
||||
GLuint vbo = m_instancingRenderer->getInternalData()->m_vbo;
|
||||
int arraySizeInBytes = numObjects * (3)*sizeof(b3Vector4);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
cl_bool blocking= CL_TRUE;
|
||||
positions= (b3Vector4*)glMapBufferRange( GL_ARRAY_BUFFER,m_instancingRenderer->getMaxShapeCapacity(),arraySizeInBytes, GL_MAP_READ_BIT );//GL_READ_WRITE);//GL_WRITE_ONLY
|
||||
GLint err = glGetError();
|
||||
assert(err==GL_NO_ERROR);
|
||||
m_data->m_instancePosOrnColor = new b3OpenCLArray<b3Vector4>(m_clData->m_clContext,m_clData->m_clQueue);
|
||||
m_data->m_instancePosOrnColor->resize(3*numObjects);
|
||||
m_data->m_instancePosOrnColor->copyFromHostPointer(positions,3*numObjects,0);
|
||||
glUnmapBuffer( GL_ARRAY_BUFFER);
|
||||
err = glGetError();
|
||||
assert(err==GL_NO_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
B3_PROFILE("stepSimulation");
|
||||
m_data->m_rigidBodyPipeline->stepSimulation(1./60.f);
|
||||
}
|
||||
|
||||
if (numObjects)
|
||||
{
|
||||
B3_PROFILE("cl2gl_convert");
|
||||
int ciErrNum = 0;
|
||||
cl_mem bodies = m_data->m_rigidBodyPipeline->getBodyBuffer();
|
||||
b3LauncherCL launch(m_clData->m_clQueue,m_data->m_copyTransformsToVBOKernel);
|
||||
launch.setBuffer(bodies);
|
||||
launch.setBuffer(m_data->m_instancePosOrnColor->getBufferCL());
|
||||
launch.setConst(numObjects);
|
||||
launch.launch1D(numObjects);
|
||||
oclCHECKERROR(ciErrNum, CL_SUCCESS);
|
||||
}
|
||||
|
||||
if (animate && numObjects)
|
||||
{
|
||||
B3_PROFILE("cl2gl_upload");
|
||||
GLint err = glGetError();
|
||||
assert(err==GL_NO_ERROR);
|
||||
GLuint vbo = m_instancingRenderer->getInternalData()->m_vbo;
|
||||
int arraySizeInBytes = numObjects * (3)*sizeof(b3Vector4);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
cl_bool blocking= CL_TRUE;
|
||||
positions= (b3Vector4*)glMapBufferRange( GL_ARRAY_BUFFER,m_instancingRenderer->getMaxShapeCapacity(),arraySizeInBytes, GL_MAP_WRITE_BIT );//GL_READ_WRITE);//GL_WRITE_ONLY
|
||||
err = glGetError();
|
||||
assert(err==GL_NO_ERROR);
|
||||
m_data->m_instancePosOrnColor->copyToHostPointer(positions,3*numObjects,0);
|
||||
glUnmapBuffer( GL_ARRAY_BUFFER);
|
||||
err = glGetError();
|
||||
assert(err==GL_NO_ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
45
Demos3/GpuDemos/rigidbody/GpuRigidBodyDemo.h
Normal file
45
Demos3/GpuDemos/rigidbody/GpuRigidBodyDemo.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef GPU_RIGID_BODY_DEMO_H
|
||||
#define GPU_RIGID_BODY_DEMO_H
|
||||
|
||||
#include "../GpuDemo.h"
|
||||
|
||||
class GpuRigidBodyDemo : public GpuDemo
|
||||
{
|
||||
protected:
|
||||
class GLInstancingRenderer* m_instancingRenderer;
|
||||
class b3gWindowInterface* m_window;
|
||||
|
||||
struct GpuRigidBodyDemoInternalData* m_data;
|
||||
|
||||
public:
|
||||
|
||||
GpuRigidBodyDemo();
|
||||
virtual ~GpuRigidBodyDemo();
|
||||
|
||||
virtual void initPhysics(const ConstructionInfo& ci);
|
||||
|
||||
virtual void setupScene(const ConstructionInfo& ci);
|
||||
|
||||
virtual void destroyScene(){};
|
||||
|
||||
virtual void exitPhysics();
|
||||
|
||||
virtual const char* getName()
|
||||
{
|
||||
return "GRBD";
|
||||
}
|
||||
static GpuDemo* MyCreateFunc()
|
||||
{
|
||||
GpuDemo* demo = new GpuRigidBodyDemo;
|
||||
return demo;
|
||||
}
|
||||
|
||||
virtual void renderScene();
|
||||
|
||||
virtual void clientMoveAndDisplay();
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif //GPU_RIGID_BODY_DEMO_H
|
||||
|
||||
30
Demos3/GpuDemos/rigidbody/GpuRigidBodyDemoInternalData.h
Normal file
30
Demos3/GpuDemos/rigidbody/GpuRigidBodyDemoInternalData.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef GPU_RIGIDBODY_INTERNAL_DATA_H
|
||||
#define GPU_RIGIDBODY_INTERNAL_DATA_H
|
||||
|
||||
#include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
|
||||
#include "Bullet3OpenCL/ParallelPrimitives/b3OpenCLArray.h"
|
||||
#include "Bullet3Common/b3Vector3.h"
|
||||
|
||||
struct GpuRigidBodyDemoInternalData
|
||||
{
|
||||
|
||||
cl_kernel m_copyTransformsToVBOKernel;
|
||||
|
||||
b3OpenCLArray<b3Vector4>* m_instancePosOrnColor;
|
||||
|
||||
class b3GpuRigidBodyPipeline* m_rigidBodyPipeline;
|
||||
|
||||
class b3GpuNarrowPhase* m_np;
|
||||
class b3GpuSapBroadphase* m_bp;
|
||||
|
||||
GpuRigidBodyDemoInternalData()
|
||||
:m_instancePosOrnColor(0),
|
||||
m_copyTransformsToVBOKernel(0), m_rigidBodyPipeline(0),
|
||||
m_np(0),
|
||||
m_bp(0)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
#endif//GPU_RIGIDBODY_INTERNAL_DATA_H
|
||||
|
||||
205
Demos3/GpuDemos/rigidbody/GpuSphereScene.cpp
Normal file
205
Demos3/GpuDemos/rigidbody/GpuSphereScene.cpp
Normal file
@@ -0,0 +1,205 @@
|
||||
#include "GpuSphereScene.h"
|
||||
#include "GpuRigidBodyDemo.h"
|
||||
#include "Bullet3Common/b3Quickprof.h"
|
||||
#include "OpenGLWindow/ShapeData.h"
|
||||
#include "OpenGLWindow/GLInstancingRenderer.h"
|
||||
#include "Bullet3Common/b3Quaternion.h"
|
||||
#include "OpenGLWindow/b3gWindowInterface.h"
|
||||
#include "Bullet3OpenCL/BroadphaseCollision/b3GpuSapBroadphase.h"
|
||||
#include "../GpuDemoInternalData.h"
|
||||
#include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
|
||||
#include "OpenGLWindow/OpenGLInclude.h"
|
||||
#include "OpenGLWindow/GLInstanceRendererInternalData.h"
|
||||
#include "Bullet3OpenCL/ParallelPrimitives/b3LauncherCL.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3GpuRigidBodyPipeline.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3GpuNarrowPhase.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3Config.h"
|
||||
#include "GpuRigidBodyDemoInternalData.h"
|
||||
#include "../gwenUserInterface.h"
|
||||
|
||||
|
||||
|
||||
|
||||
void GpuSphereScene::setupScene(const ConstructionInfo& ci)
|
||||
{
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
int numVertices = sizeof(cube_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(cube_indices)/sizeof(int);
|
||||
//int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
|
||||
int group=1;
|
||||
int mask=1;
|
||||
int index=0;
|
||||
bool writeInstanceToGpu = false;
|
||||
|
||||
if (0)
|
||||
{
|
||||
float radius = 60;
|
||||
int prevGraphicsShapeIndex = -1;
|
||||
{
|
||||
|
||||
|
||||
|
||||
if (1)//radius>=100)
|
||||
{
|
||||
int numVertices = sizeof(detailed_sphere_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(detailed_sphere_indices)/sizeof(int);
|
||||
prevGraphicsShapeIndex = ci.m_instancingRenderer->registerShape(&detailed_sphere_vertices[0],numVertices,detailed_sphere_indices,numIndices);
|
||||
} else
|
||||
{
|
||||
bool usePointSprites = false;
|
||||
if (usePointSprites)
|
||||
{
|
||||
int numVertices = sizeof(point_sphere_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(point_sphere_indices)/sizeof(int);
|
||||
prevGraphicsShapeIndex = ci.m_instancingRenderer->registerShape(&point_sphere_vertices[0],numVertices,point_sphere_indices,numIndices,B3_GL_POINTS);
|
||||
} else
|
||||
{
|
||||
if (radius>=10)
|
||||
{
|
||||
int numVertices = sizeof(medium_sphere_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(medium_sphere_indices)/sizeof(int);
|
||||
prevGraphicsShapeIndex = ci.m_instancingRenderer->registerShape(&medium_sphere_vertices[0],numVertices,medium_sphere_indices,numIndices);
|
||||
} else
|
||||
{
|
||||
int numVertices = sizeof(low_sphere_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(low_sphere_indices)/sizeof(int);
|
||||
prevGraphicsShapeIndex = ci.m_instancingRenderer->registerShape(&low_sphere_vertices[0],numVertices,low_sphere_indices,numIndices);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
b3Vector4 colors[4] =
|
||||
{
|
||||
b3Vector4(1,0,0,1),
|
||||
b3Vector4(0,1,0,1),
|
||||
b3Vector4(0,1,1,1),
|
||||
b3Vector4(1,1,0,1),
|
||||
};
|
||||
|
||||
int curColor = 0;
|
||||
|
||||
//int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
int colIndex = m_data->m_np->registerSphereShape(radius);//>registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
float mass = 0.f;
|
||||
|
||||
//b3Vector3 position((j&1)+i*2.2,1+j*2.,(j&1)+k*2.2);
|
||||
b3Vector3 position(0,0,0);
|
||||
|
||||
b3Quaternion orn(0,0,0,1);
|
||||
|
||||
b3Vector4 color = colors[curColor];
|
||||
curColor++;
|
||||
curColor&=3;
|
||||
b3Vector4 scaling(radius,radius,radius,1);
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(prevGraphicsShapeIndex,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index, writeInstanceToGpu);
|
||||
|
||||
index++;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
b3Vector4 colors[4] =
|
||||
{
|
||||
b3Vector4(1,0,0,1),
|
||||
b3Vector4(0,1,0,1),
|
||||
b3Vector4(0,1,1,1),
|
||||
b3Vector4(1,1,0,1),
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int curColor = 0;
|
||||
float radius = 61;
|
||||
//int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
int colIndex = m_data->m_np->registerSphereShape(radius);//>registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
int prevGraphicsShapeIndex = registerGraphicsSphereShape(ci,radius,false);
|
||||
|
||||
//for (int i=0;i<ci.arraySizeX;i++)
|
||||
{
|
||||
// for (int j=0;j<ci.arraySizeY;j++)
|
||||
{
|
||||
// for (int k=0;k<ci.arraySizeZ;k++)
|
||||
{
|
||||
int i=0,j=0,k=0;
|
||||
float mass = 0.f;
|
||||
|
||||
b3Vector3 position(0,0,0);
|
||||
//b3Vector3 position((j&1)+i*142.2,-51+j*142.,(j&1)+k*142.2);
|
||||
//b3Vector3 position(0,-41,0);//0,0,0);//i*radius*3,-41+j*radius*3,k*radius*3);
|
||||
|
||||
b3Quaternion orn(0,0,0,1);
|
||||
|
||||
b3Vector4 color = colors[curColor];
|
||||
curColor++;
|
||||
curColor&=3;
|
||||
b3Vector4 scaling(radius,radius,radius,1);
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(prevGraphicsShapeIndex,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index, writeInstanceToGpu);
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (1)
|
||||
{
|
||||
int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
b3Vector4 scaling(0.5,0.5,0.5,1);//1,1,1,1);//0.1,0.1,0.1,1);
|
||||
int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
b3Vector3 normal(0,-1,0);
|
||||
float constant=2;
|
||||
|
||||
|
||||
for (int j=-10;j<10;j++)
|
||||
for (int i=-10;i<10;i++)
|
||||
for (int k=0;k<30;k++)
|
||||
//int i=0;int j=0;
|
||||
{
|
||||
//int colIndex = m_data->m_np->registerPlaneShape(normal,constant);//>registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
b3Vector4 position(2*i,70+k*2,2*j+8,0);
|
||||
//b3Quaternion orn(0,0,0,1);
|
||||
b3Quaternion orn(b3Vector3(1,0,0),0.3);
|
||||
|
||||
b3Vector4 color(0,0,1,1);
|
||||
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(1.f,position,orn,colIndex,index,false);
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
if (!writeInstanceToGpu)
|
||||
{
|
||||
m_data->m_rigidBodyPipeline->writeAllInstancesToGpu();
|
||||
}
|
||||
|
||||
float camPos[4]={ci.arraySizeX,ci.arraySizeY/2,ci.arraySizeZ,0};
|
||||
//float camPos[4]={1,12.5,1.5,0};
|
||||
m_instancingRenderer->setCameraTargetPosition(camPos);
|
||||
m_instancingRenderer->setCameraDistance(130);
|
||||
|
||||
|
||||
char msg[1024];
|
||||
int numInstances = index;
|
||||
sprintf(msg,"Num objects = %d",numInstances);
|
||||
ci.m_gui->setStatusBarMessage(msg,true);
|
||||
}
|
||||
27
Demos3/GpuDemos/rigidbody/GpuSphereScene.h
Normal file
27
Demos3/GpuDemos/rigidbody/GpuSphereScene.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef GPU_SPHERE_SCENE_H
|
||||
#define GPU_SPHERE_SCENE_H
|
||||
|
||||
#include "GpuRigidBodyDemo.h"
|
||||
|
||||
class GpuSphereScene : public GpuRigidBodyDemo
|
||||
{
|
||||
public:
|
||||
|
||||
GpuSphereScene(){}
|
||||
virtual ~GpuSphereScene(){}
|
||||
virtual const char* getName()
|
||||
{
|
||||
return "GRBSphere";
|
||||
}
|
||||
|
||||
static GpuDemo* MyCreateFunc()
|
||||
{
|
||||
GpuDemo* demo = new GpuSphereScene;
|
||||
return demo;
|
||||
}
|
||||
|
||||
virtual void setupScene(const ConstructionInfo& ci);
|
||||
|
||||
};
|
||||
|
||||
#endif //GPU_SPHERE_SCENE_H
|
||||
Reference in New Issue
Block a user