From bfa14a32dfb4febe0a2d407e09a14712bb4b2bf5 Mon Sep 17 00:00:00 2001 From: David Williams Date: Thu, 24 Jul 2014 16:55:11 +0200 Subject: [PATCH] Split 'decode()' function into several variants so it's not so heavily overloaded. --- examples/Basic/main.cpp | 2 +- examples/DecodeOnGPU/main.cpp | 2 +- examples/OpenGL/main.cpp | 2 +- examples/Paging/main.cpp | 2 +- examples/SmoothLOD/main.cpp | 4 ++-- .../include/PolyVoxCore/CubicSurfaceExtractor.h | 6 +++--- .../PolyVoxCore/MarchingCubesSurfaceExtractor.h | 15 +++++++-------- library/PolyVoxCore/include/PolyVoxCore/Mesh.h | 4 ++-- 8 files changed, 18 insertions(+), 19 deletions(-) diff --git a/examples/Basic/main.cpp b/examples/Basic/main.cpp index 1f4b210e..0770e269 100644 --- a/examples/Basic/main.cpp +++ b/examples/Basic/main.cpp @@ -103,7 +103,7 @@ int main(int argc, char *argv[]) // The surface extractor outputs the mesh in an efficient compressed format which is not directly suitable for rendering. The easiest approach is to // decode this on the CPU as shown below, though more advanced applications can upload the compressed mesh to the GPU and decompress in shader code. - auto decodedMesh = decode(mesh); + auto decodedMesh = decodeMesh(mesh); //Pass the surface to the OpenGL window openGLWidget.addMesh(decodedMesh); diff --git a/examples/DecodeOnGPU/main.cpp b/examples/DecodeOnGPU/main.cpp index 2c0fff6d..7f2d83c8 100644 --- a/examples/DecodeOnGPU/main.cpp +++ b/examples/DecodeOnGPU/main.cpp @@ -151,7 +151,7 @@ int main(int argc, char *argv[]) // The surface extractor outputs the mesh in an efficient compressed format which is not directly suitable for rendering. The easiest approach is to // decode this on the CPU as shown below, though more advanced applications can upload the compressed mesh to the GPU and decompress in shader code. - //auto decodedMesh = decode(mesh); + //auto decodedMesh = decodeMesh(mesh); //Pass the surface to the OpenGL window OpenGLMeshData meshData = buildOpenGLMeshData(mesh); diff --git a/examples/OpenGL/main.cpp b/examples/OpenGL/main.cpp index 12d1068f..bdf700e0 100644 --- a/examples/OpenGL/main.cpp +++ b/examples/OpenGL/main.cpp @@ -128,7 +128,7 @@ int main(int argc, char *argv[]) auto mesh = extractMarchingCubesMesh(&volData, regToExtract); // The returned mesh needs to be decoded to be appropriate for GPU rendering. - auto decodedMesh = decode(mesh); + auto decodedMesh = decodeMesh(mesh); // Pass the surface to the OpenGL window. Note that we are also passing an offset in this multi-mesh example. This is because // the surface extractors return a mesh with 'local space' positions to reduce storage requirements and precision problems. diff --git a/examples/Paging/main.cpp b/examples/Paging/main.cpp index ee2d2d23..58e94b5d 100644 --- a/examples/Paging/main.cpp +++ b/examples/Paging/main.cpp @@ -189,7 +189,7 @@ int main(int argc, char *argv[]) auto mesh = extractCubicMesh(&volData, reg2); std::cout << "#vertices: " << mesh.getNoOfVertices() << std::endl; - auto decodedMesh = decode(mesh); + auto decodedMesh = decodeMesh(mesh); //Pass the surface to the OpenGL window openGLWidget.addMesh(decodedMesh); diff --git a/examples/SmoothLOD/main.cpp b/examples/SmoothLOD/main.cpp index 01c2fdaf..0054c5ce 100644 --- a/examples/SmoothLOD/main.cpp +++ b/examples/SmoothLOD/main.cpp @@ -92,12 +92,12 @@ int main(int argc, char *argv[]) //Extract the surface auto meshLowLOD = extractMarchingCubesMesh(&volDataLowLOD, volDataLowLOD.getEnclosingRegion()); // The returned mesh needs to be decoded to be appropriate for GPU rendering. - auto decodedMeshLowLOD = decode(meshLowLOD); + auto decodedMeshLowLOD = decodeMesh(meshLowLOD); //Extract the surface auto meshHighLOD = extractMarchingCubesMesh(&volData, PolyVox::Region(Vector3DInt32(30, 0, 0), Vector3DInt32(63, 63, 63))); // The returned mesh needs to be decoded to be appropriate for GPU rendering. - auto decodedMeshHighLOD = decode(meshHighLOD); + auto decodedMeshHighLOD = decodeMesh(meshHighLOD); //Pass the surface to the OpenGL window openGLWidget.addMesh(decodedMeshHighLOD, Vector3DInt32(30, 0, 0)); diff --git a/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractor.h b/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractor.h index 71571038..5e059e10 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractor.h +++ b/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractor.h @@ -54,7 +54,7 @@ namespace PolyVox }; /// Decodes a position from a CubicVertex - inline Vector3DFloat decode(const Vector3DUint8& encodedPosition) + inline Vector3DFloat decodePosition(const Vector3DUint8& encodedPosition) { Vector3DFloat result(encodedPosition.getX(), encodedPosition.getY(), encodedPosition.getZ()); result -= 0.5f; // Apply the required offset @@ -63,10 +63,10 @@ namespace PolyVox /// Decodes a MarchingCubesVertex by converting it into a regular Vertex which can then be directly used for rendering. template - Vertex decode(const CubicVertex& cubicVertex) + Vertex decodeVertex(const CubicVertex& cubicVertex) { Vertex result; - result.position = decode(cubicVertex.encodedPosition); + result.position = decodePosition(cubicVertex.encodedPosition); result.normal.setElements(0.0f, 0.0f, 0.0f); // Currently not calculated result.data = cubicVertex.data; // Data is not encoded return result; diff --git a/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.h b/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.h index 2ef1b103..18d385f1 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.h +++ b/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.h @@ -47,9 +47,8 @@ namespace PolyVox // Each component of the position is stored using 8.8 fixed-point encoding. Vector3DUint16 encodedPosition; - // Each component of the normal is encoded using 5 bits of this variable. - // The 16 bits are -xxxxxyyyyyzzzzz (note the left-most bit is currently - // unused). Some extra shifting and scaling is required to make it signed. + // The normal is encoded as a 16-bit unsigned integer using the 'oct16' + // encoding described here: http://jcgt.org/published/0003/02/01/ uint16_t encodedNormal; // User data @@ -57,7 +56,7 @@ namespace PolyVox }; /// Decodes a position from a MarchingCubesVertex - inline Vector3DFloat decode(const Vector3DUint16& encodedPosition) + inline Vector3DFloat decodePosition(const Vector3DUint16& encodedPosition) { Vector3DFloat result(encodedPosition.getX(), encodedPosition.getY(), encodedPosition.getZ()); result *= (1.0f / 256.0f); // Division is compile-time constant @@ -107,7 +106,7 @@ namespace PolyVox return (resultX << 8) | resultY; } - inline Vector3DFloat decode(const uint16_t& encodedNormal) + inline Vector3DFloat decodeNormal(const uint16_t& encodedNormal) { // Extract the two bytes from the uint16_t. uint16_t ux = (encodedNormal >> 8) & 0xFF; @@ -139,11 +138,11 @@ namespace PolyVox /// Decodes a MarchingCubesVertex by converting it into a regular Vertex which can then be directly used for rendering. template - Vertex decode(const MarchingCubesVertex& marchingCubesVertex) + Vertex decodeVertex(const MarchingCubesVertex& marchingCubesVertex) { Vertex result; - result.position = decode(marchingCubesVertex.encodedPosition); - result.normal = decode(marchingCubesVertex.encodedNormal); + result.position = decodePosition(marchingCubesVertex.encodedPosition); + result.normal = decodeNormal(marchingCubesVertex.encodedNormal); result.data = marchingCubesVertex.data; // Data is not encoded return result; } diff --git a/library/PolyVoxCore/include/PolyVoxCore/Mesh.h b/library/PolyVoxCore/include/PolyVoxCore/Mesh.h index a1c23b50..64356785 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Mesh.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Mesh.h @@ -72,14 +72,14 @@ namespace PolyVox }; template - Mesh< Vertex< typename MeshType::VertexType::DataType >, typename MeshType::IndexType > decode(const MeshType& mesh) + Mesh< Vertex< typename MeshType::VertexType::DataType >, typename MeshType::IndexType > decodeMesh(const MeshType& mesh) { Mesh< Vertex< typename MeshType::VertexType::DataType >, typename MeshType::IndexType > result; result.m_vecVertices.resize(mesh.m_vecVertices.size()); for(typename MeshType::IndexType ct = 0; ct < mesh.m_vecVertices.size(); ct++) { - result.m_vecVertices[ct] = decode(mesh.m_vecVertices[ct]); + result.m_vecVertices[ct] = decodeVertex(mesh.m_vecVertices[ct]); } result.m_vecTriangleIndices = mesh.m_vecTriangleIndices;