Simplified vertex types to structs with public members.

Added (currently dummy) decode methods.
This commit is contained in:
David Williams 2014-05-27 17:05:15 +02:00
parent 1d8fc25cb7
commit 764c9ac97c
6 changed files with 95 additions and 124 deletions

View File

@ -214,7 +214,10 @@ namespace PolyVox
if(rEntry.iIndex == -1) if(rEntry.iIndex == -1)
{ {
//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. //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.
rEntry.iIndex = m_meshCurrent->addVertex(CubicVertex<typename VolumeType::VoxelType>(Vector3DFloat(static_cast<float>(uX)-0.5f, static_cast<float>(uY)-0.5f, static_cast<float>(uZ)-0.5f), uMaterialIn)); CubicVertex<typename VolumeType::VoxelType> cubicVertex;
cubicVertex.position.setElements(static_cast<float>(uX)-0.5f, static_cast<float>(uY)-0.5f, static_cast<float>(uZ)-0.5f);
cubicVertex.material = uMaterialIn;
rEntry.iIndex = m_meshCurrent->addVertex(cubicVertex);
rEntry.uMaterial = uMaterialIn; rEntry.uMaterial = uMaterialIn;
return rEntry.iIndex; return rEntry.iIndex;
@ -268,7 +271,7 @@ namespace PolyVox
{ {
//All four vertices of a given quad have the same material, //All four vertices of a given quad have the same material,
//so just check that the first pair of vertices match. //so just check that the first pair of vertices match.
if(m_meshCurrent->getVertices()[q1.vertices[0]].getMaterial() == m_meshCurrent->getVertices()[q2.vertices[0]].getMaterial()) if(m_meshCurrent->getVertices()[q1.vertices[0]].material == m_meshCurrent->getVertices()[q2.vertices[0]].material)
{ {
//Now check whether quad 2 is adjacent to quad one by comparing vertices. //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 //Adjacent quads must share two vertices, and the second quad could be to the

View File

@ -459,7 +459,11 @@ namespace PolyVox
// Allow the controller to decide how the material should be derived from the voxels. // 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); const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v100, fInterp);
const MarchingCubesVertex<typename VolumeType::VoxelType> surfaceVertex(v3dPosition, v3dNormal, uMaterial); MarchingCubesVertex<typename VolumeType::VoxelType> surfaceVertex;
surfaceVertex.position = v3dPosition;
surfaceVertex.normal = v3dNormal;
surfaceVertex.material = uMaterial;
const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex);
m_pCurrentVertexIndicesX[iXVolSpace - m_regSizeInVoxels.getLowerX()][iYVolSpace - m_regSizeInVoxels.getLowerY()] = uLastVertexIndex; m_pCurrentVertexIndicesX[iXVolSpace - m_regSizeInVoxels.getLowerX()][iYVolSpace - m_regSizeInVoxels.getLowerY()] = uLastVertexIndex;
@ -488,7 +492,11 @@ namespace PolyVox
// Allow the controller to decide how the material should be derived from the voxels. // 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); const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v010, fInterp);
MarchingCubesVertex<typename VolumeType::VoxelType> surfaceVertex(v3dPosition, v3dNormal, uMaterial); MarchingCubesVertex<typename VolumeType::VoxelType> surfaceVertex;
surfaceVertex.position = v3dPosition;
surfaceVertex.normal = v3dNormal;
surfaceVertex.material = uMaterial;
uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex);
m_pCurrentVertexIndicesY[iXVolSpace - m_regSizeInVoxels.getLowerX()][iYVolSpace - m_regSizeInVoxels.getLowerY()] = uLastVertexIndex; m_pCurrentVertexIndicesY[iXVolSpace - m_regSizeInVoxels.getLowerX()][iYVolSpace - m_regSizeInVoxels.getLowerY()] = uLastVertexIndex;
@ -516,7 +524,11 @@ namespace PolyVox
// Allow the controller to decide how the material should be derived from the voxels. // 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); const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v001, fInterp);
const MarchingCubesVertex<typename VolumeType::VoxelType> surfaceVertex(v3dPosition, v3dNormal, uMaterial); MarchingCubesVertex<typename VolumeType::VoxelType> surfaceVertex;
surfaceVertex.position = v3dPosition;
surfaceVertex.normal = v3dNormal;
surfaceVertex.material = uMaterial;
const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex);
m_pCurrentVertexIndicesZ[iXVolSpace - m_regSizeInVoxels.getLowerX()][iYVolSpace - m_regSizeInVoxels.getLowerY()] = uLastVertexIndex; m_pCurrentVertexIndicesZ[iXVolSpace - m_regSizeInVoxels.getLowerX()][iYVolSpace - m_regSizeInVoxels.getLowerY()] = uLastVertexIndex;

View File

@ -100,6 +100,24 @@ namespace PolyVox
template <typename VertexType> template <typename VertexType>
std::shared_ptr< Mesh<VertexType> > extractSubset(Mesh<VertexType>& inputMesh, std::set<uint8_t> setMaterials); std::shared_ptr< Mesh<VertexType> > extractSubset(Mesh<VertexType>& inputMesh, std::set<uint8_t> setMaterials);
template <typename MeshType>
Mesh< Vertex< typename MeshType::VertexType::VoxelType > > decode(const MeshType& mesh)
{
Mesh< Vertex< typename MeshType::VertexType::VoxelType > > result;
result.m_vecVertices.reserve(mesh.m_vecVertices.size());
for(uint32_t ct = 0; ct < mesh.m_vecVertices.size(); ct++)
{
result.m_vecVertices[ct] = decode(mesh.m_vecVertices[ct]);
}
result.m_vecTriangleIndices = mesh.m_vecTriangleIndices;
result.m_Region = mesh.m_Region;
return result;
}
} }
#include "PolyVoxCore/Mesh.inl" #include "PolyVoxCore/Mesh.inl"

View File

@ -81,7 +81,7 @@ namespace PolyVox
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// CubicVertex // CubicVertex
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template<typename VoxelType> class CubicVertex; template<typename VoxelType> struct CubicVertex;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Density // Density
@ -111,7 +111,7 @@ namespace PolyVox
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// MarchingCubesVertex // MarchingCubesVertex
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template<typename VoxelType> class MarchingCubesVertex; template<typename VoxelType> struct MarchingCubesVertex;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Material // Material

View File

@ -32,134 +32,72 @@ freely, subject to the following restrictions:
#include <vector> #include <vector>
namespace PolyVox namespace PolyVox
{ {
#ifdef SWIG #ifdef SWIG
class CubicVertex struct Vertex
#else #else
template<typename _VoxelType> template<typename _VoxelType>
class POLYVOX_API CubicVertex struct POLYVOX_API Vertex
#endif #endif
{ {
public:
typedef _VoxelType VoxelType; typedef _VoxelType VoxelType;
CubicVertex()
{
}
CubicVertex(Vector3DFloat positionToSet, VoxelType materialToSet)
:position(positionToSet)
,material(materialToSet)
{
}
CubicVertex(Vector3DFloat positionToSet, Vector3DFloat normalToSet, VoxelType materialToSet)
:position(positionToSet)
,normal(normalToSet)
,material(materialToSet)
{
}
VoxelType getMaterial(void) const
{
return material;
}
const Vector3DFloat& getNormal(void) const
{
return normal;
}
const Vector3DFloat& getPosition(void) const
{
return position;
}
void setMaterial(VoxelType materialToSet)
{
material = materialToSet;
}
void setNormal(const Vector3DFloat& normalToSet)
{
normal = normalToSet;
}
void setPosition(const Vector3DFloat& positionToSet)
{
position = positionToSet;
}
public:
Vector3DFloat position; Vector3DFloat position;
Vector3DFloat normal; Vector3DFloat normal;
VoxelType material; VoxelType material;
}; };
#ifdef SWIG #ifdef SWIG
class MarchingCubesVertex struct CubicVertex
#else #else
template<typename _VoxelType> template<typename _VoxelType>
class POLYVOX_API MarchingCubesVertex struct POLYVOX_API CubicVertex
#endif #endif
{ {
public:
typedef _VoxelType VoxelType; typedef _VoxelType VoxelType;
MarchingCubesVertex()
{
}
MarchingCubesVertex(Vector3DFloat positionToSet, VoxelType materialToSet)
:position(positionToSet)
, material(materialToSet)
{
}
MarchingCubesVertex(Vector3DFloat positionToSet, Vector3DFloat normalToSet, VoxelType materialToSet)
:position(positionToSet)
, normal(normalToSet)
, material(materialToSet)
{
}
VoxelType getMaterial(void) const
{
return material;
}
const Vector3DFloat& getNormal(void) const
{
return normal;
}
const Vector3DFloat& getPosition(void) const
{
return position;
}
void setMaterial(VoxelType materialToSet)
{
material = materialToSet;
}
void setNormal(const Vector3DFloat& normalToSet)
{
normal = normalToSet;
}
void setPosition(const Vector3DFloat& positionToSet)
{
position = positionToSet;
}
public:
Vector3DFloat position; Vector3DFloat position;
Vector3DFloat normal; Vector3DFloat normal;
VoxelType material; VoxelType material;
}; };
#ifdef SWIG
struct MarchingCubesVertex
#else
template<typename _VoxelType>
struct POLYVOX_API MarchingCubesVertex
#endif
{
typedef _VoxelType VoxelType;
Vector3DFloat position;
Vector3DFloat normal;
VoxelType material;
};
// 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<typename VoxelType>
Vertex<VoxelType> decode(const CubicVertex<VoxelType>& cubicVertex)
{
Vertex<VoxelType> result;
result.position = cubicVertex.position;
result.normal = cubicVertex.normal;
result.material = cubicVertex.material;
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<typename VoxelType>
Vertex<VoxelType> decode(const MarchingCubesVertex<VoxelType>& cubicVertex)
{
Vertex<VoxelType> result;
result.position = cubicVertex.position;
result.normal = cubicVertex.normal;
result.material = cubicVertex.material;
return result;
}
} }
#endif #endif

View File

@ -172,58 +172,58 @@ void TestSurfaceExtractor::testExecute()
} }
QCOMPARE(mesh.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh.getNoOfVertices(), uExpectedVertices);
QCOMPARE(mesh.getNoOfIndices(), uExpectedIndices); QCOMPARE(mesh.getNoOfIndices(), uExpectedIndices);
QCOMPARE(mesh.getVertices()[uMaterialToCheck].getMaterial(), static_cast<int8_t>(fNoMaterial)); QCOMPARE(mesh.getVertices()[uMaterialToCheck].material, static_cast<int8_t>(fNoMaterial));
auto mesh1 = testForType<uint8_t>(); auto mesh1 = testForType<uint8_t>();
QCOMPARE(mesh1.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh1.getNoOfVertices(), uExpectedVertices);
QCOMPARE(mesh1.getNoOfIndices(), uExpectedIndices); QCOMPARE(mesh1.getNoOfIndices(), uExpectedIndices);
QCOMPARE(mesh1.getVertices()[uMaterialToCheck].getMaterial(), static_cast<uint8_t>(fNoMaterial)); QCOMPARE(mesh1.getVertices()[uMaterialToCheck].material, static_cast<uint8_t>(fNoMaterial));
auto mesh2 = testForType<int16_t>(); auto mesh2 = testForType<int16_t>();
QCOMPARE(mesh2.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh2.getNoOfVertices(), uExpectedVertices);
QCOMPARE(mesh2.getNoOfIndices(), uExpectedIndices); QCOMPARE(mesh2.getNoOfIndices(), uExpectedIndices);
QCOMPARE(mesh2.getVertices()[uMaterialToCheck].getMaterial(), static_cast<int16_t>(fNoMaterial)); QCOMPARE(mesh2.getVertices()[uMaterialToCheck].material, static_cast<int16_t>(fNoMaterial));
auto mesh3 = testForType<uint16_t>(); auto mesh3 = testForType<uint16_t>();
QCOMPARE(mesh3.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh3.getNoOfVertices(), uExpectedVertices);
QCOMPARE(mesh3.getNoOfIndices(), uExpectedIndices); QCOMPARE(mesh3.getNoOfIndices(), uExpectedIndices);
QCOMPARE(mesh3.getVertices()[uMaterialToCheck].getMaterial(), static_cast<uint16_t>(fNoMaterial)); QCOMPARE(mesh3.getVertices()[uMaterialToCheck].material, static_cast<uint16_t>(fNoMaterial));
auto mesh4 = testForType<int32_t>(); auto mesh4 = testForType<int32_t>();
QCOMPARE(mesh4.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh4.getNoOfVertices(), uExpectedVertices);
QCOMPARE(mesh4.getNoOfIndices(), uExpectedIndices); QCOMPARE(mesh4.getNoOfIndices(), uExpectedIndices);
QCOMPARE(mesh4.getVertices()[uMaterialToCheck].getMaterial(), static_cast<int32_t>(fNoMaterial)); QCOMPARE(mesh4.getVertices()[uMaterialToCheck].material, static_cast<int32_t>(fNoMaterial));
auto mesh5 = testForType<uint32_t>(); auto mesh5 = testForType<uint32_t>();
QCOMPARE(mesh5.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh5.getNoOfVertices(), uExpectedVertices);
QCOMPARE(mesh5.getNoOfIndices(), uExpectedIndices); QCOMPARE(mesh5.getNoOfIndices(), uExpectedIndices);
QCOMPARE(mesh5.getVertices()[uMaterialToCheck].getMaterial(), static_cast<uint32_t>(fNoMaterial)); QCOMPARE(mesh5.getVertices()[uMaterialToCheck].material, static_cast<uint32_t>(fNoMaterial));
auto mesh6 = testForType<float>(); auto mesh6 = testForType<float>();
QCOMPARE(mesh6.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh6.getNoOfVertices(), uExpectedVertices);
QCOMPARE(mesh6.getNoOfIndices(), uExpectedIndices); QCOMPARE(mesh6.getNoOfIndices(), uExpectedIndices);
QCOMPARE(mesh6.getVertices()[uMaterialToCheck].getMaterial(), static_cast<float>(fNoMaterial)); QCOMPARE(mesh6.getVertices()[uMaterialToCheck].material, static_cast<float>(fNoMaterial));
auto mesh7 = testForType<double>(); auto mesh7 = testForType<double>();
QCOMPARE(mesh7.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh7.getNoOfVertices(), uExpectedVertices);
QCOMPARE(mesh7.getNoOfIndices(), uExpectedIndices); QCOMPARE(mesh7.getNoOfIndices(), uExpectedIndices);
QCOMPARE(mesh7.getVertices()[uMaterialToCheck].getMaterial(), static_cast<double>(fNoMaterial)); QCOMPARE(mesh7.getVertices()[uMaterialToCheck].material, static_cast<double>(fNoMaterial));
auto mesh8 = testForType<Density8>(); auto mesh8 = testForType<Density8>();
QCOMPARE(mesh8.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh8.getNoOfVertices(), uExpectedVertices);
QCOMPARE(mesh8.getNoOfIndices(), uExpectedIndices); QCOMPARE(mesh8.getNoOfIndices(), uExpectedIndices);
QCOMPARE(mesh8.getVertices()[uMaterialToCheck].getMaterial(), static_cast<Density8>(fNoMaterial)); QCOMPARE(mesh8.getVertices()[uMaterialToCheck].material, static_cast<Density8>(fNoMaterial));
auto mesh9 = testForType<MaterialDensityPair88>(); auto mesh9 = testForType<MaterialDensityPair88>();
QCOMPARE(mesh9.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh9.getNoOfVertices(), uExpectedVertices);
QCOMPARE(mesh9.getNoOfIndices(), uExpectedIndices); QCOMPARE(mesh9.getNoOfIndices(), uExpectedIndices);
//QCOMPARE(mesh9.getVertices()[uMaterialToCheck].getMaterial(), fExpectedMaterial); //QCOMPARE(mesh9.getVertices()[uMaterialToCheck].material, fExpectedMaterial);
//Test whether the CustomSurfaceExtractor works. //Test whether the CustomSurfaceExtractor works.
/*testCustomController(floatMesh); /*testCustomController(floatMesh);
QCOMPARE(floatMesh.getNoOfVertices(), uExpectedVertices); QCOMPARE(floatMesh.getNoOfVertices(), uExpectedVertices);
QCOMPARE(floatMesh.getNoOfIndices(), uExpectedIndices); QCOMPARE(floatMesh.getNoOfIndices(), uExpectedIndices);
QCOMPARE(floatMesh.getVertices()[uMaterialToCheck].getMaterial(), fNoMaterial);*/ QCOMPARE(floatMesh.getVertices()[uMaterialToCheck].material, fNoMaterial);*/
} }
QTEST_MAIN(TestSurfaceExtractor) QTEST_MAIN(TestSurfaceExtractor)