Advertisement
Guest User

Untitled

a guest
Aug 1st, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////
  2. // DrawUnsorted
  3. ////////////////////////////////////////////////////////////
  4. bool MeshRender::DrawUnsorted(unsigned int iIndex)
  5. {
  6.     ai_assert(iIndex < scene->mNumMeshes);
  7.  
  8.     // set vertex and index buffer
  9.     devices.d3ddev->SetStreamSource(0,apcMeshes[iIndex]->piVB,0,
  10.         sizeof(Vertex));
  11.  
  12.     devices.d3ddev->SetIndices(apcMeshes[iIndex]->piIB);
  13.  
  14.     D3DPRIMITIVETYPE type;
  15.     switch (scene->mMeshes[iIndex]->mPrimitiveTypes) {
  16.         case aiPrimitiveType_POINT:
  17.             type = D3DPT_POINTLIST;break;
  18.         case aiPrimitiveType_LINE:
  19.             type = D3DPT_LINELIST;break;
  20.         case aiPrimitiveType_TRIANGLE:
  21.             type = D3DPT_TRIANGLELIST;break;
  22.     }
  23.     // and draw the mesh
  24.     devices.d3ddev->DrawIndexedPrimitive(type,
  25.                                          0,0,
  26.                                          scene->mMeshes[iIndex]->mNumVertices,0,
  27.                                          scene->mMeshes[iIndex]->mNumFaces);
  28.  
  29.     return true;
  30. }
  31.  
  32. ////////////////////////////////////////////////////////////
  33. // DrawSorted
  34. ////////////////////////////////////////////////////////////
  35. bool MeshRender::DrawSorted(unsigned int iIndex,const aiMatrix4x4& mWorld)
  36. {
  37.     ai_assert(iIndex < scene->mNumMeshes);
  38.  
  39.     MeshHelper* pcHelper = apcMeshes[iIndex];
  40.     const aiMesh* pcMesh = scene->mMeshes[iIndex];
  41.  
  42.     if (!pcHelper || !pcMesh || !pcHelper->piIB)
  43.         return false;
  44.  
  45.     if (pcMesh->mPrimitiveTypes != aiPrimitiveType_TRIANGLE || pcMesh->HasBones())
  46.         return DrawUnsorted(iIndex);
  47.  
  48.  
  49.     // compute the position of the camera in worldspace
  50.     aiMatrix4x4 mWorldInverse = mWorld;
  51.     mWorldInverse.Inverse();
  52.     mWorldInverse.Transpose();
  53.     const aiVector3D vLocalCamera = mWorldInverse * aiVector3D(0.0f, 0.0f, 15.0f);
  54.  
  55.     // well ... this is really funny now. We must compute their distance
  56.     // from the camera. We take the average distance of a face and add it
  57.     // to a map which sorts it
  58.     std::map<float,unsigned int, std::greater<float>> smap;
  59.  
  60.     for (unsigned int iFace = 0; iFace < pcMesh->mNumFaces;++iFace)
  61.     {
  62.         const aiFace* pcFace = &pcMesh->mFaces[iFace];
  63.         float fDist = 0.0f;
  64.         for (unsigned int c = 0; c < 3;++c)
  65.         {
  66.             aiVector3D vPos = pcMesh->mVertices[pcFace->mIndices[c]];
  67.             vPos -= vLocalCamera;
  68.             fDist += vPos.SquareLength();
  69.         }
  70.         smap.insert(std::pair<float, unsigned int>(fDist,iFace));
  71.     }
  72.  
  73.     // now we can lock the index buffer and rebuild it
  74.     D3DINDEXBUFFER_DESC sDesc;
  75.     pcHelper->piIB->GetDesc(&sDesc);
  76.  
  77.     if (D3DFMT_INDEX16 == sDesc.Format)
  78.     {
  79.         uint16_t* aiIndices;
  80.         pcHelper->piIB->Lock(0,0,(void**)&aiIndices,D3DLOCK_DISCARD);
  81.  
  82.         for (std::map<float,unsigned int, std::greater<float> >::const_iterator
  83.             i =  smap.begin();
  84.             i != smap.end();++i)
  85.         {
  86.             const aiFace* pcFace =  &pcMesh->mFaces[(*i).second];
  87.             *aiIndices++ = (uint16_t)pcFace->mIndices[0];
  88.             *aiIndices++ = (uint16_t)pcFace->mIndices[1];
  89.             *aiIndices++ = (uint16_t)pcFace->mIndices[2];
  90.         }
  91.     }
  92.     else if (D3DFMT_INDEX32 == sDesc.Format)
  93.     {
  94.         uint32_t* aiIndices;
  95.         pcHelper->piIB->Lock(0,0,(void**)&aiIndices,D3DLOCK_DISCARD);
  96.  
  97.         for (std::map<float,unsigned int, std::greater<float>>::const_iterator
  98.             i =  smap.begin();
  99.             i != smap.end();++i)
  100.         {
  101.             const aiFace* pcFace =  &pcMesh->mFaces[(*i).second];
  102.             *aiIndices++ = (uint32_t)pcFace->mIndices[0];
  103.             *aiIndices++ = (uint32_t)pcFace->mIndices[1];
  104.             *aiIndices++ = (uint32_t)pcFace->mIndices[2];
  105.         }
  106.     }
  107.     pcHelper->piIB->Unlock();
  108.  
  109.     // set vertex and index buffer
  110.     devices.d3ddev->SetStreamSource(0,pcHelper->piVB,0,sizeof(Vertex));
  111.  
  112.     // and draw the mesh
  113.     devices.d3ddev->SetIndices(pcHelper->piIB);
  114.     devices.d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
  115.                                          0,0,
  116.                                          pcMesh->mNumVertices,0,
  117.                                          pcMesh->mNumFaces);
  118.  
  119.     return true;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement