btGpu3DGridBroadphase.h

Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
00003 Copyright (C) 2006, 2009 Sony Computer Entertainment Inc. 
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 //----------------------------------------------------------------------------------------
00017 
00018 #ifndef BTGPU3DGRIDBROADPHASE_H
00019 #define BTGPU3DGRIDBROADPHASE_H
00020 
00021 //----------------------------------------------------------------------------------------
00022 
00023 #include "BulletCollision/BroadphaseCollision/btSimpleBroadphase.h"
00024 
00025 #include "btGpu3DGridBroadphaseSharedTypes.h"
00026 
00027 //----------------------------------------------------------------------------------------
00028 
00030 
00031 class btGpu3DGridBroadphase : public btSimpleBroadphase
00032 {
00033 protected:
00034         bool                    m_bInitialized;
00035     unsigned int        m_numBodies;
00036     unsigned int        m_numCells;
00037         unsigned int    m_maxPairsPerBody;
00038         btScalar                m_cellFactorAABB;
00039     unsigned int        m_maxBodiesPerCell;
00040         bt3DGridBroadphaseParams m_params;
00041         btScalar                m_maxRadius;
00042         // CPU data
00043     unsigned int*       m_hBodiesHash;
00044     unsigned int*       m_hCellStart;
00045         unsigned int*   m_hPairBuffStartCurr;
00046         bt3DGrid3F1U*           m_hAABB;
00047         unsigned int*   m_hPairBuff;
00048         unsigned int*   m_hPairScan;
00049         unsigned int*   m_hPairOut;
00050 // large proxies
00051         int             m_numLargeHandles;                                              
00052         int             m_maxLargeHandles;                                              
00053         int             m_LastLargeHandleIndex;                                                 
00054         btSimpleBroadphaseProxy* m_pLargeHandles;
00055         void* m_pLargeHandlesRawPtr;
00056         int             m_firstFreeLargeHandle;
00057         int allocLargeHandle()
00058         {
00059                 btAssert(m_numLargeHandles < m_maxLargeHandles);
00060                 int freeLargeHandle = m_firstFreeLargeHandle;
00061                 m_firstFreeLargeHandle = m_pLargeHandles[freeLargeHandle].GetNextFree();
00062                 m_numLargeHandles++;
00063                 if(freeLargeHandle > m_LastLargeHandleIndex)
00064                 {
00065                         m_LastLargeHandleIndex = freeLargeHandle;
00066                 }
00067                 return freeLargeHandle;
00068         }
00069         void freeLargeHandle(btSimpleBroadphaseProxy* proxy)
00070         {
00071                 int handle = int(proxy - m_pLargeHandles);
00072                 btAssert((handle >= 0) && (handle < m_maxHandles));
00073                 if(handle == m_LastLargeHandleIndex)
00074                 {
00075                         m_LastLargeHandleIndex--;
00076                 }
00077                 proxy->SetNextFree(m_firstFreeLargeHandle);
00078                 m_firstFreeLargeHandle = handle;
00079                 proxy->m_clientObject = 0;
00080                 m_numLargeHandles--;
00081         }
00082         bool isLargeProxy(const btVector3& aabbMin,  const btVector3& aabbMax);
00083         bool isLargeProxy(btBroadphaseProxy* proxy);
00084 // debug
00085         unsigned int    m_numPairsAdded;
00086         unsigned int    m_numPairsRemoved;
00087         unsigned int    m_numOverflows;
00088 // 
00089 public:
00090         btGpu3DGridBroadphase(const btVector3& worldAabbMin,const btVector3& worldAabbMax, 
00091                                            int gridSizeX, int gridSizeY, int gridSizeZ, 
00092                                            int maxSmallProxies, int maxLargeProxies, int maxPairsPerBody,
00093                                            int maxBodiesPerCell = 8,
00094                                            btScalar cellFactorAABB = btScalar(1.0f));
00095         btGpu3DGridBroadphase(  btOverlappingPairCache* overlappingPairCache,
00096                                                 const btVector3& worldAabbMin,const btVector3& worldAabbMax, 
00097                                                 int gridSizeX, int gridSizeY, int gridSizeZ, 
00098                                                 int maxSmallProxies, int maxLargeProxies, int maxPairsPerBody,
00099                                                 int maxBodiesPerCell = 8,
00100                                                 btScalar cellFactorAABB = btScalar(1.0f));
00101         virtual ~btGpu3DGridBroadphase();
00102         virtual void    calculateOverlappingPairs(btDispatcher* dispatcher);
00103 
00104         virtual btBroadphaseProxy*      createProxy(const btVector3& aabbMin,  const btVector3& aabbMax,int shapeType,void* userPtr ,short int collisionFilterGroup,short int collisionFilterMask, btDispatcher* dispatcher,void* multiSapProxy);
00105         virtual void    destroyProxy(btBroadphaseProxy* proxy,btDispatcher* dispatcher);
00106         virtual void    rayTest(const btVector3& rayFrom,const btVector3& rayTo, btBroadphaseRayCallback& rayCallback, const btVector3& aabbMin=btVector3(0,0,0),const btVector3& aabbMax=btVector3(0,0,0));
00107 
00108     
00109         virtual void    resetPool(btDispatcher* dispatcher);
00110 
00111 protected:
00112         void _initialize(       const btVector3& worldAabbMin,const btVector3& worldAabbMax, 
00113                                                 int gridSizeX, int gridSizeY, int gridSizeZ, 
00114                                                 int maxSmallProxies, int maxLargeProxies, int maxPairsPerBody,
00115                                                 int maxBodiesPerCell = 8,
00116                                                 btScalar cellFactorAABB = btScalar(1.0f));
00117         void _finalize();
00118         void addPairsToCache(btDispatcher* dispatcher);
00119         void addLarge2LargePairsToCache(btDispatcher* dispatcher);
00120 
00121 // overrides for CPU version
00122         virtual void setParameters(bt3DGridBroadphaseParams* hostParams);
00123         virtual void prepareAABB();
00124         virtual void calcHashAABB();
00125         virtual void sortHash();        
00126         virtual void findCellStart();
00127         virtual void findOverlappingPairs();
00128         virtual void findPairsLarge();
00129         virtual void computePairCacheChanges();
00130         virtual void scanOverlappingPairBuff();
00131         virtual void squeezeOverlappingPairBuff();
00132 };
00133 
00134 //----------------------------------------------------------------------------------------
00135 
00136 #endif //BTGPU3DGRIDBROADPHASE_H
00137 
00138 //----------------------------------------------------------------------------------------
00139 //----------------------------------------------------------------------------------------
00140 //----------------------------------------------------------------------------------------