OgrePolyvoxInterface::OgrePolyvoxInterface() { scene = (Ogre::SceneManager*)GraphicsManager::instance()->getInterface()->getScene(); int width, height; LOG("Creating heightmap"); float* heightmap = CreateHeightmapFromFile("..\\heightmap.png", width, height, 1.f); LOG("Creating volume"); volume = new PolyVox::Volume<PolyVox::MaterialDensityPair44>(PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(width-1, 63, height-1))); LOG("Writing heightmap to volume"); createHeightmapInVolume(volume, heightmap, width, height); mesh = new PolyVox::SurfaceMesh<PositionMaterialNormal>(); SurfaceExtractor<MaterialDensityPair44> surfaceExtractor(volume, volume->getEnclosingRegion(), mesh); surfaceExtractor.execute(); const std::vector<boost::uint32_t>& indices = mesh->getIndices(); const std::vector<PositionMaterialNormal>& vertices = mesh->getVertices(); node = scene->getRootSceneNode()->createChildSceneNode(); obj = scene->createManualObject(); int vertex_count = mesh->getNoOfVertices(); int index_count = mesh->getNoOfIndices(); obj->estimateVertexCount(vertex_count); obj->estimateIndexCount(index_count); LOG("Building manual object"); obj->begin("BaseWhite", Ogre::RenderOperation::OT_TRIANGLE_LIST); LOG("Building vertex data"); for(std::vector<PositionMaterialNormal>::const_iterator pmn = vertices.begin(); pmn != vertices.end(); pmn++) { PolyVox::Vector3DFloat position = (*pmn).getPosition(); PolyVox::Vector3DFloat normal = (*pmn).getNormal(); obj->position(position.getX(), position.getY(), position.getZ()); obj->normal(normal.getX(), normal.getY(), normal.getZ()); } LOG("Building indices"); for(int i=0; i<mesh->getNoOfIndices(); i+=3) { obj->triangle(indices[i], indices[i+1], indices[i+2]); } obj->end(); char mesh_name[32]; sprintf(mesh_name, "%i%i", rand()%1000, rand()%1000, rand()%1000, rand()%1000); LOG("Converting to mesh"); Ogre::MeshPtr ptr = obj->convertToMesh(mesh_name); Ogre::Mesh* ogremesh = ptr.get(); Ogre::Entity* entity = scene->createEntity("TilemapEntity", mesh_name); node->attachObject(entity); LOG("Done!"); }
void World::polyVoxMeshToOgreObject(PolyVox::SurfaceMesh<PolyVox::PositionMaterialNormal>* mesh,Ogre::ManualObject* mo) { // make sure mesh is allocated and valid first: if (mesh == NULL || mesh->getNoOfVertices() < 1) { Utils::log("ERROR: World::polyVoxMeshToOgreObject() got a mesh with no data."); return; } else if (mesh->getNoOfIndices() < 1) { Utils::log("ERROR: World::polyVoxMeshToOgreObject() got a mesh with no indices."); return; } const std::vector<uint32_t>& indices = mesh->getIndices(); const std::vector<PolyVox::PositionMaterialNormal>& vertices = mesh->getVertices(); uint32_t numIndices = mesh->getNoOfIndices(); uint32_t numVertices = mesh->getNoOfVertices(); mo->estimateIndexCount(numIndices); mo->estimateVertexCount(numVertices); mo->begin("BaseWhiteNoLighting", Ogre::RenderOperation::OT_TRIANGLE_LIST); PolyVox::Vector3DFloat position; PolyVox::Vector3DFloat normal; float material; int textureIndex = 0; const Ogre::Vector2 texCoords[4] = { Ogre::Vector2(0.0f, 1.0f), Ogre::Vector2(1.0f, 1.0f), Ogre::Vector2(0.0f, 0.0f), Ogre::Vector2(1.0f, 0.0f) }; // vertices: for(unsigned int i=0; i<numVertices; ++i) { position = vertices[i].getPosition(); normal = vertices[i].getNormal(); material = vertices[i].getMaterial(); mo->position((Ogre::Real)(position.getX()), (Ogre::Real)(position.getY()), (Ogre::Real)(position.getZ())); mo->normal( (Ogre::Real)(normal.getX()), (Ogre::Real)(normal.getY()), (Ogre::Real)(normal.getZ())); mo->textureCoord(texCoords[textureIndex++ % 4]); } // indices: for(unsigned int i = 0 ; i < numIndices ; i++) { mo->index((Ogre::uint32)(indices[i])); } mo->end(); }
void VoxelTerrain::mesh() { PolyVox::SurfaceMesh<PolyVox::PositionMaterialNormal> mesh; PolyVox::Region region(PolyVox::Vector3DInt32(0,0,0), PolyVox::Vector3DInt32(120, 60, 120)); //PolyVox::Vector3DInt32(120, 130, 120)); std::cout << "Begin extracting surface." << std::endl; //PolyVox::MarchingCubesSurfaceExtractor<PolyVox::LargeVolume<BYTE> > PolyVox::CubicSurfaceExtractorWithNormals<PolyVox::LargeVolume<BYTE> > surface_extractor(mVolumeData, region, &mesh); surface_extractor.execute(); std::cout << "Done extracting surface." << std::endl; begin("Terrain", Ogre::RenderOperation::OT_TRIANGLE_LIST); { const std::vector<PolyVox::PositionMaterialNormal>& vecVertices = mesh.getVertices(); const std::vector<uint32_t>& vecIndices = mesh.getIndices(); unsigned int uLodLevel = 0; int beginIndex = mesh.m_vecLodRecords[uLodLevel].beginIndex; int endIndex = mesh.m_vecLodRecords[uLodLevel].endIndex; for(int index = beginIndex; index < endIndex; ++index) { const PolyVox::PositionMaterialNormal& vertex = vecVertices[vecIndices[index]]; const PolyVox::Vector3DFloat& v3dVertexPos = vertex.getPosition(); const PolyVox::Vector3DFloat& v3dVertexNormal = vertex.getNormal(); const PolyVox::Vector3DFloat v3dFinalVertexPos = v3dVertexPos + static_cast<PolyVox::Vector3DFloat>(mesh.m_Region.getLowerCorner()); position(v3dFinalVertexPos.getX(), v3dFinalVertexPos.getY(), v3dFinalVertexPos.getZ()); normal(v3dVertexNormal.getX(), v3dVertexNormal.getY(), v3dVertexNormal.getZ()); //colour(1, 0, 0, 0.5); textureCoord(v3dFinalVertexPos.getX()/4.0f, v3dFinalVertexPos.getY()/4.0f, v3dFinalVertexPos.getZ()/4.0f); } } end(); }