From 5cbe74fbc13dae5096bd2b8f3ffab8629a950238 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 12 May 2011 14:07:15 +0000 Subject: [PATCH] From Sukender, ported across to use the new osgDB::ExternalFileWriter --- src/osgPlugins/fbx/WriterNodeVisitor.cpp | 69 ++++++------------------ src/osgPlugins/fbx/WriterNodeVisitor.h | 17 ++---- 2 files changed, 20 insertions(+), 66 deletions(-) diff --git a/src/osgPlugins/fbx/WriterNodeVisitor.cpp b/src/osgPlugins/fbx/WriterNodeVisitor.cpp index 0b8884e34..41a1ceefe 100644 --- a/src/osgPlugins/fbx/WriterNodeVisitor.cpp +++ b/src/osgPlugins/fbx/WriterNodeVisitor.cpp @@ -279,22 +279,17 @@ void PrimitiveIndexWriter::drawArrays(GLenum mode,GLint first,GLsizei count) } WriterNodeVisitor::Material::Material(WriterNodeVisitor& writerNodeVisitor, - const std::string& srcDirectory, + osgDB::ExternalFileWriter & externalWriter, const osg::StateSet* stateset, const osg::Material* mat, const osg::Texture* tex, KFbxSdkManager* pSdkManager, - const std::string& directory, - ImageSet& imageSet, - ImageFilenameSet& imageFilenameSet, - unsigned int& lastGeneratedImageFileName, const osgDB::ReaderWriter::Options * options, int index) : _index(index), _fbxMaterial(NULL), _fbxTexture(NULL), - _osgImage(NULL), - _directory(directory) + _osgImage(NULL) { osg::Vec4 diffuse(1,1,1,1), ambient(0.2,0.2,0.2,1), @@ -363,50 +358,22 @@ WriterNodeVisitor::Material::Material(WriterNodeVisitor& writerNodeVisitor, { _osgImage = tex->getImage(0); - ImageSet::iterator it = imageSet.find(_osgImage); + std::string relativePath; + externalWriter.write(*_osgImage, options, NULL, &relativePath); - if (it == imageSet.end()) + _fbxTexture = KFbxTexture::Create(pSdkManager, relativePath.c_str()); + _fbxTexture->SetFileName(relativePath.c_str()); + // Create a FBX material if needed + if (!_fbxMaterial) { - std::string canonicalPath( osgDB::getRealPath(osgDB::convertFileNameToNativeStyle(_osgImage->getFileName())) ); - std::string destPath; - std::string relativePath; - if (canonicalPath.empty()) - { - static const unsigned int MAX_IMAGE_NUMBER = UINT_MAX-1; // -1 to allow doing +1 without an overflow - unsigned int imageNumber; - for (imageNumber=lastGeneratedImageFileName+1; imageNumberGetDiffuseColor().ConnectSrcObject(_fbxTexture); } - - _fbxTexture = KFbxFileTexture::Create(pSdkManager, it->second.c_str()); - _fbxTexture->SetFileName(it->second.c_str()); } } @@ -428,7 +395,7 @@ int WriterNodeVisitor::processStateSet(const osg::StateSet* ss) { int matNum = _lastMaterialIndex; _materialMap.insert(MaterialMap::value_type(MaterialMap::key_type(ss), - Material(*this, _srcDirectory, ss, mat, tex, _pSdkManager, _directory, _imageSet, _imageFilenameSet, _lastGeneratedImageFileName, _options, matNum))); + Material(*this, _externalWriter, ss, mat, tex, _pSdkManager, _options, matNum))); ++_lastMaterialIndex; return matNum; } @@ -522,15 +489,11 @@ WriterNodeVisitor::setControlPointAndNormalsAndUV(const osg::Geode& geo, if (basevecs->getType() == osg::Array::Vec3ArrayType) { const osg::Vec3 & vec = (*static_cast(basevecs))[vertexIndex]; - // Sukender: "*new KFbxVector4"? Shouldn't it be "KFbxVector4" alone? - //mesh->SetControlPointAt(*new KFbxVector4(vec.x(), vec.y(), vec.z()), it->second); vertex.Set(vec.x(), vec.y(), vec.z()); } else if (basevecs->getType() == osg::Array::Vec3dArrayType) { const osg::Vec3d & vec = (*static_cast(basevecs))[vertexIndex]; - // Sukender: "*new KFbxVector4"? Shouldn't it be "KFbxVector4" alone? - //mesh->SetControlPointAt(*new KFbxVector4(vec.x(), vec.y(), vec.z()), it->second); vertex.Set(vec.x(), vec.y(), vec.z()); } else diff --git a/src/osgPlugins/fbx/WriterNodeVisitor.h b/src/osgPlugins/fbx/WriterNodeVisitor.h index 79ef627f5..8bf8bdf5f 100644 --- a/src/osgPlugins/fbx/WriterNodeVisitor.h +++ b/src/osgPlugins/fbx/WriterNodeVisitor.h @@ -24,6 +24,7 @@ #include #include #include +#include #if defined(_MSC_VER) #pragma warning( disable : 4505 ) @@ -80,12 +81,10 @@ class WriterNodeVisitor: public osg::NodeVisitor _currentStateSet(new osg::StateSet()), _lastMaterialIndex(0), _lastMeshIndex(0), - _lastGeneratedImageFileName(0), _curFbxNode(pScene->GetRootNode()), _options(options), _succeedLastApply(true), - _directory(osgDB::getFilePath(fileName)), - _srcDirectory(srcDirectory) + _externalWriter(srcDirectory, osgDB::getFilePath(fileName), true, 0) {} ///Tell us if last Node succeed traversing. @@ -141,15 +140,11 @@ class WriterNodeVisitor: public osg::NodeVisitor public: ///Create a KfbxMaterial and KfbxTexture from osg::Texture and osg::Material. Material(WriterNodeVisitor& writerNodeVisitor, - const std::string& srcDirectory, + osgDB::ExternalFileWriter & externalWriter, const osg::StateSet* stateset, const osg::Material* mat, const osg::Texture* tex, KFbxSdkManager* pSdkManager, - const std::string& directory, - ImageSet& imageSet, - ImageFilenameSet& imageFilenameSet, - unsigned int& lastGeneratedImageFileName, const osgDB::ReaderWriter::Options * options, int index = -1); @@ -183,7 +178,6 @@ class WriterNodeVisitor: public osg::NodeVisitor KFbxFileTexture* _fbxTexture; int _index;///< Index in the Map const osg::Image* _osgImage; - const std::string& _directory; }; protected: @@ -258,13 +252,10 @@ class WriterNodeVisitor: public osg::NodeVisitor ///We store the fbx Materials and Textures in this map. MaterialMap _materialMap; - ImageSet _imageSet; - ImageFilenameSet _imageFilenameSet; - unsigned int _lastGeneratedImageFileName; unsigned int _lastMaterialIndex; unsigned int _lastMeshIndex; const osgDB::ReaderWriter::Options* _options; - const std::string _srcDirectory; + osgDB::ExternalFileWriter _externalWriter; }; // end namespace pluginfbx