From 77958e2a489880bacbd8d22282e9d1b200088223 Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Tue, 8 Mar 2016 12:23:23 -0800 Subject: [PATCH] Change FBXReader impl to recognize inlined tex --- libraries/fbx/src/FBXReader.cpp | 30 +++++++++++++----------- libraries/fbx/src/FBXReader.h | 4 ++++ libraries/fbx/src/FBXReader_Material.cpp | 11 +++++++-- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/libraries/fbx/src/FBXReader.cpp b/libraries/fbx/src/FBXReader.cpp index 500f856450..ab11e72ffe 100644 --- a/libraries/fbx/src/FBXReader.cpp +++ b/libraries/fbx/src/FBXReader.cpp @@ -428,17 +428,17 @@ FBXLight extractLight(const FBXNode& object) { return light; } -QByteArray fileOnUrl(const QByteArray& filenameString, const QString& url) { +QByteArray fileOnUrl(const QByteArray& filepath, const QString& url) { QString path = QFileInfo(url).path(); - QByteArray filename = filenameString; - QFileInfo checkFile(path + "/" + filename.replace('\\', '/')); - //check if the file exists at the RelativeFileName - if (checkFile.exists() && checkFile.isFile()) { - filename = filename.replace('\\', '/'); - } else { - // there is no texture at the fbx dir with the filename added. Assume it is in the fbx dir. - filename = filename.mid(qMax(filename.lastIndexOf('\\'), filename.lastIndexOf('/')) + 1); + QByteArray filename = filepath; + QFileInfo checkFile(path + "/" + filepath); + + // check if the file exists at the RelativeFilename + if (!(checkFile.exists() && checkFile.isFile())) { + // if not, assume it is in the fbx directory + filename = filename.mid(filename.lastIndexOf('/') + 1); } + return filename; } @@ -765,7 +765,9 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS foreach (const FBXNode& subobject, object.children) { if (subobject.name == "RelativeFilename") { QByteArray filename = subobject.properties.at(0).toByteArray(); - filename = fileOnUrl(filename, url); + QByteArray filepath = filename.replace('\\', '/'); + filename = fileOnUrl(filepath, url); + _textureFilepaths.insert(getID(object.properties), filepath); _textureFilenames.insert(getID(object.properties), filename); } else if (subobject.name == "TextureName") { // trim the name from the timestamp @@ -849,19 +851,19 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS _textureParams.insert(getID(object.properties), tex); } } else if (object.name == "Video") { - QByteArray filename; + QByteArray filepath; QByteArray content; foreach (const FBXNode& subobject, object.children) { if (subobject.name == "RelativeFilename") { - filename = subobject.properties.at(0).toByteArray(); - filename = fileOnUrl(filename, url); + filepath= subobject.properties.at(0).toByteArray(); + filepath = filepath.replace('\\', '/'); } else if (subobject.name == "Content" && !subobject.properties.isEmpty()) { content = subobject.properties.at(0).toByteArray(); } } if (!content.isEmpty()) { - _textureContent.insert(filename, content); + _textureContent.insert(filepath, content); } } else if (object.name == "Material") { FBXMaterial material; diff --git a/libraries/fbx/src/FBXReader.h b/libraries/fbx/src/FBXReader.h index 3fefd837f3..8e1f0c681b 100644 --- a/libraries/fbx/src/FBXReader.h +++ b/libraries/fbx/src/FBXReader.h @@ -411,7 +411,11 @@ public: FBXTexture getTexture(const QString& textureID); QHash _textureNames; + // Hashes the original RelativeFilename of textures + QHash _textureFilepaths; + // Hashes the place to look for textures, in case they are not inlined QHash _textureFilenames; + // Hashes texture content by filepath, in case they are inlined QHash _textureContent; QHash _textureParams; diff --git a/libraries/fbx/src/FBXReader_Material.cpp b/libraries/fbx/src/FBXReader_Material.cpp index d87eac405f..d422f079d2 100644 --- a/libraries/fbx/src/FBXReader_Material.cpp +++ b/libraries/fbx/src/FBXReader_Material.cpp @@ -28,9 +28,16 @@ bool FBXMaterial::needTangentSpace() const { FBXTexture FBXReader::getTexture(const QString& textureID) { FBXTexture texture; - texture.filename = _textureFilenames.value(textureID); + const QByteArray& filepath = _textureFilepaths.value(textureID); + texture.content = _textureContent.value(filepath); + + if (texture.content.isEmpty()) { // the content is not inlined + texture.filename = _textureFilenames.value(textureID); + } else { // use supplied filepath for inlined content + texture.filename = filepath; + } + texture.name = _textureNames.value(textureID); - texture.content = _textureContent.value(texture.filename); texture.transform.setIdentity(); texture.texcoordSet = 0; if (_textureParams.contains(textureID)) {