obj materials importer less restrictive when reads something unexpected

This commit is contained in:
alemuntoni 2021-11-05 12:24:52 +01:00
parent 23a24290a2
commit 117daf1a76
1 changed files with 33 additions and 45 deletions

View File

@ -985,20 +985,16 @@ public:
currentMaterial.illum = 2; currentMaterial.illum = 2;
bool first = true; bool first = true;
while (!stream.eof()) while (!stream.eof()) {
{
tokens.clear(); tokens.clear();
TokenizeNextLine(stream, tokens, line, 0); TokenizeNextLine(stream, tokens, line, 0);
if (tokens.size() > 0) if (tokens.size() > 0) {
{
header.clear(); header.clear();
header = tokens[0]; header = tokens[0];
if (header.compare("newmtl")==0) if (header.compare("newmtl")==0) {
{ if (!first) {
if (!first)
{
materials.push_back(currentMaterial); materials.push_back(currentMaterial);
currentMaterial = Material(); currentMaterial = Material();
currentMaterial.index = (unsigned int)(-1); currentMaterial.index = (unsigned int)(-1);
@ -1013,43 +1009,39 @@ public:
else else
currentMaterial.materialName = line.substr(7); //space in the name, get everything after "newmtl " currentMaterial.materialName = line.substr(7); //space in the name, get everything after "newmtl "
} }
else if (header.compare("Ka")==0) else if (header.compare("Ka")==0) {
{ if (tokens.size() < 4) {
if (tokens.size() < 4) return false; currentMaterial.Ka = Point3fFrom3Tokens(tokens,1);
currentMaterial.Ka = Point3fFrom3Tokens(tokens,1); }
} }
else if (header.compare("Kd")==0) else if (header.compare("Kd")==0) {
{ if (tokens.size() < 4) {
if (tokens.size() < 4) return false; currentMaterial.Kd = Point3fFrom3Tokens(tokens,1);
currentMaterial.Kd = Point3fFrom3Tokens(tokens,1); }
} }
else if (header.compare("Ks")==0) else if (header.compare("Ks")==0) {
{ if (tokens.size() < 4) {
if (tokens.size() < 4) return false; currentMaterial.Ks = Point3fFrom3Tokens(tokens,1);
currentMaterial.Ks = Point3fFrom3Tokens(tokens,1); }
} }
else if ( (header.compare("d")==0) || else if ((header.compare("d")==0) || (header.compare("Tr")==0)) { // alpha
(header.compare("Tr")==0) ) // alpha if (tokens.size() < 2) {
{ currentMaterial.Tr = (float) atof(tokens[1].c_str());
if (tokens.size() < 2) return false; }
currentMaterial.Tr = (float) atof(tokens[1].c_str());
} }
else if (header.compare("Ns")==0) // shininess else if (header.compare("Ns")==0) { // shininess
{ if (tokens.size() < 2) {
if (tokens.size() < 2) return false; currentMaterial.Ns = float(atoi(tokens[1].c_str()));
currentMaterial.Ns = float(atoi(tokens[1].c_str())); }
} }
else if (header.compare("illum")==0) // specular illumination on/off else if (header.compare("illum")==0) { // specular illumination on/off
{ if (tokens.size() < 2) {
if (tokens.size() < 2) return false; currentMaterial.illum = atoi(tokens[1].c_str());
currentMaterial.illum = atoi(tokens[1].c_str());; }
} }
else if(header.compare("map_Kd")==0) // texture name else if(header.compare("map_Kd")==0) { // texture name
{
std::string textureName; std::string textureName;
if (tokens.size() < 2) if (tokens.size() == 2) {
return false;
else {
//the tex name is the last one (after any option) //the tex name is the last one (after any option)
textureName = tokens[tokens.size()-1]; textureName = tokens[tokens.size()-1];
} }
@ -1074,19 +1066,15 @@ public:
stream.close(); stream.close();
// Sometimes some materials have texture and no texture // Sometimes some materials have texture and no texture
// in this case for sake of uniformity we just use the first texture. // in this case for sake of uniformity we just use the first texture.
if(!textures.empty()) if(!textures.empty()) {
{ for(size_t i=0;i<materials.size();++i) {
for(size_t i=0;i<materials.size();++i) if(materials[i].map_Kd.empty()) {
{
if(materials[i].map_Kd.empty())
{
materials[i].map_Kd=textures[0]; materials[i].map_Kd=textures[0];
materials[i].index=0; materials[i].index=0;
} }
} }
} }
return true; return true;
} }