how do i know the vertex coordinate of the mesh

Started by
2 comments, last by belfegor 10 years, 10 months ago

i test using D3DXCreateTeapot, but can not find any info about its vertex coordinate

Advertisement

Well D3DXCreateTeapot returns a ID3DXMesh  object. You can call GetVertexBuffer on it. Don't forget to lock it before editing. You also need to figure out the vertex stride of the buffer. You can use D3DXGetFVFVertexSize:

D3DXGetFVFVertexSize(teapot->GetFVF())

you can grab the the fvf and & D3DFVF_XYZ to see if the FVF have vertex information .If it does then that data is usually resides at the beginning of the vertex memory block when you lock it for access.

Might do better with vertex declaration since that way have more control, for example it can give you bytes offset to some element.

Here is example chopped from my old project:


D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE];
hr = dxMesh->GetDeclaration(decl);
if(FAILED(hr))
    ...// handle error
bool havePosition  = false;
        bool haveNormal    = false;
        bool haveTexCoord0 = false;
        bool haveTexCoord1 = false;
        bool haveTangent   = false;
        bool haveBinormal  = false;

        WORD offsetToPosition  = 0;
        WORD offsetToNormal    = 0;
        WORD offsetToTexCoord0 = 0;
        WORD offsetToTexCoord1 = 0;
        WORD offsetToTangent   = 0;
        WORD offsetToBinormal  = 0;

        for(UINT i = 0; i < MAX_FVF_DECL_SIZE; ++i)
        {
            if(decl[i].Stream == 0xFF || decl[i].Stream != 0)
            {
                break;
            }

            if(decl[i].Usage == D3DDECLUSAGE_POSITION)
            {
                offsetToPosition = decl[i].Offset;
                havePosition      = true;
                continue;
            }

            if(decl[i].Usage == D3DDECLUSAGE_NORMAL)
            {
                offsetToNormal = decl[i].Offset;
                haveNormal      = true;
                continue;
            }

            if(decl[i].Usage == D3DDECLUSAGE_TEXCOORD && decl[i].UsageIndex == 0)
            {
                offsetToTexCoord0 = decl[i].Offset;
                haveTexCoord0      = true;
                continue;
            }

            if(decl[i].Usage == D3DDECLUSAGE_TEXCOORD && decl[i].UsageIndex == 1)
            {
                offsetToTexCoord1 = decl[i].Offset;
                haveTexCoord1      = true;
                continue;
            }

            if(decl[i].Usage == D3DDECLUSAGE_TANGENT)
            {
                offsetToTangent = decl[i].Offset;
                haveTangent      = true;
                continue;
            }

            if(decl[i].Usage == D3DDECLUSAGE_BINORMAL)
            {
                offsetToBinormal = decl[i].Offset;
                haveBinormal      = true;
                continue;
            }
            ...// add more what you need
        }
   

Then you lock and access what you need:


auto stride = dxMesh->GetNumBytesPerVertex();
BYTE* pVerts;
dxMesh->LockVertexBuffer(0, reinterpret_cast<LPVOID*>(&pVerts));
...
if(havePosition)
{
    for(DWORD i = 0; i < numVertices; ++i)
    {
        // assume that position is vec3
        // you can check vertex declaration for that also
        D3DXVECTOR3 pos = *reinterpret_cast<D3DXVECTOR3*>((i * stride) + pVerts + offsetToPosition);
    }
}

This topic is closed to new replies.

Advertisement