// static bool OBJLoader::parseOBJ( QString objFilename, std::shared_ptr< OBJData > pOBJData ) { // attempt to read the file QFile inputFile( objFilename ); if( !( inputFile.open( QIODevice::ReadOnly ) ) ) { return false; } int lineNumber = 0; QString line = ""; OBJGroup* pCurrentGroup = pOBJData->getGroupByName( "" ); // default group name is the empty string QTextStream inputTextStream( &inputFile ); QString delim( " " ); line = inputTextStream.readLine(); while( !( line.isNull() ) ) { if( line != "" ) { QStringList tokens = line.split( delim, QString::SkipEmptyParts ); if( tokens.size() > 0 ) { QString commandToken = tokens[ 0 ]; if( commandToken == "mtllib" ) { QString mtlRelativeFilename = tokens[ 1 ]; QFileInfo objFileInfo( objFilename ); QDir objDir = objFileInfo.dir(); QString mtlAbsoluteFilename = objDir.absolutePath() + "/" + mtlRelativeFilename; parseMTL( mtlAbsoluteFilename, pOBJData ); } else if( commandToken == "g" ) { QString newGroupName; if( tokens.size() < 2 ) { fprintf( stderr, "Warning: group has no name, defaulting to ""\nline: %d\n%s", lineNumber, qPrintable( line ) ); newGroupName = ""; } else { newGroupName = tokens[ 1 ]; } if( newGroupName != pCurrentGroup->name() ) { if( pOBJData->containsGroup( newGroupName ) ) { pCurrentGroup = pOBJData->getGroupByName( newGroupName ); } else { pCurrentGroup = pOBJData->addGroup( newGroupName ); } } } else if( commandToken == "v" ) { // TODO: error checking on all of these and tear down pOBJData OBJLoader::parsePosition( lineNumber, line, tokens, pOBJData ); } else if( commandToken == "vt" ) { OBJLoader::parseTextureCoordinate( lineNumber, line, tokens, pOBJData ); } else if( commandToken == "vn" ) { OBJLoader::parseNormal( lineNumber, line, tokens, pOBJData ); } else if( commandToken == "usemtl" ) { pCurrentGroup->addMaterial( tokens[ 1 ] ); } else if( commandToken == "f" || commandToken == "fo" ) { OBJLoader::parseFace( lineNumber, line, tokens, pCurrentGroup ); } } } ++lineNumber; line = inputTextStream.readLine(); } return true; }
// static bool OBJLoader::parseOBJ( const std::string& objFilename, OBJData* pOBJData ) { // Attempt to read the file. std::ifstream inputFile( objFilename ); if (inputFile.fail()) { return false; } int lineNumber = 0; std::string line = ""; // Default group name is the empty string. OBJGroup& currentGroup = pOBJData->addGroup( "" ); // Default material name is the empty string. pOBJData->addMaterial( "" ); const std::string& delim( " " ); while( std::getline( inputFile, line ) ) { if( line == "" || pystring::startswith( line, "#" ) || pystring::startswith( line, "//" ) ) { ++lineNumber; continue; } std::vector< std::string > tokens; pystring::split( line, tokens, delim ); if( tokens.size() == 0 ) { ++lineNumber; continue; } const std::string& commandToken = tokens[ 0 ]; if( commandToken == "mtllib" ) { const std::string& mtlRelativeFilename = tokens[ 1 ]; std::string dirname = pystring::os::path::dirname( objFilename ); std::string mtlFilename = pystring::os::path::join( dirname, mtlRelativeFilename ); parseMTL( mtlFilename, pOBJData ); } else if( commandToken == "g" ) { std::string newGroupName; if( tokens.size() < 2 ) { fprintf( stderr, "Warning: group has no name, defaulting to ""\n" "line %d: %s", lineNumber, line.c_str() ); newGroupName = ""; } else { newGroupName = line.substr( 2 ); } if( newGroupName != currentGroup.name() ) { if( pOBJData->containsGroup( newGroupName ) ) { currentGroup = pOBJData->getGroupByName( newGroupName ); } else { currentGroup = pOBJData->addGroup( newGroupName ); } } } else if( commandToken == "v" ) { // TODO: error checking on all of these. OBJLoader::parsePosition( lineNumber, line, tokens, pOBJData ); } else if( commandToken == "vt" ) { OBJLoader::parseTextureCoordinate( lineNumber, line, tokens, pOBJData ); } else if( commandToken == "vn" ) { OBJLoader::parseNormal( lineNumber, line, tokens, pOBJData ); } else if( commandToken == "usemtl" ) { currentGroup.addMaterial( tokens[ 1 ] ); } else if( commandToken == "f" || commandToken == "fo" ) { OBJLoader::parseFace( lineNumber, line, tokens, currentGroup ); } ++lineNumber; } return true; }