From 660cb74877426cbfe24cc07d3d9364dfa3492d2b Mon Sep 17 00:00:00 2001 From: Michael PLATINGS Date: Thu, 18 Mar 2010 19:02:12 +0000 Subject: [PATCH] Workaround for models with inverted transparency --- src/osgPlugins/fbx/ReaderWriterFBX.cpp | 2 ++ .../fbx/fbxMaterialToOsgStateSet.cpp | 35 ++++++++++++++++++- src/osgPlugins/fbx/fbxMaterialToOsgStateSet.h | 2 ++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/osgPlugins/fbx/ReaderWriterFBX.cpp b/src/osgPlugins/fbx/ReaderWriterFBX.cpp index 955502449..3ae768207 100644 --- a/src/osgPlugins/fbx/ReaderWriterFBX.cpp +++ b/src/osgPlugins/fbx/ReaderWriterFBX.cpp @@ -270,6 +270,8 @@ ReaderWriterFBX::readNode(const std::string& filenameInit, if (res.success()) { + fbxMaterialToOsgStateSet.checkInvertTransparency(); + resolveBindMatrices(*res.getNode(), boneBindMatrices, nodeMap); osg::Node* osgNode = res.getNode(); diff --git a/src/osgPlugins/fbx/fbxMaterialToOsgStateSet.cpp b/src/osgPlugins/fbx/fbxMaterialToOsgStateSet.cpp index 053885328..9a1d2d83e 100644 --- a/src/osgPlugins/fbx/fbxMaterialToOsgStateSet.cpp +++ b/src/osgPlugins/fbx/fbxMaterialToOsgStateSet.cpp @@ -80,7 +80,9 @@ FbxMaterialToOsgStateSet::convert(const KFbxSurfaceMaterial* pFbxMat) static_cast(pFbxPhong->GetShininess().Get())); } } - return std::make_pair(pOsgMat.release(), pOsgTex.release()); + StateSetContent result(pOsgMat.release(), pOsgTex.release()); + _kFbxMaterialMap.insert(KFbxMaterialMap::value_type(pFbxMat, result)); + return result; } osg::ref_ptr @@ -109,3 +111,34 @@ FbxMaterialToOsgStateSet::fbxTextureToOsgTexture(const KFbxTexture* fbx) return NULL; } } + +void FbxMaterialToOsgStateSet::checkInvertTransparency() +{ + int zeroAlpha = 0, oneAlpha = 0; + for (KFbxMaterialMap::const_iterator it = _kFbxMaterialMap.begin(); it != _kFbxMaterialMap.end(); ++it) + { + const osg::Material* pMaterial = it->second.first; + float alpha = pMaterial->getDiffuse(osg::Material::FRONT).a(); + if (alpha > 0.999f) + { + ++oneAlpha; + } + else if (alpha < 0.001f) + { + ++zeroAlpha; + } + } + + if (zeroAlpha > oneAlpha) + { + //Transparency values seem to be back to front so invert them. + + for (KFbxMaterialMap::const_iterator it = _kFbxMaterialMap.begin(); it != _kFbxMaterialMap.end(); ++it) + { + osg::Material* pMaterial = it->second.first; + osg::Vec4 diffuse = pMaterial->getDiffuse(osg::Material::FRONT); + diffuse.a() = 1.0f - diffuse.a(); + pMaterial->setDiffuse(osg::Material::FRONT_AND_BACK, diffuse); + } + } +} \ No newline at end of file diff --git a/src/osgPlugins/fbx/fbxMaterialToOsgStateSet.h b/src/osgPlugins/fbx/fbxMaterialToOsgStateSet.h index a9a4b83d6..7c7a157d7 100644 --- a/src/osgPlugins/fbx/fbxMaterialToOsgStateSet.h +++ b/src/osgPlugins/fbx/fbxMaterialToOsgStateSet.h @@ -33,6 +33,8 @@ public: FbxMaterialToOsgStateSet::FbxMaterialToOsgStateSet(const std::string& dir, const osgDB::Options* options) : _options(options), _dir(dir) {} + + void checkInvertTransparency(); private: //Convert a texture fbx to an osg texture. osg::ref_ptr