expose getVoxelEnclosingPoint() to JS, and clean up JS version of findRayIntersection()

This commit is contained in:
ZappoMan 2014-03-02 20:28:20 -08:00
parent f245aa6ac5
commit bde39d9832
2 changed files with 36 additions and 15 deletions

View file

@ -84,21 +84,18 @@ void VoxelsScriptingInterface::eraseVoxel(float x, float y, float z, float scale
RayToVoxelIntersectionResult VoxelsScriptingInterface::findRayIntersection(const PickRay& ray) {
RayToVoxelIntersectionResult result;
if (_tree) {
if (_tree->tryLockForRead()) {
OctreeElement* element;
result.intersects = _tree->findRayIntersection(ray.origin, ray.direction, element, result.distance, result.face);
if (result.intersects) {
VoxelTreeElement* voxel = (VoxelTreeElement*)element;
result.voxel.x = voxel->getCorner().x;
result.voxel.y = voxel->getCorner().y;
result.voxel.z = voxel->getCorner().z;
result.voxel.s = voxel->getScale();
result.voxel.red = voxel->getColor()[0];
result.voxel.green = voxel->getColor()[1];
result.voxel.blue = voxel->getColor()[2];
result.intersection = ray.origin + (ray.direction * result.distance);
}
_tree->unlock();
OctreeElement* element;
result.intersects = _tree->findRayIntersection(ray.origin, ray.direction, element, result.distance, result.face);
if (result.intersects) {
VoxelTreeElement* voxel = (VoxelTreeElement*)element;
result.voxel.x = voxel->getCorner().x;
result.voxel.y = voxel->getCorner().y;
result.voxel.z = voxel->getCorner().z;
result.voxel.s = voxel->getScale();
result.voxel.red = voxel->getColor()[0];
result.voxel.green = voxel->getColor()[1];
result.voxel.blue = voxel->getColor()[2];
result.intersection = ray.origin + (ray.direction * result.distance);
}
}
return result;
@ -121,3 +118,22 @@ glm::vec3 VoxelsScriptingInterface::getFaceVector(const QString& face) {
return glm::vec3(0, 0, 0); //error case
}
VoxelDetail VoxelsScriptingInterface::getVoxelEnclosingPoint(const glm::vec3& point) {
VoxelDetail result = { 0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 0 };
if (_tree) {
OctreeElement* element = _tree->getElementEnclosingPoint(point);
if (element) {
VoxelTreeElement* voxel = static_cast<VoxelTreeElement*>(element);
result.x = voxel->getCorner().x;
result.y = voxel->getCorner().y;
result.z = voxel->getCorner().z;
result.s = voxel->getScale();
result.red = voxel->getColor()[0];
result.green = voxel->getColor()[1];
result.blue = voxel->getColor()[2];
}
}
return result;
}

View file

@ -70,6 +70,11 @@ public slots:
/// returns a voxel space axis aligned vector for the face, useful in doing voxel math
glm::vec3 getFaceVector(const QString& face);
/// checks the local voxel tree for the smallest voxel enclosing the point
/// \param point the x,y,z coordinates of the point (in meter units)
/// \return VoxelDetail - if no voxel encloses the point then VoxelDetail items will be 0
VoxelDetail getVoxelEnclosingPoint(const glm::vec3& point);
private:
void queueVoxelAdd(PacketType addPacketType, VoxelDetail& addVoxelDetails);