Skip to content

Commit

Permalink
looking for mtllib in .obj
Browse files Browse the repository at this point in the history
  • Loading branch information
ponchio committed Feb 6, 2025
1 parent 989e907 commit 2e022cb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
24 changes: 18 additions & 6 deletions src/nxsbuild/objloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ObjLoader::ObjLoader(QString filename, QString _mtl):
if(!file.open(QFile::ReadOnly))
throw QString("could not open file %1. Error: %2").arg(filename).arg(file.errorString());

readMTL();
readMTL(file);
}

ObjLoader::~ObjLoader() {
Expand Down Expand Up @@ -83,20 +83,32 @@ void ObjLoader::cacheTextureUV() {
}
}

void ObjLoader::readMTL() {
void ObjLoader::readMTL(QFile &file) {

quint64 pos = file.pos();

char buffer[1024];

if (!mtl.isNull()) {
if(!QFileInfo::exists(mtl))
throw QString("Could not find .mtl file: %1").arg(mtl);
} else {
}

if(mtl.isNull()) { //look for mtllib
while (1) {
if(file.readLine(buffer, 1000) == -1)
break;
if(strncmp(buffer, "mtllib", 6) != 0)
continue;

QString m = QString(buffer).mid(7).trimmed();
if (QFileInfo::exists(m))
mtl = m;
break;
}
}
if(mtl.isNull()) { //assume the name is the same
QString fname = file.fileName();
QFileInfo info = QFileInfo(fname);

//assuming mtl base file name the same as obj
mtl = info.path() + "/" + info.completeBaseName() + ".mtl";
}

Expand Down
2 changes: 1 addition & 1 deletion src/nxsbuild/objloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ObjLoader: public MeshLoader {

private:

void readMTL();
void readMTL(QFile &file); //obj file passed to search for mtllib
void cacheTextureUV();
void cacheVertices();

Expand Down

0 comments on commit 2e022cb

Please sign in to comment.