# Calculating Tangent and Binormal

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

## Recommended Posts

I'm using the following code to calculate the mesh tangent and binormal, but I'm getting a black screen:

D3DVERTEXELEMENT9 g_vertexElements[6] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD, 0},
{0, 20, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_NORMAL, 0},
{0, 32, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TANGENT, 0},
{0, 44, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_BINORMAL, 0},
D3DDECL_END()
};

LPD3DXMESH finalMesh;
pMesh->CloneMesh(D3DXMESH_32BIT | D3DXMESH_SYSTEMMEM, g_vertexElements, m_d3dDevice, &finalMesh);
D3DXComputeTangent(finalMesh, 0, 0, 0, FALSE, 0);

What's wrong with the above code?

##### Share on other sites

You can create both by using this command

D3DXComputeTangentFrameEx

Here is an example directly from my engine

// generate tangent and bi-normal
hr=D3DXComputeTangentFrameEx(
pMesh,
D3DDECLUSAGE_TEXCOORD, 0,
D3DDECLUSAGE_TANGENT, 0,
D3DDECLUSAGE_BINORMAL, 0,
D3DDECLUSAGE_NORMAL, 0,
D3DXTANGENT_DONT_ORTHOGONALIZE|D3DXTANGENT_WEIGHT_BY_AREA,
// 0.01f, 0.25f, 0.01f,
-1.01f, -0.01f, -1.01f,
&pTMesh,
NULL);
if(FAILED(hr)) {
DB3D.TraceDXError2Hash("D3DXComputeTangentFrameEx() failed",ModelL.X[num].FlNm,hr,ModelL.X[num].NNHash);
goto ex;
}



##### Share on other sites

@LancerSolurus: I'm still getting black screen when I run the program.

Here is the code:

D3DVERTEXELEMENT9 vertexElements[6] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD, 0},
{0, 20, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_NORMAL, 0},
{0, 32, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TANGENT, 0},
{0, 44, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_BINORMAL, 0},
D3DDECL_END()
};

pMesh->CloneMesh(D3DXMESH_32BIT | D3DXMESH_SYSTEMMEM, vertexElements, m_d3dDevice, &finalMesh);

adjacency = new DWORD[finalMesh->GetNumFaces() * 3];

hr = D3DXComputeTangentFrameEx(finalMesh,
D3DDECLUSAGE_TEXCOORD, 0,
D3DDECLUSAGE_TANGENT, 0,
D3DDECLUSAGE_BINORMAL, 0,
D3DDECLUSAGE_NORMAL, 0,
D3DXTANGENT_DONT_ORTHOGONALIZE|D3DXTANGENT_WEIGHT_BY_AREA,
// 0.01f, 0.25f, 0.01f,
-1.01f, -0.01f, -1.01f,
&finalMesh,
NULL);
assert(SUCCEEDED(hr));

Edited by Medo3337

Any assistance?

##### Share on other sites

That part looks fine, although in my instance I used a separate input and output mesh so there was no chance of a corruption to the mesh. Where you have the [6] you can make it [] which will make it auto-size the array for you.

##### Share on other sites

@LancerSolurus: I tried that, same problem, nothing changed.

##### Share on other sites

Have you tried dropping the tangent and binormal and see if it renders? If it doesn't then you have other issues... Even if it doesn't generate the TBN properly with the above code, you will see the model in-game, it would just look like the dot product is incorrect. Unless you post how you render the model and your shader, there is no telling what the issue is. I wrote up a completely new renderer for my HP editor in 2 days that uses the above code and it works fine.

No offense, I have seen tons of posts by you lately and i get the feeling you are rushing your coding without understanding what your code does. Break it down into small parts so you can eliminate what your problem is. I've been programming over 30 years and I still do this for any new code. That way I can enable/disable parts until I can track down any bugs fairly easily. Once it's working perfectly then I combine it into one routine (if need be, sometimes it isn't needed). So post your code and let us dig through your code for errors, some members here are pretty proficient at it. The code you posted for generating the TBN looks totally correct to me anyways and would work fine in my game engine, it points to that you are having other problems elsewhere in your render code. This is not a rant, just being straight-forward with you.

• 9
• 18
• 29
• 11
• 24