关闭

Ogre 获取实体顶点和索引

标签: vectorfloat
449人阅读 评论(0) 收藏 举报

bool added_shared = false;
 size_t current_offset = 0;
 size_t shared_offset = 0;
 size_t next_offset = 0;
 size_t index_offset = 0;
 size_t index_count = 0;
 size_t vertex_count = 0;

 MeshPtr ogreHeadMesh = polacub1->getMesh();
 for(unsigned short i = 0; i< ogreHeadMesh->getNumSubMeshes(); i++)
 {
  SubMesh * submesh = ogreHeadMesh->getSubMesh(i);
  if(submesh->useSharedVertices)
  {
   if(!added_shared)
   {
    vertex_count += ogreHeadMesh->sharedVertexData->vertexCount;
    added_shared = true;
   }
  }
  else
  {
   vertex_count += submesh->vertexData->vertexCount;
  }

  index_count += submesh->indexData->indexCount;
 }

Ogre::Vector3* vertices = new Vector3[vertex_count];
 long* indices = new long[index_count];

 for(unsigned short i = 0; i < ogreHeadMesh->getNumSubMeshes(); i++)
 {
  SubMesh* submesh = ogreHeadMesh->getSubMesh(i);
  VertexData* vertex_data = submesh->useSharedVertices ? ogreHeadMesh->sharedVertexData : submesh->vertexData;
  if((!submesh->useSharedVertices) || (submesh->useSharedVertices && !added_shared))
  {
   if(submesh->useSharedVertices)
   {
    added_shared = true;
    shared_offset = current_offset;
   }
   const VertexElement* posElem = vertex_data->vertexDeclaration->findElementBySemantic(Ogre::VES_POSITION);
   Ogre::HardwareVertexBufferSharedPtr vbuf = vertex_data->vertexBufferBinding->getBuffer(posElem->getSource());
   unsigned char* vertex = static_cast<unsigned char*>(vbuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));

   float* pReal;
   for(size_t j = 0; j < vertex_data->vertexCount; j++,vertex += vbuf->getVertexSize())
   {
    posElem->baseVertexPointerToElement(vertex,&pReal);
    Vector3 pt(pReal[0],pReal[1],pReal[2]);

    vertices[current_offset+j] = (Quaternion::IDENTITY * (pt * Vector3::UNIT_SCALE)) + Vector3::ZERO;

   }
   vbuf->unlock();

   next_offset += vertex_data->vertexCount;
  }

  IndexData* index_data = submesh->indexData;
  size_t numTris = index_data->indexCount / 3;

  Ogre::HardwareIndexBufferSharedPtr ibuf = index_data->indexBuffer;
  bool use32bitindexs = (ibuf->getType() == Ogre::HardwareIndexBuffer::IT_32BIT);
  unsigned long* pLong = static_cast<unsigned long*>(ibuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
  unsigned short* pShort = reinterpret_cast<unsigned short*>(pLong);

  size_t offset = (submesh->useSharedVertices) ? shared_offset : current_offset;

  if(use32bitindexs)
  {
   for(size_t k = 0; k < numTris * 3; k++)
   {
    indices[index_offset++] = pLong[k] + static_cast<unsigned long>(offset);

   }
  }
  else

  {
   for(size_t k = 0; k < numTris * 3; k++)
   {
    indices[index_offset++] = static_cast<unsigned long>(pShort[k]) + static_cast<unsigned long>(offset);

   }
  }
  ibuf->unlock();
  current_offset = next_offset;

 }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:21757次
    • 积分:346
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:17篇
    • 译文:0篇
    • 评论:0条
    博客链接