History

make RenderablePolyVoxEntityItem::setSphere affect any voxel the sphere touches rather than just the ones whose center the sphere touches

Seth Alves committed 9 years ago

Commit df875ab1 parent 048da6e3 files · +58 −8Browse files

Unified · Split

3 files
  1. libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp +31 −8
  2. libraries/shared/src/AABox.cpp +26 −0
  3. libraries/shared/src/AABox.h +1 −0
libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp+31−8
@@ -414,19 +414,41 @@ bool RenderablePolyVoxEntityItem::setSphere(glm::vec3 centerWorldCoords, float r · 413 unmodified lines
414 glm::ivec3 lowI = glm::clamp(low, glm::vec3(0.0f), _voxelVolumeSize);414 glm::ivec3 lowI = glm::clamp(low, glm::vec3(0.0f), _voxelVolumeSize);
415 glm::ivec3 highI = glm::clamp(high, glm::vec3(0.0f), _voxelVolumeSize);415 glm::ivec3 highI = glm::clamp(high, glm::vec3(0.0f), _voxelVolumeSize);
416 416
417 // This three-level for loop iterates over every voxel in the volume that might be in the sphere
418 // withWriteLock([&] {
419 // for (int z = lowI.z; z < highI.z; z++) {
420 // for (int y = lowI.y; y < highI.y; y++) {
421 // for (int x = lowI.x; x < highI.x; x++) {
422 // // Store our current position as a vector...
423 // glm::vec4 pos(x + 0.5f, y + 0.5f, z + 0.5f, 1.0); // consider voxels cenetered on their coordinates
424 // // convert to world coordinates
425 // glm::vec3 worldPos = glm::vec3(vtwMatrix * pos);
426 // // compute how far the current position is from the center of the volume
427 // float fDistToCenter = glm::distance(worldPos, centerWorldCoords);
428 // // If the current voxel is less than 'radius' units from the center then we set its value
429 // if (fDistToCenter <= radiusWorldCoords) {
430 // result |= setVoxelInternal(x, y, z, toValue);
431 // }
432 // }
433 // }
434 // }
435 // });
436
437
438 glm::vec3 radials(radiusWorldCoords / voxelSize.x,
439 radiusWorldCoords / voxelSize.y,
440 radiusWorldCoords / voxelSize.z);
441 float minRadial = glm::min(radials.x, radials.y);
442 minRadial = glm::min(minRadial, radials.z);
443
417 // This three-level for loop iterates over every voxel in the volume that might be in the sphere444 // This three-level for loop iterates over every voxel in the volume that might be in the sphere
418 withWriteLock([&] {445 withWriteLock([&] {
419 for (int z = lowI.z; z < highI.z; z++) {446 for (int z = lowI.z; z < highI.z; z++) {
420 for (int y = lowI.y; y < highI.y; y++) {447 for (int y = lowI.y; y < highI.y; y++) {
421 for (int x = lowI.x; x < highI.x; x++) {448 for (int x = lowI.x; x < highI.x; x++) {
422 // Store our current position as a vector...449 AABox voxelBox(glm::vec3(x - 0.5f, y - 0.5f, z - 0.5f), glm::vec3(1.0f, 1.0f, 1.0f));
423 glm::vec4 pos(x + 0.5f, y + 0.5f, z + 0.5f, 1.0); // consider voxels cenetered on their coordinates450 // if (voxelBox.touchesSphere(centerInVoxelCoords, minRadial)) {
424 // convert to world coordinates451 if (voxelBox.touchesAAEllipsoid(centerInVoxelCoords, radials)) {
425 glm::vec3 worldPos = glm::vec3(vtwMatrix * pos);
426 // compute how far the current position is from the center of the volume
427 float fDistToCenter = glm::distance(worldPos, centerWorldCoords);
428 // If the current voxel is less than 'radius' units from the center then we set its value
429 if (fDistToCenter <= radiusWorldCoords) {
430 result |= setVoxelInternal(x, y, z, toValue);452 result |= setVoxelInternal(x, y, z, toValue);
431 }453 }
432 }454 }
@@ -434,6 +456,7 @@ bool RenderablePolyVoxEntityItem::setSphere(glm::vec3 centerWorldCoords, float r · 1 unmodified line
434 }456 }
435 });457 });
436 458
459
437 if (result) {460 if (result) {
438 compressVolumeDataAndSendEditPacket();461 compressVolumeDataAndSendEditPacket();
439 }462 }
libraries/shared/src/AABox.cpp+26−0
@@ -436,6 +436,32 @@ glm::vec3 AABox::getClosestPointOnFace(const glm::vec4& origin, const glm::vec4& · 435 unmodified lines
436 return getClosestPointOnFace(glm::vec3(origin), face);436 return getClosestPointOnFace(glm::vec3(origin), face);
437}437}
438 438
439bool AABox::touchesAAEllipsoid(const glm::vec3& center, glm::vec3 radials) const {
440 if (_corner.x - radials.x > center.x ||
441 _corner.y - radials.y > center.y ||
442 _corner.z - radials.z > center.z ||
443 _corner.x + _scale.x + radials.x < center.x ||
444 _corner.y + _scale.y + radials.y < center.y ||
445 _corner.z + _scale.z + radials.z < center.z) {
446 return false;
447 }
448
449 for (int i = 0; i < FACE_COUNT; i++) {
450 glm::vec3 closest = getClosestPointOnFace(center, (BoxFace)i) - center;
451 float x = closest.x;
452 float y = closest.y;
453 float z = closest.z;
454 float a = radials.x;
455 float b = radials.y;
456 float c = radials.z;
457 if (x*x/(a*a) + y*y/(b*b) + z*z/(c*c) < 1.0f) {
458 return true;
459 }
460 }
461 return false;
462}
463
464
439glm::vec4 AABox::getPlane(BoxFace face) const {465glm::vec4 AABox::getPlane(BoxFace face) const {
440 switch (face) {466 switch (face) {
441 case MIN_X_FACE: return glm::vec4(-1.0f, 0.0f, 0.0f, _corner.x);467 case MIN_X_FACE: return glm::vec4(-1.0f, 0.0f, 0.0f, _corner.x);
libraries/shared/src/AABox.h+1−0
@@ -70,6 +70,7 @@ public: · 69 unmodified lines
70 bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance,70 bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance,
71 BoxFace& face, glm::vec3& surfaceNormal) const;71 BoxFace& face, glm::vec3& surfaceNormal) const;
72 bool touchesSphere(const glm::vec3& center, float radius) const; // fast but may generate false positives72 bool touchesSphere(const glm::vec3& center, float radius) const; // fast but may generate false positives
73 bool touchesAAEllipsoid(const glm::vec3& center, glm::vec3 radials) const;
73 bool findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration) const;74 bool findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration) const;
74 bool findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, glm::vec3& penetration) const;75 bool findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, glm::vec3& penetration) const;
75 76