Guest User

Untitled

a guest
Jul 21st, 2010
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.89 KB | None | 0 0
  1. #ifndef MESHUTILS_H
  2. #define MESHUTILS_H
  3.  
  4. #include "Ogre.h"
  5.  
  6. using namespace Ogre;
  7.  
  8. /**
  9.  * Struct used to pass mesh data among libraries or to load from a file.
  10.  * Such as loading a heightfield file into a physics library.
  11.  */
  12. struct MeshData {
  13.         unsigned int vertexCount;
  14.         unsigned int triangleCount;
  15.         Vector3* vertices;
  16.         Vector3* triangles;
  17.  
  18.         // todo xmin, xmax, ....
  19.  
  20.         MeshData() :
  21.                 vertexCount(0), triangleCount(0), vertices(0), triangles(0) {}
  22.         ~MeshData() {
  23.             delete [] vertices;
  24.             delete [] triangles;
  25.         }
  26.     };
  27.  
  28.  
  29. class MeshUtils
  30.     {
  31.     public:
  32.         static MeshData* getMeshData(const MeshPtr& mesh);
  33.         static std::pair<int, int> getMeshCount(const MeshPtr& mesh);
  34.         static void meshBuffersToArrays(const MeshPtr& mesh, Vector3* vertices, unsigned long* indices);
  35.         static ManualObject createWire(const MeshPtr& mesh);
  36.     };
  37.  
  38. #endif
  39.  
  40.  
  41. ---------------------------------------------------------------------------------------------
  42.  
  43. #include "MeshUtils.h"
  44.  
  45. using namespace Ogre;
  46.  
  47. MeshData* MeshUtils::getMeshData(const MeshPtr& mesh)
  48. {
  49.     MeshData* md = new MeshData();
  50.  
  51.     std::pair<int, int> count = MeshUtils::getMeshCount(mesh);
  52.     md->vertexCount = count.first;
  53.     md->triangleCount = count.second / 3;
  54.  
  55.     unsigned long* indices = new unsigned long[count.second];
  56.    
  57.     md->vertices = new Vector3[md->vertexCount];
  58.     md->triangles = new Vector3[md->triangleCount];
  59.  
  60.     meshBuffersToArrays(mesh, md->vertices, indices);
  61.     for (size_t i = 0; i < md->triangleCount; i+=3) {
  62.             md->triangles->x = indices[i];
  63.             md->triangles->y = indices[i+1];
  64.             md->triangles->z = indices[i+2];
  65.         }
  66.  
  67.     return md;
  68. }
  69.  
  70.  
  71. std::pair<int, int> MeshUtils::getMeshCount(const MeshPtr& mesh)
  72. {
  73.     size_t vertex_count = 0;
  74.     size_t index_count = 0;
  75.     bool added_shared = false;
  76.  
  77.     for (int j = 0; j < mesh->getNumSubMeshes(); j++) {
  78.             SubMesh* submesh = mesh->getSubMesh(j);
  79.  
  80. //          VertexData* vertex_data = submesh->vertexData;
  81. //          IndexData* index_data = submesh->indexData;
  82.  
  83.             // We only need to add the shared vertices once
  84.             if (submesh->useSharedVertices) {
  85.                     if (!added_shared) {
  86.                             vertex_count += mesh->sharedVertexData->vertexCount;
  87.                             added_shared = true;
  88.                         }
  89.                 } else {
  90.                     vertex_count += submesh->vertexData->vertexCount;
  91.                 }
  92.  
  93.             // Add the indices
  94.             index_count += submesh->indexData->indexCount;
  95.         }
  96.  
  97.     return std::make_pair(vertex_count, index_count);
  98. }
  99.  
  100. void MeshUtils::meshBuffersToArrays(const MeshPtr& mesh, Vector3* vertices, unsigned long* indices)
  101. {
  102.     bool added_shared = false;
  103.     size_t current_offset = 0;
  104.     size_t shared_offset = 0;
  105.     size_t next_offset = 0;
  106.     size_t index_offset = 0;
  107.  
  108.     //const Ogre::Vector3 &position = ent->getParentNode()->_getDerivedPosition();
  109.     //const Ogre::Quaternion &orient = ent->getParentNode()->_getDerivedOrientation();
  110.     //const Ogre::Vector3 &scale = ent->getParentNode()->_getDerivedScale();
  111.  
  112.     const Ogre::Vector3 &position = Vector3::ZERO;
  113.     const Ogre::Quaternion &orient = Quaternion::IDENTITY;
  114.     const Ogre::Vector3 &scale = Vector3::UNIT_SCALE;
  115.  
  116.     Mesh::SubMeshIterator itr = mesh->getSubMeshIterator();
  117.     while (itr.hasMoreElements()) {
  118.             SubMesh* submesh = itr.getNext();
  119.  
  120.             Ogre::VertexData* vertex_data = submesh->useSharedVertices ? mesh->sharedVertexData : submesh->vertexData;
  121.  
  122.             if ((!submesh->useSharedVertices) || (submesh->useSharedVertices && !added_shared)) {
  123.                     if (submesh->useSharedVertices) {
  124.                             added_shared = true;
  125.                             shared_offset = current_offset;
  126.                         }
  127.  
  128.                     const Ogre::VertexElement* posElem =
  129.                         vertex_data->vertexDeclaration->findElementBySemantic(Ogre::VES_POSITION);
  130.  
  131.                     Ogre::HardwareVertexBufferSharedPtr vbuf =
  132.                         vertex_data->vertexBufferBinding->getBuffer(posElem->getSource());
  133.  
  134.                     unsigned char* vertex =
  135.                         static_cast<unsigned char*> (vbuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
  136.  
  137.                     // There is _no_ baseVertexPointerToElement() which takes an Ogre::Real or a double
  138.                     //  as second argument. So make it float, to avoid trouble when Ogre::Real will
  139.                     //  be comiled/typedefed as double:
  140.                     //      Ogre::Real* pReal;
  141.                     float* pReal;
  142.  
  143.                     for (size_t k = 0; k < vertex_data->vertexCount; ++k, vertex += vbuf->getVertexSize()) {
  144.                             posElem->baseVertexPointerToElement(vertex, &pReal);
  145.  
  146.                             Ogre::Vector3 pt(pReal[0], pReal[1], pReal[2]);
  147.  
  148.                             vertices[current_offset + k] = (orient * (pt * scale)) + position;
  149.                             //vertices[current_offset + k] = pt;
  150.                         }
  151.  
  152.                     vbuf->unlock();
  153.                     next_offset += vertex_data->vertexCount;
  154.                 }
  155.  
  156.  
  157.             Ogre::IndexData* index_data = submesh->indexData;
  158.             size_t numTris = index_data->indexCount / 3;
  159.             Ogre::HardwareIndexBufferSharedPtr ibuf = index_data->indexBuffer;
  160.  
  161.             bool use32bitindexes = (ibuf->getType() == Ogre::HardwareIndexBuffer::IT_32BIT);
  162.  
  163.             unsigned long* pLong = static_cast<unsigned long*> (ibuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
  164.             unsigned short* pShort = reinterpret_cast<unsigned short*> (pLong);
  165.  
  166.  
  167.             size_t offset = (submesh->useSharedVertices) ? shared_offset : current_offset;
  168.  
  169.             if (use32bitindexes) {
  170.                     for (size_t k = 0; k < numTris * 3; ++k) {
  171.                             indices[index_offset++] = pLong[k] + static_cast<unsigned long> (offset);
  172.                         }
  173.                 } else {
  174.                     for (size_t k = 0; k < numTris * 3; ++k) {
  175.                             indices[index_offset++] = static_cast<unsigned long> (pShort[k]) +
  176.                                                       static_cast<unsigned long> (offset);
  177.                         }
  178.                 }
  179.  
  180.             ibuf->unlock();
  181.             current_offset = next_offset;
  182.         }
  183. }
  184.  
  185. ManualObject MeshUtils::createWire(const MeshPtr& mesh)
  186. {
  187.     ManualObject mo("ManualObject");   
  188.      
  189.     MeshData* md = MeshUtils::getMeshData(mesh);
  190.  
  191.     mo.begin("BaseWhiteNoLighting", RenderOperation::OT_TRIANGLE_LIST);
  192.  
  193.     for(unsigned int i=0; i<md->triangleCount; i++)
  194.     {
  195.         Vector3 v = md->triangles[i];
  196.         mo.position(v.x, v.y, v.z);
  197.         std::cout << v << std::endl;
  198.     }
  199.     mo.end();
  200.    
  201.     return mo;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment