| 895 | } |
| 896 | |
| 897 | void Model::CreateWithAssimp(ID3D11Device* device, const wchar* fileName, bool forceSRGB) |
| 898 | { |
| 899 | Assert_(FileExists(fileName)); |
| 900 | |
| 901 | std::string fileNameAnsi = WStringToAnsi(fileName); |
| 902 | |
| 903 | Assimp::Importer importer; |
| 904 | uint32 flags = aiProcess_CalcTangentSpace | |
| 905 | aiProcess_Triangulate | |
| 906 | aiProcess_JoinIdenticalVertices | |
| 907 | aiProcess_MakeLeftHanded | |
| 908 | aiProcess_PreTransformVertices | |
| 909 | aiProcess_RemoveRedundantMaterials | |
| 910 | aiProcess_OptimizeMeshes | |
| 911 | aiProcess_FlipUVs | |
| 912 | aiProcess_FlipWindingOrder ; |
| 913 | const aiScene* scene = importer.ReadFile(fileNameAnsi, flags); |
| 914 | |
| 915 | if(scene == nullptr) |
| 916 | throw Exception(L"Failed to load scene " + std::wstring(fileName) + |
| 917 | L": " + AnsiToWString(importer.GetErrorString())); |
| 918 | |
| 919 | if(scene->mNumMeshes == 0) |
| 920 | throw Exception(L"Scene " + std::wstring(fileName) + L" has no meshes"); |
| 921 | |
| 922 | if(scene->mNumMaterials == 0) |
| 923 | throw Exception(L"Scene " + std::wstring(fileName) + L" has no materials"); |
| 924 | |
| 925 | fileDirectory = GetDirectoryFromFilePath(fileName); |
| 926 | |
| 927 | // Load the materials |
| 928 | const uint64 numMaterials = scene->mNumMaterials; |
| 929 | for(uint64 i = 0; i < numMaterials; ++i) |
| 930 | { |
| 931 | MeshMaterial material; |
| 932 | const aiMaterial& mat = *scene->mMaterials[i]; |
| 933 | |
| 934 | aiString diffuseTexPath; |
| 935 | aiString normalMapPath; |
| 936 | aiString roughnessMapPath; |
| 937 | aiString metallicMapPath; |
| 938 | if(mat.GetTexture(aiTextureType_DIFFUSE, 0, &diffuseTexPath) == aiReturn_SUCCESS) |
| 939 | material.DiffuseMapName = GetFileName(AnsiToWString(diffuseTexPath.C_Str()).c_str()); |
| 940 | |
| 941 | if(mat.GetTexture(aiTextureType_NORMALS, 0, &normalMapPath) == aiReturn_SUCCESS |
| 942 | || mat.GetTexture(aiTextureType_HEIGHT, 0, &normalMapPath) == aiReturn_SUCCESS) |
| 943 | material.NormalMapName = GetFileName(AnsiToWString(normalMapPath.C_Str()).c_str()); |
| 944 | |
| 945 | if(mat.GetTexture(aiTextureType_SHININESS, 0, &roughnessMapPath) == aiReturn_SUCCESS) |
| 946 | material.RoughnessMapName = GetFileName(AnsiToWString(roughnessMapPath.C_Str()).c_str()); |
| 947 | |
| 948 | if(mat.GetTexture(aiTextureType_AMBIENT, 0, &metallicMapPath) == aiReturn_SUCCESS) |
| 949 | material.MetallicMapName = GetFileName(AnsiToWString(metallicMapPath.C_Str()).c_str()); |
| 950 | |
| 951 | LoadMaterialResources(material, fileDirectory, device, forceSRGB); |
| 952 | |
| 953 | meshMaterials.push_back(material); |
| 954 | } |
no test coverage detected