| [10780] | 1 | #include <sstream> |
|---|
| 2 | #include <memory> |
|---|
| 3 | |
|---|
| 4 | #include <osg/Notify> |
|---|
| 5 | #include <osg/MatrixTransform> |
|---|
| 6 | #include <osg/Material> |
|---|
| [11109] | 7 | #include <osg/PositionAttitudeTransform> |
|---|
| [10780] | 8 | #include <osg/Texture2D> |
|---|
| [11109] | 9 | #include <osgDB/ConvertUTF> |
|---|
| [10780] | 10 | #include <osgDB/FileNameUtils> |
|---|
| 11 | #include <osgDB/FileUtils> |
|---|
| 12 | #include <osgDB/ReadFile> |
|---|
| 13 | #include <osgDB/Registry> |
|---|
| 14 | #include <osgAnimation/AnimationManagerBase> |
|---|
| [11109] | 15 | #include <osgAnimation/Bone> |
|---|
| [11153] | 16 | #include <osgAnimation/RigGeometry> |
|---|
| [10780] | 17 | #include <osgAnimation/Skeleton> |
|---|
| [11153] | 18 | #include <osgAnimation/VertexInfluence> |
|---|
| [10780] | 19 | |
|---|
| 20 | #if defined(_MSC_VER) |
|---|
| 21 | #pragma warning( disable : 4505 ) |
|---|
| 22 | #endif |
|---|
| 23 | #include <fbxsdk.h> |
|---|
| 24 | |
|---|
| 25 | #include "ReaderWriterFBX.h" |
|---|
| 26 | #include "fbxRNode.h" |
|---|
| [11109] | 27 | #include "fbxMaterialToOsgStateSet.h" |
|---|
| 28 | #include "WriterNodeVisitor.h" |
|---|
| [10780] | 29 | |
|---|
| [11109] | 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | bool isBasicRootNode(const osg::Node& node) |
|---|
| 34 | { |
|---|
| 35 | const osg::Group* osgGroup = node.asGroup(); |
|---|
| 36 | if (!osgGroup) |
|---|
| 37 | { |
|---|
| 38 | |
|---|
| 39 | return false; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | const osg::Transform* transform = osgGroup->asTransform(); |
|---|
| 44 | if (transform) |
|---|
| 45 | { |
|---|
| 46 | if (const osg::MatrixTransform* matrixTransform = transform->asMatrixTransform()) |
|---|
| 47 | { |
|---|
| 48 | if (!matrixTransform->getMatrix().isIdentity()) |
|---|
| 49 | { |
|---|
| 50 | |
|---|
| 51 | return false; |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | else if (const osg::PositionAttitudeTransform* pat = transform->asPositionAttitudeTransform()) |
|---|
| 55 | { |
|---|
| 56 | if (pat->getPosition() != osg::Vec3d() || |
|---|
| 57 | pat->getAttitude() != osg::Quat() || |
|---|
| 58 | pat->getScale() != osg::Vec3d(1.0f, 1.0f, 1.0f) || |
|---|
| 59 | pat->getPivotPoint() != osg::Vec3d()) |
|---|
| 60 | { |
|---|
| 61 | |
|---|
| 62 | return false; |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | else |
|---|
| 66 | { |
|---|
| 67 | |
|---|
| 68 | return false; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | if (node.getStateSet()) |
|---|
| 74 | { |
|---|
| 75 | osg::ref_ptr<osg::StateSet> emptyStateSet = new osg::StateSet; |
|---|
| 76 | if (node.getStateSet()->compare(*emptyStateSet, true) != 0) |
|---|
| 77 | { |
|---|
| 78 | return false; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | return true; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | class CleanUpFbx |
|---|
| 87 | { |
|---|
| 88 | KFbxSdkManager* m_pSdkManager; |
|---|
| 89 | public: |
|---|
| 90 | explicit CleanUpFbx(KFbxSdkManager* pSdkManager) : m_pSdkManager(pSdkManager) |
|---|
| 91 | {} |
|---|
| 92 | |
|---|
| 93 | ~CleanUpFbx() |
|---|
| 94 | { |
|---|
| 95 | KFbxIOSettings::IOSettingsRef().FreeIOSettings(); |
|---|
| 96 | m_pSdkManager->Destroy(); |
|---|
| 97 | } |
|---|
| 98 | }; |
|---|
| 99 | |
|---|
| [11262] | 100 | |
|---|
| 101 | |
|---|
| 102 | void findLinkedFbxSkeletonNodes(KFbxNode* pNode, std::set<const KFbxNode*>& fbxSkeletons) |
|---|
| 103 | { |
|---|
| 104 | if (const KFbxGeometry* pMesh = dynamic_cast<const KFbxGeometry*>(pNode->GetNodeAttribute())) |
|---|
| 105 | { |
|---|
| 106 | for (int i = 0; i < pMesh->GetDeformerCount(KFbxDeformer::eSKIN); ++i) |
|---|
| 107 | { |
|---|
| 108 | const KFbxSkin* pSkin = (const KFbxSkin*)pMesh->GetDeformer(i, KFbxDeformer::eSKIN); |
|---|
| 109 | |
|---|
| 110 | for (int j = 0; j < pSkin->GetClusterCount(); ++j) |
|---|
| 111 | { |
|---|
| 112 | const KFbxNode* pSkeleton = pSkin->GetCluster(j)->GetLink(); |
|---|
| 113 | fbxSkeletons.insert(pSkeleton); |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | for (int i = 0; i < pNode->GetChildCount(); ++i) |
|---|
| 119 | { |
|---|
| 120 | findLinkedFbxSkeletonNodes(pNode->GetChild(i), fbxSkeletons); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| [11153] | 124 | void resolveBindMatrices( |
|---|
| 125 | osg::Node& root, |
|---|
| 126 | const BindMatrixMap& boneBindMatrices, |
|---|
| 127 | const std::map<KFbxNode*, osg::Node*>& nodeMap) |
|---|
| 128 | { |
|---|
| 129 | std::set<std::string> nodeNames; |
|---|
| 130 | for (std::map<KFbxNode*, osg::Node*>::const_iterator it = nodeMap.begin(); it != nodeMap.end(); ++it) |
|---|
| 131 | { |
|---|
| 132 | nodeNames.insert(it->second->getName()); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | for (BindMatrixMap::const_iterator it = boneBindMatrices.begin(); |
|---|
| 136 | it != boneBindMatrices.end();) |
|---|
| 137 | { |
|---|
| 138 | KFbxNode* const fbxBone = it->first.first; |
|---|
| 139 | std::map<KFbxNode*, osg::Node*>::const_iterator nodeIt = nodeMap.find(fbxBone); |
|---|
| 140 | if (nodeIt != nodeMap.end()) |
|---|
| 141 | { |
|---|
| 142 | const osg::Matrix bindMatrix = it->second; |
|---|
| 143 | osgAnimation::Bone& osgBone = dynamic_cast<osgAnimation::Bone&>(*nodeIt->second); |
|---|
| 144 | osgBone.setInvBindMatrixInSkeletonSpace(bindMatrix); |
|---|
| 145 | |
|---|
| 146 | ++it; |
|---|
| 147 | for (; it != boneBindMatrices.end() && it->first.first == fbxBone; ++it) |
|---|
| 148 | { |
|---|
| 149 | if (it->second != bindMatrix) |
|---|
| 150 | { |
|---|
| 151 | std::string name; |
|---|
| 152 | for (int i = 0;; ++i) |
|---|
| 153 | { |
|---|
| 154 | std::stringstream ss; |
|---|
| 155 | ss << osgBone.getName() << '_' << i; |
|---|
| 156 | name = ss.str(); |
|---|
| 157 | if (nodeNames.insert(name).second) |
|---|
| 158 | { |
|---|
| 159 | break; |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | osgAnimation::Bone* newBone = new osgAnimation::Bone(name); |
|---|
| 163 | newBone->setDefaultUpdateCallback(); |
|---|
| 164 | newBone->setInvBindMatrixInSkeletonSpace(it->second); |
|---|
| 165 | osgBone.addChild(newBone); |
|---|
| 166 | |
|---|
| 167 | osgAnimation::RigGeometry* pRigGeometry = it->first.second; |
|---|
| [11262] | 168 | |
|---|
| [11153] | 169 | osgAnimation::VertexInfluenceMap* vertexInfluences = pRigGeometry->getInfluenceMap(); |
|---|
| 170 | |
|---|
| 171 | osgAnimation::VertexInfluenceMap::iterator vimIt = vertexInfluences->find(osgBone.getName()); |
|---|
| 172 | if (vimIt != vertexInfluences->end()) |
|---|
| 173 | { |
|---|
| 174 | osgAnimation::VertexInfluence vi; |
|---|
| 175 | vi.swap(vimIt->second); |
|---|
| 176 | vertexInfluences->erase(vimIt); |
|---|
| 177 | osgAnimation::VertexInfluence& vi2 = (*vertexInfluences)[name]; |
|---|
| 178 | vi.swap(vi2); |
|---|
| 179 | vi2.setName(name); |
|---|
| 180 | } |
|---|
| 181 | else |
|---|
| 182 | { |
|---|
| [11252] | 183 | osg::notify(osg::WARN) << "No vertex influences found for \"" << osgBone.getName() << "\"" << std::endl; |
|---|
| [11153] | 184 | } |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | else |
|---|
| 189 | { |
|---|
| [11252] | 190 | osg::notify(osg::WARN) << "No bone found for \"" << fbxBone->GetName() << "\"" << std::endl; |
|---|
| 191 | ++it; |
|---|
| [11153] | 192 | } |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| [10780] | 196 | osgDB::ReaderWriter::ReadResult |
|---|
| [11109] | 197 | ReaderWriterFBX::readNode(const std::string& filenameInit, |
|---|
| 198 | const Options* options) const |
|---|
| [10780] | 199 | { |
|---|
| 200 | try |
|---|
| 201 | { |
|---|
| [11109] | 202 | std::string ext(osgDB::getLowerCaseFileExtension(filenameInit)); |
|---|
| 203 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| [10780] | 204 | |
|---|
| [11109] | 205 | std::string filename(osgDB::findDataFile(filenameInit, options)); |
|---|
| 206 | if (filename.empty()) return ReadResult::FILE_NOT_FOUND; |
|---|
| [10780] | 207 | |
|---|
| 208 | KFbxSdkManager* pSdkManager = KFbxSdkManager::Create(); |
|---|
| 209 | |
|---|
| 210 | if (!pSdkManager) |
|---|
| 211 | { |
|---|
| 212 | return ReadResult::ERROR_IN_READING_FILE; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| [11109] | 215 | CleanUpFbx cleanUpFbx(pSdkManager); |
|---|
| [10780] | 216 | |
|---|
| [11109] | 217 | KFbxScene* pScene = KFbxScene::Create(pSdkManager, ""); |
|---|
| [10780] | 218 | |
|---|
| [11109] | 219 | |
|---|
| 220 | #ifdef OSG_USE_UTF8_FILENAME |
|---|
| 221 | const std::string& utf8filename(filename); |
|---|
| 222 | #else |
|---|
| [11141] | 223 | std::string utf8filename(osgDB::convertStringFromCurrentCodePageToUTF8(filename)); |
|---|
| [11109] | 224 | #endif |
|---|
| [10780] | 225 | |
|---|
| 226 | int fileFormat; |
|---|
| 227 | if (!pSdkManager->GetIOPluginRegistry()->DetectFileFormat(utf8filename.c_str(), fileFormat)) |
|---|
| 228 | { |
|---|
| 229 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 230 | } |
|---|
| [11109] | 231 | KFbxImporter* lImporter = KFbxImporter::Create(pSdkManager, ""); |
|---|
| [10780] | 232 | |
|---|
| [11139] | 233 | if (!lImporter->Initialize(utf8filename.c_str(), fileFormat)) |
|---|
| [10780] | 234 | { |
|---|
| 235 | return std::string(lImporter->GetLastErrorString()); |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | if (!lImporter->IsFBX()) |
|---|
| 239 | { |
|---|
| 240 | return ReadResult::ERROR_IN_READING_FILE; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| [11109] | 243 | for (int i = 0; i < lImporter->GetTakeCount(); i++) |
|---|
| [10780] | 244 | { |
|---|
| 245 | KFbxTakeInfo* lTakeInfo = lImporter->GetTakeInfo(i); |
|---|
| 246 | |
|---|
| 247 | lTakeInfo->mSelect = true; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | if (!lImporter->Import(pScene)) |
|---|
| 251 | { |
|---|
| 252 | return std::string(lImporter->GetLastErrorString()); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| [11109] | 255 | |
|---|
| 256 | |
|---|
| [10780] | 257 | if (KFbxNode* pNode = pScene->GetRootNode()) |
|---|
| 258 | { |
|---|
| [11141] | 259 | pScene->SetCurrentTake(pScene->GetCurrentTakeName()); |
|---|
| [11139] | 260 | |
|---|
| [11141] | 261 | bool useFbxRoot = false; |
|---|
| [11109] | 262 | if (options) |
|---|
| 263 | { |
|---|
| 264 | std::istringstream iss(options->getOptionString()); |
|---|
| 265 | std::string opt; |
|---|
| 266 | while (iss >> opt) |
|---|
| 267 | { |
|---|
| 268 | if (opt == "UseFbxRoot") |
|---|
| 269 | { |
|---|
| 270 | useFbxRoot = true; |
|---|
| 271 | } |
|---|
| 272 | } |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| [10780] | 275 | osg::ref_ptr<osgAnimation::AnimationManagerBase> pAnimationManager; |
|---|
| [11139] | 276 | bool bIsBone = false; |
|---|
| [10780] | 277 | int nLightCount = 0; |
|---|
| [11109] | 278 | osg::ref_ptr<Options> localOptions = NULL; |
|---|
| 279 | if (options) |
|---|
| 280 | localOptions = options->cloneOptions(); |
|---|
| 281 | else |
|---|
| 282 | localOptions = new osgDB::Options(); |
|---|
| 283 | localOptions->setObjectCacheHint(osgDB::ReaderWriter::Options::CACHE_IMAGES); |
|---|
| 284 | |
|---|
| 285 | std::string filePath = osgDB::getFilePath(filename); |
|---|
| 286 | FbxMaterialToOsgStateSet fbxMaterialToOsgStateSet(filePath, localOptions.get()); |
|---|
| [11139] | 287 | |
|---|
| [11262] | 288 | std::set<const KFbxNode*> fbxSkeletons; |
|---|
| 289 | findLinkedFbxSkeletonNodes(pNode, fbxSkeletons); |
|---|
| 290 | |
|---|
| [11141] | 291 | std::map<KFbxNode*, osg::Node*> nodeMap; |
|---|
| [11153] | 292 | BindMatrixMap boneBindMatrices; |
|---|
| [11141] | 293 | std::map<KFbxNode*, osgAnimation::Skeleton*> skeletonMap; |
|---|
| [10780] | 294 | ReadResult res = readFbxNode(*pSdkManager, pNode, pAnimationManager, |
|---|
| [11139] | 295 | bIsBone, nLightCount, fbxMaterialToOsgStateSet, nodeMap, |
|---|
| [11262] | 296 | boneBindMatrices, fbxSkeletons, skeletonMap, localOptions.get()); |
|---|
| [11109] | 297 | |
|---|
| [10780] | 298 | if (res.success()) |
|---|
| 299 | { |
|---|
| [11251] | 300 | fbxMaterialToOsgStateSet.checkInvertTransparency(); |
|---|
| 301 | |
|---|
| [11153] | 302 | resolveBindMatrices(*res.getNode(), boneBindMatrices, nodeMap); |
|---|
| [11139] | 303 | |
|---|
| [11141] | 304 | osg::Node* osgNode = res.getNode(); |
|---|
| 305 | osgNode->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL,osg::StateAttribute::ON); |
|---|
| 306 | osgNode->getOrCreateStateSet()->setMode(GL_NORMALIZE,osg::StateAttribute::ON); |
|---|
| [11139] | 307 | |
|---|
| [10780] | 308 | if (pAnimationManager.valid()) |
|---|
| 309 | { |
|---|
| 310 | if (osgNode->getUpdateCallback()) |
|---|
| 311 | { |
|---|
| 312 | osg::Group* osgGroup = new osg::Group; |
|---|
| 313 | osgGroup->addChild(osgNode); |
|---|
| 314 | osgNode = osgGroup; |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | pAnimationManager->buildTargetReference(); |
|---|
| 319 | osgNode->setUpdateCallback(pAnimationManager.get()); |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | KFbxAxisSystem fbxAxis = pScene->GetGlobalSettings().GetAxisSystem(); |
|---|
| [11109] | 323 | |
|---|
| 324 | if (fbxAxis != KFbxAxisSystem::OpenGL) |
|---|
| [10780] | 325 | { |
|---|
| [11109] | 326 | int upSign; |
|---|
| 327 | KFbxAxisSystem::eUpVector eUp = fbxAxis.GetUpVector(upSign); |
|---|
| 328 | bool bLeftHanded = fbxAxis.GetCoorSystem() == KFbxAxisSystem::LeftHanded; |
|---|
| [10780] | 329 | float fSign = upSign < 0 ? -1.0f : 1.0f; |
|---|
| 330 | float zScale = bLeftHanded ? -1.0f : 1.0f; |
|---|
| 331 | |
|---|
| 332 | osg::Matrix mat; |
|---|
| 333 | switch (eUp) |
|---|
| 334 | { |
|---|
| 335 | case KFbxAxisSystem::XAxis: |
|---|
| 336 | mat.set(0,fSign,0,0,-fSign,0,0,0,0,0,zScale,0,0,0,0,1); |
|---|
| 337 | break; |
|---|
| 338 | case KFbxAxisSystem::YAxis: |
|---|
| 339 | mat.set(1,0,0,0,0,fSign,0,0,0,0,fSign*zScale,0,0,0,0,1); |
|---|
| 340 | break; |
|---|
| 341 | case KFbxAxisSystem::ZAxis: |
|---|
| 342 | mat.set(1,0,0,0,0,0,-fSign*zScale,0,0,fSign,0,0,0,0,0,1); |
|---|
| 343 | break; |
|---|
| 344 | } |
|---|
| [11109] | 345 | |
|---|
| 346 | osg::Transform* pTransformTemp = osgNode->asTransform(); |
|---|
| 347 | osg::MatrixTransform* pMatrixTransform = pTransformTemp ? |
|---|
| 348 | pTransformTemp->asMatrixTransform() : NULL; |
|---|
| 349 | if (pMatrixTransform) |
|---|
| 350 | { |
|---|
| 351 | pMatrixTransform->setMatrix(pMatrixTransform->getMatrix() * mat); |
|---|
| 352 | } |
|---|
| 353 | else |
|---|
| 354 | { |
|---|
| 355 | pMatrixTransform = new osg::MatrixTransform(mat); |
|---|
| 356 | if (useFbxRoot && isBasicRootNode(*osgNode)) |
|---|
| 357 | { |
|---|
| 358 | |
|---|
| 359 | osg::Group* osgGroup = osgNode->asGroup(); |
|---|
| 360 | for(unsigned int i = 0; i < osgGroup->getNumChildren(); ++i) |
|---|
| 361 | { |
|---|
| 362 | pMatrixTransform->addChild(osgGroup->getChild(i)); |
|---|
| 363 | } |
|---|
| 364 | pMatrixTransform->setName(osgGroup->getName()); |
|---|
| 365 | } |
|---|
| 366 | else |
|---|
| 367 | { |
|---|
| 368 | pMatrixTransform->addChild(osgNode); |
|---|
| 369 | } |
|---|
| 370 | } |
|---|
| 371 | osgNode = pMatrixTransform; |
|---|
| [10780] | 372 | } |
|---|
| 373 | |
|---|
| [11109] | 374 | osgNode->setName(filenameInit); |
|---|
| [10780] | 375 | return osgNode; |
|---|
| 376 | } |
|---|
| 377 | } |
|---|
| 378 | } |
|---|
| 379 | catch (...) |
|---|
| 380 | { |
|---|
| [11141] | 381 | osg::notify(osg::WARN) << "Exception thrown while importing \"" << filenameInit << '\"' << std::endl; |
|---|
| [10780] | 382 | } |
|---|
| 383 | |
|---|
| 384 | return ReadResult::ERROR_IN_READING_FILE; |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| [11109] | 387 | osgDB::ReaderWriter::WriteResult ReaderWriterFBX::writeNode( |
|---|
| 388 | const osg::Node& node, |
|---|
| 389 | const std::string& filename, |
|---|
| 390 | const Options* options) const |
|---|
| 391 | { |
|---|
| 392 | try |
|---|
| 393 | { |
|---|
| 394 | std::string ext = osgDB::getLowerCaseFileExtension(filename); |
|---|
| 395 | if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; |
|---|
| 396 | |
|---|
| 397 | osg::ref_ptr<Options> localOptions = options ? |
|---|
| 398 | static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; |
|---|
| 399 | localOptions->getDatabasePathList().push_front(osgDB::getFilePath(filename)); |
|---|
| 400 | |
|---|
| 401 | KFbxSdkManager* pSdkManager = KFbxSdkManager::Create(); |
|---|
| 402 | |
|---|
| 403 | if (!pSdkManager) |
|---|
| 404 | { |
|---|
| 405 | return WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | CleanUpFbx cleanUpFbx(pSdkManager); |
|---|
| 409 | |
|---|
| 410 | bool useFbxRoot = false; |
|---|
| 411 | if (options) |
|---|
| 412 | { |
|---|
| 413 | std::istringstream iss(options->getOptionString()); |
|---|
| 414 | std::string opt; |
|---|
| 415 | while (iss >> opt) |
|---|
| 416 | { |
|---|
| 417 | if (opt == "Embedded") |
|---|
| 418 | { |
|---|
| 419 | IOSREF.SetBoolProp(EXP_FBX_EMBEDDED, true); |
|---|
| 420 | if (KFbxIOSettings::IOSettingsRef().IsIOSettingsAllocated()) |
|---|
| 421 | KFbxIOSettings::IOSettingsRef().AllocateIOSettings(*pSdkManager); |
|---|
| 422 | } |
|---|
| 423 | else if (opt == "UseFbxRoot") |
|---|
| 424 | { |
|---|
| 425 | useFbxRoot = true; |
|---|
| 426 | } |
|---|
| 427 | } |
|---|
| 428 | } |
|---|
| 429 | |
|---|
| 430 | KFbxScene* pScene = KFbxScene::Create(pSdkManager, ""); |
|---|
| 431 | WriterNodeVisitor writerNodeVisitor(pScene, pSdkManager, filename, |
|---|
| 432 | options, osgDB::getFilePath(node.getName().empty() ? filename : node.getName())); |
|---|
| 433 | if (useFbxRoot && isBasicRootNode(node)) |
|---|
| 434 | { |
|---|
| 435 | |
|---|
| 436 | const osg::Group * osgGroup = node.asGroup(); |
|---|
| 437 | for(unsigned int child=0; child<osgGroup->getNumChildren(); ++child) { |
|---|
| 438 | const_cast<osg::Node *>(osgGroup->getChild(child))->accept(writerNodeVisitor); |
|---|
| 439 | } |
|---|
| 440 | } |
|---|
| 441 | else { |
|---|
| 442 | |
|---|
| 443 | const_cast<osg::Node&>(node).accept(writerNodeVisitor); |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | KFbxExporter* lExporter = KFbxExporter::Create(pSdkManager, ""); |
|---|
| 447 | pScene->GetGlobalSettings().SetAxisSystem(KFbxAxisSystem::eOpenGL); |
|---|
| 448 | |
|---|
| 449 | |
|---|
| 450 | if (!osgDB::makeDirectoryForFile(filename)) { |
|---|
| 451 | osg::notify(osg::NOTICE) << "Can't create directory for file '" << filename << "'. FBX SDK may fail creating the file." << std::endl; |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | #ifdef OSG_USE_UTF8_FILENAME |
|---|
| [11110] | 456 | const std::string& utf8filename(filename); |
|---|
| [11109] | 457 | #else |
|---|
| [11141] | 458 | std::string utf8filename(osgDB::convertStringFromCurrentCodePageToUTF8(filename)); |
|---|
| [11109] | 459 | #endif |
|---|
| 460 | |
|---|
| 461 | if (!lExporter->Initialize(utf8filename.c_str())) |
|---|
| 462 | { |
|---|
| 463 | return std::string(lExporter->GetLastErrorString()); |
|---|
| 464 | } |
|---|
| 465 | if (!lExporter->Export(pScene)) |
|---|
| 466 | { |
|---|
| 467 | return std::string(lExporter->GetLastErrorString()); |
|---|
| 468 | } |
|---|
| 469 | |
|---|
| 470 | return WriteResult::FILE_SAVED; |
|---|
| 471 | } |
|---|
| 472 | catch (const std::string& s) |
|---|
| 473 | { |
|---|
| 474 | return s; |
|---|
| 475 | } |
|---|
| 476 | catch (const char* s) |
|---|
| 477 | { |
|---|
| 478 | return std::string(s); |
|---|
| 479 | } |
|---|
| 480 | catch (...) |
|---|
| 481 | { |
|---|
| 482 | } |
|---|
| 483 | |
|---|
| 484 | return WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 485 | } |
|---|
| 486 | |
|---|
| [10780] | 487 | |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | REGISTER_OSGPLUGIN(fbx, ReaderWriterFBX) |
|---|