Jump to content
  • Advertisement
Sign in to follow this  

problem generating tangent/binormals for normal mapping

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

Hi, I've been trying to get normal mapping on a sphere working for a few days now with no success, I've narrowed the problem (i think) down to the fact that the tangent/binormal's on my mesh are all set to 0, but to my knowledge they should be being set correctly.

I've torn out most of my pixel/vertex shader code so that now it just displays the binormal vector in RGB (this is how I know the binormals/tangents are always 0), however weirdly the normals are getting set correctly. Weirder still If I load a .x mesh from a file and process it, the binormals/tangents are set correctly, however if I use the same code on a mesh generated from the d3dXCreateSphere function the binormals/tangents are always 0.

Any help would be much appreciated, my code is below.

My vertex declaration

D3DVERTEXELEMENT9 NMapVertexElements[] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0},
{0, 24, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 0},
{0, 36, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
{0, 48, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
D3DDECL_END()
};
HR(gd3dDevice->CreateVertexDeclaration(NMapVertexElements, &NMapVertex::Decl));


creating the mesh

//This is the weird bit, If I use a mesh loaded from a .x file, and run it through this code, the tangent/binormals get set, however if I create the mesh myself using the d3dxCreateSphere function,
//then the tangents/binormals don't get set properly
//LoadXFile("BasicColumnScene.x", &tempMesh, mSceneMtrls, mSceneTextures);
HR(D3DXCreateSphere(gd3dDevice, 1.0, 64,64, &mSceneMesh, 0));

// Get the vertex declaration for the NMapVertex.
D3DVERTEXELEMENT9 elems[MAX_FVF_DECL_SIZE];
UINT numElems = 0;
HR(NMapVertex::Decl->GetDeclaration(elems, &numElems));

// Clone the mesh to the NMapVertex format.
ID3DXMesh* clonedMesh = 0;
HR(mSceneMesh->CloneMesh(mSceneMesh->GetOptions(), elems, gd3dDevice, &clonedMesh));

// Now use D3DXComputeTangentFrameEx to build the TNB-basis for each vertex
// in the mesh.
HR(D3DXComputeTangentFrameEx(
clonedMesh, // Input mesh
D3DDECLUSAGE_TEXCOORD, 0, // Vertex element of input tex-coords.
D3DDECLUSAGE_BINORMAL, 0, // Vertex element to output binormal.
D3DDECLUSAGE_TANGENT, 0, // Vertex element to output tangent.
D3DDECLUSAGE_NORMAL, 0, // Vertex element to output normal.
clonedMesh->GetOptions(), // Options
0, // Adjacency
0.01f, 0.25f, 0.01f, // Thresholds for handling errors
&mSceneMesh, // Output mesh
0)); // Vertex Remapping

Share this post


Link to post
Share on other sites
Advertisement
The sphere generated by D3DX doesn't have texture coordinates, and you need texture coordinates to generate a tangent frame.

Share this post


Link to post
Share on other sites

The sphere generated by D3DX doesn't have texture coordinates, and you need texture coordinates to generate a tangent frame.


Thanks! I set some texture coordinates before calling D3DXComputeTangentFrameEx (don't know if I set them correctly though) and now my tangents and binormals are being set.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!