From c90d4a9514be17971344c6872dfe8d6a0c87e09a Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 4 Mar 2014 15:07:30 -0800 Subject: [PATCH] fix failed lock causing particle collisions to not work --- libraries/octree/src/Octree.cpp | 79 ++++++++++++++----- libraries/octree/src/Octree.h | 29 ++++--- .../particles/src/ParticleCollisionSystem.cpp | 4 +- 3 files changed, 79 insertions(+), 33 deletions(-) diff --git a/libraries/octree/src/Octree.cpp b/libraries/octree/src/Octree.cpp index 05760ef675..4515deb6b5 100644 --- a/libraries/octree/src/Octree.cpp +++ b/libraries/octree/src/Octree.cpp @@ -590,16 +590,26 @@ bool findRayIntersectionOp(OctreeElement* node, void* extraData) { } bool Octree::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, - OctreeElement*& node, float& distance, BoxFace& face, bool tryLock) { + OctreeElement*& node, float& distance, BoxFace& face, Octree::lockType lockType) { RayArgs args = { origin / (float)(TREE_SCALE), direction, node, distance, face }; - if (!tryLock) { + bool gotLock = false; + if (lockType == Octree::Lock) { lockForRead(); + gotLock = true; + } else if (lockType == Octree::TryLock) { + gotLock = tryLockForRead(); + if (!gotLock) { + return args.found; // if we wanted to tryLock, and we couldn't then just bail... + } } - if (tryLock && tryLockForRead()) { - recurseTreeWithOperation(findRayIntersectionOp, &args); + + recurseTreeWithOperation(findRayIntersectionOp, &args); + + if (gotLock) { unlock(); } + return args.found; } @@ -635,7 +645,7 @@ bool findSpherePenetrationOp(OctreeElement* element, void* extraData) { } bool Octree::findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration, - void** penetratedObject, bool tryLock) { + void** penetratedObject, Octree::lockType lockType) { SphereArgs args = { center / (float)(TREE_SCALE), @@ -644,17 +654,27 @@ bool Octree::findSpherePenetration(const glm::vec3& center, float radius, glm::v false, NULL }; penetration = glm::vec3(0.0f, 0.0f, 0.0f); - - if (!tryLock) { + + bool gotLock = false; + if (lockType == Octree::Lock) { lockForRead(); - } - if (tryLock && tryLockForRead()) { - recurseTreeWithOperation(findSpherePenetrationOp, &args); - if (penetratedObject) { - *penetratedObject = args.penetratedObject; + gotLock = true; + } else if (lockType == Octree::TryLock) { + gotLock = tryLockForRead(); + if (!gotLock) { + return args.found; // if we wanted to tryLock, and we couldn't then just bail... } + } + + recurseTreeWithOperation(findSpherePenetrationOp, &args); + if (penetratedObject) { + *penetratedObject = args.penetratedObject; + } + + if (gotLock) { unlock(); } + return args.found; } @@ -689,7 +709,7 @@ bool findCapsulePenetrationOp(OctreeElement* node, void* extraData) { } bool Octree::findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, - glm::vec3& penetration, bool tryLock) { + glm::vec3& penetration, Octree::lockType lockType) { CapsuleArgs args = { start / (float)(TREE_SCALE), @@ -699,11 +719,20 @@ bool Octree::findCapsulePenetration(const glm::vec3& start, const glm::vec3& end false }; penetration = glm::vec3(0.0f, 0.0f, 0.0f); - if (!tryLock) { + bool gotLock = false; + if (lockType == Octree::Lock) { lockForRead(); + gotLock = true; + } else if (lockType == Octree::TryLock) { + gotLock = tryLockForRead(); + if (!gotLock) { + return args.found; // if we wanted to tryLock, and we couldn't then just bail... + } } - if (tryLock && tryLockForRead()) { - recurseTreeWithOperation(findCapsulePenetrationOp, &args); + + recurseTreeWithOperation(findCapsulePenetrationOp, &args); + + if (gotLock) { unlock(); } return args.found; @@ -732,18 +761,28 @@ bool getElementEnclosingOperation(OctreeElement* element, void* extraData) { return true; // keep looking } -OctreeElement* Octree::getElementEnclosingPoint(const glm::vec3& point, bool tryLock) { +OctreeElement* Octree::getElementEnclosingPoint(const glm::vec3& point, Octree::lockType lockType) { GetElementEnclosingArgs args; args.point = point; args.element = NULL; - if (!tryLock) { + bool gotLock = false; + if (lockType == Octree::Lock) { lockForRead(); + gotLock = true; + } else if (lockType == Octree::TryLock) { + gotLock = tryLockForRead(); + if (!gotLock) { + return args.element; // if we wanted to tryLock, and we couldn't then just bail... + } } - if (tryLock && tryLockForRead()) { - recurseTreeWithOperation(getElementEnclosingOperation, (void*)&args); + + recurseTreeWithOperation(getElementEnclosingOperation, (void*)&args); + + if (gotLock) { unlock(); } + return args.element; } diff --git a/libraries/octree/src/Octree.h b/libraries/octree/src/Octree.h index 21a6929034..4c237b5f56 100644 --- a/libraries/octree/src/Octree.h +++ b/libraries/octree/src/Octree.h @@ -221,16 +221,29 @@ public: void clearDirtyBit() { _isDirty = false; } void setDirtyBit() { _isDirty = true; } + // Octree does not currently handle its own locking, caller must use these to lock/unlock + void lockForRead() { _lock.lockForRead(); } + bool tryLockForRead() { return _lock.tryLockForRead(); } + void lockForWrite() { _lock.lockForWrite(); } + bool tryLockForWrite() { return _lock.tryLockForWrite(); } + void unlock() { _lock.unlock(); } + // output hints from the encode process + typedef enum { + Lock, + TryLock, + NoLock + } lockType; + bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, - OctreeElement*& node, float& distance, BoxFace& face, bool tryLock = true); + OctreeElement*& node, float& distance, BoxFace& face, Octree::lockType lockType = Octree::TryLock); bool findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration, - void** penetratedObject = NULL, bool tryLock = true); + void** penetratedObject = NULL, Octree::lockType lockType = Octree::TryLock); bool findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, - glm::vec3& penetration, bool tryLock = true); + glm::vec3& penetration, Octree::lockType lockType = Octree::TryLock); - OctreeElement* getElementEnclosingPoint(const glm::vec3& point, bool tryLock = true); + OctreeElement* getElementEnclosingPoint(const glm::vec3& point, Octree::lockType lockType = Octree::TryLock); // Note: this assumes the fileFormat is the HIO individual voxels code files void loadOctreeFile(const char* fileName, bool wantColorRandomizer); @@ -238,13 +251,7 @@ public: // these will read/write files that match the wireformat, excluding the 'V' leading void writeToSVOFile(const char* filename, OctreeElement* node = NULL); bool readFromSVOFile(const char* filename); - - // Octree does not currently handle its own locking, caller must use these to lock/unlock - void lockForRead() { _lock.lockForRead(); } - bool tryLockForRead() { return _lock.tryLockForRead(); } - void lockForWrite() { _lock.lockForWrite(); } - bool tryLockForWrite() { return _lock.tryLockForWrite(); } - void unlock() { _lock.unlock(); } + unsigned long getOctreeElementsCount(); diff --git a/libraries/particles/src/ParticleCollisionSystem.cpp b/libraries/particles/src/ParticleCollisionSystem.cpp index 2d272a8f1f..b36b6a3a04 100644 --- a/libraries/particles/src/ParticleCollisionSystem.cpp +++ b/libraries/particles/src/ParticleCollisionSystem.cpp @@ -58,7 +58,7 @@ bool ParticleCollisionSystem::updateOperation(OctreeElement* element, void* extr void ParticleCollisionSystem::update() { // update all particles - if (_particles->tryLockForWrite()) { + if (_particles->tryLockForRead()) { _particles->recurseTreeWithOperation(updateOperation, this); _particles->unlock(); } @@ -117,7 +117,7 @@ void ParticleCollisionSystem::updateCollisionWithParticles(Particle* particleA) const float COLLISION_FREQUENCY = 0.5f; glm::vec3 penetration; Particle* particleB; - if (_particles->findSpherePenetration(center, radius, penetration, (void**)&particleB)) { + if (_particles->findSpherePenetration(center, radius, penetration, (void**)&particleB, Octree::NoLock)) { // NOTE: 'penetration' is the depth that 'particleA' overlaps 'particleB'. // That is, it points from A into B.