From dd5e34bc92411472d639130f7d5d65a8691d1ed4 Mon Sep 17 00:00:00 2001 From: David Williams Date: Tue, 22 Jul 2014 23:48:18 +0200 Subject: [PATCH] Moved normal encoding logic. --- .../MarchingCubesSurfaceExtractor.h | 14 ++++++++ .../MarchingCubesSurfaceExtractor.inl | 33 ++----------------- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.h b/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.h index 4b96b432..e927af7a 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.h +++ b/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.h @@ -64,6 +64,20 @@ namespace PolyVox return result; } + inline uint16_t encodeNormal(const Vector3DFloat& normal) + { + Vector3DFloat v3dNormal = normal; + v3dNormal += Vector3DFloat(1.0f, 1.0f, 1.0f); + uint16_t encodedX = static_cast(roundToNearestInteger(v3dNormal.getX() * 15.5f)); + uint16_t encodedY = static_cast(roundToNearestInteger(v3dNormal.getY() * 15.5f)); + uint16_t encodedZ = static_cast(roundToNearestInteger(v3dNormal.getZ() * 15.5f)); + POLYVOX_ASSERT(encodedX < 32, "Encoded value out of range"); + POLYVOX_ASSERT(encodedY < 32, "Encoded value out of range"); + POLYVOX_ASSERT(encodedZ < 32, "Encoded value out of range"); + uint16_t encodedNormal = (encodedX << 10) | (encodedY << 5) | encodedZ; + return encodedNormal; + } + /// Decodes a normal from a MarchingCubesVertex inline Vector3DFloat decode(const uint16_t encodedNormal) { diff --git a/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl b/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl index 19968cd6..cbbcd9e8 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl @@ -451,21 +451,12 @@ namespace PolyVox v3dNormal.normalise(); } - v3dNormal += Vector3DFloat(1.0f, 1.0f, 1.0f); - uint16_t encodedX = static_cast(roundToNearestInteger(v3dNormal.getX() * 15.5f)); - uint16_t encodedY = static_cast(roundToNearestInteger(v3dNormal.getY() * 15.5f)); - uint16_t encodedZ = static_cast(roundToNearestInteger(v3dNormal.getZ() * 15.5f)); - POLYVOX_ASSERT(encodedX < 32, "Encoded value out of range"); - POLYVOX_ASSERT(encodedY < 32, "Encoded value out of range"); - POLYVOX_ASSERT(encodedZ < 32, "Encoded value out of range"); - uint16_t encodedNormal = (encodedX << 10) | (encodedY << 5) | encodedZ; - // Allow the controller to decide how the material should be derived from the voxels. const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v100, fInterp); MarchingCubesVertex surfaceVertex; surfaceVertex.encodedPosition = v3dScaledPosition; - surfaceVertex.encodedNormal = encodedNormal; + surfaceVertex.encodedNormal = encodeNormal(v3dNormal); surfaceVertex.data = uMaterial; const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); @@ -494,21 +485,12 @@ namespace PolyVox v3dNormal.normalise(); } - v3dNormal += Vector3DFloat(1.0f, 1.0f, 1.0f); - uint16_t encodedX = static_cast(roundToNearestInteger(v3dNormal.getX() * 15.5f)); - uint16_t encodedY = static_cast(roundToNearestInteger(v3dNormal.getY() * 15.5f)); - uint16_t encodedZ = static_cast(roundToNearestInteger(v3dNormal.getZ() * 15.5f)); - POLYVOX_ASSERT(encodedX < 32, "Encoded value out of range"); - POLYVOX_ASSERT(encodedY < 32, "Encoded value out of range"); - POLYVOX_ASSERT(encodedZ < 32, "Encoded value out of range"); - uint16_t encodedNormal = (encodedX << 10) | (encodedY << 5) | encodedZ; - // Allow the controller to decide how the material should be derived from the voxels. const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v010, fInterp); MarchingCubesVertex surfaceVertex; surfaceVertex.encodedPosition = v3dScaledPosition; - surfaceVertex.encodedNormal = encodedNormal; + surfaceVertex.encodedNormal = encodeNormal(v3dNormal); surfaceVertex.data = uMaterial; uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); @@ -536,21 +518,12 @@ namespace PolyVox v3dNormal.normalise(); } - v3dNormal += Vector3DFloat(1.0f, 1.0f, 1.0f); - uint16_t encodedX = static_cast(roundToNearestInteger(v3dNormal.getX() * 15.5f)); - uint16_t encodedY = static_cast(roundToNearestInteger(v3dNormal.getY() * 15.5f)); - uint16_t encodedZ = static_cast(roundToNearestInteger(v3dNormal.getZ() * 15.5f)); - POLYVOX_ASSERT(encodedX < 32, "Encoded value out of range"); - POLYVOX_ASSERT(encodedY < 32, "Encoded value out of range"); - POLYVOX_ASSERT(encodedZ < 32, "Encoded value out of range"); - uint16_t encodedNormal = (encodedX << 10) | (encodedY << 5) | encodedZ; - // Allow the controller to decide how the material should be derived from the voxels. const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v001, fInterp); MarchingCubesVertex surfaceVertex; surfaceVertex.encodedPosition = v3dScaledPosition; - surfaceVertex.encodedNormal = encodedNormal; + surfaceVertex.encodedNormal = encodeNormal(v3dNormal); surfaceVertex.data = uMaterial; const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex);