remove more cruft from VoxelSystem, move getElementEnclosingPoint() to Octree class where it belongs

This commit is contained in:
ZappoMan 2014-03-02 20:12:52 -08:00
parent 855e351f39
commit f245aa6ac5
5 changed files with 95 additions and 70 deletions

View file

@ -1774,38 +1774,6 @@ void VoxelSystem::forceRedrawEntireTree() {
setupNewVoxelsForDrawing(); setupNewVoxelsForDrawing();
} }
class VoxelAndPoint {
public:
VoxelTreeElement* voxel;
glm::vec3 point;
};
// Find the smallest colored voxel enclosing a point (if there is one)
bool VoxelSystem::getVoxelEnclosingOperation(OctreeElement* element, void* extraData) {
VoxelTreeElement* voxel = (VoxelTreeElement*)element;
VoxelAndPoint* args = (VoxelAndPoint*) extraData;
AABox voxelBox = voxel->getAABox();
if (voxelBox.contains(args->point)) {
if (voxel->isColored() && voxel->isLeaf()) {
// we've reached a solid leaf containing the point, return the node.
args->voxel = voxel;
return false;
}
} else {
// The point is not inside this voxel, so stop recursing.
return false;
}
return true; // keep looking
}
VoxelTreeElement* VoxelSystem::getVoxelEnclosing(const glm::vec3& point) {
VoxelAndPoint voxelAndPoint;
voxelAndPoint.point = point;
voxelAndPoint.voxel = NULL;
_tree->recurseTreeWithOperation(getVoxelEnclosingOperation, (void*) &voxelAndPoint);
return voxelAndPoint.voxel;
}
bool VoxelSystem::isViewChanging() { bool VoxelSystem::isViewChanging() {
bool result = false; // assume the best bool result = false; // assume the best

View file

@ -84,8 +84,6 @@ public:
virtual void elementDeleted(OctreeElement* element); virtual void elementDeleted(OctreeElement* element);
virtual void elementUpdated(OctreeElement* element); virtual void elementUpdated(OctreeElement* element);
VoxelTreeElement* getVoxelEnclosing(const glm::vec3& point);
public slots: public slots:
void nodeAdded(SharedNodePointer node); void nodeAdded(SharedNodePointer node);
void nodeKilled(SharedNodePointer node); void nodeKilled(SharedNodePointer node);

View file

@ -87,23 +87,25 @@ void Hand::simulate(float deltaTime, bool isMine) {
// Voxel Drumming with fingertips if enabled // Voxel Drumming with fingertips if enabled
if (Menu::getInstance()->isOptionChecked(MenuOption::VoxelDrumming)) { if (Menu::getInstance()->isOptionChecked(MenuOption::VoxelDrumming)) {
VoxelTreeElement* fingerNode = Application::getInstance()->getVoxels()->getVoxelEnclosing( OctreeElement* fingerElement = Application::getInstance()->getVoxelTree()->getElementEnclosingPoint(
glm::vec3(fingerTipPosition / (float)TREE_SCALE)); glm::vec3(fingerTipPosition / (float)TREE_SCALE));
if (fingerNode) { VoxelTreeElement* fingerVoxel = static_cast<VoxelTreeElement*>(fingerElement);
if (fingerVoxel) {
if (!palm.getIsCollidingWithVoxel()) { if (!palm.getIsCollidingWithVoxel()) {
// Collision has just started // Collision has just started
palm.setIsCollidingWithVoxel(true); palm.setIsCollidingWithVoxel(true);
handleVoxelCollision(&palm, fingerTipPosition, fingerNode, deltaTime); handleVoxelCollision(&palm, fingerTipPosition, fingerVoxel, deltaTime);
// Set highlight voxel // Set highlight voxel
VoxelDetail voxel; VoxelDetail voxel;
glm::vec3 pos = fingerNode->getCorner(); glm::vec3 pos = fingerVoxel->getCorner();
voxel.x = pos.x; voxel.x = pos.x;
voxel.y = pos.y; voxel.y = pos.y;
voxel.z = pos.z; voxel.z = pos.z;
voxel.s = fingerNode->getScale(); voxel.s = fingerVoxel->getScale();
voxel.red = fingerNode->getColor()[0]; voxel.red = fingerVoxel->getColor()[0];
voxel.green = fingerNode->getColor()[1]; voxel.green = fingerVoxel->getColor()[1];
voxel.blue = fingerNode->getColor()[2]; voxel.blue = fingerVoxel->getColor()[2];
Application::getInstance()->setHighlightVoxel(voxel); Application::getInstance()->setHighlightVoxel(voxel);
Application::getInstance()->setIsHighlightVoxel(true); Application::getInstance()->setIsHighlightVoxel(true);
} }

View file

@ -588,9 +588,16 @@ bool findRayIntersectionOp(OctreeElement* node, void* extraData) {
} }
bool Octree::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, bool Octree::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
OctreeElement*& node, float& distance, BoxFace& face) { OctreeElement*& node, float& distance, BoxFace& face, bool tryLock) {
RayArgs args = { origin / (float)(TREE_SCALE), direction, node, distance, face }; RayArgs args = { origin / (float)(TREE_SCALE), direction, node, distance, face };
recurseTreeWithOperation(findRayIntersectionOp, &args);
if (!tryLock) {
lockForRead();
}
if (tryLock && tryLockForRead()) {
recurseTreeWithOperation(findRayIntersectionOp, &args);
unlock();
}
return args.found; return args.found;
} }
@ -626,25 +633,27 @@ bool findSpherePenetrationOp(OctreeElement* element, void* extraData) {
} }
bool Octree::findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration, bool Octree::findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration,
void** penetratedObject) { void** penetratedObject, bool tryLock) {
bool result = false; // assume no penetration SphereArgs args = {
if (tryLockForRead()) { center / (float)(TREE_SCALE),
SphereArgs args = { radius / (float)(TREE_SCALE),
center / (float)(TREE_SCALE), penetration,
radius / (float)(TREE_SCALE), false,
penetration, NULL };
false, penetration = glm::vec3(0.0f, 0.0f, 0.0f);
NULL };
penetration = glm::vec3(0.0f, 0.0f, 0.0f); if (!tryLock) {
lockForRead();
}
if (tryLock && tryLockForRead()) {
recurseTreeWithOperation(findSpherePenetrationOp, &args); recurseTreeWithOperation(findSpherePenetrationOp, &args);
if (penetratedObject) { if (penetratedObject) {
*penetratedObject = args.penetratedObject; *penetratedObject = args.penetratedObject;
} }
unlock(); unlock();
result = args.found;
} }
return result; return args.found;
} }
class CapsuleArgs { class CapsuleArgs {
@ -677,22 +686,67 @@ bool findCapsulePenetrationOp(OctreeElement* node, void* extraData) {
return false; return false;
} }
bool Octree::findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, glm::vec3& penetration) { bool Octree::findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius,
bool result = false; // assume no penetration glm::vec3& penetration, bool tryLock) {
if (tryLockForRead()) {
CapsuleArgs args = { CapsuleArgs args = {
start / (float)(TREE_SCALE), start / (float)(TREE_SCALE),
end / (float)(TREE_SCALE), end / (float)(TREE_SCALE),
radius / (float)(TREE_SCALE), radius / (float)(TREE_SCALE),
penetration }; penetration,
penetration = glm::vec3(0.0f, 0.0f, 0.0f); false };
penetration = glm::vec3(0.0f, 0.0f, 0.0f);
if (!tryLock) {
lockForRead();
}
if (tryLock && tryLockForRead()) {
recurseTreeWithOperation(findCapsulePenetrationOp, &args); recurseTreeWithOperation(findCapsulePenetrationOp, &args);
result = args.found;
unlock(); unlock();
} }
return result; return args.found;
} }
class GetElementEnclosingArgs {
public:
OctreeElement* element;
glm::vec3 point;
};
// Find the smallest colored voxel enclosing a point (if there is one)
bool getElementEnclosingOperation(OctreeElement* element, void* extraData) {
GetElementEnclosingArgs* args = static_cast<GetElementEnclosingArgs*>(extraData);
AABox elementBox = element->getAABox();
if (elementBox.contains(args->point)) {
if (element->hasContent() && element->isLeaf()) {
// we've reached a solid leaf containing the point, return the node.
args->element = element;
return false;
}
} else {
// The point is not inside this voxel, so stop recursing.
return false;
}
return true; // keep looking
}
OctreeElement* Octree::getElementEnclosingPoint(const glm::vec3& point, bool tryLock) {
GetElementEnclosingArgs args;
args.point = point;
args.element = NULL;
if (!tryLock) {
lockForRead();
}
if (tryLock && tryLockForRead()) {
recurseTreeWithOperation(getElementEnclosingOperation, (void*)&args);
unlock();
}
return args.element;
}
int Octree::encodeTreeBitstream(OctreeElement* node, int Octree::encodeTreeBitstream(OctreeElement* node,
OctreePacketData* packetData, OctreeElementBag& bag, OctreePacketData* packetData, OctreeElementBag& bag,
EncodeBitstreamParams& params) { EncodeBitstreamParams& params) {

View file

@ -222,12 +222,15 @@ public:
void setDirtyBit() { _isDirty = true; } void setDirtyBit() { _isDirty = true; }
bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
OctreeElement*& node, float& distance, BoxFace& face); OctreeElement*& node, float& distance, BoxFace& face, bool tryLock = true);
bool findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration, bool findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration,
void** penetratedObject = NULL); void** penetratedObject = NULL, bool tryLock = true);
bool findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, glm::vec3& penetration); bool findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius,
glm::vec3& penetration, bool tryLock = true);
OctreeElement* getElementEnclosingPoint(const glm::vec3& point, bool tryLock = true);
// Note: this assumes the fileFormat is the HIO individual voxels code files // Note: this assumes the fileFormat is the HIO individual voxels code files
void loadOctreeFile(const char* fileName, bool wantColorRandomizer); void loadOctreeFile(const char* fileName, bool wantColorRandomizer);