   Generating tangent and binormal vectors from mesh
  I've recently been scouring the forums trying to figure out how to generate a tangent frame for a mesh using theD3DXComputeTangentFramefunction, and have been having a lot of trouble. This is my function for loading the mesh from a .x and generating the tangent frame:StaticMesh* Graphics::LoadStaticMeshFromX(LPCSTR file_path){D3DMATERIAL9 *materials;LPD3DXMESH mesh;DWORD material_count;LPD3DXBUFFER material_buffer;LPDIRECT3DTEXTURE9 *textures;D3DXLoadMeshFromX(file_path, D3DXMESH_SYSTEMMEM, this->d3d_device, NULL, &material_buffer, NULL, &material_count, &mesh);D3DXMATERIAL *temp_materials = (D3DXMATERIAL*)material_buffer->GetBufferPointer();materials = new D3DMATERIAL9[material_count];textures = new LPDIRECT3DTEXTURE9[material_count];for(DWORD i = 0; i < material_count; i++){materials = temp_materials.MatD3D;textures = this->LoadTexture(temp_materials.pTextureFilename);}HRESULT h_result = 0;LPD3DXBUFFER errors = 0;LPD3DXMESH cloned_mesh = 0;h_result = mesh->CloneMesh(mesh->GetOptions(), this->vertex_elements, this->d3d_device,&cloned_mesh);DWORD *adjacency;adjacency = new DWORD[mesh->GetNumFaces() * 3];h_result = mesh->GenerateAdjacency(0.01f, adjacency);h_result = D3DXCleanMesh(D3DXCLEAN_BOWTIES,cloned_mesh,adjacency,&cloned_mesh, adjacency,&errors);h_result = D3DXValidMesh(cloned_mesh, adjacency, &errors);delete [] adjacency;mesh->Release();h_result = D3DXComputeTangentFrame(cloned_mesh, 0);return new StaticMesh(materials, textures, material_count, cloned_mesh);}Everything returns without error. The problem is that when I try to render the mesh mesh->drawsubset(0)...I get this:Unhandled exception at 0x6cd9c0bd in Engine.exe: 0xC0000005: Access violation reading location 0x00000024.If remove all the clone and tangent code and just return the original mesh the code runs fine. Any suggestions?
  Hi, you have to make room for the tangents on your FVF.   ;  Either that or the loading failed and "mesh" is still a null pointer. Check the return values of all D3DX calls, and switch to the debug runtime in the DirectX control panel to get verbose output for all errors.   ;  You're right _Camus_, I wasn't making room for the tangents. My vertex elements were not set up properly. I changed them to this and now it seems to work:const D3DVERTEXELEMENT9 vertex_elements[6] ={  {0, 0,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION,0},  {0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},  {0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},  {0, 36, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0},  {0, 48, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 0},  D3DDECL_END()};Thanks! Now I have to make sure all the vectors are perpendicular to each other at the shader level.   