1 | /* |
---|
2 | Bullet Continuous Collision Detection and Physics Library |
---|
3 | Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/ |
---|
4 | |
---|
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: |
---|
10 | |
---|
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. |
---|
14 | */ |
---|
15 | |
---|
16 | #include "btStridingMeshInterface.h" |
---|
17 | |
---|
18 | btStridingMeshInterface::~btStridingMeshInterface() |
---|
19 | { |
---|
20 | |
---|
21 | } |
---|
22 | |
---|
23 | |
---|
24 | void btStridingMeshInterface::InternalProcessAllTriangles(btInternalTriangleIndexCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const |
---|
25 | { |
---|
26 | (void)aabbMin; |
---|
27 | (void)aabbMax; |
---|
28 | int numtotalphysicsverts = 0; |
---|
29 | int part,graphicssubparts = getNumSubParts(); |
---|
30 | const unsigned char * vertexbase; |
---|
31 | const unsigned char * indexbase; |
---|
32 | int indexstride; |
---|
33 | PHY_ScalarType type; |
---|
34 | PHY_ScalarType gfxindextype; |
---|
35 | int stride,numverts,numtriangles; |
---|
36 | int gfxindex; |
---|
37 | btVector3 triangle[3]; |
---|
38 | btScalar* graphicsbase; |
---|
39 | |
---|
40 | btVector3 meshScaling = getScaling(); |
---|
41 | |
---|
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++) |
---|
44 | { |
---|
45 | getLockedReadOnlyVertexIndexBase(&vertexbase,numverts,type,stride,&indexbase,indexstride,numtriangles,gfxindextype,part); |
---|
46 | numtotalphysicsverts+=numtriangles*3; //upper bound |
---|
47 | |
---|
48 | switch (gfxindextype) |
---|
49 | { |
---|
50 | case PHY_INTEGER: |
---|
51 | { |
---|
52 | for (gfxindex=0;gfxindex<numtriangles;gfxindex++) |
---|
53 | { |
---|
54 | unsigned int* tri_indices= (unsigned int*)(indexbase+gfxindex*indexstride); |
---|
55 | graphicsbase = (btScalar*)(vertexbase+tri_indices[0]*stride); |
---|
56 | triangle[0].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(),graphicsbase[2]*meshScaling.getZ()); |
---|
57 | graphicsbase = (btScalar*)(vertexbase+tri_indices[1]*stride); |
---|
58 | triangle[1].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(), graphicsbase[2]*meshScaling.getZ()); |
---|
59 | graphicsbase = (btScalar*)(vertexbase+tri_indices[2]*stride); |
---|
60 | triangle[2].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(), graphicsbase[2]*meshScaling.getZ()); |
---|
61 | callback->internalProcessTriangleIndex(triangle,part,gfxindex); |
---|
62 | } |
---|
63 | break; |
---|
64 | } |
---|
65 | case PHY_SHORT: |
---|
66 | { |
---|
67 | for (gfxindex=0;gfxindex<numtriangles;gfxindex++) |
---|
68 | { |
---|
69 | unsigned short int* tri_indices= (unsigned short int*)(indexbase+gfxindex*indexstride); |
---|
70 | graphicsbase = (btScalar*)(vertexbase+tri_indices[0]*stride); |
---|
71 | triangle[0].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(),graphicsbase[2]*meshScaling.getZ()); |
---|
72 | graphicsbase = (btScalar*)(vertexbase+tri_indices[1]*stride); |
---|
73 | triangle[1].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(), graphicsbase[2]*meshScaling.getZ()); |
---|
74 | graphicsbase = (btScalar*)(vertexbase+tri_indices[2]*stride); |
---|
75 | triangle[2].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(), graphicsbase[2]*meshScaling.getZ()); |
---|
76 | callback->internalProcessTriangleIndex(triangle,part,gfxindex); |
---|
77 | } |
---|
78 | break; |
---|
79 | } |
---|
80 | default: |
---|
81 | btAssert((gfxindextype == PHY_INTEGER) || (gfxindextype == PHY_SHORT)); |
---|
82 | } |
---|
83 | |
---|
84 | unLockReadOnlyVertexBase(part); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | void btStridingMeshInterface::calculateAabbBruteForce(btVector3& aabbMin,btVector3& aabbMax) |
---|
89 | { |
---|
90 | |
---|
91 | struct AabbCalculationCallback : public btInternalTriangleIndexCallback |
---|
92 | { |
---|
93 | btVector3 m_aabbMin; |
---|
94 | btVector3 m_aabbMax; |
---|
95 | |
---|
96 | AabbCalculationCallback() |
---|
97 | { |
---|
98 | m_aabbMin.setValue(btScalar(1e30),btScalar(1e30),btScalar(1e30)); |
---|
99 | m_aabbMax.setValue(btScalar(-1e30),btScalar(-1e30),btScalar(-1e30)); |
---|
100 | } |
---|
101 | |
---|
102 | virtual void internalProcessTriangleIndex(btVector3* triangle,int partId,int triangleIndex) |
---|
103 | { |
---|
104 | (void)partId; |
---|
105 | (void)triangleIndex; |
---|
106 | |
---|
107 | m_aabbMin.setMin(triangle[0]); |
---|
108 | m_aabbMax.setMax(triangle[0]); |
---|
109 | m_aabbMin.setMin(triangle[1]); |
---|
110 | m_aabbMax.setMax(triangle[1]); |
---|
111 | m_aabbMin.setMin(triangle[2]); |
---|
112 | m_aabbMax.setMax(triangle[2]); |
---|
113 | } |
---|
114 | }; |
---|
115 | |
---|
116 | //first calculate the total aabb for all triangles |
---|
117 | AabbCalculationCallback aabbCallback; |
---|
118 | aabbMin.setValue(btScalar(-1e30),btScalar(-1e30),btScalar(-1e30)); |
---|
119 | aabbMax.setValue(btScalar(1e30),btScalar(1e30),btScalar(1e30)); |
---|
120 | InternalProcessAllTriangles(&aabbCallback,aabbMin,aabbMax); |
---|
121 | |
---|
122 | aabbMin = aabbCallback.m_aabbMin; |
---|
123 | aabbMax = aabbCallback.m_aabbMax; |
---|
124 | } |
---|