diff --git a/code/ObjFileImporter.cpp b/code/ObjFileImporter.cpp index f5c727ec9..9bc2fc506 100644 --- a/code/ObjFileImporter.cpp +++ b/code/ObjFileImporter.cpp @@ -177,7 +177,7 @@ void ObjFileImporter::InternReadFile( const std::string &file, aiScene* pScene, m_progress->UpdateFileRead(1, 3); // parse the file into a temporary representation - ObjFileParser parser(m_Buffer, modelName, pIOHandler, m_progress); + ObjFileParser parser(m_Buffer, modelName, pIOHandler, m_progress, file); // And create the proper return structures out of it CreateDataFromImport(parser.GetModel(), pScene); diff --git a/code/ObjFileParser.cpp b/code/ObjFileParser.cpp index f23cea879..a6f515aba 100644 --- a/code/ObjFileParser.cpp +++ b/code/ObjFileParser.cpp @@ -61,13 +61,14 @@ const std::string ObjFileParser::DEFAULT_MATERIAL = AI_DEFAULT_MATERIAL_NAME; // ------------------------------------------------------------------- // Constructor with loaded data and directories. -ObjFileParser::ObjFileParser(std::vector &data,const std::string &modelName, IOSystem *io, ProgressHandler* progress ) : +ObjFileParser::ObjFileParser(std::vector &data, const std::string &modelName, IOSystem *io, ProgressHandler* progress, const std::string &originalObjFileName) : m_DataIt(data.begin()), m_DataItEnd(data.end()), m_pModel(NULL), m_uiLine(0), m_pIO( io ), - m_progress(progress) + m_progress(progress), + m_originalObjFileName(originalObjFileName) { std::fill_n(m_buffer,Buffersize,0); @@ -573,9 +574,15 @@ void ObjFileParser::getMaterialLib() IOStream *pFile = m_pIO->Open( absName ); if (!pFile ) { - DefaultLogger::get()->error( "OBJ: Unable to locate material file " + strMatName ); - m_DataIt = skipLine( m_DataIt, m_DataItEnd, m_uiLine ); - return; + DefaultLogger::get()->error("OBJ: Unable to locate material file " + strMatName); + std::string strMatFallbackName = m_originalObjFileName.substr(0, m_originalObjFileName.length() - 3) + "mtl"; + DefaultLogger::get()->info("OBJ: Opening fallback material file " + strMatFallbackName); + pFile = m_pIO->Open(strMatFallbackName); + if (!pFile) { + DefaultLogger::get()->error("OBJ: Unable to locate fallback material file " + strMatName); + m_DataIt = skipLine(m_DataIt, m_DataItEnd, m_uiLine); + return; + } } // Import material library data from file. diff --git a/code/ObjFileParser.h b/code/ObjFileParser.h index e16de49a8..7170f0d50 100644 --- a/code/ObjFileParser.h +++ b/code/ObjFileParser.h @@ -72,7 +72,7 @@ public: public: /// \brief Constructor with data array. - ObjFileParser(std::vector &Data,const std::string &strModelName, IOSystem* io, ProgressHandler* progress); + ObjFileParser(std::vector &Data, const std::string &strModelName, IOSystem* io, ProgressHandler* progress, const std::string &originalObjFileName); /// \brief Destructor ~ObjFileParser(); /// \brief Model getter. @@ -143,6 +143,8 @@ private: //! Pointer to progress handler ProgressHandler* m_progress; /// Path to the current model + // name of the obj file where the buffer comes from + const std::string& m_originalObjFileName; }; } // Namespace Assimp