| 194 | } |
| 195 | |
| 196 | ref<TriangleMesh> TriangleMesh::createFromFile(const std::filesystem::path& path, ImportFlags importFlags) |
| 197 | { |
| 198 | if (!std::filesystem::exists(path)) |
| 199 | { |
| 200 | logWarning("Failed to load triangle mesh from '{}': File not found", path); |
| 201 | return nullptr; |
| 202 | } |
| 203 | |
| 204 | Assimp::Importer importer; |
| 205 | |
| 206 | unsigned int flags = |
| 207 | aiProcess_FlipUVs | |
| 208 | aiProcess_Triangulate | |
| 209 | aiProcess_PreTransformVertices; |
| 210 | flags |= is_set(importFlags, ImportFlags::GenSmoothNormals) ? aiProcess_GenSmoothNormals : aiProcess_GenNormals; |
| 211 | flags |= is_set(importFlags, ImportFlags::JoinIdenticalVertices) ? aiProcess_JoinIdenticalVertices : 0; |
| 212 | |
| 213 | const aiScene* scene = nullptr; |
| 214 | |
| 215 | if (hasExtension(path, "gz")) |
| 216 | { |
| 217 | auto decompressed = decompressFile(path); |
| 218 | scene = importer.ReadFileFromMemory(decompressed.data(), decompressed.size(), flags); |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | scene = importer.ReadFile(path.string().c_str(), flags); |
| 223 | } |
| 224 | |
| 225 | if (!scene) |
| 226 | { |
| 227 | logWarning("Failed to load triangle mesh from '{}': {}", path, importer.GetErrorString()); |
| 228 | return nullptr; |
| 229 | } |
| 230 | |
| 231 | VertexList vertices; |
| 232 | IndexList indices; |
| 233 | |
| 234 | size_t vertexCount = 0; |
| 235 | size_t indexCount = 0; |
| 236 | |
| 237 | for (size_t meshIdx = 0; meshIdx < scene->mNumMeshes; ++meshIdx) |
| 238 | { |
| 239 | vertexCount += scene->mMeshes[meshIdx]->mNumVertices; |
| 240 | indexCount += scene->mMeshes[meshIdx]->mNumFaces * 3; |
| 241 | } |
| 242 | |
| 243 | vertices.reserve(vertexCount); |
| 244 | indices.reserve(indexCount); |
| 245 | |
| 246 | for (size_t meshIdx = 0; meshIdx < scene->mNumMeshes; ++meshIdx) |
| 247 | { |
| 248 | size_t indexBase = vertices.size(); |
| 249 | auto mesh = scene->mMeshes[meshIdx]; |
| 250 | for (size_t vertexIdx = 0; vertexIdx < mesh->mNumVertices; ++vertexIdx) |
| 251 | { |
| 252 | const auto& vertex = mesh->mVertices[vertexIdx]; |
| 253 | const auto& normal = mesh->mNormals[vertexIdx]; |
nothing calls this directly
no test coverage detected