diff --git a/examples/SmoothLOD/main.cpp b/examples/SmoothLOD/main.cpp index 60b35c98..01c2fdaf 100644 --- a/examples/SmoothLOD/main.cpp +++ b/examples/SmoothLOD/main.cpp @@ -91,13 +91,17 @@ 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); //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); //Pass the surface to the OpenGL window - openGLWidget.addMesh(meshHighLOD, Vector3DInt32(30, 0, 0)); - openGLWidget.addMesh(meshLowLOD, Vector3DInt32(0, 0, 0), 63.0f / 31.0f); + openGLWidget.addMesh(decodedMeshHighLOD, Vector3DInt32(30, 0, 0)); + openGLWidget.addMesh(decodedMeshLowLOD, Vector3DInt32(0, 0, 0), 63.0f / 31.0f); openGLWidget.setViewableRegion(volData.getEnclosingRegion()); diff --git a/examples/common/OpenGLWidget.h b/examples/common/OpenGLWidget.h index eaff700a..930cb3d8 100644 --- a/examples/common/OpenGLWidget.h +++ b/examples/common/OpenGLWidget.h @@ -92,8 +92,8 @@ public: // Finally a surface extractor will probably output additional data. This is highly application dependant. For this example code // we're just uploading it as a set of bytes which we can read individually, but real code will want to do something specialised here. glEnableVertexAttribArray(2); //We're talking about shader attribute '2' - GLint size = (std::min)(sizeof(typename MeshType::VertexType::VoxelType), size_t(4)); // Can't upload more that 4 components (vec4 is GLSL's biggest type) - glVertexAttribIPointer(2, size, GL_UNSIGNED_BYTE, sizeof(typename MeshType::VertexType), (GLvoid*)(offsetof(typename MeshType::VertexType, material))); + GLint size = (std::min)(sizeof(typename MeshType::VertexType::DataType), size_t(4)); // Can't upload more that 4 components (vec4 is GLSL's biggest type) + glVertexAttribIPointer(2, size, GL_UNSIGNED_BYTE, sizeof(typename MeshType::VertexType), (GLvoid*)(offsetof(typename MeshType::VertexType, data))); // We're done uploading and can now unbind. glBindVertexArray(0); diff --git a/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractor.inl b/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractor.inl index da7803cd..7fe9286b 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractor.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractor.inl @@ -222,7 +222,7 @@ namespace PolyVox //No vertices matched and we've now hit an empty space. Fill it by creating a vertex. The 0.5f offset is because vertices set between voxels in order to build cubes around them. CubicVertex cubicVertex; cubicVertex.position.setElements(static_cast(uX), static_cast(uY), static_cast(uZ)); - cubicVertex.material = uMaterialIn; + cubicVertex.data = uMaterialIn; rEntry.iIndex = m_meshCurrent->addVertex(cubicVertex); rEntry.uMaterial = uMaterialIn; @@ -275,9 +275,9 @@ namespace PolyVox template bool CubicSurfaceExtractor::mergeQuads(Quad& q1, Quad& q2) { - //All four vertices of a given quad have the same material, + //All four vertices of a given quad have the same data, //so just check that the first pair of vertices match. - if(m_meshCurrent->getVertices()[q1.vertices[0]].material == m_meshCurrent->getVertices()[q2.vertices[0]].material) + if (m_meshCurrent->getVertices()[q1.vertices[0]].data == m_meshCurrent->getVertices()[q2.vertices[0]].data) { //Now check whether quad 2 is adjacent to quad one by comparing vertices. //Adjacent quads must share two vertices, and the second quad could be to the diff --git a/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl b/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl index bbe18ad2..1b50431e 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl @@ -463,7 +463,7 @@ namespace PolyVox MarchingCubesVertex surfaceVertex; surfaceVertex.position = v3dPositionAsUint; surfaceVertex.normal = v3dNormal; - surfaceVertex.material = uMaterial; + surfaceVertex.data = uMaterial; const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); m_pCurrentVertexIndicesX[iXVolSpace - m_regSizeInVoxels.getLowerX()][iYVolSpace - m_regSizeInVoxels.getLowerY()] = uLastVertexIndex; @@ -497,7 +497,7 @@ namespace PolyVox MarchingCubesVertex surfaceVertex; surfaceVertex.position = v3dPositionAsUint; surfaceVertex.normal = v3dNormal; - surfaceVertex.material = uMaterial; + surfaceVertex.data = uMaterial; uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); m_pCurrentVertexIndicesY[iXVolSpace - m_regSizeInVoxels.getLowerX()][iYVolSpace - m_regSizeInVoxels.getLowerY()] = uLastVertexIndex; @@ -530,7 +530,7 @@ namespace PolyVox MarchingCubesVertex surfaceVertex; surfaceVertex.position = v3dPositionAsUint; surfaceVertex.normal = v3dNormal; - surfaceVertex.material = uMaterial; + surfaceVertex.data = uMaterial; const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); m_pCurrentVertexIndicesZ[iXVolSpace - m_regSizeInVoxels.getLowerX()][iYVolSpace - m_regSizeInVoxels.getLowerY()] = uLastVertexIndex; diff --git a/library/PolyVoxCore/include/PolyVoxCore/Mesh.h b/library/PolyVoxCore/include/PolyVoxCore/Mesh.h index d23f7716..f0ce39ea 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Mesh.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Mesh.h @@ -102,9 +102,9 @@ namespace PolyVox std::shared_ptr< Mesh > extractSubset(Mesh& inputMesh, std::set setMaterials); template - Mesh< Vertex< typename MeshType::VertexType::VoxelType > > decode(const MeshType& mesh) + Mesh< Vertex< typename MeshType::VertexType::DataType > > decode(const MeshType& mesh) { - Mesh< Vertex< typename MeshType::VertexType::VoxelType > > result; + Mesh< Vertex< typename MeshType::VertexType::DataType > > result; result.m_vecVertices.resize(mesh.m_vecVertices.size()); for(uint32_t ct = 0; ct < mesh.m_vecVertices.size(); ct++) diff --git a/library/PolyVoxCore/include/PolyVoxCore/VertexTypes.h b/library/PolyVoxCore/include/PolyVoxCore/VertexTypes.h index 1044f472..dcab6c17 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/VertexTypes.h +++ b/library/PolyVoxCore/include/PolyVoxCore/VertexTypes.h @@ -36,68 +36,68 @@ namespace PolyVox #ifdef SWIG struct Vertex #else - template + template struct POLYVOX_API Vertex #endif { - typedef _VoxelType VoxelType; + typedef _DataType DataType; Vector3DFloat position; Vector3DFloat normal; - VoxelType material; + DataType data; }; #ifdef SWIG struct CubicVertex #else - template + template struct POLYVOX_API CubicVertex #endif { - typedef _VoxelType VoxelType; + typedef _DataType DataType; Vector3DUint8 position; uint8_t normal; - VoxelType material; + DataType data; }; #ifdef SWIG struct MarchingCubesVertex #else - template + template struct POLYVOX_API MarchingCubesVertex #endif { - typedef _VoxelType VoxelType; + typedef _DataType DataType; Vector3DUint16 position; Vector3DFloat normal; - VoxelType material; + DataType data; }; // Hopefully the compiler will implement the 'Return value optimization' here, but // performance critical code will most likely decode the vertices in a shader anyway. - template - Vertex decode(const CubicVertex& cubicVertex) + template + Vertex decode(const CubicVertex& cubicVertex) { - Vertex result; + Vertex result; Vector3DUint8 temp = cubicVertex.position; // For some reason we can't cast Vector3DUint8 to Vector3DFloat - investigate why. result.position = Vector3DFloat(temp.getX(), temp.getY(), temp.getZ()) - Vector3DFloat(0.5, 0.5, 0.5); //result.normal = cubicVertex.normal; - result.material = cubicVertex.material; + result.data = cubicVertex.data; return result; } // Hopefully the compiler will implement the 'Return value optimization' here, but // performance critical code will most likely decode the vertices in a shader anyway. - template - Vertex decode(const MarchingCubesVertex& cubicVertex) + template + Vertex decode(const MarchingCubesVertex& cubicVertex) { - Vertex result; + Vertex result; 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; + result.data = cubicVertex.data; return result; } } diff --git a/tests/TestSurfaceExtractor.cpp b/tests/TestSurfaceExtractor.cpp index e621fa99..682fa0dc 100644 --- a/tests/TestSurfaceExtractor.cpp +++ b/tests/TestSurfaceExtractor.cpp @@ -162,7 +162,7 @@ void TestSurfaceExtractor::testExecute() const static uint32_t uExpectedVertices = 4731; const static uint32_t uExpectedIndices = 12810; const static uint32_t uMaterialToCheck = 3000; - const static float fExpectedMaterial = 42.0f; + const static float fExpectedData = 42.0f; const static float fNoMaterial = 1.0f; Mesh > mesh; @@ -172,58 +172,58 @@ void TestSurfaceExtractor::testExecute() } QCOMPARE(mesh.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); + QCOMPARE(mesh.getVertices()[uMaterialToCheck].data, static_cast(fExpectedData)); auto mesh1 = testForType(); QCOMPARE(mesh1.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh1.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh1.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); + QCOMPARE(mesh1.getVertices()[uMaterialToCheck].data, static_cast(fExpectedData)); auto mesh2 = testForType(); QCOMPARE(mesh2.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh2.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh2.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); + QCOMPARE(mesh2.getVertices()[uMaterialToCheck].data, static_cast(fExpectedData)); auto mesh3 = testForType(); QCOMPARE(mesh3.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh3.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh3.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); + QCOMPARE(mesh3.getVertices()[uMaterialToCheck].data, static_cast(fExpectedData)); auto mesh4 = testForType(); QCOMPARE(mesh4.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh4.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh4.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); + QCOMPARE(mesh4.getVertices()[uMaterialToCheck].data, static_cast(fExpectedData)); auto mesh5 = testForType(); QCOMPARE(mesh5.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh5.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh5.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); + QCOMPARE(mesh5.getVertices()[uMaterialToCheck].data, static_cast(fExpectedData)); auto mesh6 = testForType(); QCOMPARE(mesh6.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh6.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh6.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); + QCOMPARE(mesh6.getVertices()[uMaterialToCheck].data, static_cast(fExpectedData)); auto mesh7 = testForType(); QCOMPARE(mesh7.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh7.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh7.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); + QCOMPARE(mesh7.getVertices()[uMaterialToCheck].data, static_cast(fExpectedData)); auto mesh8 = testForType(); QCOMPARE(mesh8.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh8.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh8.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); + QCOMPARE(mesh8.getVertices()[uMaterialToCheck].data, static_cast(fExpectedData)); auto mesh9 = testForType(); QCOMPARE(mesh9.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh9.getNoOfIndices(), uExpectedIndices); - //QCOMPARE(mesh9.getVertices()[uMaterialToCheck].material, fExpectedMaterial); + //QCOMPARE(mesh9.getVertices()[uMaterialToCheck].data, fExpectedMaterial); //Test whether the CustomSurfaceExtractor works. /*testCustomController(floatMesh); QCOMPARE(floatMesh.getNoOfVertices(), uExpectedVertices); QCOMPARE(floatMesh.getNoOfIndices(), uExpectedIndices); - QCOMPARE(floatMesh.getVertices()[uMaterialToCheck].material, fNoMaterial);*/ + QCOMPARE(floatMesh.getVertices()[uMaterialToCheck].data, fExpectedData);*/ } QTEST_MAIN(TestSurfaceExtractor)