Advertisement
Guest User

NavDebugMesh

a guest
Aug 16th, 2014
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. #ifndef NavDebugMesh_HEADER_DEFINED
  2. #define NavDebugMesh_HEADER_DEFINED
  3.  
  4. #include <d3dx9.h>
  5. #include "Recast/Recast.h"
  6.  
  7. class NavDebugMesh
  8. {
  9. private:
  10.  
  11.     struct Vertex
  12.     {
  13.         D3DXVECTOR3 p;
  14.         D3DXVECTOR3 c;
  15.     };
  16.  
  17.     LPDIRECT3DDEVICE9            m_device;
  18.     LPDIRECT3DVERTEXDECLARATION9 m_VDecl;
  19.     LPDIRECT3DVERTEXBUFFER9      m_VB;
  20.     LPDIRECT3DINDEXBUFFER9       m_IB;
  21.  
  22. public:
  23.  
  24.     NavDebugMesh(LPDIRECT3DDEVICE9 device, rcPolyMeshDetail* dmesh)
  25.         : m_device(device), m_VDecl(nullptr), m_VB(nullptr), m_IB(nullptr)
  26.     {
  27.         m_device->AddRef();
  28.  
  29.         HRESULT hr = 0;
  30.  
  31.         D3DVERTEXELEMENT9 vertexElements[] =
  32.         {
  33.             { 0,  0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
  34.             { 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR,    0 },
  35.             D3DDECL_END()
  36.         };
  37.  
  38.         hr = m_device->CreateVertexDeclaration(vertexElements, &m_VDecl);
  39.         if (FAILED(hr))
  40.         {
  41.             MessageBox(0, "CreateVertexDeclaration failed!", 0, 0);
  42.         }
  43.  
  44.         hr = m_device->CreateVertexBuffer(sizeof(Vertex) * dmesh->nverts, D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &m_VB, nullptr);
  45.         if (FAILED(hr))
  46.         {
  47.             MessageBox(0, "CreateVertexBuffer failed!", 0, 0);
  48.         }
  49.  
  50.         hr = m_device->CreateIndexBuffer(sizeof(DWORD) * dmesh->ntris * 3, D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_MANAGED, &m_IB, nullptr);
  51.         if (FAILED(hr))
  52.         {
  53.             MessageBox(0, "CreateIndexBuffer failed!", 0, 0);
  54.         }
  55.  
  56.         Vertex* pVert;
  57.         m_VB->Lock(0, 0, (void**)&pVert, 0);
  58.         const D3DXVECTOR3* verts = (const D3DXVECTOR3*)dmesh->verts;
  59.         for (int i = 0; i < dmesh->nverts; ++i)
  60.         {
  61.             pVert[i].p = verts[i];
  62.             pVert[i].c = D3DXVECTOR3(0.78f, 1.0f, 1.0f);
  63.         }
  64.         m_VB->Unlock();
  65.  
  66.         DWORD* pInd;
  67.         m_IB->Lock(0, 0, (void**)&pInd, 0);
  68.         int cnt = 0;
  69.         int tri_offset = 0;
  70.         int old_nverts = 0;
  71.         for (int p = 0; p < dmesh->nmeshes; ++p)
  72.         {
  73.             unsigned int* m = &(dmesh->meshes[p * 4]);
  74.             const unsigned short bverts = m[0]; // `b' means "beginning of"!!
  75.             const unsigned short nverts = m[1];
  76.             const unsigned short btris = m[2];
  77.             const unsigned short ntris = m[3];
  78.             const unsigned char* tris = &(dmesh->tris[btris * 4]);
  79.  
  80.             tri_offset += old_nverts;
  81.  
  82.             for (int n = 0; n < ntris; ++n)
  83.             {
  84.                 for (int k = 0; k < 3; ++k)
  85.                 {
  86.                     int tri = tris[n * 4 + k] + tri_offset;
  87.                     pInd[cnt] = tri;
  88.                     cnt++;
  89.                 }
  90.             }
  91.             old_nverts = nverts;
  92.         }
  93.         m_IB->Unlock();
  94.     }
  95.  
  96.     ~NavDebugMesh()
  97.     {
  98.         Release();
  99.     }
  100.  
  101.     void Release()
  102.     {
  103.         m_device->Release();
  104.         m_VDecl->Release();
  105.         m_VB->Release();
  106.         m_IB->Release();
  107.     }
  108.  
  109.     void Draw()
  110.     {
  111.         D3DVERTEXBUFFER_DESC vbDesc;
  112.         m_VB->GetDesc(&vbDesc);
  113.         UINT numVerts = vbDesc.Size / sizeof(Vertex);
  114.  
  115.         D3DINDEXBUFFER_DESC ibDesc;
  116.         m_IB->GetDesc(&ibDesc);
  117.         UINT numTris = ibDesc.Size / sizeof(DWORD) / 3;
  118.  
  119.         m_device->SetVertexDeclaration(m_VDecl);
  120.         m_device->SetStreamSource(0, m_VB, 0, sizeof(Vertex));
  121.         m_device->SetIndices(m_IB);
  122.         m_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, numVerts, 0, numTris);
  123.     }
  124. };
  125.  
  126. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement