Jump to content
  • Advertisement
Sign in to follow this  
Programmer101

Generating Tangent Frame

This topic is 3892 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

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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!