Index: include/CubicSurfaceExtractorWithIndices.h =================================================================== --- include/CubicSurfaceExtractorWithIndices.h (revision 0) +++ include/CubicSurfaceExtractorWithIndices.h (revision 0) @@ -0,0 +1,58 @@ +/******************************************************************************* +Copyright (c) 2005-2009 David Williams + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. +*******************************************************************************/ + +#ifndef __PolyVox_CubicSurfaceExtractorWithIndices_H__ +#define __PolyVox_CubicSurfaceExtractorWithIndices_H__ + +#include "PolyVoxForwardDeclarations.h" +#include "VolumeSampler.h" + +#include "PolyVoxImpl/TypeDef.h" + +namespace PolyVox +{ + template + class CubicSurfaceExtractorWithIndices + { + public: + CubicSurfaceExtractorWithIndices(Volume* volData, Region region, SurfaceMesh* result); + + void execute(); + + private: + //The volume data and a sampler to access it. + Volume* m_volData; + VolumeSampler m_sampVolume; + + //The surface patch we are currently filling. + SurfaceMesh* m_meshCurrent; + + //Information about the region we are currently processing + Region m_regSizeInVoxels; + Region m_regSizeInCells; + }; +} + +#include "CubicSurfaceExtractorWithIndices.inl" + +#endif Index: include/CubicSurfaceExtractorWithIndices.inl =================================================================== --- include/CubicSurfaceExtractorWithIndices.inl (revision 0) +++ include/CubicSurfaceExtractorWithIndices.inl (revision 0) @@ -0,0 +1,149 @@ +/******************************************************************************* +Copyright (c) 2005-2009 David Williams + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. +*******************************************************************************/ + +#include "Array.h" +#include "MaterialDensityPair.h" +#include "SurfaceMesh.h" +#include "PolyVoxImpl/MarchingCubesTables.h" +#include "VertexTypes.h" + +namespace PolyVox +{ + template + CubicSurfaceExtractorWithIndices::CubicSurfaceExtractorWithIndices(Volume* volData, Region region, SurfaceMesh* result) + :m_volData(volData) + ,m_sampVolume(volData) + ,m_regSizeInVoxels(region) + ,m_meshCurrent(result) + { + m_regSizeInCells = m_regSizeInVoxels; + m_regSizeInCells.setUpperCorner(m_regSizeInCells.getUpperCorner() - Vector3DInt16(1,1,1)); + + m_meshCurrent->clear(); + } + + template + void CubicSurfaceExtractorWithIndices::execute() + { + for(int16_t z = m_regSizeInVoxels.getLowerCorner().getZ(); z < m_regSizeInVoxels.getUpperCorner().getZ(); z++) + { + for(int16_t y = m_regSizeInVoxels.getLowerCorner().getY(); y < m_regSizeInVoxels.getUpperCorner().getY(); y++) + { + for(int16_t x = m_regSizeInVoxels.getLowerCorner().getX(); x < m_regSizeInVoxels.getUpperCorner().getX(); x++) + { + uint16_t regX = x - m_regSizeInVoxels.getLowerCorner().getX(); + uint16_t regY = y - m_regSizeInVoxels.getLowerCorner().getY(); + uint16_t regZ = z - m_regSizeInVoxels.getLowerCorner().getZ(); + + int currentVoxel = m_volData->getVoxelAt(x,y,z).getDensity() >= VoxelType::getThreshold(); + + int plusXVoxel = m_volData->getVoxelAt(x+1,y,z).getDensity() >= VoxelType::getThreshold(); + if(currentVoxel > plusXVoxel) + { + int material = m_volData->getVoxelAt(x,y,z).getMaterial(); + + uint32_t v0 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX + 0.5f, regY - 0.5f, regZ - 0.5f), 0, material)); + uint32_t v1 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX + 0.5f, regY - 0.5f, regZ + 0.5f), 0, material)); + uint32_t v2 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX + 0.5f, regY + 0.5f, regZ - 0.5f), 0, material)); + uint32_t v3 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX + 0.5f, regY + 0.5f, regZ + 0.5f), 0, material)); + + m_meshCurrent->addTriangleCubic(v0,v2,v1); + m_meshCurrent->addTriangleCubic(v1,v2,v3); + } + if(currentVoxel < plusXVoxel) + { + int material = m_volData->getVoxelAt(x+1,y,z).getMaterial(); + + uint32_t v0 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX + 0.5f, regY - 0.5f, regZ - 0.5f), 1, material)); + uint32_t v1 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX + 0.5f, regY - 0.5f, regZ + 0.5f), 1, material)); + uint32_t v2 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX + 0.5f, regY + 0.5f, regZ - 0.5f), 1, material)); + uint32_t v3 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX + 0.5f, regY + 0.5f, regZ + 0.5f), 1, material)); + + m_meshCurrent->addTriangleCubic(v0,v1,v2); + m_meshCurrent->addTriangleCubic(v1,v3,v2); + } + + int plusYVoxel = m_volData->getVoxelAt(x,y+1,z).getDensity() >= VoxelType::getThreshold(); + if(currentVoxel > plusYVoxel) + { + int material = m_volData->getVoxelAt(x,y,z).getMaterial(); + + uint32_t v0 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX - 0.5f, regY + 0.5f, regZ - 0.5f), 2, material)); + uint32_t v1 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX - 0.5f, regY + 0.5f, regZ + 0.5f), 2, material)); + uint32_t v2 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX + 0.5f, regY + 0.5f, regZ - 0.5f), 2, material)); + uint32_t v3 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX + 0.5f, regY + 0.5f, regZ + 0.5f), 2, material)); + + m_meshCurrent->addTriangleCubic(v0,v1,v2); + m_meshCurrent->addTriangleCubic(v1,v3,v2); + } + if(currentVoxel < plusYVoxel) + { + int material = m_volData->getVoxelAt(x,y+1,z).getMaterial(); + + uint32_t v0 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX - 0.5f, regY + 0.5f, regZ - 0.5f), 3, material)); + uint32_t v1 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX - 0.5f, regY + 0.5f, regZ + 0.5f), 3, material)); + uint32_t v2 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX + 0.5f, regY + 0.5f, regZ - 0.5f), 3, material)); + uint32_t v3 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX + 0.5f, regY + 0.5f, regZ + 0.5f), 3, material)); + + m_meshCurrent->addTriangleCubic(v0,v2,v1); + m_meshCurrent->addTriangleCubic(v1,v2,v3); + } + + int plusZVoxel = m_volData->getVoxelAt(x,y,z+1).getDensity() >= VoxelType::getThreshold(); + if(currentVoxel > plusZVoxel) + { + int material = m_volData->getVoxelAt(x,y,z).getMaterial(); + + uint32_t v0 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX - 0.5f, regY - 0.5f, regZ + 0.5f), 4, material)); + uint32_t v1 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX - 0.5f, regY + 0.5f, regZ + 0.5f), 4, material)); + uint32_t v2 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX + 0.5f, regY - 0.5f, regZ + 0.5f), 4, material)); + uint32_t v3 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX + 0.5f, regY + 0.5f, regZ + 0.5f), 4, material)); + + m_meshCurrent->addTriangleCubic(v0,v2,v1); + m_meshCurrent->addTriangleCubic(v1,v2,v3); + } + if(currentVoxel < plusZVoxel) + { + int material = m_volData->getVoxelAt(x,y,z+1).getMaterial(); + + uint32_t v0 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX - 0.5f, regY - 0.5f, regZ + 0.5f), 5, material)); + uint32_t v1 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX - 0.5f, regY + 0.5f, regZ + 0.5f), 5, material)); + uint32_t v2 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX + 0.5f, regY - 0.5f, regZ + 0.5f), 5, material)); + uint32_t v3 = m_meshCurrent->addVertex(PositionMaterialIndex(Vector3DFloat(regX + 0.5f, regY + 0.5f, regZ + 0.5f), 5, material)); + + m_meshCurrent->addTriangleCubic(v0,v1,v2); + m_meshCurrent->addTriangleCubic(v1,v3,v2); + } + } + } + } + + m_meshCurrent->m_Region = m_regSizeInVoxels; + + m_meshCurrent->m_vecLodRecords.clear(); + LodRecord lodRecord; + lodRecord.beginIndex = 0; + lodRecord.endIndex = m_meshCurrent->getNoOfIndices(); + m_meshCurrent->m_vecLodRecords.push_back(lodRecord); + } +} \ No newline at end of file Index: include/VertexTypes.h =================================================================== --- include/VertexTypes.h (revision 1438) +++ include/VertexTypes.h (working copy) @@ -79,6 +79,44 @@ Vector3DFloat normal; float material; //FIXME: This shouldn't be float on CPU? }; + + /******************************************************************************* + Mapping between indices and normals + + 0 - 1.0f, 0.0f, 0.0f + 1 - -1.0f, 0.0f, 0.0f + 2 - 0.0f, 1.0f, 0.0f + 3 - 0.0f, -1.0f, 0.0f + 4 - 0.0f, 0.0f, 1.0f + 5 - 0.0f, 0.0f, -1.0f + *******************************************************************************/ + +#ifdef SWIG + class PositionMaterialIndex +#else + class POLYVOXCORE_API PositionMaterialIndex +#endif + { + public: + PositionMaterialIndex(); + PositionMaterialIndex(Vector3DFloat positionToSet, float materialToSet); + PositionMaterialIndex(Vector3DFloat positionToSet, char indexToSet, float materialToSet); + + float getMaterial(void) const; + char getIndex(void) const; + const Vector3DFloat& getPosition(void) const; + + void setMaterial(float materialToSet); + void setIndex(char indexToSet); + void setPosition(const Vector3DFloat& positionToSet); + + public: + //Nicely fits into seven floats, meaning we + //can squeeze in one more for material blending. + Vector3DFloat position; + char index; + float material; //FIXME: This shouldn't be float on CPU? + }; } #endif Index: source/VertexTypes.cpp =================================================================== --- source/VertexTypes.cpp (revision 1438) +++ source/VertexTypes.cpp (working copy) @@ -75,6 +75,54 @@ position = positionToSet; } + PositionMaterialIndex::PositionMaterialIndex() + { + } + + PositionMaterialIndex::PositionMaterialIndex(Vector3DFloat positionToSet, float materialToSet) + :position(positionToSet) + ,material(materialToSet) + { + + } + + PositionMaterialIndex::PositionMaterialIndex(Vector3DFloat positionToSet, char indexToSet, float materialToSet) + :position(positionToSet) + ,index(indexToSet) + ,material(materialToSet) + { + } + + float PositionMaterialIndex::getMaterial(void) const + { + return material; + } + + char PositionMaterialIndex::getIndex(void) const + { + return index; + } + + const Vector3DFloat& PositionMaterialIndex::getPosition(void) const + { + return position; + } + + void PositionMaterialIndex::setMaterial(float materialToSet) + { + material = materialToSet; + } + + void PositionMaterialIndex::setIndex(char indexToSet) + { + index = indexToSet; + } + + void PositionMaterialIndex::setPosition(const Vector3DFloat& positionToSet) + { + position = positionToSet; + } + //////////////////////////////////////////////////////////////////////////////// // PositionMaterial ////////////////////////////////////////////////////////////////////////////////