From b09de4ff653a7f50d25a1f4f25e59d89265ab851 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Wed, 26 Aug 2015 21:51:28 -0700 Subject: [PATCH] fix worldCoordsToVoxelCoords for non-edged voxels. voxel.js now adds/delete where you click rather than near to there. --- examples/voxels.js | 1 - .../src/RenderablePolyVoxEntityItem.cpp | 30 +++++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/examples/voxels.js b/examples/voxels.js index 4c344a7eee..d9049e2b0c 100644 --- a/examples/voxels.js +++ b/examples/voxels.js @@ -213,7 +213,6 @@ function attemptVoxelChange(pickRayDir, intersection) { } var voxelPosition = Entities.worldCoordsToVoxelCoords(intersection.entityID, intersection.intersection); - voxelPosition = Vec3.subtract(voxelPosition, {x: 0.5, y: 0.5, z: 0.5}); var pickRayDirInVoxelSpace = Entities.localCoordsToVoxelCoords(intersection.entityID, pickRayDir); pickRayDirInVoxelSpace = Vec3.normalize(pickRayDirInVoxelSpace); diff --git a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp index e986866bce..9f3d4faec5 100644 --- a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp @@ -382,17 +382,7 @@ PolyVox::RaycastResult RenderablePolyVoxEntityItem::doRayCast(glm::vec4 originIn _volDataLock.unlock(); // result is in voxel-space coordinates. - switch (_voxelSurfaceStyle) { - case PolyVoxEntityItem::SURFACE_MARCHING_CUBES: - case PolyVoxEntityItem::SURFACE_CUBIC: - result = callback._result + glm::vec4(0.5f, 0.5f, 0.5f, 0.0f); - break; - case PolyVoxEntityItem::SURFACE_EDGED_CUBIC: - case PolyVoxEntityItem::SURFACE_EDGED_MARCHING_CUBES: - result = callback._result - glm::vec4(0.5f, 0.5f, 0.5f, 0.0f); - break; - } - + result = callback._result - glm::vec4(0.5f, 0.5f, 0.5f, 0.0f); return raycastResult; } @@ -559,7 +549,19 @@ glm::vec3 RenderablePolyVoxEntityItem::voxelCoordsToWorldCoords(glm::vec3& voxel } glm::vec3 RenderablePolyVoxEntityItem::worldCoordsToVoxelCoords(glm::vec3& worldCoords) const { - return glm::vec3(worldToVoxelMatrix() * glm::vec4(worldCoords, 1.0f)); + glm::vec3 result = glm::vec3(worldToVoxelMatrix() * glm::vec4(worldCoords, 1.0f)); + switch (_voxelSurfaceStyle) { + case PolyVoxEntityItem::SURFACE_MARCHING_CUBES: + case PolyVoxEntityItem::SURFACE_CUBIC: + result += glm::vec3(0.5f, 0.5f, 0.5f); + break; + case PolyVoxEntityItem::SURFACE_EDGED_CUBIC: + case PolyVoxEntityItem::SURFACE_EDGED_MARCHING_CUBES: + result -= glm::vec3(0.5f, 0.5f, 0.5f); + break; + } + + return result; } glm::vec3 RenderablePolyVoxEntityItem::voxelCoordsToLocalCoords(glm::vec3& voxelCoords) const { @@ -754,6 +756,10 @@ void RenderablePolyVoxEntityItem::decompressVolumeDataAsync() { } _volDataLock.lockForWrite(); + if (!_volData) { + _volDataLock.unlock(); + return; + } _volDataDirty = true; for (int z = 0; z < voxelZSize; z++) { for (int y = 0; y < voxelYSize; y++) {