• Advertisement
Sign in to follow this  

What is the quickest way to render a LPD3DXMESH?

This topic is 1279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'd like to render a LPD3DXMESH quickly.. Do I still need to lock index/vertex buffers? Or there is a way to accomplish this on the fly? Thanks Jack

Share this post


Link to post
Share on other sites
Advertisement

I am not sure why you think you need to do anything beyond DrawSubset.

Share this post


Link to post
Share on other sites

Can you describe why mesh->DrawSubset() doesn't work for you?

 

Ninja'd ph34r.png

Edited by Buckeye

Share this post


Link to post
Share on other sites

Actually I just have a little problem that I don't have access to the material buffer to loop with

I want to render the m_dxDebugMesh on the screen.

Thanks

Jack

 

bool NavMesher::buildDebugMesh(rcPolyMeshDetail* dmesh)
{
    if(dmesh->nmeshes == 0)
    {
        MessageBox(0,"getMeshDataFromPolyMeshDetail(): dmesh->nmeshes == 0\n", 0,0);
        return false;
    }

    D3DVERTEXELEMENT9 vertexElements[] =
    {
        {0, 0,  D3DDECLTYPE_FLOAT3,  D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
        {0, 12, D3DDECLTYPE_FLOAT3,  D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,   0},
        {0, 24, D3DDECLTYPE_FLOAT2,  D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
        D3DDECL_END()
    };

    if(FAILED(D3DXCreateMesh(dmesh->ntris, dmesh->nverts, D3DXMESH_MANAGED, vertexElements, m_d3d9device, &m_dxDebugMesh)))
    {
        MessageBox(0, "Failed to create dx mesh!", 0, 0);
        return false;
    }

    struct VERTEX
    {
        D3DXVECTOR3 p;
        D3DXVECTOR3 n;
        D3DXVECTOR2 t;
    };
    WORD* pInd;
    VERTEX* pVert;

    m_dxDebugMesh->LockVertexBuffer(0, (LPVOID*)&pVert);
    m_dxDebugMesh->LockIndexBuffer(0, (LPVOID*)&pInd);

    const D3DXVECTOR3* verts = (const D3DXVECTOR3*)dmesh->verts;
    for(int i = 0; i < dmesh->nverts; ++i)
    {
        pVert[i].p = verts[i];
        pVert[i].n = D3DXVECTOR3(0.f,0.0f,0.0f); // temp
        pVert[i].t = D3DXVECTOR2(0.0f,0.0f); // temp
    }

    // does not work like this!
    /*const unsigned char* tris = dmesh->tris;
    for(int i = 0; i < dmesh->ntris * 3; ++i)
    {
        pInd[i] = tris[i];
    }*/
    
    // this is piece of code for indices found on Irrlicht forum
    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_dxDebugMesh->UnlockVertexBuffer();
    m_dxDebugMesh->UnlockIndexBuffer();

    return true;
}

Share this post


Link to post
Share on other sites

You mean like D3DMATERIAL9? It is not part of the mesh, you might be confused as your framework that you are using is usually loading X from file, with functions that can obtain that information, like D3DXLoadMeshFromX.

 

You can create material yourself:

D3DMATERIAL9 mat;
memset(&mat, 0, sizeof(mat));
mat.diffuse.b = 0xff;
...
device->SetMaterial(mat);
m_dxDebugMesh->DrawSubset(0);

 

or better yet draw it with a shader.

 

navmesh.jpg

 

If you want to draw outlines with lines, like they did in Recast/Detour demo, you can use mesh adjacency info, here is piece of code that i used with dx9:

DWORD numFaces = navDebugMesh->GetNumFaces();
    DWORD* meshAdj = new DWORD[numFaces * 3];
    hr = navDebugMesh->GenerateAdjacency(0.001f, meshAdj);
    if (FAILED(hr))
    {
        MessageBox(....);
    }

    const D3DXVECTOR3 outlineColor(0.0f, 0.0f, 0.0f);
    const D3DXVECTOR3 innerColor(0.85f, 0.85f, 0.85f);

    struct MeshVertex
    {
        D3DXVECTOR3 p;
        D3DXVECTOR3 n;
        D3DXVECTOR2 t;
    };
    WORD* pInd;
    MeshVertex* pVert;
    navDebugMesh->LockVertexBuffer(0, (LPVOID*)&pVert);
    navDebugMesh->LockIndexBuffer(0, (LPVOID*)&pInd);

    for (DWORD i = 0; i < numFaces; ++i)
    {
        if (0xFFFFFFFF == meshAdj[i * 3 + 0])
        {
            Line line;
            line.start = pVert[pInd[i * 3 + 0]].p;
            line.end = pVert[pInd[i * 3 + 1]].p;
            line.color = outlineColor;
            lRenderer->addLine(line);
        }
        else
        {
            Line line;
            line.start = pVert[pInd[i * 3 + 0]].p;
            line.end = pVert[pInd[i * 3 + 1]].p;
            line.color = innerColor;
            lRenderer->addLine(line);
        }

        if (0xFFFFFFFF == meshAdj[i * 3 + 1])
        {
            Line line;
            line.start = pVert[pInd[i * 3 + 1]].p;
            line.end = pVert[pInd[i * 3 + 2]].p;
            line.color = outlineColor;
            lRenderer->addLine(line);
        }
        else
        {
            Line line;
            line.start = pVert[pInd[i * 3 + 1]].p;
            line.end = pVert[pInd[i * 3 + 2]].p;
            line.color = innerColor;
            lRenderer->addLine(line);
        }

        if (0xFFFFFFFF == meshAdj[i * 3 + 2])
        {
            Line line;
            line.start = pVert[pInd[i * 3 + 2]].p;
            line.end = pVert[pInd[i * 3 + 0]].p;
            line.color = outlineColor;
            lRenderer->addLine(line);
        }
        else
        {
            Line line;
            line.start = pVert[pInd[i * 3 + 2]].p;
            line.end = pVert[pInd[i * 3 + 0]].p;
            line.color = innerColor;
            lRenderer->addLine(line);
        }
    }

    navDebugMesh->UnlockVertexBuffer();
    navDebugMesh->UnlockIndexBuffer();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement