From 2761bca4965f49124cff905684bd35af92e9ac72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Blissing?= Date: Thu, 3 May 2018 12:56:08 +0200 Subject: [PATCH] Check existence of path before reading image When loading texture images inside the FBX plugin check that the path exists before trying to read the image. This is done to avoid unnecessary warnings inside the readRefImageFile function. --- .../fbx/fbxMaterialToOsgStateSet.cpp | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/osgPlugins/fbx/fbxMaterialToOsgStateSet.cpp b/src/osgPlugins/fbx/fbxMaterialToOsgStateSet.cpp index 64520dd88..fa91b90a9 100644 --- a/src/osgPlugins/fbx/fbxMaterialToOsgStateSet.cpp +++ b/src/osgPlugins/fbx/fbxMaterialToOsgStateSet.cpp @@ -169,16 +169,37 @@ StateSetContent FbxMaterialToOsgStateSet::convert(const FbxSurfaceMaterial* pFbx osg::ref_ptr FbxMaterialToOsgStateSet::fbxTextureToOsgTexture(const FbxFileTexture* fbx) { - ImageMap::iterator it = _imageMap.find(fbx->GetFileName()); + // Try to find image in cache + ImageMap::iterator it = _imageMap.find(fbx->GetFileName()); if (it != _imageMap.end()) return it->second; - osg::ref_ptr pImage = NULL; + - // Warning: fbx->GetRelativeFileName() is relative TO EXECUTION DIR - // fbx->GetFileName() is as stored initially in the FBX - if ((pImage = osgDB::readRefImageFile(osgDB::concatPaths(_dir, fbx->GetFileName()), _options)) || // First try "export dir/name" - (pImage = osgDB::readRefImageFile(fbx->GetFileName(), _options)) || // Then try "name" (if absolute) - (pImage = osgDB::readRefImageFile(osgDB::concatPaths(_dir, fbx->GetRelativeFileName()), _options))) // Else try "current dir/name" + // Try to locate valid filename + std::string filename = ""; + + // Warning: fbx->GetRelativeFileName() is relative TO EXECUTION DIR + // fbx->GetFileName() is as stored initially in the FBX + if (osgDB::fileExists(osgDB::concatPaths(_dir, fbx->GetFileName()))) // First try "export dir/name" + { + filename = osgDB::concatPaths(_dir, fbx->GetFileName()); + } + else if (osgDB::fileExists(fbx->GetFileName())) // Then try "name" (if absolute) + { + filename = fbx->GetFileName(); + } + else if (osgDB::fileExists(osgDB::concatPaths(_dir, fbx->GetRelativeFileName()))) // Else try "current dir/name" + { + filename = osgDB::concatPaths(_dir, fbx->GetRelativeFileName()); + } + else + { + OSG_WARN << "Could not find valid file for " << fbx->GetFileName() << std::endl; + return NULL; + } + + osg::ref_ptr pImage = osgDB::readRefImageFile(filename, _options); + if (pImage.valid()) { osg::ref_ptr pOsgTex = new osg::Texture2D; pOsgTex->setImage(pImage.get());