Back to index

supertuxkart  0.5+dfsg1
btStridingMeshInterface.cpp
Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 #include "btStridingMeshInterface.h"
00017 
00018 btStridingMeshInterface::~btStridingMeshInterface()
00019 {
00020 
00021 }
00022 
00023 
00024 void   btStridingMeshInterface::InternalProcessAllTriangles(btInternalTriangleIndexCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
00025 {
00026        (void)aabbMin;
00027        (void)aabbMax;
00028        int numtotalphysicsverts = 0;
00029        int part,graphicssubparts = getNumSubParts();
00030        const unsigned char * vertexbase;
00031        const unsigned char * indexbase;
00032        int indexstride;
00033        PHY_ScalarType type;
00034        PHY_ScalarType gfxindextype;
00035        int stride,numverts,numtriangles;
00036        int gfxindex;
00037        btVector3 triangle[3];
00038        btScalar* graphicsbase;
00039 
00040        btVector3 meshScaling = getScaling();
00041 
00043        for (part=0;part<graphicssubparts ;part++)
00044        {
00045               getLockedReadOnlyVertexIndexBase(&vertexbase,numverts,type,stride,&indexbase,indexstride,numtriangles,gfxindextype,part);
00046               numtotalphysicsverts+=numtriangles*3; //upper bound
00047 
00048               switch (gfxindextype)
00049               {
00050               case PHY_INTEGER:
00051                      {
00052                             for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
00053                             {
00054                                    int* tri_indices= (int*)(indexbase+gfxindex*indexstride);
00055                                    graphicsbase = (btScalar*)(vertexbase+tri_indices[0]*stride);
00056                                    triangle[0].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(),graphicsbase[2]*meshScaling.getZ());
00057                                    graphicsbase = (btScalar*)(vertexbase+tri_indices[1]*stride);
00058                                    triangle[1].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(),       graphicsbase[2]*meshScaling.getZ());
00059                                    graphicsbase = (btScalar*)(vertexbase+tri_indices[2]*stride);
00060                                    triangle[2].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(),       graphicsbase[2]*meshScaling.getZ());
00061                                    callback->internalProcessTriangleIndex(triangle,part,gfxindex);
00062                             }
00063                             break;
00064                      }
00065               case PHY_SHORT:
00066                      {
00067                             for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
00068                             {
00069                                    short int* tri_indices= (short int*)(indexbase+gfxindex*indexstride);
00070                                    graphicsbase = (btScalar*)(vertexbase+tri_indices[0]*stride);
00071                                    triangle[0].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(),graphicsbase[2]*meshScaling.getZ());
00072                                    graphicsbase = (btScalar*)(vertexbase+tri_indices[1]*stride);
00073                                    triangle[1].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(),       graphicsbase[2]*meshScaling.getZ());
00074                                    graphicsbase = (btScalar*)(vertexbase+tri_indices[2]*stride);
00075                                    triangle[2].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(),       graphicsbase[2]*meshScaling.getZ());
00076                                    callback->internalProcessTriangleIndex(triangle,part,gfxindex);
00077                             }
00078                             break;
00079                      }
00080               default:
00081                      btAssert((gfxindextype == PHY_INTEGER) || (gfxindextype == PHY_SHORT));
00082               }
00083 
00084               unLockReadOnlyVertexBase(part);
00085        }
00086 }
00087 
00088 void   btStridingMeshInterface::calculateAabbBruteForce(btVector3& aabbMin,btVector3& aabbMax)
00089 {
00090 
00091        struct AabbCalculationCallback : public btInternalTriangleIndexCallback
00092        {
00093               btVector3     m_aabbMin;
00094               btVector3     m_aabbMax;
00095 
00096               AabbCalculationCallback()
00097               {
00098                      m_aabbMin.setValue(btScalar(1e30),btScalar(1e30),btScalar(1e30));
00099                      m_aabbMax.setValue(btScalar(-1e30),btScalar(-1e30),btScalar(-1e30));
00100               }
00101 
00102               virtual void internalProcessTriangleIndex(btVector3* triangle,int partId,int  triangleIndex)
00103               {
00104                      (void)partId;
00105                      (void)triangleIndex;
00106 
00107                      m_aabbMin.setMin(triangle[0]);
00108                      m_aabbMax.setMax(triangle[0]);
00109                      m_aabbMin.setMin(triangle[1]);
00110                      m_aabbMax.setMax(triangle[1]);
00111                      m_aabbMin.setMin(triangle[2]);
00112                      m_aabbMax.setMax(triangle[2]);
00113               }
00114        };
00115 
00116               //first calculate the total aabb for all triangles
00117        AabbCalculationCallback     aabbCallback;
00118        aabbMin.setValue(btScalar(-1e30),btScalar(-1e30),btScalar(-1e30));
00119        aabbMax.setValue(btScalar(1e30),btScalar(1e30),btScalar(1e30));
00120        InternalProcessAllTriangles(&aabbCallback,aabbMin,aabbMax);
00121 
00122        aabbMin = aabbCallback.m_aabbMin;
00123        aabbMax = aabbCallback.m_aabbMax;
00124 }