diff --git a/examples/OpenGL/main.cpp b/examples/OpenGL/main.cpp index a4dc8ed4..a95d66e5 100644 --- a/examples/OpenGL/main.cpp +++ b/examples/OpenGL/main.cpp @@ -127,8 +127,11 @@ int main(int argc, char *argv[]) // Perform the extraction for this region of the volume auto mesh = extractMarchingCubesMesh(&volData, regToExtract); + // The returned mesh needs to be decoded to be appropriate for GPU rendering. + auto decodedMesh = decode(mesh); + //Pass the surface to the OpenGL window - openGLWidget.addMesh(mesh, Vector3DInt32(x, y, z)); + openGLWidget.addMesh(decodedMesh, Vector3DInt32(x, y, z)); meshCounter++; } diff --git a/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl b/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl index 6b6a688b..bbe18ad2 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl @@ -446,6 +446,7 @@ namespace PolyVox const float fInterp = static_cast(m_tThreshold - m_controller.convertToDensity(v000)) / static_cast(m_controller.convertToDensity(v100) - m_controller.convertToDensity(v000)); const Vector3DFloat v3dPosition(static_cast(iXVolSpace - m_regSizeInVoxels.getLowerX()) + fInterp, static_cast(iYVolSpace - m_regSizeInVoxels.getLowerY()), static_cast(iZVolSpace - m_regSizeInCells.getLowerZ())); + const Vector3DUint16 v3dPositionAsUint(static_cast(v3dPosition.getX() * 256.0f), static_cast(v3dPosition.getY() * 256.0f), static_cast(v3dPosition.getZ() * 256.0f)); Vector3DFloat v3dNormal = (n100*fInterp) + (n000*(1-fInterp)); @@ -460,7 +461,7 @@ namespace PolyVox const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v100, fInterp); MarchingCubesVertex surfaceVertex; - surfaceVertex.position = v3dPosition; + surfaceVertex.position = v3dPositionAsUint; surfaceVertex.normal = v3dNormal; surfaceVertex.material = uMaterial; @@ -479,6 +480,7 @@ namespace PolyVox const float fInterp = static_cast(m_tThreshold - m_controller.convertToDensity(v000)) / static_cast(m_controller.convertToDensity(v010) - m_controller.convertToDensity(v000)); const Vector3DFloat v3dPosition(static_cast(iXVolSpace - m_regSizeInVoxels.getLowerX()), static_cast(iYVolSpace - m_regSizeInVoxels.getLowerY()) + fInterp, static_cast(iZVolSpace - m_regSizeInVoxels.getLowerZ())); + const Vector3DUint16 v3dPositionAsUint(static_cast(v3dPosition.getX() * 256.0f), static_cast(v3dPosition.getY() * 256.0f), static_cast(v3dPosition.getZ() * 256.0f)); Vector3DFloat v3dNormal = (n010*fInterp) + (n000*(1-fInterp)); @@ -493,7 +495,7 @@ namespace PolyVox const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v010, fInterp); MarchingCubesVertex surfaceVertex; - surfaceVertex.position = v3dPosition; + surfaceVertex.position = v3dPositionAsUint; surfaceVertex.normal = v3dNormal; surfaceVertex.material = uMaterial; @@ -512,6 +514,7 @@ namespace PolyVox const float fInterp = static_cast(m_tThreshold - m_controller.convertToDensity(v000)) / static_cast(m_controller.convertToDensity(v001) - m_controller.convertToDensity(v000)); const Vector3DFloat v3dPosition(static_cast(iXVolSpace - m_regSizeInVoxels.getLowerX()), static_cast(iYVolSpace - m_regSizeInVoxels.getLowerY()), static_cast(iZVolSpace - m_regSizeInVoxels.getLowerZ()) + fInterp); + const Vector3DUint16 v3dPositionAsUint(static_cast(v3dPosition.getX() * 256.0f), static_cast(v3dPosition.getY() * 256.0f), static_cast(v3dPosition.getZ() * 256.0f)); Vector3DFloat v3dNormal = (n001*fInterp) + (n000*(1-fInterp)); // The gradient for a voxel can be zero (e.g. solid voxel surrounded by empty ones) and so @@ -525,7 +528,7 @@ namespace PolyVox const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v001, fInterp); MarchingCubesVertex surfaceVertex; - surfaceVertex.position = v3dPosition; + surfaceVertex.position = v3dPositionAsUint; surfaceVertex.normal = v3dNormal; surfaceVertex.material = uMaterial; diff --git a/library/PolyVoxCore/include/PolyVoxCore/VertexTypes.h b/library/PolyVoxCore/include/PolyVoxCore/VertexTypes.h index fc53fc2e..1044f472 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/VertexTypes.h +++ b/library/PolyVoxCore/include/PolyVoxCore/VertexTypes.h @@ -70,7 +70,7 @@ namespace PolyVox { typedef _VoxelType VoxelType; - Vector3DFloat position; + Vector3DUint16 position; Vector3DFloat normal; VoxelType material; }; @@ -94,7 +94,8 @@ namespace PolyVox Vertex decode(const MarchingCubesVertex& cubicVertex) { Vertex result; - result.position = cubicVertex.position; + result.position = Vector3DFloat(cubicVertex.position.getX(), cubicVertex.position.getY(), cubicVertex.position.getZ()); + result.position *= (1.0 / 256.0); result.normal = cubicVertex.normal; result.material = cubicVertex.material; return result;