I wrote a function to try to cut down on updating duplicate regions, but I now end up with a checkerboard mesh. One region visible, the next not and so on. It just adds any updated or created regions to a std::map and checks against that. But where is my logic flawed in doing that?
From what I can tell, I'm somehow ending up with regions that have no verts or indices to display a mesh from. Is it possible that every other region has it's verts in the neighboring region? This is getting confusing but I'm trying to cut down the draw time.
Code:
void WorldCraft::updateNeighbors(Vector3DInt32 polyMesh) {
polysIter = polys.find(Vector3DInt32(polyMesh.getX() - 1, polyMesh.getY(), polyMesh.getZ()));
if(polysIter == polys.end()){
polys.insert(std::pair<Vector3DInt32, Vector3DInt32>(Vector3DInt32(polyMesh.getX() - 1, polyMesh.getY(), polyMesh.getZ()), Vector3DInt32(polyMesh.getX() - 1, polyMesh.getY(), polyMesh.getZ())));
createOrUpdate(Vector3DInt32(polyMesh.getX() - 1, polyMesh.getY(), polyMesh.getZ()));
}
polysIter = polys.find(Vector3DInt32(polyMesh.getX() + 1, polyMesh.getY(), polyMesh.getZ()));
if(polysIter == polys.end()){
polys.insert(std::pair<Vector3DInt32, Vector3DInt32>(Vector3DInt32(polyMesh.getX() + 1, polyMesh.getY(), polyMesh.getZ()), Vector3DInt32(polyMesh.getX() + 1, polyMesh.getY(), polyMesh.getZ())));
createOrUpdate(Vector3DInt32(polyMesh.getX() + 1, polyMesh.getY(), polyMesh.getZ()));
}
polysIter = polys.find(Vector3DInt32(polyMesh.getX(), polyMesh.getY()-1, polyMesh.getZ()));
if(polysIter == polys.end()){
polys.insert(std::pair<Vector3DInt32, Vector3DInt32>(Vector3DInt32(polyMesh.getX(), polyMesh.getY()-1, polyMesh.getZ()), Vector3DInt32(polyMesh.getX(), polyMesh.getY()-1, polyMesh.getZ())));
createOrUpdate(Vector3DInt32(polyMesh.getX() , polyMesh.getY()- 1, polyMesh.getZ()));
}
polysIter = polys.find(Vector3DInt32(polyMesh.getX(), polyMesh.getY()+1, polyMesh.getZ()));
if(polysIter == polys.end()){
polys.insert(std::pair<Vector3DInt32, Vector3DInt32>(Vector3DInt32(polyMesh.getX(), polyMesh.getY()+1, polyMesh.getZ()), Vector3DInt32(polyMesh.getX(), polyMesh.getY()+1, polyMesh.getZ())));
createOrUpdate(Vector3DInt32(polyMesh.getX() , polyMesh.getY()+ 1, polyMesh.getZ()));
}
polysIter = polys.find(Vector3DInt32(polyMesh.getX(), polyMesh.getY(), polyMesh.getZ()-1));
if(polysIter == polys.end()){
polys.insert(std::pair<Vector3DInt32, Vector3DInt32>(Vector3DInt32(polyMesh.getX(), polyMesh.getY(), polyMesh.getZ()-1), Vector3DInt32(polyMesh.getX(), polyMesh.getY(), polyMesh.getZ()-1)));
createOrUpdate(Vector3DInt32(polyMesh.getX(), polyMesh.getY(), polyMesh.getZ() - 1));
}
polysIter = polys.find(Vector3DInt32(polyMesh.getX(), polyMesh.getY(), polyMesh.getZ()+1));
if(polysIter == polys.end()){
polys.insert(std::pair<Vector3DInt32, Vector3DInt32>(Vector3DInt32(polyMesh.getX(), polyMesh.getY(), polyMesh.getZ()+1), Vector3DInt32(polyMesh.getX(), polyMesh.getY(), polyMesh.getZ()+1)));
createOrUpdate(Vector3DInt32(polyMesh.getX(), polyMesh.getY(), polyMesh.getZ() + 1));
}
}
and here's my region filling code -
Code:
void WorldCraft::fillVoxelsBetween(Vector3DInt32 start, Vector3DInt32 end, Density8 tValue) {
int startx, starty, startz, endx, endy, endz;
int startxPoly, startyPoly, startzPoly, endxPoly, endyPoly, endzPoly;
if(start.getX() < end.getX() ) { startx = start.getX(); endx = end.getX(); } else { startx = end.getX(); endx = start.getX(); }
if(start.getY() < end.getY() ) { starty = start.getY(); endy = end.getY(); } else { starty = end.getY(); endy = start.getY(); }
if(start.getZ() < end.getZ() ) { startz = start.getZ(); endz = end.getZ(); } else { startz = end.getZ(); endz = start.getZ(); }
for( int x = startx; x < endx; x++) {
for ( int y = starty; y < endy; y ++) {
for ( int z = startz; z < endz; z++) {
volume.setVoxelAt(Vector3DInt32(x,y,z), tValue);
}
}
}
startxPoly = Ogre::Math::Floor(startx / divisor);
startyPoly = Ogre::Math::Floor(starty / divisor);
startzPoly = Ogre::Math::Floor(startz / divisor);
endxPoly = Ogre::Math::Floor(endx / divisor);
endyPoly = Ogre::Math::Floor(endy / divisor);
endzPoly = Ogre::Math::Floor(endz / divisor);
for( int x = startxPoly; x < endxPoly + 1; x++) {
for ( int y = startyPoly; y < endyPoly + 1; y ++) {
for ( int z = startzPoly; z < endzPoly + 1; z++) {
polysIter = polys.find(Vector3DInt32(x,y,z));
createOrUpdate(Vector3DInt32(x,y,z));
if(polysIter == polys.end()){
polys.insert(std::pair<Vector3DInt32, Vector3DInt32>(Vector3DInt32(x,y,z), Vector3DInt32(x,y,z)));
updateNeighbors(Vector3DInt32(x,y,z));
}
}
}
}
CEGUI::Window * myWindow = mKeyDevices.windowManager->getWindow("Root/VertBuffer");
myWindow->setText("");
for(polysIter = polys.begin(); polysIter != polys.end(); polysIter++) {
guiOutput(myWindow, const_cast<Vector3DInt32&>((*polysIter).first));
}
polys.clear();
}