diff --git a/libraries/voxels/src/AABox.cpp b/libraries/voxels/src/AABox.cpp index 1d85b3181a..3c28401c66 100755 --- a/libraries/voxels/src/AABox.cpp +++ b/libraries/voxels/src/AABox.cpp @@ -80,37 +80,37 @@ bool AABox::contains(const glm::vec3& point) const { isWithin(point.z, _corner.z, _size.z); } -// finds the intersection between the closer plane in one direction -static bool findIntersection(float origin, float direction, float corner, float size, float* t) { +// finds the intersection between a ray and the facing plane on one axis +static bool findIntersection(float origin, float direction, float corner, float size, float& distance) { if (direction > EPSILON) { - *t = (corner - origin) / direction; + distance = (corner - origin) / direction; return true; } else if (direction < -EPSILON) { - *t = (corner + size - origin) / direction; + distance = (corner + size - origin) / direction; return true; } return false; } -bool AABox::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float* t) const { +bool AABox::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance) const { // handle the trivial case where the box contains the origin if (contains(origin)) { - *t = 0; + distance = 0; return true; } - // check each direction - float nt; - if (findIntersection(origin.x, direction.x, _corner.x, _size.x, &nt) && nt >= 0 && - isWithin(origin.y + nt*direction.y, _corner.y, _size.y) && - isWithin(origin.z + nt*direction.z, _corner.z, _size.z) || - findIntersection(origin.y, direction.y, _corner.y, _size.y, &nt) && nt >= 0 && - isWithin(origin.x + nt*direction.x, _corner.x, _size.x) && - isWithin(origin.z + nt*direction.z, _corner.z, _size.z) || - findIntersection(origin.z, direction.z, _corner.z, _size.z, &nt) && nt >= 0 && - isWithin(origin.y + nt*direction.y, _corner.y, _size.y) && - isWithin(origin.x + nt*direction.x, _corner.x, _size.x)) { - *t = nt; + // check each axis + float axisDistance; + if (findIntersection(origin.x, direction.x, _corner.x, _size.x, axisDistance) && axisDistance >= 0 && + isWithin(origin.y + axisDistance*direction.y, _corner.y, _size.y) && + isWithin(origin.z + axisDistance*direction.z, _corner.z, _size.z) || + findIntersection(origin.y, direction.y, _corner.y, _size.y, axisDistance) && axisDistance >= 0 && + isWithin(origin.x + axisDistance*direction.x, _corner.x, _size.x) && + isWithin(origin.z + axisDistance*direction.z, _corner.z, _size.z) || + findIntersection(origin.z, direction.z, _corner.z, _size.z, axisDistance) && axisDistance >= 0 && + isWithin(origin.y + axisDistance*direction.y, _corner.y, _size.y) && + isWithin(origin.x + axisDistance*direction.x, _corner.x, _size.x)) { + distance = axisDistance; return true; } return false; diff --git a/libraries/voxels/src/AABox.h b/libraries/voxels/src/AABox.h index 0dae354860..0184355de2 100755 --- a/libraries/voxels/src/AABox.h +++ b/libraries/voxels/src/AABox.h @@ -36,7 +36,7 @@ public: const glm::vec3& getSize() const { return _size; }; bool contains(const glm::vec3& point) const; - bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float* t) const; + bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance) const; private: glm::vec3 _corner; diff --git a/libraries/voxels/src/VoxelTree.cpp b/libraries/voxels/src/VoxelTree.cpp index 263c2f016e..23027de420 100644 --- a/libraries/voxels/src/VoxelTree.cpp +++ b/libraries/voxels/src/VoxelTree.cpp @@ -551,8 +551,8 @@ class RayArgs { public: glm::vec3 origin; glm::vec3 direction; - VoxelNode** node; - float* t; + VoxelNode*& node; + float& distance; bool found; }; @@ -560,24 +560,24 @@ bool findRayOperation(VoxelNode* node, void* extraData) { RayArgs* args = static_cast(extraData); AABox box; node->getAABox(box); - float t; - if (!box.findRayIntersection(args->origin, args->direction, &t)) { + float distance; + if (!box.findRayIntersection(args->origin, args->direction, distance)) { return false; } if (!node->isLeaf()) { return true; // recurse on children } - if (!args->found || t < *(args->t)) { - *(args->node) = node; - *(args->t) = t; + if (!args->found || distance < args->distance) { + args->node = node; + args->distance = distance; args->found = true; } return false; } -bool VoxelTree::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, VoxelNode** node, float* t) +bool VoxelTree::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, VoxelNode*& node, float& distance) { - RayArgs args = { origin / (float)TREE_SCALE, direction, node, t }; + RayArgs args = { origin / (float)TREE_SCALE, direction, node, distance }; recurseTreeWithOperation(findRayOperation, &args); return args.found; } diff --git a/libraries/voxels/src/VoxelTree.h b/libraries/voxels/src/VoxelTree.h index e0346ebdb5..221a04138b 100644 --- a/libraries/voxels/src/VoxelTree.h +++ b/libraries/voxels/src/VoxelTree.h @@ -62,7 +62,7 @@ public: void clearDirtyBit() { _isDirty = false; }; unsigned long int getNodesChangedFromBitstream() const { return _nodesChangedFromBitstream; }; - bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, VoxelNode** node, float* t); + bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, VoxelNode*& node, float& distance); private: int encodeTreeBitstreamRecursion(int maxEncodeLevel, int& currentEncodeLevel,