Guest User

some code to go from PolyVox mesh to ogre manual object.

a guest
Oct 6th, 2010
894
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. VolumeMapView::_manualFromMesh(PolyVox::SurfaceMesh* mesh, Ogre::ManualObject* manual)
  2. {
  3.   const float ATLAS_WIDTH = 512.0f;
  4.   const float TEX_WIDTH = 32.0f;
  5.   const float BlockOffset = TEX_WIDTH / ATLAS_WIDTH;
  6.   const float pixelOffset = 1.0 / ATLAS_WIDTH;
  7.   const float blockEOffset = 1.0 / TEX_WIDTH;
  8.   using std::vector;
  9.   const vector<uint32_t>& indices = mesh->getIndices();
  10.   const vector<SurfaceVertex>& vertices = mesh->getVertices();
  11.  
  12.   if(vertices.size() < 1)
  13.     return;
  14.  
  15.   //Build the Ogre Manual Object. We first iterate through the vertices add position to that.
  16.   //
  17.   const Ogre::Vector2 texCoords[4] =
  18.     { Ogre::Vector2(0.0f, 1.0f), Ogre::Vector2(1.0f, 1.0f), Ogre::Vector2(0.0f, 0.0f), Ogre::Vector2(1.0f, 0.0f) };
  19.   size_t texIdx = 0;
  20.  
  21.   _manual->estimateVertexCount(indices.size());
  22.   _manual->estimateVertexCount(vertices.size());
  23.  
  24.   _manual->begin("PRJZ/Minecraft", Ogre::RenderOperation::OT_TRIANGLE_LIST, "PROJECT_ZOMBIE");
  25.  
  26.   for (vector<SurfaceVertex>::const_iterator itVertex = vertices.begin(); itVertex != vertices.end(); ++itVertex)
  27.     {
  28.       const SurfaceVertex& vertex = *itVertex;
  29.       const Vector3DFloat& vertPos = vertex.getPosition();
  30.       //const Vector3DFloat& finalPos = vertPos + static_cast<Vector3DFloat> (mesh.m_Region.getLowerCorner());
  31.       Vector3DFloat origin(_origin.x, _origin.y, _origin.z);
  32.       const Vector3DFloat& finalPos = vertPos + origin;
  33.       manual->position(finalPos.getX(), finalPos.getY(), finalPos.getZ());
  34.       manual->normal(vertex.getNormal().getX(), vertex.getNormal().getY(), vertex.getNormal().getZ());
  35.       //Assume that the passed in position are counter clockwise starting from top left corner.
  36.       manual->textureCoord(texCoords[texIdx % 4]);
  37.       texIdx++;
  38.  
  39.       Ogre::ColourValue val;
  40.       uint8_t material = vertex.getMaterial() + 0.5;
  41.       val.r = (float) (material - 1) * BlockOffset;// - pixelOffset;
  42.       val.g = 0.0f;
  43.       val.b = 0.0f;
  44.       val.a = 1.0f;
  45.       manual->colour(val);
  46.     }
  47.   //Then iterate through the indices add create the indices list.
  48.   for (vector<uint32_t>::const_iterator itIdx = indices.begin(); itIdx != indices.end(); ++itIdx)
  49.     {
  50.       manual->index(*itIdx);
  51.       uint32_t ix = *itIdx;
  52.       //cout << "INDEX!: " << ix << endl;
  53.     }
  54.   manual->end();
  55.  
  56.   //add it to root scene.
  57.   _root->attachObject(manual);
  58.   _root->setVisible(true);
  59.   //scnMgr->getRootSceneNode()->createChildSceneNode()->attachObject(manual);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment