Skip to content

Commit

Permalink
fix for space in material name, filename, texture
Browse files Browse the repository at this point in the history
added support for space in material name, material filename, texture
name
  • Loading branch information
mcallieri committed Feb 21, 2017
1 parent e8ec992 commit c547c56
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions wrap/io_trimesh/import_obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ namespace vcg {
std::vector<CoordType> normals; // vertex normals
std::vector<ObjIndexedFace> indexedFaces;
std::vector< std::string > tokens;
std::string line;
std::string header;

short currentMaterialIdx = 0; // index of current material into materials vector
Expand Down Expand Up @@ -287,7 +288,7 @@ namespace vcg {
while (!stream.eof())
{
tokens.clear();
TokenizeNextLine(stream, tokens,&vertexColorVector);
TokenizeNextLine(stream, tokens, line, &vertexColorVector);

unsigned int numTokens = static_cast<unsigned int>(tokens.size());
if (numTokens > 0)
Expand Down Expand Up @@ -594,13 +595,23 @@ namespace vcg {
else if ((header.compare("mtllib")==0) && (tokens.size() > 1)) // material library
{
// obtain the name of the file containing materials library
std::string materialFileName = tokens[1];
std::string materialFileName;
if (tokens.size() == 2)
materialFileName = tokens[1]; //play it safe
else
materialFileName = line.substr(7); //get everything after "mtllib "

if (!LoadMaterials( materialFileName.c_str(), materials, m.textures))
result = E_MATERIAL_FILE_NOT_FOUND;
}
else if ((header.compare("usemtl")==0) && (tokens.size() > 1)) // material usage
{
std::string materialName = tokens[1];
std::string materialName;
if (tokens.size() == 2)
materialName = tokens[1]; //play it safe
else
materialName = line.substr(7); //get everything after "usemtl "

bool found = false;
unsigned i = 0;
while (!found && (i < materials.size()))
Expand Down Expand Up @@ -736,10 +747,10 @@ namespace vcg {
* \param stream The object providing the input stream
* \param tokens The "tokens" in the next line
*/
inline static void TokenizeNextLine(std::ifstream &stream, std::vector< std::string > &tokens, std::vector<Color4b> *colVec)
inline static void TokenizeNextLine(std::ifstream &stream, std::vector< std::string > &tokens, std::string &line, std::vector<Color4b> *colVec)
{
if(stream.eof()) return;
std::string line;

do
{
std::getline(stream, line);
Expand Down Expand Up @@ -942,6 +953,7 @@ namespace vcg {
return false;

std::vector< std::string > tokens;
std::string line;
std::string header;

materials.clear();
Expand All @@ -952,7 +964,7 @@ namespace vcg {
while (!stream.eof())
{
tokens.clear();
TokenizeNextLine(stream, tokens,0);
TokenizeNextLine(stream, tokens, line, 0);

if (tokens.size() > 0)
{
Expand All @@ -972,7 +984,10 @@ namespace vcg {
//strcpy(currentMaterial.name, tokens[1].c_str());
if(tokens.size() < 2)
return false;
currentMaterial.materialName=tokens[1];
else if (tokens.size() == 2)
currentMaterial.materialName = tokens[1]; //play it safe
else
currentMaterial.materialName = line.substr(7); //space in the name, get everything after "newmtl "
}
else if (header.compare("Ka")==0)
{
Expand Down Expand Up @@ -1027,10 +1042,14 @@ namespace vcg {
}
else if( (header.compare("map_Kd")==0) || (header.compare("map_Ka")==0) ) // texture name
{
std::string textureName;
if (tokens.size() < 2)
return false;
std::string textureName = tokens[1];
//strcpy(currentMaterial.textureFileName, textureName.c_str());
else if (tokens.size() == 2)
textureName = tokens[1]; //play it safe
else
textureName = line.substr(7); //get everything after "map_Kd " or "map_Ka "

currentMaterial.map_Kd=textureName;

// adding texture name into textures vector (if not already present)
Expand Down

0 comments on commit c547c56

Please sign in to comment.