gimpact 0.2 preparation

This commit is contained in:
ejcoumans
2007-06-22 16:58:26 +00:00
parent ca24744b9d
commit a399784077
6 changed files with 0 additions and 1048 deletions

View File

@@ -1,8 +0,0 @@
INCLUDE_DIRECTORIES(
${BULLET_PHYSICS_SOURCE_DIR}/src ${BULLET_PHYSICS_SOURCE_DIR}/Extras/GIMPACT/include ${BULLET_PHYSICS_SOURCE_DIR}/Extras/GIMPACTBullet
)
ADD_LIBRARY(LibGIMPACTBullet
btConcaveConcaveCollisionAlgorithm.cpp
btGIMPACTMeshShape.cpp
)

View File

@@ -1,13 +0,0 @@
SubDir TOP Extras GIMPACTBullet ;
Library GIMPACTBullet : [ Wildcard *.h *.cpp ] : noinstall ;
CFlags GIMPACTBullet : [ FIncludes $(TOP)/Extras/GIMPACTBullet $(TOP)/Extras/GIMPACT/include ] ;
LibDepends GIMPACTBullet : GIMPACT ;
MsvcIncDirs GIMPACTBullet :
"../../Extras/GIMPACTBullet"
"../../Extras/GIMPACT/include" ;
#InstallHeader [ Wildcard *.h ] : GIMPACTBullet ;

View File

@@ -1,494 +0,0 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/*
Author: Francisco Le<4C>n N<>jera
Concave-Concave Collision
*/
#include "BulletCollision/CollisionDispatch/btManifoldResult.h"
#include "LinearMath/btIDebugDraw.h"
#include "btConcaveConcaveCollisionAlgorithm.h"
#include "BulletCollision/CollisionDispatch/btCollisionObject.h"
#include "btGIMPACTMeshShape.h"
#include "BulletCollision/CollisionShapes/btStaticPlaneShape.h"
#include "GIMPACT/gimpact.h"
//! Class for accessing the plane equation
class btPlaneShape : public btStaticPlaneShape
{
public:
btPlaneShape(const btVector3& v, float f)
:btStaticPlaneShape(v,f)
{
}
void get_plane_equation(float equation[4])
{
equation[0] = m_planeNormal[0];
equation[1] = m_planeNormal[1];
equation[2] = m_planeNormal[2];
equation[3] = m_planeConstant;
}
void get_plane_equation_transformed(const btTransform & trans,float equation[4])
{
/*mat4f plane_trans;
IDENTIFY_MATRIX_4X4(plane_trans);
COPY_MATRIX_3X3(plane_trans,trans.getBasis());
MAT_SET_TRANSLATION(plane_trans,trans.getOrigin());
float ptemp[4]
//vec4f ptemp;
get_plane_equation(ptemp);
MAT_TRANSFORM_PLANE_4X4(equation,plane_trans,ptemp);*/
equation[0] = trans.getBasis().getRow(0).dot(m_planeNormal);
equation[1] = trans.getBasis().getRow(1).dot(m_planeNormal);
equation[2] = trans.getBasis().getRow(2).dot(m_planeNormal);
equation[3] = trans.getOrigin().dot(m_planeNormal) + m_planeConstant;
}
};
btConcaveConcaveCollisionAlgorithm::btConcaveConcaveCollisionAlgorithm( const btCollisionAlgorithmConstructionInfo& ci, btCollisionObject* body0,btCollisionObject* body1)
: btCollisionAlgorithm(ci)
{
}
btConcaveConcaveCollisionAlgorithm::~btConcaveConcaveCollisionAlgorithm()
{
clearCache();
}
void btConcaveConcaveCollisionAlgorithm::clearCache()
{
btPersistentManifold* mainfold;
for (int i=0;i<this->m_mainfoldsPtr.size() ; i++)
{
mainfold = m_mainfoldsPtr[i];
m_dispatcher->releaseManifold(mainfold);
}
m_mainfoldsPtr.clear();
}
btPersistentManifold* btConcaveConcaveCollisionAlgorithm::newContactMainfold(btCollisionObject* body0,btCollisionObject* body1)
{
btPersistentManifold* newmainfold;
newmainfold = m_dispatcher->getNewManifold(body0,body1);
m_mainfoldsPtr.push_back(newmainfold);
return newmainfold;
}
void process_gimpact_contacts(GIM_CONTACT * pcontacts,
int contact_count,
btConcaveConcaveCollisionAlgorithm * algorithm,
btCollisionObject* body0,
btCollisionObject* body1,
btManifoldResult* resultOut, bool swapped = false)
{
int i, ci = MANIFOLD_CACHE_SIZE;//Max point size
btPersistentManifold * current_mainfold = 0;
btCollisionObject* pbody0 = swapped?body1:body0;
btCollisionObject* pbody1 = swapped?body0:body1;
float csign = swapped?-1.f:1.f;
btVector3 cpoint;
btVector3 cnormal;
for(i=0;i<contact_count;i++)
{
if(ci>=MANIFOLD_CACHE_SIZE)
{
current_mainfold = algorithm->newContactMainfold(pbody0,pbody1);
resultOut->setPersistentManifold(current_mainfold);
ci=0;
}
cpoint.setValue(pcontacts->m_point[0],pcontacts->m_point[1],pcontacts->m_point[2]);
//Normal points to body0
cnormal.setValue(csign*pcontacts->m_normal[0],csign*pcontacts->m_normal[1],csign*pcontacts->m_normal[2]);
resultOut->addContactPoint(cnormal,cpoint,-pcontacts->m_depth);
pcontacts++;
ci++;
}
}
void process_gimpact_plane_contacts(vec4f * pcontacts,
vec4f planenormal,
int contact_count,
btConcaveConcaveCollisionAlgorithm * algorithm,
btCollisionObject* body0,
btCollisionObject* body1,
btManifoldResult* resultOut, bool swapped = false)
{
int i, ci = MANIFOLD_CACHE_SIZE;//Max point size
btPersistentManifold * current_mainfold = 0;
btCollisionObject* pbody0 = swapped?body1:body0;
btCollisionObject* pbody1 = swapped?body0:body1;
float csign = swapped?-1.f:1.f;
btVector3 cpoint;
btVector3 cnormal;
for(i=0;i<contact_count;i++)
{
if(ci>=MANIFOLD_CACHE_SIZE)
{
current_mainfold = algorithm->newContactMainfold(pbody0,pbody1);
resultOut->setPersistentManifold(current_mainfold);
ci=0;
}
cpoint.setValue(pcontacts[i][0],pcontacts[i][1],pcontacts[i][2]);
//Normal points to body0
cnormal.setValue(csign*planenormal[0],csign*planenormal[1],csign*planenormal[2]);
resultOut->addContactPoint(cnormal,cpoint,-pcontacts[i][3]);
ci++;
}
}
class CONCAVE_TRIANGLE_TOKEN
{
public:
GIM_TRIANGLE_DATA m_tridata;
int partId;
int triangleIndex;
CONCAVE_TRIANGLE_TOKEN()
{
m_tridata.m_has_planes = 0;
partId = 0;
triangleIndex = 0;
}
CONCAVE_TRIANGLE_TOKEN(const CONCAVE_TRIANGLE_TOKEN & token)
{
m_tridata.m_has_planes = 0;
VEC_COPY(m_tridata.m_vertices[0],token.m_tridata.m_vertices[0]);
VEC_COPY(m_tridata.m_vertices[1],token.m_tridata.m_vertices[1]);
VEC_COPY(m_tridata.m_vertices[2],token.m_tridata.m_vertices[2]);
partId = token.partId;
triangleIndex = token.triangleIndex;
}
};
void bt_gimpact_gimpact_collision(btConcaveConcaveCollisionAlgorithm * algorithm,
btCollisionObject* body0,
btCollisionObject* body1,
btManifoldResult* resultOut)
{
btGIMPACTMeshShape* tri0b = static_cast<btGIMPACTMeshShape*>( body0->getCollisionShape());
btGIMPACTMeshShape* tri1b = static_cast<btGIMPACTMeshShape*>( body1->getCollisionShape());
tri0b->prepareMeshes(body0->getWorldTransform());
tri1b->prepareMeshes(body1->getWorldTransform());
size_t i,j;
size_t parts0 = tri0b->m_gim_trimesh_parts.size();
size_t parts1 = tri1b->m_gim_trimesh_parts.size();
GIM_TRIMESH * trimesh0;
GIM_TRIMESH * trimesh1;
GDYNAMIC_ARRAY contacts;
GIM_CONTACT * pcontacts;
for(i=0;i<parts0;i++)
{
for(j=0;j<parts1;j++)
{
trimesh0 = (GIM_TRIMESH * )tri0b->m_gim_trimesh_parts[i];
trimesh1 = (GIM_TRIMESH * )tri1b->m_gim_trimesh_parts[j];
GIM_CREATE_CONTACT_LIST(contacts);
gim_trimesh_trimesh_collision(trimesh0,trimesh1,&contacts);
if(contacts.m_size>0)
{
pcontacts = GIM_DYNARRAY_POINTER(GIM_CONTACT,contacts);
process_gimpact_contacts(pcontacts,contacts.m_size,algorithm,body0,body1,resultOut);
}
GIM_DYNARRAY_DESTROY(contacts);
}
}
}
void bt_gimpact_plane_collision(btConcaveConcaveCollisionAlgorithm * algorithm,
btCollisionObject* tribody0,
btCollisionObject* planebody1,
btManifoldResult* resultOut,bool swapped)
{
btGIMPACTMeshShape* tri0b = static_cast<btGIMPACTMeshShape*>( tribody0->getCollisionShape());
btPlaneShape * plane1b = static_cast<btPlaneShape *>( planebody1->getCollisionShape());
tri0b->prepareMeshes(tribody0->getWorldTransform());
////////////////////////////////Getting plane////////////////////////////////////
vec4f pnormal;
plane1b->get_plane_equation_transformed(planebody1->getWorldTransform(),pnormal);
////////////////////////////////End Getting plane////////////////////////////////////
size_t i;
size_t parts0 = tri0b->m_gim_trimesh_parts.size();
GIM_TRIMESH * trimesh0;
GDYNAMIC_ARRAY contacts;
vec4f * pcontacts;
for(i=0;i<parts0;i++)
{
trimesh0 = (GIM_TRIMESH * )tri0b->m_gim_trimesh_parts[i];
GIM_CREATE_TRIMESHPLANE_CONTACTS(contacts);
gim_trimesh_plane_collision(trimesh0,pnormal,&contacts);
if(contacts.m_size>0)
{
pcontacts = GIM_DYNARRAY_POINTER(vec4f,contacts);
process_gimpact_plane_contacts(pcontacts,pnormal,
contacts.m_size,algorithm,tribody0,planebody1,resultOut,swapped);
}
GIM_DYNARRAY_DESTROY(contacts);
}
}
///For each triangle in the concave mesh that overlaps with the AABB of a convex (m_convexProxy), processTriangle is called.
class btConcaveTriangleCallback : public btTriangleCallback
{
public:
btCollisionObject* m_body;
mat4f m_transform;
btAlignedObjectArray<CONCAVE_TRIANGLE_TOKEN> m_triangles;
btConcaveTriangleCallback(btCollisionObject* body)
{
m_body = body;
IDENTIFY_MATRIX_4X4(m_transform);
COPY_MATRIX_3X3(m_transform,body->getWorldTransform().getBasis());
MAT_SET_TRANSLATION(m_transform,body->getWorldTransform().getOrigin());
m_triangles.reserve(100);
}
void setTimeStepAndCounters(float collisionMarginTriangle,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
{}
virtual ~btConcaveTriangleCallback(){
}
virtual void processTriangle(btVector3* triangle, int partId, int triangleIndex)
{
CONCAVE_TRIANGLE_TOKEN token;
token.m_tridata.m_has_planes = 0;
token.partId = partId;
token.triangleIndex = triangleIndex;
//Copy vertices
MAT_DOT_VEC_3X4(token.m_tridata.m_vertices[0],m_transform,triangle[0]);
MAT_DOT_VEC_3X4(token.m_tridata.m_vertices[1],m_transform,triangle[1]);
MAT_DOT_VEC_3X4(token.m_tridata.m_vertices[2],m_transform,triangle[2]);
m_triangles.push_back(token);
}
void clearCache(){}
};
void bt_concave_concave_collision(btConcaveConcaveCollisionAlgorithm * algorithm,
btCollisionObject* tribody0,
btCollisionObject* tribody1,
btManifoldResult* resultOut)
{
btConcaveShape* tri0b = static_cast<btConcaveShape*>( tribody0->getCollisionShape());
btConcaveShape* tri1b = static_cast<btConcaveShape*>( tribody1->getCollisionShape());
//Get First AABB
btVector3 aabbMin0,aabbMax0;
tri0b->getAabb(tribody0->getWorldTransform(),aabbMin0,aabbMax0);
//Get Second AABB
btVector3 aabbMin1,aabbMax1;
tri1b->getAabb(tribody1->getWorldTransform(),aabbMin1,aabbMax1);
//Transform boxes to local spaces
aabb3f aabb0 = {
aabbMin0[0],aabbMax0[0],
aabbMin0[1],aabbMax0[1],
aabbMin0[2],aabbMax0[2],
};
aabb3f aabb1 = {
aabbMin1[0],aabbMax1[0],
aabbMin1[1],aabbMax1[1],
aabbMin1[2],aabbMax1[2],
};
mat4f transform;
IDENTIFY_MATRIX_4X4(transform);
// body0 inverse transform
btTransform transinv = tribody0->getWorldTransform().inverse();
COPY_MATRIX_3X3(transform,transinv.getBasis());
MAT_SET_TRANSLATION(transform,transinv.getOrigin());
//Transform box1 to body0 space
AABB_TRANSFORM(aabb1,aabb1,transform);
AABB_GET_MIN(aabb1,aabbMin1);
AABB_GET_MAX(aabb1,aabbMax1);
btConcaveTriangleCallback callback0(tribody0);
tri0b->processAllTriangles(&callback0,aabbMin1,aabbMax1);
if(callback0.m_triangles.size()==0) return;
// body1 inverse transform
transinv = tribody1->getWorldTransform().inverse();
COPY_MATRIX_3X3(transform,transinv.getBasis());
MAT_SET_TRANSLATION(transform,transinv.getOrigin());
//Transform box0 to body1 space
AABB_TRANSFORM(aabb0,aabb0,transform);
AABB_GET_MIN(aabb0,aabbMin0);
AABB_GET_MAX(aabb0,aabbMax0);
btConcaveTriangleCallback callback1(tribody1);
tri1b->processAllTriangles(&callback1,aabbMin0,aabbMax0);
if(callback1.m_triangles.size()==0) return;
////////////////////////////////Collide triangles////////////////////////////////////
//dummy contacts
GDYNAMIC_ARRAY dummycontacts;
GIM_CREATE_CONTACT_LIST(dummycontacts);
//Auxiliary triangle data
GIM_TRIANGLE_CONTACT_DATA tri_contact_data;
size_t i,j,ci;
int colresult;
for(i=0;i<callback0.m_triangles.size();i++)
{
for(j=0;j<callback1.m_triangles.size();j++)
{
//collide triangles
colresult = gim_triangle_triangle_collision(
&callback0.m_triangles[i].m_tridata,
&callback1.m_triangles[j].m_tridata,&tri_contact_data);
if(colresult == 1)
{
//Add contacts
for (ci=0;ci<tri_contact_data.m_point_count ;ci++ )
{
GIM_PUSH_CONTACT(dummycontacts, tri_contact_data.m_points[ci],tri_contact_data.m_separating_normal ,tri_contact_data.m_penetration_depth,tribody0, tribody1, callback0.m_triangles[i].triangleIndex, callback1.m_triangles[j].triangleIndex);
}
}
}
}
if(dummycontacts.m_size == 0) //reject
{
GIM_DYNARRAY_DESTROY(dummycontacts);
return;
}
//dummy contacts
GDYNAMIC_ARRAY contacts;
GIM_CREATE_CONTACT_LIST(contacts);
//merge contacts
gim_merge_contacts(&dummycontacts,&contacts);
GIM_CONTACT * pcontacts = GIM_DYNARRAY_POINTER(GIM_CONTACT,contacts);
process_gimpact_contacts(pcontacts,contacts.m_size,algorithm,tribody0,tribody1,resultOut);
//Terminate
GIM_DYNARRAY_DESTROY(dummycontacts);
GIM_DYNARRAY_DESTROY(contacts);
}
void btConcaveConcaveCollisionAlgorithm::processCollision (btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
{
clearCache();
if (body0->getCollisionShape()->getShapeType()==GIMPACT_SHAPE_PROXYTYPE && body1->getCollisionShape()->getShapeType()==GIMPACT_SHAPE_PROXYTYPE )
{
bt_gimpact_gimpact_collision(this,body0,body1,resultOut);
}
else if (body0->getCollisionShape()->getShapeType()==STATIC_PLANE_PROXYTYPE&& body1->getCollisionShape()->getShapeType()==GIMPACT_SHAPE_PROXYTYPE )
{
bt_gimpact_plane_collision(this,body1,body0,resultOut,true);
}
else if (body0->getCollisionShape()->getShapeType()==GIMPACT_SHAPE_PROXYTYPE&& body1->getCollisionShape()->getShapeType()==STATIC_PLANE_PROXYTYPE)
{
bt_gimpact_plane_collision(this,body0,body1,resultOut,false);
}
else if(body0->getCollisionShape()->isConcave() && body1->getCollisionShape()->isConcave() )
{
bt_concave_concave_collision(this,body0,body1,resultOut);
}
}
btScalar btConcaveConcaveCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
{
return 1.f;
}
///////////////////////////////////// REGISTERING ALGORITHM //////////////////////////////////////////////
//! Use this function for register the algorithm externally
void btConcaveConcaveCollisionAlgorithm::registerAlgorithm(btCollisionDispatcher * dispatcher)
{
dispatcher->registerCollisionCreateFunc(GIMPACT_SHAPE_PROXYTYPE,GIMPACT_SHAPE_PROXYTYPE ,new btConcaveConcaveCollisionAlgorithm::CreateFunc);
dispatcher->registerCollisionCreateFunc(GIMPACT_SHAPE_PROXYTYPE,STATIC_PLANE_PROXYTYPE ,new btConcaveConcaveCollisionAlgorithm::CreateFunc);
dispatcher->registerCollisionCreateFunc(STATIC_PLANE_PROXYTYPE,GIMPACT_SHAPE_PROXYTYPE ,new btConcaveConcaveCollisionAlgorithm::CreateFunc);
dispatcher->registerCollisionCreateFunc(GIMPACT_SHAPE_PROXYTYPE,TRIANGLE_MESH_SHAPE_PROXYTYPE,new btConcaveConcaveCollisionAlgorithm::CreateFunc);
dispatcher->registerCollisionCreateFunc(TRIANGLE_MESH_SHAPE_PROXYTYPE,GIMPACT_SHAPE_PROXYTYPE,new btConcaveConcaveCollisionAlgorithm::CreateFunc);
dispatcher->registerCollisionCreateFunc(STATIC_PLANE_PROXYTYPE,TRIANGLE_MESH_SHAPE_PROXYTYPE,new btConcaveConcaveCollisionAlgorithm::CreateFunc);
dispatcher->registerCollisionCreateFunc(TRIANGLE_MESH_SHAPE_PROXYTYPE,STATIC_PLANE_PROXYTYPE,new btConcaveConcaveCollisionAlgorithm::CreateFunc);
dispatcher->registerCollisionCreateFunc(TRIANGLE_MESH_SHAPE_PROXYTYPE,TRIANGLE_MESH_SHAPE_PROXYTYPE,new btConcaveConcaveCollisionAlgorithm::CreateFunc);
}

View File

@@ -1,65 +0,0 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef BVH_CONCAVE_COLLISION_ALGORITHM_H
#define BVH_CONCAVE_COLLISION_ALGORITHM_H
#include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
#include "BulletCollision/BroadphaseCollision/btDispatcher.h"
#include "BulletCollision/BroadphaseCollision/btBroadphaseInterface.h"
#include "BulletCollision/CollisionShapes/btBvhTriangleMeshShape.h"
#include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h"
class btDispatcher;
#include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
#include "BulletCollision/CollisionDispatch/btCollisionCreateFunc.h"
#include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
#include "LinearMath/btAlignedObjectArray.h"
/// btConcaveConcaveCollisionAlgorithm supports collision between btBvhTriangleMeshShape shapes
class btConcaveConcaveCollisionAlgorithm : public btCollisionAlgorithm
{
protected:
btAlignedObjectArray<btPersistentManifold*> m_mainfoldsPtr;
public:
btConcaveConcaveCollisionAlgorithm( const btCollisionAlgorithmConstructionInfo& ci,btCollisionObject* body0,btCollisionObject* body1);
virtual ~btConcaveConcaveCollisionAlgorithm();
virtual void processCollision (btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut);
btScalar calculateTimeOfImpact(btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut);
void clearCache();
btPersistentManifold* newContactMainfold(btCollisionObject* body0,btCollisionObject* body1);
struct CreateFunc :public btCollisionAlgorithmCreateFunc
{
virtual btCollisionAlgorithm* CreateCollisionAlgorithm(btCollisionAlgorithmConstructionInfo& ci, btCollisionObject* body0,btCollisionObject* body1)
{
return new btConcaveConcaveCollisionAlgorithm(ci,body0,body1);
}
};
//! Use this function for register the algorithm externally
static void registerAlgorithm(btCollisionDispatcher * dispatcher);
};
#endif //BVH_CONCAVE_COLLISION_ALGORITHM_H

View File

@@ -1,353 +0,0 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
//#define DISABLE_BVH
#include "btGIMPACTMeshShape.h"
#include "GIMPACT/gimpact.h"
int g_gimpact_references = 0;
//Mechanism for initialize and terminate GIMPACT structures
void increase_gimpact_reference()
{
g_gimpact_references++;
if(g_gimpact_references >1 ) return;
gimpact_init();
}
void decrease_gimpact_reference()
{
if(g_gimpact_references <=0 ) return;
g_gimpact_references--;
if(g_gimpact_references >0 ) return;
gimpact_terminate();
}
/////////////////////////// btGIMPACTMeshData class/////////////////////////////////////////////////////////////
void btGIMPACTMeshData::clearMeshParts()
{
for(int i = 0;i<m_meshes.size();i++)
{
gim_trimesh_data_dec_ref(m_meshes[i]);
}
m_meshes.clear();
}
void btGIMPACTMeshData::addMeshPart(btStridingMeshInterface* meshInterface, int part)
{
//Construct the trimesh
// The buffer configuration
const unsigned char *vertexbase;
int numverts;
PHY_ScalarType stype;
int vertexStride;
const unsigned char *indexbase;
int indexstride;
int numfaces;
PHY_ScalarType indicestype;
meshInterface->getLockedReadOnlyVertexIndexBase(&vertexbase, numverts,stype,vertexStride,&indexbase,
indexstride,numfaces,indicestype,part);
GUINT int_type;
switch (indicestype)
{
case PHY_INTEGER:
int_type = G_STYPE_INT;
break;
case PHY_SHORT:
default :
int_type = G_STYPE_SHORT;
break;
}
GUINT vert_type;
switch (stype)
{
case PHY_FLOAT:
vert_type = G_STYPE_REAL;
break;
case PHY_DOUBLE:
default :
vert_type = G_STYPE_REAL2;
break;
}
GBUFFER_ARRAY buffer_vertex_array;
GBUFFER_ARRAY buffer_triindex_array;
GUINT trimesh_data_handle;
//Create shared buffer for indices
gim_create_shared_buffer_from_data(
vertexbase, numverts*vertexStride,
&buffer_vertex_array.m_buffer_id);
GIM_BUFFER_ARRAY_INIT_OFFSET_STRIDE(
buffer_vertex_array,buffer_vertex_array.m_buffer_id,
numverts,0,vertexStride);
//Create shared buffer for vertices
gim_create_shared_buffer_from_data(indexbase,
numfaces*indexstride, &buffer_triindex_array.m_buffer_id);
GIM_BUFFER_ARRAY_INIT_OFFSET_STRIDE(
buffer_triindex_array,buffer_triindex_array.m_buffer_id,
numfaces,0,indexstride);
//Create the trimesh data
gim_trimesh_data_create_from_arrays(
&trimesh_data_handle,
&buffer_vertex_array,vert_type,
&buffer_triindex_array,int_type);
//always call this after create a buffer_array
GIM_BUFFER_ARRAY_DESTROY(buffer_vertex_array);
GIM_BUFFER_ARRAY_DESTROY(buffer_triindex_array);
//Build Bounding volume tree
gim_trimesh_data_build_aabbtree(trimesh_data_handle);
gim_trimesh_data_inc_ref(trimesh_data_handle);
m_meshes.push_back(trimesh_data_handle);
}
void btGIMPACTMeshData::processMeshParts(btStridingMeshInterface* meshInterface)
{
clearMeshParts();
this->m_meshInterface = meshInterface;
int meshcount = meshInterface->getNumSubParts();
for(int i = 0;i<meshcount;i++)
{
addMeshPart(meshInterface,i);
}
}
btGIMPACTMeshData::btGIMPACTMeshData(btStridingMeshInterface* meshInterface)
{
increase_gimpact_reference();
processMeshParts(meshInterface);
}
btGIMPACTMeshData::~btGIMPACTMeshData()
{
clearMeshParts();
decrease_gimpact_reference();
}
/////////////////////////// btGIMPACTMeshShape class/////////////////////////////////////////////////////////////
void btGIMPACTMeshShape::clearMeshParts()
{
GIM_TRIMESH * ptrimesh;
for(int i = 0;i<m_gim_trimesh_parts.size();i++)
{
ptrimesh = (GIM_TRIMESH * )m_gim_trimesh_parts[i];
gim_trimesh_destroy(ptrimesh);
gim_free(ptrimesh,0);
}
m_gim_trimesh_parts.clear();
}
void btGIMPACTMeshShape::processMeshParts(btGIMPACTMeshData * meshdata)
{
clearMeshParts();
this->m_meshdata = meshdata;
BT_GIMPACT_TRIMESH_HANDLE gimhandle;
GIM_TRIMESH * ptrimesh;
for(int i = 0;i<m_meshdata->m_meshes.size();i++)
{
ptrimesh = (GIM_TRIMESH *)gim_alloc(sizeof(GIM_TRIMESH));
gim_trimesh_create(ptrimesh,m_meshdata->m_meshes[i],1,0);
gimhandle = (BT_GIMPACT_TRIMESH_HANDLE) ptrimesh;
m_gim_trimesh_parts.push_back(gimhandle);
}
}
btGIMPACTMeshShape::btGIMPACTMeshShape(btGIMPACTMeshData * meshdata)
{
m_scale.setValue(1.0f,1.0f,1.0f);
processMeshParts(meshdata);
}
btGIMPACTMeshShape::~btGIMPACTMeshShape()
{
clearMeshParts();
}
void btGIMPACTMeshShape::prepareMeshes(const btTransform & trans) const
{
mat4f gim_trans;
IDENTIFY_MATRIX_4X4(gim_trans);
COPY_MATRIX_3X3(gim_trans,trans.getBasis());
btVector3 scaling = getLocalScaling();
SCALE_VEC_MATRIX_3X3(gim_trans,scaling,gim_trans);
MAT_SET_TRANSLATION(gim_trans,trans.getOrigin());
GIM_TRIMESH * ptrimesh;
for(int i = 0;i<m_gim_trimesh_parts.size();i++)
{
ptrimesh = (GIM_TRIMESH * )m_gim_trimesh_parts[i];
gim_trimesh_set_tranform(ptrimesh, gim_trans);
}
}
///getAabb returns the axis aligned bounding box in the coordinate frame of the given transform t.
void btGIMPACTMeshShape::getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const
{
prepareMeshes(t);
aabb3f meshbox, global_box;
INVALIDATE_AABB(global_box);
GIM_TRIMESH * ptrimesh;
for(int i = 0;i<m_gim_trimesh_parts.size();i++)
{
ptrimesh = (GIM_TRIMESH * )m_gim_trimesh_parts[i];
gim_trimesh_get_aabb(ptrimesh,&meshbox);
MERGEBOXES(global_box,meshbox);
}
aabbMin[0] = global_box.minX;
aabbMin[1] = global_box.minY;
aabbMin[2] = global_box.minZ;
aabbMax[0] = global_box.maxX;
aabbMax[1] = global_box.maxY;
aabbMax[2] = global_box.maxZ;
}
void btGIMPACTMeshShape::setLocalScaling(const btVector3& scaling)
{
m_scale = scaling;
}
const btVector3& btGIMPACTMeshShape::getLocalScaling() const
{
return m_scale ;
}
void btGIMPACTMeshShape::calculateLocalInertia(btScalar mass,btVector3& inertia)
{
btTransform t;
t.setIdentity();
btVector3 aabbMin;
btVector3 aabbMax;
getAabb(t,aabbMin,aabbMax);
//not yet, return box inertia
btVector3 halfExtents = (aabbMax-aabbMin)*0.5f;
btScalar lx=2.f*(halfExtents.x());
btScalar ly=2.f*(halfExtents.y());
btScalar lz=2.f*(halfExtents.z());
const btScalar x2 = lx*lx;
const btScalar y2 = ly*ly;
const btScalar z2 = lz*lz;
const btScalar scaledmass = mass * 0.08333333f;
inertia = scaledmass * (btVector3(y2+z2,x2+z2,x2+y2));
}
void btGIMPACTMeshShape::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
{
/*btTransform t;
t.setIdentity();
prepareMeshes(t);*/
GDYNAMIC_ARRAY collision_result;
GIM_TRIMESH * ptrimesh;
int i;
unsigned int j;
vec3f trivec[3];
btVector3 btrivec[3];
GUINT * boxesresult;
btVector3 scalevec = this->getLocalScaling();
aabb3f test_aabb;
test_aabb.minX = aabbMin[0]/scalevec[0];
test_aabb.minY = aabbMin[1]/scalevec[1];
test_aabb.minZ = aabbMin[2]/scalevec[2];
test_aabb.maxX = aabbMax[0]/scalevec[0];
test_aabb.maxY = aabbMax[1]/scalevec[1];
test_aabb.maxZ = aabbMax[2]/scalevec[2];
for(i = 0;i<m_gim_trimesh_parts.size();i++)
{
ptrimesh = (GIM_TRIMESH * )m_gim_trimesh_parts[i];
GIM_CREATE_BOXQUERY_LIST(collision_result);
gim_trimesh_midphase_box_collision_local(ptrimesh,&test_aabb,&collision_result);
boxesresult = GIM_DYNARRAY_POINTER(GUINT,collision_result);
//collide triangles
//Locks trimesh
gim_trimesh_locks_work_data(ptrimesh);
for(j=0;j<collision_result.m_size;j++)
{
gim_trimesh_get_triangle_vertices_local(ptrimesh,boxesresult[j],trivec[0],trivec[1],trivec[2]);
btrivec[0].setValue(trivec[0][0]*scalevec[0],trivec[0][1]*scalevec[1],trivec[0][2]*scalevec[2]);
btrivec[1].setValue(trivec[1][0]*scalevec[0],trivec[1][1]*scalevec[1],trivec[1][2]*scalevec[2]);
btrivec[2].setValue(trivec[2][0]*scalevec[0],trivec[2][1]*scalevec[1],trivec[2][2]*scalevec[2]);
callback->processTriangle(btrivec,(int)i,(int)boxesresult[j]);
}
///unlocks
gim_trimesh_unlocks_work_data(ptrimesh);
GIM_DYNARRAY_DESTROY(collision_result);
}
}

View File

@@ -1,115 +0,0 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef GIMPACT_MESH_SHAPE_H
#define GIMPACT_MESH_SHAPE_H
#include "BulletCollision/CollisionShapes/btConcaveShape.h"
#include "BulletCollision/CollisionShapes/btStridingMeshInterface.h"
#include "LinearMath/btAlignedObjectArray.h"
//#define GIMPACT_SHAPE_PROXYTYPE (MAX_BROADPHASE_COLLISION_TYPES + 1)
//! Handle representation for each mesh part
/*!
Each mesh part must have a GIMPACT trimesh data (GIM_TRIMESH_DATA).
*/
typedef unsigned long BT_GIMPACT_TRIMESH_DATA_HANDLE;
class BT_GIMPACT_TRIMESH_DATA_HANDLE_ARRAY: public btAlignedObjectArray<BT_GIMPACT_TRIMESH_DATA_HANDLE>
{
public:
};
class btGIMPACTMeshData
{
protected:
void clearMeshParts();
void addMeshPart(btStridingMeshInterface* meshInterface, int part);
void processMeshParts(btStridingMeshInterface* meshInterface);
public:
btStridingMeshInterface* m_meshInterface;
BT_GIMPACT_TRIMESH_DATA_HANDLE_ARRAY m_meshes;
btGIMPACTMeshData(btStridingMeshInterface* meshInterface);
virtual ~btGIMPACTMeshData();
};
//! Handle representation for each mesh part
/*!
Each mesh part must have a GIMPACT trimesh (GIM_TRIMESH).
*/
typedef void * BT_GIMPACT_TRIMESH_HANDLE;
class BT_GIMPACT_TRIMESH_HANDLE_ARRAY: public btAlignedObjectArray<BT_GIMPACT_TRIMESH_HANDLE>
{
public:
};
///
///Uses an interface to access the triangles to allow for sharing graphics/physics triangles.
class btGIMPACTMeshShape : public btConcaveShape
{
protected:
btGIMPACTMeshData * m_meshdata;
btVector3 m_scale;
void clearMeshParts();
void processMeshParts(btGIMPACTMeshData * meshdata);
public:
BT_GIMPACT_TRIMESH_HANDLE_ARRAY m_gim_trimesh_parts;
btGIMPACTMeshShape(btGIMPACTMeshData * meshInterface);
virtual ~btGIMPACTMeshShape();
virtual int getShapeType() const
{
return GIMPACT_SHAPE_PROXYTYPE;
}
//! Function for retrieve triangles.
/*!
It gives the triangles in local space
*/
virtual void processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const;
//debugging
virtual char* getName()const {return "GIMPACT_SHAPE_PROXYTYPE";}
virtual void prepareMeshes(const btTransform & trans) const;
///getAabb returns the axis aligned bounding box in the coordinate frame of the given transform t.
virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const;
virtual void setLocalScaling(const btVector3& scaling) ;
virtual const btVector3& getLocalScaling() const ;
virtual void calculateLocalInertia(btScalar mass,btVector3& inertia);
};
#endif //GIMPACT_MESH_SHAPE_H