diff --git a/examples/voxels.js b/examples/voxels.js index 547585198c..0776bce627 100644 --- a/examples/voxels.js +++ b/examples/voxels.js @@ -3,6 +3,7 @@ var shiftHeld = false; Script.include([ "libraries/toolBars.js", + "libraries/utils.js", ]); var isActive = false; @@ -23,10 +24,6 @@ function floorVector(v) { return {x: Math.floor(v.x), y: Math.floor(v.y), z: Math.floor(v.z)}; } -function vectorToString(v){ - return "{" + v.x + ", " + v.y + ", " + v.z + "}"; -} - var toolBar = (function () { var that = {}, toolBar, @@ -251,16 +248,10 @@ function addTerrainBlock() { if (alreadyThere) { // there is already a terrain block under MyAvatar. // try in front of the avatar. - print("alreadyThere = " + alreadyThere); - print("MyAvatar.position = " + vectorToString(MyAvatar.position)); - print("baseLocation = " + vectorToString(baseLocation)); facingPosition = Vec3.sum(MyAvatar.position, Vec3.multiply(8.0, Quat.getFront(Camera.getOrientation()))); facingPosition = Vec3.sum(facingPosition, {x:8, y:8, z:8}); - print("facingPosition = " + vectorToString(facingPosition)); baseLocation = getTerrainAlignedLocation(facingPosition); - print("baseLocation = " + vectorToString(baseLocation)); alreadyThere = lookupTerrainForLocation(baseLocation); - print("alreadyThere = " + alreadyThere); if (alreadyThere) { return; } @@ -281,8 +272,6 @@ function addTerrainBlock() { var AvatarPositionInVoxelCoords = Entities.worldCoordsToVoxelCoords(polyVoxID, MyAvatar.position); // TODO -- how to find the avatar's feet? var topY = Math.round(AvatarPositionInVoxelCoords.y) - 4; - // Entities.setAllVoxels(polyVoxID, 255); - // Entities.setVoxelsInCuboid(polyVoxID, {x:0, y:topY, z:0}, {x:16, y:64 - topY, z:16}, 0); Entities.setVoxelsInCuboid(polyVoxID, {x:0, y:0, z:0}, {x:16, y:topY, z:16}, 255); diff --git a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp index f393391780..43f97c23ee 100644 --- a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp @@ -136,7 +136,7 @@ glm::mat4 RenderablePolyVoxEntityItem::voxelToLocalMatrix() const { glm::vec3 center = getCenterPosition(); glm::vec3 position = getPosition(); glm::vec3 positionToCenter = center - position; - positionToCenter -= getDimensions() * glm::vec3(0.5f, 0.5f, 0.5f) - getSurfacePositionAdjustment(); + positionToCenter -= getDimensions() * Vectors::HALF - getSurfacePositionAdjustment(); glm::mat4 centerToCorner = glm::translate(glm::mat4(), positionToCenter); glm::mat4 scaled = glm::scale(centerToCorner, scale); return scaled; @@ -387,8 +387,8 @@ bool RenderablePolyVoxEntityItem::findDetailedRayIntersection(const glm::vec3& o glm::vec3 result3 = vec3(result); AABox voxelBox; - voxelBox += result3 + glm::vec3(-0.5f, -0.5f, -0.5f); - voxelBox += result3 + glm::vec3(0.5f, 0.5f, 0.5f); + voxelBox += result3 - Vectors::HALF; + voxelBox += result3 + Vectors::HALF; float voxelDistance; @@ -589,9 +589,9 @@ namespace render { glm::vec3 RenderablePolyVoxEntityItem::voxelCoordsToWorldCoords(glm::vec3& voxelCoords) const { glm::vec3 adjustedCoords; if (isEdged(_voxelSurfaceStyle)) { - adjustedCoords = voxelCoords + glm::vec3(0.5f, 0.5f, 0.5f); + adjustedCoords = voxelCoords + Vectors::HALF; } else { - adjustedCoords = voxelCoords - glm::vec3(0.5f, 0.5f, 0.5f); + adjustedCoords = voxelCoords - Vectors::HALF; } return glm::vec3(voxelToWorldMatrix() * glm::vec4(adjustedCoords, 1.0f)); } @@ -599,9 +599,9 @@ glm::vec3 RenderablePolyVoxEntityItem::voxelCoordsToWorldCoords(glm::vec3& voxel glm::vec3 RenderablePolyVoxEntityItem::worldCoordsToVoxelCoords(glm::vec3& worldCoords) const { glm::vec3 result = glm::vec3(worldToVoxelMatrix() * glm::vec4(worldCoords, 1.0f)); if (isEdged(_voxelSurfaceStyle)) { - return result - glm::vec3(0.5f, 0.5f, 0.5f); + return result - Vectors::HALF; } - return result + glm::vec3(0.5f, 0.5f, 0.5f); + return result + Vectors::HALF; } glm::vec3 RenderablePolyVoxEntityItem::voxelCoordsToLocalCoords(glm::vec3& voxelCoords) const { @@ -656,7 +656,7 @@ void RenderablePolyVoxEntityItem::setVoxelVolumeSize(glm::vec3 voxelVolumeSize) bool RenderablePolyVoxEntityItem::inUserBounds(const PolyVox::SimpleVolume* vol, PolyVoxEntityItem::PolyVoxSurfaceStyle surfaceStyle, - int x, int y, int z) { + int x, int y, int z) const { // x, y, z are in user voxel-coords, not adjusted-for-edge voxel-coords. if (isEdged(surfaceStyle)) { if (x < 0 || y < 0 || z < 0 || diff --git a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h index 3b10a12bd2..01578b5e58 100644 --- a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h +++ b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h @@ -141,7 +141,7 @@ private: int _onCount; // how many non-zero voxels are in _volData bool inUserBounds(const PolyVox::SimpleVolume* vol, PolyVoxEntityItem::PolyVoxSurfaceStyle surfaceStyle, - int x, int y, int z); + int x, int y, int z) const; uint8_t getVoxelInternal(int x, int y, int z); bool setVoxelInternal(int x, int y, int z, uint8_t toValue); bool updateOnCount(int x, int y, int z, uint8_t toValue); diff --git a/libraries/shared/src/GLMHelpers.cpp b/libraries/shared/src/GLMHelpers.cpp index 4ca8ed330b..ead08f5654 100644 --- a/libraries/shared/src/GLMHelpers.cpp +++ b/libraries/shared/src/GLMHelpers.cpp @@ -27,6 +27,7 @@ const vec3 Vectors::MAX{ FLT_MAX }; const vec3 Vectors::MIN{ -FLT_MAX }; const vec3 Vectors::ZERO{ 0.0f }; const vec3 Vectors::ONE{ 1.0f }; +const vec3 Vectors::HALF{ 0.5f }; const vec3& Vectors::RIGHT = Vectors::UNIT_X; const vec3& Vectors::UP = Vectors::UNIT_Y; const vec3& Vectors::FRONT = Vectors::UNIT_NEG_Z; diff --git a/libraries/shared/src/GLMHelpers.h b/libraries/shared/src/GLMHelpers.h index 4b03ed2525..e05e075e5d 100644 --- a/libraries/shared/src/GLMHelpers.h +++ b/libraries/shared/src/GLMHelpers.h @@ -70,6 +70,7 @@ public: static const vec3 MIN; static const vec3 ZERO; static const vec3 ONE; + static const vec3 HALF; static const vec3& RIGHT; static const vec3& UP; static const vec3& FRONT;