Ogre中mesh的顶点数据与索引数据

【转】Ogre的mesh的顶点数据与索引数据

转载自: http://blog.csdn.net/zhuxiaoyang2000/article/details/6565962
//读取的数据用于物理引擎的计算

/** * 获取Ogre的Mesh信息

* @param mesh: Ogre的Mesh指针

* @return void

*/

void ActorNode::GetMeshInformation(const Ogre::Mesh* const mesh,

                     size_t &vertex_count,Ogre::Vector3* &vertices,

                      size_t &index_count,unsigned long* &indices,

                       const Ogre::Vector3 &position /* = Ogre::Vector3::ZERO */,

                      const Ogre::Quaternion &orient /* = Ogre::Quaternion::IDENTITY */,

                      const Ogre::Vector3 &scale /* = Ogre::Vector3::UNIT_SCALE */)

{

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

vertex_count = index_count = 0;

// 计算顶点数据与索引数据的大小
for ( unsigned short i = 0; i < mesh->getNumSubMeshes(); ++i)
{
   Ogre::SubMesh* submesh = mesh->getSubMesh(i);

   // 只需要添加the shared vertices一次
   if(submesh->useSharedVertices)
   {
    if( !added_shared )
    {
     vertex_count += mesh->sharedVertexData->vertexCount;
     added_shared = true;
    }
   }
   else
   {
    vertex_count += submesh->vertexData->vertexCount;
   }
   // Add the indices 索引
   index_count += submesh->indexData->indexCount;
}

// Allocate space for the vertices and indices
// 分配内存
vertices = new Ogre::Vector3[vertex_count];
indices = new unsigned long[index_count];

added_shared = false;

// 添加具体的数据
for (unsigned short i = 0; i < mesh->getNumSubMeshes(); ++i)
{
   Ogre::SubMesh* submesh = mesh->getSubMesh(i);

   Ogre::VertexData* vertex_data = submesh->useSharedVertices ? mesh->sharedVertexData : submesh->vertexData;

   if ((!submesh->useSharedVertices) || (submesh->useSharedVertices && !added_shared))
   {
    if(submesh->useSharedVertices)
    {
     added_shared = true;
     shared_offset = current_offset;
    }

    // 位置索引
    const Ogre::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));

    // There is _no_ baseVertexPointerToElement() which takes an Ogre::Real or a double
    // as second argument. So make it float, to avoid trouble when Ogre::Real will
    // be comiled/typedefed as double:
    //Ogre::Real* pReal;
    float* pReal;

    for( size_t j = 0; j < vertex_data->vertexCount; ++j, vertex += vbuf->getVertexSize())
    {
     // 添加顶点数据
     posElem->baseVertexPointerToElement(vertex, &pReal);
     Ogre::Vector3 pt(pReal[0], pReal[1], pReal[2]);
     vertices[current_offset + j] = (orient * (pt * scale)) + position;
    }

    vbuf->unlock();
    next_offset += vertex_data->vertexCount;
   }


   Ogre::IndexData* index_data = submesh->indexData;
   size_t numTris = index_data->indexCount / 3;
   Ogre::HardwareIndexBufferSharedPtr ibuf = index_data->indexBuffer;

   bool use32bitindexes = (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 ( use32bitindexes )
   {
    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;
  }

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值