2
Bullet Continuous Collision Detection and Physics Library
3
Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
5
This software is provided 'as-is', without any express or implied warranty.
6
In no event will the authors be held liable for any damages arising from the use of this software.
7
Permission is granted to anyone to use this software for any purpose,
8
including commercial applications, and to alter it and redistribute it freely,
9
subject to the following restrictions:
11
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.
12
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13
3. This notice may not be removed or altered from any source distribution.
16
#include "btStridingMeshInterface.h"
17
#include "LinearMath/btSerializer.h"
19
btStridingMeshInterface::~btStridingMeshInterface()
25
void btStridingMeshInterface::InternalProcessAllTriangles(btInternalTriangleIndexCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
29
int numtotalphysicsverts = 0;
30
int part,graphicssubparts = getNumSubParts();
31
const unsigned char * vertexbase;
32
const unsigned char * indexbase;
35
PHY_ScalarType gfxindextype;
36
int stride,numverts,numtriangles;
38
btVector3 triangle[3];
40
btVector3 meshScaling = getScaling();
42
///if the number of parts is big, the performance might drop due to the innerloop switch on indextype
43
for (part=0;part<graphicssubparts ;part++)
45
getLockedReadOnlyVertexIndexBase(&vertexbase,numverts,type,stride,&indexbase,indexstride,numtriangles,gfxindextype,part);
46
numtotalphysicsverts+=numtriangles*3; //upper bound
48
///unlike that developers want to pass in double-precision meshes in single-precision Bullet build
49
///so disable this feature by default
50
///see patch http://code.google.com/p/bullet/issues/detail?id=213
63
for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
65
unsigned int* tri_indices= (unsigned int*)(indexbase+gfxindex*indexstride);
66
graphicsbase = (float*)(vertexbase+tri_indices[0]*stride);
67
triangle[0].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(),graphicsbase[2]*meshScaling.getZ());
68
graphicsbase = (float*)(vertexbase+tri_indices[1]*stride);
69
triangle[1].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(), graphicsbase[2]*meshScaling.getZ());
70
graphicsbase = (float*)(vertexbase+tri_indices[2]*stride);
71
triangle[2].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(), graphicsbase[2]*meshScaling.getZ());
72
callback->internalProcessTriangleIndex(triangle,part,gfxindex);
78
for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
80
unsigned short int* tri_indices= (unsigned short int*)(indexbase+gfxindex*indexstride);
81
graphicsbase = (float*)(vertexbase+tri_indices[0]*stride);
82
triangle[0].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(),graphicsbase[2]*meshScaling.getZ());
83
graphicsbase = (float*)(vertexbase+tri_indices[1]*stride);
84
triangle[1].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(), graphicsbase[2]*meshScaling.getZ());
85
graphicsbase = (float*)(vertexbase+tri_indices[2]*stride);
86
triangle[2].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(), graphicsbase[2]*meshScaling.getZ());
87
callback->internalProcessTriangleIndex(triangle,part,gfxindex);
93
for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
95
unsigned char* tri_indices= (unsigned char*)(indexbase+gfxindex*indexstride);
96
graphicsbase = (float*)(vertexbase+tri_indices[0]*stride);
97
triangle[0].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(),graphicsbase[2]*meshScaling.getZ());
98
graphicsbase = (float*)(vertexbase+tri_indices[1]*stride);
99
triangle[1].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(), graphicsbase[2]*meshScaling.getZ());
100
graphicsbase = (float*)(vertexbase+tri_indices[2]*stride);
101
triangle[2].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(), graphicsbase[2]*meshScaling.getZ());
102
callback->internalProcessTriangleIndex(triangle,part,gfxindex);
107
btAssert((gfxindextype == PHY_INTEGER) || (gfxindextype == PHY_SHORT));
114
double* graphicsbase;
116
switch (gfxindextype)
120
for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
122
unsigned int* tri_indices= (unsigned int*)(indexbase+gfxindex*indexstride);
123
graphicsbase = (double*)(vertexbase+tri_indices[0]*stride);
124
triangle[0].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(),(btScalar)graphicsbase[2]*meshScaling.getZ());
125
graphicsbase = (double*)(vertexbase+tri_indices[1]*stride);
126
triangle[1].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(), (btScalar)graphicsbase[2]*meshScaling.getZ());
127
graphicsbase = (double*)(vertexbase+tri_indices[2]*stride);
128
triangle[2].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(), (btScalar)graphicsbase[2]*meshScaling.getZ());
129
callback->internalProcessTriangleIndex(triangle,part,gfxindex);
135
for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
137
unsigned short int* tri_indices= (unsigned short int*)(indexbase+gfxindex*indexstride);
138
graphicsbase = (double*)(vertexbase+tri_indices[0]*stride);
139
triangle[0].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(),(btScalar)graphicsbase[2]*meshScaling.getZ());
140
graphicsbase = (double*)(vertexbase+tri_indices[1]*stride);
141
triangle[1].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(), (btScalar)graphicsbase[2]*meshScaling.getZ());
142
graphicsbase = (double*)(vertexbase+tri_indices[2]*stride);
143
triangle[2].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(), (btScalar)graphicsbase[2]*meshScaling.getZ());
144
callback->internalProcessTriangleIndex(triangle,part,gfxindex);
150
for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
152
unsigned char* tri_indices= (unsigned char*)(indexbase+gfxindex*indexstride);
153
graphicsbase = (double*)(vertexbase+tri_indices[0]*stride);
154
triangle[0].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(),(btScalar)graphicsbase[2]*meshScaling.getZ());
155
graphicsbase = (double*)(vertexbase+tri_indices[1]*stride);
156
triangle[1].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(), (btScalar)graphicsbase[2]*meshScaling.getZ());
157
graphicsbase = (double*)(vertexbase+tri_indices[2]*stride);
158
triangle[2].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(), (btScalar)graphicsbase[2]*meshScaling.getZ());
159
callback->internalProcessTriangleIndex(triangle,part,gfxindex);
164
btAssert((gfxindextype == PHY_INTEGER) || (gfxindextype == PHY_SHORT));
169
btAssert((type == PHY_FLOAT) || (type == PHY_DOUBLE));
172
unLockReadOnlyVertexBase(part);
176
void btStridingMeshInterface::calculateAabbBruteForce(btVector3& aabbMin,btVector3& aabbMax)
179
struct AabbCalculationCallback : public btInternalTriangleIndexCallback
184
AabbCalculationCallback()
186
m_aabbMin.setValue(btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT));
187
m_aabbMax.setValue(btScalar(-BT_LARGE_FLOAT),btScalar(-BT_LARGE_FLOAT),btScalar(-BT_LARGE_FLOAT));
190
virtual void internalProcessTriangleIndex(btVector3* triangle,int partId,int triangleIndex)
195
m_aabbMin.setMin(triangle[0]);
196
m_aabbMax.setMax(triangle[0]);
197
m_aabbMin.setMin(triangle[1]);
198
m_aabbMax.setMax(triangle[1]);
199
m_aabbMin.setMin(triangle[2]);
200
m_aabbMax.setMax(triangle[2]);
204
//first calculate the total aabb for all triangles
205
AabbCalculationCallback aabbCallback;
206
aabbMin.setValue(btScalar(-BT_LARGE_FLOAT),btScalar(-BT_LARGE_FLOAT),btScalar(-BT_LARGE_FLOAT));
207
aabbMax.setValue(btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT));
208
InternalProcessAllTriangles(&aabbCallback,aabbMin,aabbMax);
210
aabbMin = aabbCallback.m_aabbMin;
211
aabbMax = aabbCallback.m_aabbMax;
216
///fills the dataBuffer and returns the struct name (and 0 on failure)
217
const char* btStridingMeshInterface::serialize(void* dataBuffer, btSerializer* serializer) const
219
btStridingMeshInterfaceData* trimeshData = (btStridingMeshInterfaceData*) dataBuffer;
221
trimeshData->m_numMeshParts = getNumSubParts();
223
//void* uniquePtr = 0;
225
trimeshData->m_meshPartsPtr = 0;
227
if (trimeshData->m_numMeshParts)
229
btChunk* chunk = serializer->allocate(sizeof(btMeshPartData),trimeshData->m_numMeshParts);
230
btMeshPartData* memPtr = (btMeshPartData*)chunk->m_oldPtr;
231
trimeshData->m_meshPartsPtr = (btMeshPartData *)serializer->getUniquePointer(memPtr);
234
// int numtotalphysicsverts = 0;
235
int part,graphicssubparts = getNumSubParts();
236
const unsigned char * vertexbase;
237
const unsigned char * indexbase;
240
PHY_ScalarType gfxindextype;
241
int stride,numverts,numtriangles;
243
// btVector3 triangle[3];
245
btVector3 meshScaling = getScaling();
247
///if the number of parts is big, the performance might drop due to the innerloop switch on indextype
248
for (part=0;part<graphicssubparts ;part++,memPtr++)
250
getLockedReadOnlyVertexIndexBase(&vertexbase,numverts,type,stride,&indexbase,indexstride,numtriangles,gfxindextype,part);
251
memPtr->m_numTriangles = numtriangles;//indices = 3*numtriangles
252
memPtr->m_numVertices = numverts;
253
memPtr->m_indices16 = 0;
254
memPtr->m_indices32 = 0;
255
memPtr->m_3indices16 = 0;
256
memPtr->m_vertices3f = 0;
257
memPtr->m_vertices3d = 0;
259
switch (gfxindextype)
263
int numindices = numtriangles*3;
267
btChunk* chunk = serializer->allocate(sizeof(btIntIndexData),numindices);
268
btIntIndexData* tmpIndices = (btIntIndexData*)chunk->m_oldPtr;
269
memPtr->m_indices32 = (btIntIndexData*)serializer->getUniquePointer(tmpIndices);
270
for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
272
unsigned int* tri_indices= (unsigned int*)(indexbase+gfxindex*indexstride);
273
tmpIndices[gfxindex*3].m_value = tri_indices[0];
274
tmpIndices[gfxindex*3+1].m_value = tri_indices[1];
275
tmpIndices[gfxindex*3+2].m_value = tri_indices[2];
277
serializer->finalizeChunk(chunk,"btIntIndexData",BT_ARRAY_CODE,(void*)chunk->m_oldPtr);
285
btChunk* chunk = serializer->allocate(sizeof(btShortIntIndexTripletData),numtriangles);
286
btShortIntIndexTripletData* tmpIndices = (btShortIntIndexTripletData*)chunk->m_oldPtr;
287
memPtr->m_3indices16 = (btShortIntIndexTripletData*) serializer->getUniquePointer(tmpIndices);
288
for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
290
unsigned short int* tri_indices= (unsigned short int*)(indexbase+gfxindex*indexstride);
291
tmpIndices[gfxindex].m_values[0] = tri_indices[0];
292
tmpIndices[gfxindex].m_values[1] = tri_indices[1];
293
tmpIndices[gfxindex].m_values[2] = tri_indices[2];
295
serializer->finalizeChunk(chunk,"btShortIntIndexTripletData",BT_ARRAY_CODE,(void*)chunk->m_oldPtr);
303
btChunk* chunk = serializer->allocate(sizeof(btCharIndexTripletData),numtriangles);
304
btCharIndexTripletData* tmpIndices = (btCharIndexTripletData*)chunk->m_oldPtr;
305
memPtr->m_3indices8 = (btCharIndexTripletData*) serializer->getUniquePointer(tmpIndices);
306
for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
308
unsigned char* tri_indices= (unsigned char*)(indexbase+gfxindex*indexstride);
309
tmpIndices[gfxindex].m_values[0] = tri_indices[0];
310
tmpIndices[gfxindex].m_values[1] = tri_indices[1];
311
tmpIndices[gfxindex].m_values[2] = tri_indices[2];
313
serializer->finalizeChunk(chunk,"btCharIndexTripletData",BT_ARRAY_CODE,(void*)chunk->m_oldPtr);
332
btChunk* chunk = serializer->allocate(sizeof(btVector3FloatData),numverts);
333
btVector3FloatData* tmpVertices = (btVector3FloatData*) chunk->m_oldPtr;
334
memPtr->m_vertices3f = (btVector3FloatData *)serializer->getUniquePointer(tmpVertices);
335
for (int i=0;i<numverts;i++)
337
graphicsbase = (float*)(vertexbase+i*stride);
338
tmpVertices[i].m_floats[0] = graphicsbase[0];
339
tmpVertices[i].m_floats[1] = graphicsbase[1];
340
tmpVertices[i].m_floats[2] = graphicsbase[2];
342
serializer->finalizeChunk(chunk,"btVector3FloatData",BT_ARRAY_CODE,(void*)chunk->m_oldPtr);
351
btChunk* chunk = serializer->allocate(sizeof(btVector3DoubleData),numverts);
352
btVector3DoubleData* tmpVertices = (btVector3DoubleData*) chunk->m_oldPtr;
353
memPtr->m_vertices3d = (btVector3DoubleData *) serializer->getUniquePointer(tmpVertices);
354
for (int i=0;i<numverts;i++)
356
double* graphicsbase = (double*)(vertexbase+i*stride);//for now convert to float, might leave it at double
357
tmpVertices[i].m_floats[0] = graphicsbase[0];
358
tmpVertices[i].m_floats[1] = graphicsbase[1];
359
tmpVertices[i].m_floats[2] = graphicsbase[2];
361
serializer->finalizeChunk(chunk,"btVector3DoubleData",BT_ARRAY_CODE,(void*)chunk->m_oldPtr);
367
btAssert((type == PHY_FLOAT) || (type == PHY_DOUBLE));
370
unLockReadOnlyVertexBase(part);
373
serializer->finalizeChunk(chunk,"btMeshPartData",BT_ARRAY_CODE,chunk->m_oldPtr);
377
m_scaling.serializeFloat(trimeshData->m_scaling);
378
return "btStridingMeshInterfaceData";