Generating Tangent Frame

Started by
1 comment, last by Programmer101 16 years, 5 months ago
This is a question on generating the tangents and binormals for an animated mesh. I added a quick method that adds tangents and binormals to a the vertex declaration and generates them. I call this method right before the program clones each mesh for each bone and sets up the matrices. The problem is that, when I do this the mesh gets all jumbled and ends up looking more like triangle soup than what it should look like. I'm thinking it has to do with the mesh being animated because this code works for static meshes. I've tried commenting code out to find that the mesh automatically becomes jumbled when I clone it to a new vertex format. Here is the code:
BOOL GenerateTangentFrame(LPD3DXMESH &m, bool optimize, LPDIRECT3DDEVICE9 g_pD3DDevice)
{
	LPD3DXMESH pTempMesh = NULL;
	D3DVERTEXELEMENT9 pDecl[] = {{0,
		0,
		D3DDECLTYPE_FLOAT3,
		D3DDECLMETHOD_DEFAULT,
		D3DDECLUSAGE_POSITION,
		0},
		{0,
		12,
		D3DDECLTYPE_FLOAT3,
		D3DDECLMETHOD_DEFAULT,
		D3DDECLUSAGE_NORMAL,
		0},
		{0,
		24,
		D3DDECLTYPE_FLOAT3,
		D3DDECLMETHOD_DEFAULT,
		D3DDECLUSAGE_TANGENT,
		0},
		{0,
		36,
		D3DDECLTYPE_FLOAT3,
		D3DDECLMETHOD_DEFAULT,
		D3DDECLUSAGE_BINORMAL,
		0},
		{0,
		48,
		D3DDECLTYPE_FLOAT2,
		D3DDECLMETHOD_DEFAULT,
		D3DDECLUSAGE_TEXCOORD,
		0},
		D3DDECL_END()};

    if(m.pMesh)
    {
        if(FAILED(m.pMesh->CloneMesh( m.pMesh->GetOptions(), pDecl,
                                        g_pD3DDevice, &pTempMesh)))
        {
            SAFE_RELEASE(pTempMesh);
            return FALSE;
        }
    }

	if(m)
    {
        HRESULT hr;

        DWORD *rgdwAdjacency = NULL;
        rgdwAdjacency = new DWORD[m->GetNumFaces() * 3];
        if( rgdwAdjacency == NULL )
            return FALSE;
        V(m->GenerateAdjacency(1e-6f,rgdwAdjacency));

        float fPartialEdgeThreshold;
        float fSingularPointThreshold;
        float fNormalEdgeThreshold;
        if(optimize)
        {
            fPartialEdgeThreshold = 0.01f;
            fSingularPointThreshold = 0.25f;
            fNormalEdgeThreshold = 0.01f;
        }
        else
        {
            fPartialEdgeThreshold = -1.01f;
            fSingularPointThreshold = 0.01f;
            fNormalEdgeThreshold = -1.01f;
        }

        // Compute tangents, which are required for normal mapping
        hr = D3DXComputeTangentFrameEx(m, 
                                        D3DDECLUSAGE_TEXCOORD, 0, 
                                        D3DDECLUSAGE_TANGENT, 0,
                                        D3DDECLUSAGE_BINORMAL, 0, 
                                        D3DDECLUSAGE_NORMAL, 0,
                                        0, rgdwAdjacency, 
                                        fPartialEdgeThreshold, fSingularPointThreshold, fNormalEdgeThreshold, 
                                        &pTempMesh, NULL);

        SAFE_DELETE_ARRAY(rgdwAdjacency);
        if(FAILED(hr))
            return FALSE;
	}

	SAFE_RELEASE(m);
    m = pTempMesh;

	return TRUE;
}
Advertisement
Your new declaration has no blendweight or blendindices elements. You should either put those in yourself, or you should get the original declaration and add tangents and binormals to it.
OK. I think I did try to add blend weights and indices, but the same problem occurred. The strangest part is that when I have a declaration of only the position, normal, and texture coordinates it works.

BTW, how would I add to the current declaration? I know how to get it but isn't it a fixed array?

This topic is closed to new replies.

Advertisement