[1966] | 1 | /* |
---|
| 2 | Bullet Continuous Collision Detection and Physics Library |
---|
| 3 | Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com |
---|
| 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 <new> |
---|
| 17 | #include "BulletCollision/CollisionShapes/btCollisionShape.h" |
---|
| 18 | #include "LinearMath/btAlignedAllocator.h" |
---|
| 19 | #include "SpuBatchRaycaster.h" |
---|
| 20 | |
---|
| 21 | SpuBatchRaycaster::SpuBatchRaycaster (class btThreadSupportInterface* threadInterface, int maxNumOutstandingTasks) |
---|
| 22 | { |
---|
| 23 | m_threadInterface = threadInterface; |
---|
| 24 | |
---|
| 25 | castUponObjectWrappers = NULL; |
---|
| 26 | numCastUponObjectWrappers = 0; |
---|
| 27 | |
---|
| 28 | m_spuRaycastTaskProcess = new SpuRaycastTaskProcess(m_threadInterface,maxNumOutstandingTasks); // FIXME non constant |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | SpuBatchRaycaster::~SpuBatchRaycaster () |
---|
| 32 | { |
---|
| 33 | if (castUponObjectWrappers) |
---|
| 34 | { |
---|
| 35 | btAlignedFree (castUponObjectWrappers); |
---|
| 36 | castUponObjectWrappers = NULL; |
---|
| 37 | } |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | void |
---|
| 41 | SpuBatchRaycaster::setCollisionObjects (btCollisionObjectArray& castUponObjects, int numCastUponObjects) |
---|
| 42 | { |
---|
| 43 | if (castUponObjectWrappers) |
---|
| 44 | { |
---|
| 45 | btAlignedFree (castUponObjectWrappers); |
---|
| 46 | castUponObjectWrappers = NULL; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | castUponObjectWrappers = (SpuCollisionObjectWrapper*)btAlignedAlloc (sizeof(SpuCollisionObjectWrapper) * numCastUponObjects,16); |
---|
| 50 | numCastUponObjectWrappers = numCastUponObjects; |
---|
| 51 | |
---|
| 52 | for (int i = 0; i < numCastUponObjectWrappers; i++) |
---|
| 53 | { |
---|
| 54 | castUponObjectWrappers[i] = SpuCollisionObjectWrapper(castUponObjects[i]); |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | void |
---|
| 59 | SpuBatchRaycaster::setCollisionObjectsSkipPE (btCollisionObjectArray& castUponObjects, int numCastUponObjects) |
---|
| 60 | { |
---|
| 61 | if (castUponObjectWrappers) |
---|
| 62 | { |
---|
| 63 | btAlignedFree (castUponObjectWrappers); |
---|
| 64 | castUponObjectWrappers = NULL; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | int numNonPEShapes = 0; |
---|
| 68 | for (int i = 0; i < numCastUponObjects; i++) |
---|
| 69 | { |
---|
| 70 | const btCollisionShape* shape = castUponObjects[i]->getCollisionShape(); |
---|
| 71 | |
---|
| 72 | if (shape->getShapeType () == BOX_SHAPE_PROXYTYPE || |
---|
| 73 | shape->getShapeType () == SPHERE_SHAPE_PROXYTYPE || |
---|
| 74 | shape->getShapeType () == CAPSULE_SHAPE_PROXYTYPE) |
---|
| 75 | { |
---|
| 76 | continue; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | numNonPEShapes++; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | castUponObjectWrappers = (SpuCollisionObjectWrapper*)btAlignedAlloc (sizeof(SpuCollisionObjectWrapper) * numNonPEShapes,16); |
---|
| 83 | numCastUponObjectWrappers = numNonPEShapes; |
---|
| 84 | |
---|
| 85 | int index = 0; |
---|
| 86 | for (int i = 0; i < numCastUponObjects; i++) |
---|
| 87 | { |
---|
| 88 | const btCollisionShape* shape = castUponObjects[i]->getCollisionShape(); |
---|
| 89 | |
---|
| 90 | if (shape->getShapeType () == BOX_SHAPE_PROXYTYPE || |
---|
| 91 | shape->getShapeType () == SPHERE_SHAPE_PROXYTYPE || |
---|
| 92 | shape->getShapeType () == CAPSULE_SHAPE_PROXYTYPE) |
---|
| 93 | { |
---|
| 94 | continue; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | castUponObjectWrappers[index] = SpuCollisionObjectWrapper(castUponObjects[i]); |
---|
| 98 | index++; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | // printf("Number of shapes bullet is casting against: %d\n", numNonPEShapes); |
---|
| 102 | btAssert (index == numNonPEShapes); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | void |
---|
| 106 | SpuBatchRaycaster::addRay (const btVector3& rayFrom, const btVector3& rayTo, const btScalar hitFraction) |
---|
| 107 | { |
---|
| 108 | SpuRaycastTaskWorkUnitOut workUnitOut; |
---|
| 109 | workUnitOut.hitFraction = hitFraction; |
---|
| 110 | workUnitOut.hitNormal = btVector3(0.0, 1.0, 0.0); |
---|
| 111 | |
---|
| 112 | rayBatchOutput.push_back (workUnitOut); |
---|
| 113 | |
---|
| 114 | SpuRaycastTaskWorkUnit workUnit; |
---|
| 115 | workUnit.rayFrom = rayFrom; |
---|
| 116 | workUnit.rayTo = rayTo; |
---|
| 117 | rayBatch.push_back (workUnit); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | void |
---|
| 121 | SpuBatchRaycaster::clearRays () |
---|
| 122 | { |
---|
| 123 | rayBatch.clear (); |
---|
| 124 | rayBatchOutput.clear (); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | void |
---|
| 128 | SpuBatchRaycaster::performBatchRaycast () |
---|
| 129 | { |
---|
| 130 | m_spuRaycastTaskProcess->initialize2 (castUponObjectWrappers, numCastUponObjectWrappers); |
---|
| 131 | |
---|
| 132 | for (int i = 0; i < rayBatch.size(); i++) |
---|
| 133 | { |
---|
| 134 | rayBatch[i].output = &rayBatchOutput[i]; // assign output memory location |
---|
| 135 | m_spuRaycastTaskProcess->addWorkToTask(rayBatch[i]); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | m_spuRaycastTaskProcess->flush2 (); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | const SpuRaycastTaskWorkUnitOut& |
---|
| 142 | SpuBatchRaycaster::operator [] (int i) const |
---|
| 143 | { |
---|
| 144 | return rayBatchOutput[i]; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | int |
---|
| 148 | SpuBatchRaycaster::getNumRays () const |
---|
| 149 | { |
---|
| 150 | return rayBatchOutput.size(); |
---|
| 151 | } |
---|