void CXFileEntity::ExportX(int exp) { char buf[200]; sprintf(buf,"models\\%i\\model_e.x",exp); int hr=D3DXSaveMeshToX(buf, m_firstMesh->MeshData.pMesh, NULL, NULL, NULL, 0, D3DXF_FILEFORMAT_TEXT); }
void D3D9Mesh::SaveToXFile(const String &Filename) { String FilenameCopy = Filename; D3DXMATERIAL M; ZeroMemory(&M, sizeof(D3DXMATERIAL)); //call the DirectX function to save to an XFile, D3DXSaveMeshToX(FilenameCopy.CString(), _Mesh, NULL, &M, NULL, 0, D3DXF_FILESAVE_TOFILE); }
HRESULT DirectXMesh::SaveMeshToFile(const WCHAR* filename) { HRESULT hr; DWORD dwFormat = D3DXF_FILEFORMAT_TEXT; DWORD numFaces = mMesh->GetNumFaces(); PD(L"number of faces: ", numFaces); PD(L"number of vertices: ", mMesh->GetNumVertices()); DWORD* pAdjacency = new DWORD[numFaces * 3]; hr = mMesh->GenerateAdjacency(1e-6f, pAdjacency); PD(hr, L"generate adjacency"); if(FAILED(hr)) return hr; DWORD dwNumMeshes = 0; hr = mMesh->GetAttributeTable( NULL, &dwNumMeshes ); PD(hr, L"get number of meshes"); if(FAILED(hr)) return hr; PD(L"number of meshes: ", dwNumMeshes); D3DXMATERIAL* materials = new D3DXMATERIAL[dwNumMeshes]; for(int i = 0; i < dwNumMeshes; ++i) { materials[i].MatD3D.Ambient = D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.0f); materials[i].MatD3D.Specular = D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.0f); materials[i].MatD3D.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.0f); materials[i].MatD3D.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 0.0f); materials[i].pTextureFilename = NULL; } PD(L"save mesh with #vertices: ", mMesh->GetNumVertices()); PD(L"save mesh with #faces: ", mMesh->GetNumFaces()); hr = D3DXSaveMeshToX(filename, mMesh, pAdjacency, materials, NULL, dwNumMeshes, dwFormat); PD(hr, L"save mesh to file"); PD(hr); delete [] pAdjacency; return hr; }
HRESULT AiPathReader::CreateMesh(LPDIRECT3DDEVICE9 pD3DDevice, LPD3DXMESH &pMesh) { //Todo: ID3DXMesh::SetAttributeTable (set materials how they are defined in the face struct) DWORD dwFVF = (D3DFVF_XYZ | D3DFVF_NORMAL); struct D3DVERTEX { D3DXVECTOR3 p; D3DXVECTOR3 n; }; HRESULT r = D3DXCreateMeshFVF(3 * nodes.size(), 3 * nodes.size(), D3DXMESH_MANAGED, dwFVF, pD3DDevice, &pMesh); if(FAILED(r)) { return r; } D3DVERTEX *vertexBuffer; WORD *indexBuffer = nullptr; unsigned long *pAdjacency = new unsigned long[nodes.size() * 3]; pMesh->LockIndexBuffer(0, reinterpret_cast<void **>(&indexBuffer)); pMesh->LockVertexBuffer(0, reinterpret_cast<void **>(&vertexBuffer)); for(int i = 0; i < nodes.size(); i++) { auto face = nodes[i]; for(int j = 0; j < 3; j++) { D3DVERTEX &vert = vertexBuffer[3 * i + j]; vert.p.x = face.triangle[j].x; vert.p.y = face.triangle[j].y; vert.p.z = face.triangle[j].z; indexBuffer[3 * i + j] = 3 * i + j; pAdjacency[3 * i + j] = (face.adjacency[j] == 0xffff) ? 0xffffffffUL : face.adjacency[j]; } } //D3DXWeldVertices(pMesh, D3DXWELDEPSILONS_WELDALL, nullptr, pAdjacency, nullptr, nullptr, nullptr); //pMesh->OptimizeInplace(D3DXMESHOPT_COMPACT | D3DXMESHOPT_IGNOREVERTS | D3DXMESHOPT_STRIPREORDER, newAdjacency, pAdjacency, nullptr, nullptr); delete[] pAdjacency; HRESULT hr = D3DXComputeNormals(pMesh, nullptr); D3DXMATERIAL *m_pMaterials = nullptr; DWORD m_dwNumMaterials = 0; hr = D3DXSaveMeshToX("NavMesh.x", pMesh, nullptr, m_pMaterials, nullptr, m_dwNumMaterials, D3DXF_FILEFORMAT_BINARY); pMesh->UnlockVertexBuffer(); pMesh->UnlockIndexBuffer(); return S_OK; }
///////////////////////////////////// // Name: MDLSaveToXFile // Purpose: this is used for cheating in // GEN300, good for terrain! // Output: file is created // Return: none ///////////////////////////////////// PUBLIC void MDLSaveToXFile(hMDL model, char *filename) { D3DXMATERIAL *d3dxmats; MemAlloc((void**)&d3dxmats, sizeof(D3DXMATERIAL)*model->numMaterial, M_ZERO); for(int i = 0; i < model->numMaterial; i++) { memcpy(&d3dxmats[i].MatD3D, &model->materials[i], sizeof(D3DMATERIAL8)); if(model->textures[i]) d3dxmats[i].pTextureFilename = model->textures[i]->filename; } D3DXSaveMeshToX( filename, model->mesh, (DWORD*)model->adjacencyBuffer->GetBufferPointer(), d3dxmats, model->numMaterial, DXFILEFORMAT_BINARY); MemFree((void**)&d3dxmats); }
/** * Exports all geometry into a D3D .x file into the current working folder. * @param Filename Desired filename (may include path) * @param bShow Whether the D3D .x file viewer should be invoked. If shown, we'll block until it has been closed. */ void F3DVisualizer::Export( const TCHAR* Filename, bool bShow/*=false*/ ) { ID3DXMesh* Mesh; Mesh = NULL; int32 NumPrimitives = NumTriangles() + NumLines()*2; int32 NumVertices = NumTriangles()*3 + NumLines()*4; HRESULT Result = D3DXCreateMeshFVF( NumPrimitives, NumVertices, D3DXMESH_32BIT, D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_DIFFUSE|D3DFVF_SPECULAR, D3D->D3DDevice, &Mesh ); void* VertexBuffer = NULL; void* IndexBuffer = NULL; Result = Mesh->LockVertexBuffer(D3DLOCK_DISCARD, &VertexBuffer); Result = Mesh->LockIndexBuffer(D3DLOCK_DISCARD, &IndexBuffer); D3DXVertex* Vertices = (D3DXVertex*)VertexBuffer; uint32* Indices = (uint32*) IndexBuffer; int32 NumVerticesStored = 0; int32 NumIndicesStored = 0; // Add the triangles to the vertexbuffer and indexbuffer. for ( int32 TriangleIndex=0; TriangleIndex < NumTriangles(); ++TriangleIndex ) { const FVector4& P1 = Triangles[TriangleIndex].Vertices[0]; const FVector4& P2 = Triangles[TriangleIndex].Vertices[1]; const FVector4& P3 = Triangles[TriangleIndex].Vertices[2]; const FColor& Color = Triangles[TriangleIndex].Color; Vertices[NumVerticesStored+0].Pos[0] = P1[0]; Vertices[NumVerticesStored+0].Pos[1] = P1[1]; Vertices[NumVerticesStored+0].Pos[2] = P1[2]; Vertices[NumVerticesStored+0].Color1 = Color.DWColor(); Vertices[NumVerticesStored+0].Color2 = 0; Vertices[NumVerticesStored+1].Pos[0] = P2[0]; Vertices[NumVerticesStored+1].Pos[1] = P2[1]; Vertices[NumVerticesStored+1].Pos[2] = P2[2]; Vertices[NumVerticesStored+1].Color1 = Color.DWColor(); Vertices[NumVerticesStored+1].Color2 = 0; Vertices[NumVerticesStored+2].Pos[0] = P3[0]; Vertices[NumVerticesStored+2].Pos[1] = P3[1]; Vertices[NumVerticesStored+2].Pos[2] = P3[2]; Vertices[NumVerticesStored+2].Color1 = Color.DWColor(); Vertices[NumVerticesStored+2].Color2 = 0; // Reverse triangle winding order for the .x file. Indices[NumIndicesStored+0] = NumVerticesStored + 0; Indices[NumIndicesStored+1] = NumVerticesStored + 2; Indices[NumIndicesStored+2] = NumVerticesStored + 1; NumVerticesStored += 3; NumIndicesStored += 3; } // Add the lines to the vertexbuffer and indexbuffer. for ( int32 LineIndex=0; LineIndex < NumLines(); ++LineIndex ) { const FVector4& P1 = Lines[LineIndex].Vertices[0]; const FVector4& P2 = Lines[LineIndex].Vertices[1]; const FColor& Color = Lines[LineIndex].Color; Vertices[NumVerticesStored+0].Pos[0] = P1[0]; Vertices[NumVerticesStored+0].Pos[1] = P1[1] - 5.0f; Vertices[NumVerticesStored+0].Pos[2] = P1[2]; Vertices[NumVerticesStored+0].Color1 = 0; Vertices[NumVerticesStored+0].Color2 = Color.DWColor(); Vertices[NumVerticesStored+1].Pos[0] = P1[0]; Vertices[NumVerticesStored+1].Pos[1] = P1[1] + 5.0f; Vertices[NumVerticesStored+1].Pos[2] = P1[2]; Vertices[NumVerticesStored+1].Color1 = 0; Vertices[NumVerticesStored+1].Color2 = Color.DWColor(); Vertices[NumVerticesStored+2].Pos[0] = P2[0]; Vertices[NumVerticesStored+2].Pos[1] = P2[1] - 5.0f; Vertices[NumVerticesStored+2].Pos[2] = P2[2]; Vertices[NumVerticesStored+2].Color1 = 0; Vertices[NumVerticesStored+2].Color2 = Color.DWColor(); Vertices[NumVerticesStored+3].Pos[0] = P2[0]; Vertices[NumVerticesStored+3].Pos[1] = P2[1] + 5.0f; Vertices[NumVerticesStored+3].Pos[2] = P2[2]; Vertices[NumVerticesStored+3].Color1 = 0; Vertices[NumVerticesStored+3].Color2 = Color.DWColor(); Indices[NumIndicesStored+0] = NumVerticesStored+0; Indices[NumIndicesStored+1] = NumVerticesStored+2; Indices[NumIndicesStored+2] = NumVerticesStored+1; Indices[NumIndicesStored+3] = NumVerticesStored+2; Indices[NumIndicesStored+4] = NumVerticesStored+3; Indices[NumIndicesStored+5] = NumVerticesStored+1; NumVerticesStored += 4; NumIndicesStored += 6; } Mesh->UnlockVertexBuffer(); Mesh->UnlockIndexBuffer(); Result = D3DXComputeNormals( Mesh, NULL ); D3DXMATERIAL MeshMaterial; MeshMaterial.MatD3D.Ambient.r = 0.1f; MeshMaterial.MatD3D.Ambient.g = 0.1f; MeshMaterial.MatD3D.Ambient.b = 0.1f; MeshMaterial.MatD3D.Ambient.a = 0.0f; MeshMaterial.MatD3D.Diffuse.r = 1.0f; MeshMaterial.MatD3D.Diffuse.g = 1.0f; MeshMaterial.MatD3D.Diffuse.b = 1.0f; MeshMaterial.MatD3D.Diffuse.a = 1.0f; MeshMaterial.MatD3D.Emissive.r = 1.0f; MeshMaterial.MatD3D.Emissive.g = 1.0f; MeshMaterial.MatD3D.Emissive.b = 1.0f; MeshMaterial.MatD3D.Emissive.a = 1.0f; MeshMaterial.MatD3D.Specular.r = 1.0f; MeshMaterial.MatD3D.Specular.g = 1.0f; MeshMaterial.MatD3D.Specular.b = 1.0f; MeshMaterial.MatD3D.Specular.a = 1.0f; MeshMaterial.MatD3D.Power = 16.0f; MeshMaterial.pTextureFilename = NULL; D3DXEFFECTINSTANCE EffectInstance; EffectInstance.pEffectFilename = "D3DExport.fx"; EffectInstance.NumDefaults = 0; EffectInstance.pDefaults = NULL; // Write out the .x file. D3DXSaveMeshToX( Filename, Mesh, NULL, &MeshMaterial, &EffectInstance, 1, D3DXF_FILEFORMAT_BINARY ); Mesh->Release(); // Write out the .fx file, if it doesn't always exist. HANDLE ShaderFile = CreateFile( TEXT("D3DExport.fx"), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); if (ShaderFile != INVALID_HANDLE_VALUE) { ::DWORD BytesWritten; WriteFile(ShaderFile, ShaderFxFile, (uint32)FCStringAnsi::Strlen(ShaderFxFile), &BytesWritten, NULL); CloseHandle( ShaderFile ); } // If specified, run the default viewer for .x files and block until it's closed. if ( bShow ) { system( TCHAR_TO_ANSI(Filename) ); } }
//-------------------------------------------------------------------------------------- // Saves the mesh to X-file //-------------------------------------------------------------------------------------- void SaveMeshToXFile() { HRESULT hr; // Fill out D3DXMATERIAL structures UINT numMaterials = g_MeshLoader.GetNumMaterials(); D3DXMATERIAL* pMaterials = new D3DXMATERIAL[numMaterials]; char* pStrTexture = new char[MAX_PATH * numMaterials]; if( ( pMaterials != NULL ) && ( pStrTexture != NULL ) ) { for( UINT i = 0; i < g_MeshLoader.GetNumMaterials(); i++ ) { Material* pMat = g_MeshLoader.GetMaterial( i ); if( pMat != NULL ) { pMaterials[i].MatD3D.Ambient.r = pMat->vAmbient.x; pMaterials[i].MatD3D.Ambient.g = pMat->vAmbient.y; pMaterials[i].MatD3D.Ambient.b = pMat->vAmbient.z; pMaterials[i].MatD3D.Ambient.a = pMat->fAlpha; pMaterials[i].MatD3D.Diffuse.r = pMat->vDiffuse.x; pMaterials[i].MatD3D.Diffuse.g = pMat->vDiffuse.y; pMaterials[i].MatD3D.Diffuse.b = pMat->vDiffuse.z; pMaterials[i].MatD3D.Diffuse.a = pMat->fAlpha; pMaterials[i].MatD3D.Specular.r = pMat->vSpecular.x; pMaterials[i].MatD3D.Specular.g = pMat->vSpecular.y; pMaterials[i].MatD3D.Specular.b = pMat->vSpecular.z; pMaterials[i].MatD3D.Specular.a = pMat->fAlpha; pMaterials[i].MatD3D.Emissive.r = 0; pMaterials[i].MatD3D.Emissive.g = 0; pMaterials[i].MatD3D.Emissive.b = 0; pMaterials[i].MatD3D.Emissive.a = 0; pMaterials[i].MatD3D.Power = ( float )pMat->nShininess; WideCharToMultiByte( CP_ACP, 0, pMat->strTexture, -1, ( pStrTexture + i * MAX_PATH ), MAX_PATH, NULL, NULL ); pMaterials[i].pTextureFilename = ( pStrTexture + i * MAX_PATH ); } } // Write to file in same directory where the .obj file was found WCHAR strBuf[ MAX_PATH ]; swprintf_s( strBuf, MAX_PATH - 1, L"%s\\%s", g_MeshLoader.GetMediaDirectory(), L"MeshFromOBJ.x" ); hr = D3DXSaveMeshToX( strBuf, g_MeshLoader.GetMesh(), NULL, pMaterials, NULL, numMaterials, D3DXF_FILEFORMAT_TEXT ); if( SUCCEEDED( hr ) ) { swprintf_s( g_strFileSaveMessage, MAX_PATH - 1, L"Created %s", strBuf ); } else { DXTRACE_ERR( L"SaveMeshToXFile()::D3DXSaveMeshToX", hr ); swprintf_s( g_strFileSaveMessage, MAX_PATH - 1, L"Error creating %s, check debug output", strBuf ); } } SAFE_DELETE_ARRAY( pMaterials ); SAFE_DELETE_ARRAY( pStrTexture ); }