Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef NavDebugMesh_HEADER_DEFINED
- #define NavDebugMesh_HEADER_DEFINED
- #include <d3dx9.h>
- #include "Recast/Recast.h"
- class NavDebugMesh
- {
- private:
- struct Vertex
- {
- D3DXVECTOR3 p;
- D3DXVECTOR3 c;
- };
- LPDIRECT3DDEVICE9 m_device;
- LPDIRECT3DVERTEXDECLARATION9 m_VDecl;
- LPDIRECT3DVERTEXBUFFER9 m_VB;
- LPDIRECT3DINDEXBUFFER9 m_IB;
- public:
- NavDebugMesh(LPDIRECT3DDEVICE9 device, rcPolyMeshDetail* dmesh)
- : m_device(device), m_VDecl(nullptr), m_VB(nullptr), m_IB(nullptr)
- {
- m_device->AddRef();
- HRESULT hr = 0;
- D3DVERTEXELEMENT9 vertexElements[] =
- {
- { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
- { 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
- D3DDECL_END()
- };
- hr = m_device->CreateVertexDeclaration(vertexElements, &m_VDecl);
- if (FAILED(hr))
- {
- MessageBox(0, "CreateVertexDeclaration failed!", 0, 0);
- }
- hr = m_device->CreateVertexBuffer(sizeof(Vertex) * dmesh->nverts, D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &m_VB, nullptr);
- if (FAILED(hr))
- {
- MessageBox(0, "CreateVertexBuffer failed!", 0, 0);
- }
- hr = m_device->CreateIndexBuffer(sizeof(DWORD) * dmesh->ntris * 3, D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_MANAGED, &m_IB, nullptr);
- if (FAILED(hr))
- {
- MessageBox(0, "CreateIndexBuffer failed!", 0, 0);
- }
- Vertex* pVert;
- m_VB->Lock(0, 0, (void**)&pVert, 0);
- const D3DXVECTOR3* verts = (const D3DXVECTOR3*)dmesh->verts;
- for (int i = 0; i < dmesh->nverts; ++i)
- {
- pVert[i].p = verts[i];
- pVert[i].c = D3DXVECTOR3(0.78f, 1.0f, 1.0f);
- }
- m_VB->Unlock();
- DWORD* pInd;
- m_IB->Lock(0, 0, (void**)&pInd, 0);
- int cnt = 0;
- int tri_offset = 0;
- int old_nverts = 0;
- for (int p = 0; p < dmesh->nmeshes; ++p)
- {
- unsigned int* m = &(dmesh->meshes[p * 4]);
- const unsigned short bverts = m[0]; // `b' means "beginning of"!!
- const unsigned short nverts = m[1];
- const unsigned short btris = m[2];
- const unsigned short ntris = m[3];
- const unsigned char* tris = &(dmesh->tris[btris * 4]);
- tri_offset += old_nverts;
- for (int n = 0; n < ntris; ++n)
- {
- for (int k = 0; k < 3; ++k)
- {
- int tri = tris[n * 4 + k] + tri_offset;
- pInd[cnt] = tri;
- cnt++;
- }
- }
- old_nverts = nverts;
- }
- m_IB->Unlock();
- }
- ~NavDebugMesh()
- {
- Release();
- }
- void Release()
- {
- m_device->Release();
- m_VDecl->Release();
- m_VB->Release();
- m_IB->Release();
- }
- void Draw()
- {
- D3DVERTEXBUFFER_DESC vbDesc;
- m_VB->GetDesc(&vbDesc);
- UINT numVerts = vbDesc.Size / sizeof(Vertex);
- D3DINDEXBUFFER_DESC ibDesc;
- m_IB->GetDesc(&ibDesc);
- UINT numTris = ibDesc.Size / sizeof(DWORD) / 3;
- m_device->SetVertexDeclaration(m_VDecl);
- m_device->SetStreamSource(0, m_VB, 0, sizeof(Vertex));
- m_device->SetIndices(m_IB);
- m_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, numVerts, 0, numTris);
- }
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement