I've revamped my code but I still have trouble getting the entire mesh to display. Sometimes - especially when trying to display multiple PolyMeshes which is just the voxel representation of that region. You'll see what I mean by the picture - the mesh should have a top and full sides, but it doesn't. If I click to add to the top, then the top will show for some reason. Here's my updated code also -
Code:
// RECENT PolyMeshToMesh
bool WorldCraft::PolyMeshToMesh(Polymesh &p)
{
// ************ start mesh build code
// Make a surfaceMesh from the voxelData
// Extract the surface
//PolyVox::SurfaceExtractor<SimpleVolume, Density8> surf(&resultVolume, fullRegion2, &mesh); // Use if smoothing
PolyVox::CubicSurfaceExtractorWithNormals<LargeVolume, Density8> surf(&volume, p.region, &mesh); // Use for semi-smooth surface
//PolyVox::CubicSurfaceExtractorWithNormals <SimpleVolume, Density8> surf(pMesh.volume, pMesh.volume->getEnclosingRegion(), &mesh); // Use if not smoothing
try {
surf.execute();
}
catch(...) {
cout << "Failed execute Surface Extractor" << endl;
}
vertsAppended = false;
indicesAppended = false;
if (p.mMeshAttached == true){
//p.meshOBJ->unload();
// Destroy all the attached objects
SceneNode::ObjectIterator itObject = p.node->getAttachedObjectIterator();
while (itObject.hasMoreElements() )
{
Entity * pObject = static_cast<Entity *>(itObject.getNext());
String name = pObject->getName();
if (name.compare(0,10,"nodeMarker") != 0) {
pObject->detachFromParent();
mKeyDevices.mSceneMgr->destroyEntity( pObject );
}
}
p.entity = NULL;
Ogre::MeshManager::getSingleton().remove("CustomMesh"+ Ogre::StringConverter::toString(p.position / divisor));
}
if( vecVertices.size() > 0 && vecIndices.size() > 0) {
/// Create the mesh via the MeshManager
Ogre::ResourceManager::ResourceCreateOrRetrieveResult result = MeshManager::getSingleton().createOrRetrieve("CustomMesh"+ Ogre::StringConverter::toString(p.position / divisor), "General", true,tfm,0,HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE,HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE,false,false);
p.meshOBJ = result.first;
/// Create one submesh
p.submeshOBJ = p.meshOBJ->createSubMesh();
// We first create a VertexData
// Then, we link it to our Mesh/SubMesh :
#ifdef SHARED_GEOMETRY
mMesh->sharedVertexData = data;
#else
p.submeshOBJ->useSharedVertices = false; // This value is 'true' by default
p.submeshOBJ->vertexData = new Ogre::VertexData();
#endif
// We have to provide the number of vertices we'll put into this Mesh/SubMesh
p.submeshOBJ->vertexData->vertexCount = vecVertices.size();
// Then we can create our VertexDeclaration
Ogre::VertexDeclaration* decl = p.submeshOBJ->vertexData->vertexDeclaration;
// define the vertex format
VertexDeclaration* vertexDecl = p.submeshOBJ->vertexData->vertexDeclaration;
size_t currOffset = 0;
// positions
vertexDecl->addElement(0, currOffset, VET_FLOAT3, VES_POSITION);
// allocate the vertex buffer
HardwareVertexBufferSharedPtr vBuf = HardwareBufferManager::getSingleton().createVertexBuffer(vertexDecl->getVertexSize(0), p.submeshOBJ->vertexData->vertexCount, HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE, false);
VertexBufferBinding* binding = p.submeshOBJ->vertexData->vertexBufferBinding;
binding->setBinding(0, vBuf);
float* pVertex = static_cast<float*>(vBuf->lock(HardwareBuffer::HBL_DISCARD));
// allocate index buffer
Ogre::HardwareIndexBufferSharedPtr ibuf = Ogre::HardwareBufferManager::getSingleton().createIndexBuffer(
Ogre::HardwareIndexBuffer::IT_16BIT, // You can use several different value types here
vecIndices.size(), // The number of indices you'll put in that buffer
Ogre::HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE // Properties
);
unsigned short* pIndices = static_cast<unsigned short*>(ibuf->lock(HardwareBuffer::HBL_DISCARD));
// Create new Scene Node
if (p.node == NULL) {
p.node = mKeyDevices.mSceneMgr->getRootSceneNode()->createChildSceneNode(p.position);
}
// Print debug info to gui windows
showDebugStats(p);
// Begin writing to manualObject
unsigned int uLodLevel = 0;
for(int v = 0; v < vecVertices.size(); v++)
{
PolyVox::PositionMaterialNormal vertex = vecVertices[v];
const PolyVox::Vector3DFloat& v3dVertexPos = vertex.getPosition();
*pVertex++ = v3dVertexPos.getX();
*pVertex++ = v3dVertexPos.getY();
*pVertex++ = v3dVertexPos.getZ();
}
for(int index = 0; index < vecIndices.size(); index++)
{
*pIndices++ = vecIndices.at(index);
}
vBuf->unlock();
ibuf->unlock();
/// Set parameters of the submesh
p.submeshOBJ->indexData->indexBuffer = ibuf;
p.submeshOBJ->indexData->indexCount = vecIndices.size();
p.submeshOBJ->indexData->indexStart = 0;
/// Set bounding information (for culling)
AxisAlignedBox aabox = AxisAlignedBox(0, 0, 0, divisor, divisor, divisor);
p.meshOBJ->_setBounds(aabox);
p.meshOBJ->_setBoundingSphereRadius((aabox.getMaximum()-aabox.getMinimum()).length()/2.0);
/// Notify -Mesh object that it has been loaded
if(p.mMeshAttached == false) p.meshOBJ->load();
p.entity = mKeyDevices.mSceneMgr->createEntity("CustomMesh"+ Ogre::StringConverter::toString(p.position / divisor));
p.entity->setMaterialName("Worldcraft/Greengrass");
p.node->attachObject(p.entity);
if (p.mMeshAttached == false) p.mMeshAttached = true;
return true;
}
return false;
}