uint ModelData::ReadFromDisk( const String& ModelName) { const String FilePath = FindModelPath(ModelName); FILE* pFile = fopen(FilePath.Val(), "rb"); this->SetName(ModelName.Val()); if(pFile) { uint Header=0, Size=0; while(fread(&Header, sizeof(Header), 1, pFile) == 1) { if(Header == LibraryMeshData) { m_MeshData.ReadFromDisk(pFile); } else if(Header == LibrarySurfaceData) { m_SurfaceData.ReadFromDisk(pFile); } else if(Header == LibraryMaterialData) { m_MaterialData.ReadFromDisk(pFile); } else if(Header == LibraryBoneData) { m_BoneData.ReadFromDisk(pFile); } else if(Header == LibraryAnimations) { m_Animations.ReadFromDisk(pFile); } } return true; } else return false; }
std::string C3DModelLoader::FindModelPath(std::string name) const { const std::string& fileExt = FileSystem::GetExtension(name); if (fileExt.empty()) { for (ParserMap::const_iterator it = parsers.begin(); it != parsers.end(); ++it) { const std::string& supportedExt = it->first; if (CFileHandler::FileExists(name + "." + supportedExt, SPRING_VFS_ZIP)) { name += "." + supportedExt; break; } } } if (!CFileHandler::FileExists(name, SPRING_VFS_ZIP)) { if (name.find("objects3d/") == std::string::npos) { return FindModelPath("objects3d/" + name); } } return name; }
S3DModel* C3DModelLoader::Load3DModel(std::string modelName) { GML_RECMUTEX_LOCK(model); // Load3DModel StringToLowerInPlace(modelName); // search in cache first ModelMap::iterator ci; ParserMap::iterator pi; if ((ci = cache.find(modelName)) != cache.end()) { return models[ci->second]; } const std::string modelPath = FindModelPath(modelName); if ((ci = cache.find(modelPath)) != cache.end()) { return models[ci->second]; } // not found in cache, create the model and cache it const std::string& fileExt = StringToLower(FileSystem::GetExtension(modelPath)); if ((pi = parsers.find(fileExt)) != parsers.end()) { IModelParser* p = pi->second; S3DModel* model = NULL; S3DModelPiece* root = NULL; try { model = p->Load(modelPath); } catch (const content_error& ex) { LOG_L(L_WARNING, "could not load model \"%s\" (reason: %s)", modelName.c_str(), ex.what()); goto dummy; } if ((root = model->GetRootPiece()) != NULL) { CreateLists(root); } AddModelToCache(model, modelName, modelPath); CheckModelNormals(model); return model; } LOG_L(L_ERROR, "could not find a parser for model \"%s\" (unknown format?)", modelName.c_str()); dummy: // crash-dummy S3DModel* model = new S3DModel(); model->type = MODELTYPE_3DO; model->numPieces = 1; // give it one dummy piece model->SetRootPiece(ModelTypeToModelPiece(MODELTYPE_3DO)); model->GetRootPiece()->SetCollisionVolume(new CollisionVolume("box", -UpVector, ZeroVector)); if (model->GetRootPiece() != NULL) { CreateLists(model->GetRootPiece()); } AddModelToCache(model, modelName, modelPath); return model; }