diff --git a/code/ObjFileImporter.cpp b/code/ObjFileImporter.cpp index a1f6fb9ed..9ea93c93c 100644 --- a/code/ObjFileImporter.cpp +++ b/code/ObjFileImporter.cpp @@ -145,6 +145,13 @@ void ObjFileImporter::InternReadFile( const std::string& pFile, aiScene* pScene, modelName = pFile; } + // This next stage takes ~ 1/3th of the total readFile task + // so should amount for 1/3th of the progress + // only update every 100KB or it'll be too slow + unsigned int progress = 0; + unsigned int progressCounter = 0; + const unsigned int updateProgressEveryBytes = 100 * 1024; + const unsigned int progressTotal = (3*m_Buffer.size()/updateProgressEveryBytes); // process all '\' std::vector ::iterator iter = m_Buffer.begin(); while (iter != m_Buffer.end()) @@ -159,10 +166,19 @@ void ObjFileImporter::InternReadFile( const std::string& pFile, aiScene* pScene, } else ++iter; + + if (++progressCounter >= updateProgressEveryBytes) + { + m_progress->UpdateFileRead(++progress, progressTotal); + progressCounter = 0; + } } + // 1/3rd progress + m_progress->UpdateFileRead(1, 3); + // parse the file into a temporary representation - ObjFileParser parser(m_Buffer, modelName, pIOHandler); + ObjFileParser parser(m_Buffer, modelName, pIOHandler, m_progress); // And create the proper return structures out of it CreateDataFromImport(parser.GetModel(), pScene); diff --git a/code/ObjFileParser.cpp b/code/ObjFileParser.cpp index e88eb8483..29cca5952 100644 --- a/code/ObjFileParser.cpp +++ b/code/ObjFileParser.cpp @@ -61,12 +61,13 @@ 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 ) : +ObjFileParser::ObjFileParser(std::vector &data,const std::string &modelName, IOSystem *io, ProgressHandler* progress ) : m_DataIt(data.begin()), m_DataItEnd(data.end()), m_pModel(NULL), m_uiLine(0), - m_pIO( io ) + m_pIO( io ), + m_progress(progress) { std::fill_n(m_buffer,Buffersize,0); @@ -106,8 +107,28 @@ void ObjFileParser::parseFile() if (m_DataIt == m_DataItEnd) return; + // only update every 100KB or it'll be too slow + const unsigned int updateProgressEveryBytes = 100 * 1024; + unsigned int progressCounter = 0; + const unsigned int bytesToProcess = std::distance(m_DataIt, m_DataItEnd); + const unsigned int progressTotal = 3 * bytesToProcess; + const unsigned int progressOffset = bytesToProcess; + unsigned int processed = 0; + + DataArrayIt lastDataIt = m_DataIt; + while (m_DataIt != m_DataItEnd) { + // Handle progress reporting + processed += std::distance(lastDataIt, m_DataIt); + lastDataIt = m_DataIt; + if (processed > (progressCounter * updateProgressEveryBytes)) + { + progressCounter++; + m_progress->UpdateFileRead(progressOffset + processed*2, progressTotal); + } + + // parse line switch (*m_DataIt) { case 'v': // Parse a vertex texture coordinate diff --git a/code/ObjFileParser.h b/code/ObjFileParser.h index 5ca2b09c6..e16de49a8 100644 --- a/code/ObjFileParser.h +++ b/code/ObjFileParser.h @@ -59,6 +59,7 @@ namespace ObjFile { class ObjFileImporter; class IOSystem; +class ProgressHandler; /// \class ObjFileParser /// \brief Parser for a obj waveform file @@ -71,7 +72,7 @@ public: public: /// \brief Constructor with data array. - ObjFileParser(std::vector &Data,const std::string &strModelName, IOSystem* io); + ObjFileParser(std::vector &Data,const std::string &strModelName, IOSystem* io, ProgressHandler* progress); /// \brief Destructor ~ObjFileParser(); /// \brief Model getter. @@ -139,6 +140,8 @@ private: char m_buffer[Buffersize]; /// Pointer to IO system instance. IOSystem *m_pIO; + //! Pointer to progress handler + ProgressHandler* m_progress; /// Path to the current model };