| 1 | #include <cassert> |
|---|
| 2 | #include <sstream> |
|---|
| 3 | |
|---|
| 4 | #include <osg/BlendFunc> |
|---|
| 5 | #include <osg/Geode> |
|---|
| 6 | #include <osg/Image> |
|---|
| 7 | #include <osg/MatrixTransform> |
|---|
| 8 | |
|---|
| 9 | #include <osgUtil/TriStripVisitor> |
|---|
| 10 | |
|---|
| 11 | #include <osgDB/ReadFile> |
|---|
| 12 | |
|---|
| 13 | #include <osgAnimation/RigGeometry> |
|---|
| 14 | #include <osgAnimation/MorphGeometry> |
|---|
| 15 | #include <osgAnimation/BasicAnimationManager> |
|---|
| 16 | |
|---|
| 17 | #if defined(_MSC_VER) |
|---|
| 18 | #pragma warning( disable : 4505 ) |
|---|
| 19 | #endif |
|---|
| 20 | #include <fbxsdk.h> |
|---|
| 21 | |
|---|
| 22 | #include "fbxRMesh.h" |
|---|
| 23 | #include "fbxRNode.h" |
|---|
| 24 | |
|---|
| 25 | enum GeometryType |
|---|
| 26 | { |
|---|
| 27 | GEOMETRY_STATIC, |
|---|
| 28 | GEOMETRY_RIG, |
|---|
| 29 | GEOMETRY_MORPH |
|---|
| 30 | }; |
|---|
| 31 | |
|---|
| 32 | osg::Vec3 convertVec3(const KFbxVector4& v) |
|---|
| 33 | { |
|---|
| 34 | return osg::Vec3( |
|---|
| 35 | static_cast<float>(v[0]), |
|---|
| 36 | static_cast<float>(v[1]), |
|---|
| 37 | static_cast<float>(v[2])); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | osg::Vec2 convertVec2(const KFbxVector2& v) |
|---|
| 41 | { |
|---|
| 42 | return osg::Vec2( |
|---|
| 43 | static_cast<float>(v[0]), |
|---|
| 44 | static_cast<float>(v[1])); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | osg::Vec4 convertColor(const KFbxColor& color) |
|---|
| 48 | { |
|---|
| 49 | return osg::Vec4( |
|---|
| 50 | static_cast<float>(color.mRed), |
|---|
| 51 | static_cast<float>(color.mGreen), |
|---|
| 52 | static_cast<float>(color.mBlue), |
|---|
| 53 | static_cast<float>(color.mAlpha)); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | template <typename T> |
|---|
| 57 | bool layerElementValid(const KFbxLayerElementTemplate<T>* pLayerElement) |
|---|
| 58 | { |
|---|
| 59 | if (!pLayerElement) |
|---|
| 60 | return false; |
|---|
| 61 | |
|---|
| 62 | switch (pLayerElement->GetMappingMode()) |
|---|
| 63 | { |
|---|
| 64 | case KFbxLayerElement::eBY_CONTROL_POINT: |
|---|
| 65 | case KFbxLayerElement::eBY_POLYGON_VERTEX: |
|---|
| 66 | case KFbxLayerElement::eBY_POLYGON: |
|---|
| 67 | break; |
|---|
| 68 | default: |
|---|
| 69 | return false; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | switch (pLayerElement->GetReferenceMode()) |
|---|
| 73 | { |
|---|
| 74 | case KFbxLayerElement::eDIRECT: |
|---|
| 75 | case KFbxLayerElement::eINDEX_TO_DIRECT: |
|---|
| 76 | return true; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | return false; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | template <typename T> |
|---|
| 83 | int getVertexIndex(const KFbxLayerElementTemplate<T>* pLayerElement, |
|---|
| 84 | KFbxMesh* fbxMesh, |
|---|
| 85 | int nPolygon, int nPolyVertex, int nMeshVertex) |
|---|
| 86 | { |
|---|
| 87 | int index = 0; |
|---|
| 88 | |
|---|
| 89 | switch (pLayerElement->GetMappingMode()) |
|---|
| 90 | { |
|---|
| 91 | case KFbxLayerElement::eBY_CONTROL_POINT: |
|---|
| 92 | index = fbxMesh->GetPolygonVertex(nPolygon, nPolyVertex); |
|---|
| 93 | break; |
|---|
| 94 | case KFbxLayerElement::eBY_POLYGON_VERTEX: |
|---|
| 95 | index = nMeshVertex; |
|---|
| 96 | break; |
|---|
| 97 | case KFbxLayerElement::eBY_POLYGON: |
|---|
| 98 | index = nPolygon; |
|---|
| 99 | break; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | if (pLayerElement->GetReferenceMode() == KFbxLayerElement::eDIRECT) |
|---|
| 103 | { |
|---|
| 104 | return index; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | return pLayerElement->GetIndexArray().GetAt(index); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | template <typename T> |
|---|
| 111 | int getPolygonIndex(const KFbxLayerElementTemplate<T>* pLayerElement, int nPolygon) |
|---|
| 112 | { |
|---|
| 113 | if (pLayerElement && |
|---|
| 114 | pLayerElement->GetMappingMode() == KFbxLayerElement::eBY_POLYGON) |
|---|
| 115 | { |
|---|
| 116 | switch (pLayerElement->GetReferenceMode()) |
|---|
| 117 | { |
|---|
| 118 | case KFbxLayerElement::eDIRECT: |
|---|
| 119 | return nPolygon; |
|---|
| 120 | case KFbxLayerElement::eINDEX_TO_DIRECT: |
|---|
| 121 | return pLayerElement->GetIndexArray().GetAt(nPolygon); |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | return 0; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | template <typename FbxT> |
|---|
| 129 | FbxT getElement(const KFbxLayerElementTemplate<FbxT>* pLayerElement, |
|---|
| 130 | KFbxMesh* fbxMesh, |
|---|
| 131 | int nPolygon, int nPolyVertex, int nMeshVertex) |
|---|
| 132 | { |
|---|
| 133 | return pLayerElement->GetDirectArray().GetAt(getVertexIndex( |
|---|
| 134 | pLayerElement, fbxMesh, nPolygon, nPolyVertex, nMeshVertex)); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | typedef std::map<unsigned, osg::ref_ptr<osg::Geometry> > GeometryMap; |
|---|
| 138 | |
|---|
| 139 | osg::Geometry* getGeometry(osg::Geode* pGeode, GeometryMap& geometryMap, |
|---|
| 140 | std::vector<StateSetContent>& stateSetList, |
|---|
| 141 | GeometryType gt, unsigned int mti, bool bNormal, bool bTexCoord, bool bColor) |
|---|
| 142 | { |
|---|
| 143 | GeometryMap::iterator it = geometryMap.find(mti); |
|---|
| 144 | |
|---|
| 145 | if (it != geometryMap.end()) |
|---|
| 146 | { |
|---|
| 147 | return it->second.get(); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | osg::ref_ptr<osg::Geometry> pGeometry; |
|---|
| 151 | if (gt == GEOMETRY_MORPH) |
|---|
| 152 | { |
|---|
| 153 | pGeometry = new osgAnimation::MorphGeometry; |
|---|
| 154 | } |
|---|
| 155 | else |
|---|
| 156 | { |
|---|
| 157 | pGeometry = new osg::Geometry; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | pGeometry->setVertexData(osg::Geometry::ArrayData(new osg::Vec3Array, osg::Geometry::BIND_PER_VERTEX)); |
|---|
| 161 | if (bNormal) pGeometry->setNormalData(osg::Geometry::ArrayData(new osg::Vec3Array, osg::Geometry::BIND_PER_VERTEX)); |
|---|
| 162 | if (bTexCoord) pGeometry->setTexCoordData(0, osg::Geometry::ArrayData(new osg::Vec2Array, osg::Geometry::BIND_PER_VERTEX)); |
|---|
| 163 | if (bColor) pGeometry->setColorData(osg::Geometry::ArrayData(new osg::Vec4Array, osg::Geometry::BIND_PER_VERTEX)); |
|---|
| 164 | |
|---|
| 165 | if (mti < stateSetList.size()) |
|---|
| 166 | { |
|---|
| 167 | bool transparent = false; |
|---|
| 168 | const StateSetContent& ss = stateSetList[mti]; |
|---|
| 169 | if (osg::Material* pMaterial = ss.first) |
|---|
| 170 | { |
|---|
| 171 | pGeometry->getOrCreateStateSet()->setAttributeAndModes(pMaterial); |
|---|
| 172 | transparent = pMaterial->getDiffuse(osg::Material::FRONT).w() < 1.0f; |
|---|
| 173 | } |
|---|
| 174 | if (osg::Texture2D* pTexture = ss.second) |
|---|
| 175 | { |
|---|
| 176 | pGeometry->getOrCreateStateSet()->setTextureAttributeAndModes(0, pTexture); |
|---|
| 177 | if (!transparent && pTexture->getImage()) |
|---|
| 178 | { |
|---|
| 179 | transparent = pTexture->getImage()->isImageTranslucent(); |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | if (transparent) |
|---|
| 184 | { |
|---|
| 185 | pGeometry->getOrCreateStateSet()->setAttributeAndModes(new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | geometryMap.insert(std::pair<unsigned, osg::ref_ptr<osg::Geometry> >(mti, pGeometry)); |
|---|
| 190 | pGeode->addDrawable(pGeometry.get()); |
|---|
| 191 | |
|---|
| 192 | return pGeometry.get(); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | osgAnimation::VertexInfluence& getVertexInfluence( |
|---|
| 196 | osgAnimation::VertexInfluenceMap& vim, const std::string& name) |
|---|
| 197 | { |
|---|
| 198 | osgAnimation::VertexInfluenceMap::iterator it = vim.lower_bound(name); |
|---|
| 199 | if (it == vim.end() || name != it->first) |
|---|
| 200 | { |
|---|
| 201 | it = vim.insert(it, osgAnimation::VertexInfluenceMap::value_type( |
|---|
| 202 | name, osgAnimation::VertexInfluence())); |
|---|
| 203 | it->second.setName(name); |
|---|
| 204 | } |
|---|
| 205 | return it->second; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | void addChannel( |
|---|
| 209 | osgAnimation::Channel* pChannel, |
|---|
| 210 | osg::ref_ptr<osgAnimation::AnimationManagerBase>& pAnimManager, |
|---|
| 211 | const char* pTakeName) |
|---|
| 212 | { |
|---|
| 213 | if (!pChannel) |
|---|
| 214 | { |
|---|
| 215 | return; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | if (!pAnimManager) pAnimManager = new osgAnimation::BasicAnimationManager; |
|---|
| 219 | |
|---|
| 220 | osgAnimation::Animation* pAnimation = 0; |
|---|
| 221 | const osgAnimation::AnimationList& anims = pAnimManager->getAnimationList(); |
|---|
| 222 | for (size_t i = 0; i < anims.size(); ++i) |
|---|
| 223 | { |
|---|
| 224 | if (anims[i]->getName() == pTakeName) |
|---|
| 225 | { |
|---|
| 226 | pAnimation = anims[i].get(); |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | if (!pAnimation) |
|---|
| 231 | { |
|---|
| 232 | pAnimation = new osgAnimation::Animation; |
|---|
| 233 | pAnimation->setName(pTakeName); |
|---|
| 234 | pAnimManager->registerAnimation(pAnimation); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | pAnimation->addChannel(pChannel); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | void readAnimation(KFbxNode* pNode, const std::string& targetName, |
|---|
| 241 | osg::ref_ptr<osgAnimation::AnimationManagerBase>& pAnimationManager, |
|---|
| 242 | KFbxMesh* pMesh, int nShape) |
|---|
| 243 | { |
|---|
| 244 | for (int i = 1; i < pNode->GetTakeNodeCount(); ++i) |
|---|
| 245 | { |
|---|
| 246 | const char* pTakeName = pNode->GetTakeNodeName(i); |
|---|
| 247 | |
|---|
| 248 | KFCurve* pCurve = pMesh->GetShapeChannel(nShape, false, pTakeName); |
|---|
| 249 | if (!pCurve) |
|---|
| 250 | { |
|---|
| 251 | continue; |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | int nKeys = pCurve->KeyGetCount(); |
|---|
| 255 | if (!nKeys) |
|---|
| 256 | { |
|---|
| 257 | continue; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | osgAnimation::FloatLinearChannel* pChannel = new osgAnimation::FloatLinearChannel; |
|---|
| 261 | std::vector<osgAnimation::TemplateKeyframe<float> >& keyFrameCntr = *pChannel->getOrCreateSampler()->getOrCreateKeyframeContainer(); |
|---|
| 262 | |
|---|
| 263 | for (int k = 0; k < nKeys; ++k) |
|---|
| 264 | { |
|---|
| 265 | KFCurveKey key = pCurve->KeyGet(k); |
|---|
| 266 | float fTime = static_cast<float>(key.GetTime().GetSecondDouble()); |
|---|
| 267 | float fValue = static_cast<float>(key.GetValue() * 0.01); |
|---|
| 268 | keyFrameCntr.push_back(osgAnimation::FloatKeyframe(fTime,fValue)); |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | pChannel->setTargetName(targetName); |
|---|
| 272 | std::stringstream ss; |
|---|
| 273 | ss << nShape; |
|---|
| 274 | pChannel->setName(ss.str()); |
|---|
| 275 | addChannel(pChannel, pAnimationManager, pTakeName); |
|---|
| 276 | } |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | void addBindMatrix( |
|---|
| 280 | BindMatrixMap& boneBindMatrices, |
|---|
| 281 | KFbxNode* pBone, |
|---|
| 282 | const osg::Matrix& bindMatrix, |
|---|
| 283 | osgAnimation::RigGeometry* pRigGeometry) |
|---|
| 284 | { |
|---|
| 285 | boneBindMatrices.insert(BindMatrixMap::value_type( |
|---|
| 286 | BindMatrixMap::key_type(pBone, pRigGeometry), bindMatrix)); |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | osgDB::ReaderWriter::ReadResult readMesh(KFbxSdkManager& pSdkManager, |
|---|
| 290 | KFbxNode* pNode, KFbxMesh* fbxMesh, |
|---|
| 291 | osg::ref_ptr<osgAnimation::AnimationManagerBase>& pAnimationManager, |
|---|
| 292 | std::vector<StateSetContent>& stateSetList, |
|---|
| 293 | const char* szName, |
|---|
| 294 | BindMatrixMap& boneBindMatrices, |
|---|
| 295 | const std::set<const KFbxNode*>& fbxSkeletons, |
|---|
| 296 | std::map<KFbxNode*, osgAnimation::Skeleton*>& skeletonMap) |
|---|
| 297 | { |
|---|
| 298 | GeometryMap geometryMap; |
|---|
| 299 | |
|---|
| 300 | osg::Geode* pGeode = new osg::Geode; |
|---|
| 301 | pGeode->setName(szName); |
|---|
| 302 | |
|---|
| 303 | const KFbxLayer* pFbxLayer = fbxMesh->GetLayer(0); |
|---|
| 304 | const KFbxLayerElementNormal* pFbxNormals = 0; |
|---|
| 305 | const KFbxLayerElementUV* pFbxUVs = 0; |
|---|
| 306 | const KFbxLayerElementVertexColor* pFbxColors = 0; |
|---|
| 307 | const KFbxLayerElementMaterial* pFbxMaterials = 0; |
|---|
| 308 | |
|---|
| 309 | const KFbxVector4* pFbxVertices = fbxMesh->GetControlPoints(); |
|---|
| 310 | |
|---|
| 311 | if (pFbxLayer) |
|---|
| 312 | { |
|---|
| 313 | pFbxNormals = pFbxLayer->GetNormals(); |
|---|
| 314 | pFbxColors = pFbxLayer->GetVertexColors(); |
|---|
| 315 | pFbxUVs = pFbxLayer->GetUVs(); |
|---|
| 316 | pFbxMaterials = pFbxLayer->GetMaterials(); |
|---|
| 317 | |
|---|
| 318 | if (!layerElementValid(pFbxNormals)) pFbxNormals = 0; |
|---|
| 319 | if (!layerElementValid(pFbxColors)) pFbxColors = 0; |
|---|
| 320 | if (!layerElementValid(pFbxUVs)) pFbxUVs = 0; |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | int nPolys = fbxMesh->GetPolygonCount(); |
|---|
| 324 | |
|---|
| 325 | int nDeformerCount = fbxMesh->GetDeformerCount(KFbxDeformer::eSKIN); |
|---|
| 326 | int nMorphShapeCount = 0; |
|---|
| 327 | |
|---|
| 328 | GeometryType geomType = GEOMETRY_STATIC; |
|---|
| 329 | |
|---|
| 330 | //determine the type of geometry |
|---|
| 331 | if (nDeformerCount) |
|---|
| 332 | { |
|---|
| 333 | geomType = GEOMETRY_RIG; |
|---|
| 334 | } |
|---|
| 335 | else if (nMorphShapeCount = fbxMesh->GetShapeCount()) |
|---|
| 336 | { |
|---|
| 337 | geomType = GEOMETRY_MORPH; |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | typedef std::pair<osg::Geometry*, int> GIPair; |
|---|
| 341 | typedef std::multimap<int, GIPair> FbxToOsgVertexMap; |
|---|
| 342 | typedef std::map<GIPair, int> OsgToFbxNormalMap; |
|---|
| 343 | FbxToOsgVertexMap fbxToOsgVertMap; |
|---|
| 344 | OsgToFbxNormalMap osgToFbxNormMap; |
|---|
| 345 | |
|---|
| 346 | for (int i = 0, nVertex = 0; i < nPolys; ++i) |
|---|
| 347 | { |
|---|
| 348 | int lPolygonSize = fbxMesh->GetPolygonSize(i); |
|---|
| 349 | |
|---|
| 350 | int materialIndex = getPolygonIndex(pFbxMaterials, i); |
|---|
| 351 | |
|---|
| 352 | osg::Geometry* pGeometry = getGeometry(pGeode, geometryMap, |
|---|
| 353 | stateSetList, geomType, materialIndex, |
|---|
| 354 | pFbxNormals != 0, pFbxUVs != 0, pFbxColors != 0); |
|---|
| 355 | |
|---|
| 356 | osg::Vec3Array* pVertices = static_cast<osg::Vec3Array*>( |
|---|
| 357 | pGeometry->getVertexArray()); |
|---|
| 358 | osg::Vec3Array* pNormals = static_cast<osg::Vec3Array*>( |
|---|
| 359 | pGeometry->getNormalArray()); |
|---|
| 360 | osg::Vec2Array* pTexCoords = static_cast<osg::Vec2Array*>( |
|---|
| 361 | pGeometry->getTexCoordArray(0)); |
|---|
| 362 | osg::Vec4Array* pColors = static_cast<osg::Vec4Array*>( |
|---|
| 363 | pGeometry->getColorArray()); |
|---|
| 364 | |
|---|
| 365 | int nVertex0 = nVertex; |
|---|
| 366 | nVertex += (std::min)(2, lPolygonSize); |
|---|
| 367 | |
|---|
| 368 | //convert polygon to triangles |
|---|
| 369 | for (int j = 2; j < lPolygonSize; ++j, ++nVertex) |
|---|
| 370 | { |
|---|
| 371 | int v0 = fbxMesh->GetPolygonVertex(i, 0), |
|---|
| 372 | v1 = fbxMesh->GetPolygonVertex(i, j - 1), |
|---|
| 373 | v2 = fbxMesh->GetPolygonVertex(i, j); |
|---|
| 374 | |
|---|
| 375 | fbxToOsgVertMap.insert(FbxToOsgVertexMap::value_type(v0, GIPair(pGeometry, pVertices->size()))); |
|---|
| 376 | fbxToOsgVertMap.insert(FbxToOsgVertexMap::value_type(v1, GIPair(pGeometry, pVertices->size() + 1))); |
|---|
| 377 | fbxToOsgVertMap.insert(FbxToOsgVertexMap::value_type(v2, GIPair(pGeometry, pVertices->size() + 2))); |
|---|
| 378 | |
|---|
| 379 | pVertices->push_back(convertVec3(pFbxVertices[v0])); |
|---|
| 380 | pVertices->push_back(convertVec3(pFbxVertices[v1])); |
|---|
| 381 | pVertices->push_back(convertVec3(pFbxVertices[v2])); |
|---|
| 382 | |
|---|
| 383 | if (pNormals) |
|---|
| 384 | { |
|---|
| 385 | int n0 = getVertexIndex(pFbxNormals, fbxMesh, i, 0, nVertex0); |
|---|
| 386 | int n1 = getVertexIndex(pFbxNormals, fbxMesh, i, j - 1, nVertex - 1); |
|---|
| 387 | int n2 = getVertexIndex(pFbxNormals, fbxMesh, i, j, nVertex); |
|---|
| 388 | |
|---|
| 389 | osgToFbxNormMap.insert(OsgToFbxNormalMap::value_type(GIPair(pGeometry, pNormals->size()), n0)); |
|---|
| 390 | osgToFbxNormMap.insert(OsgToFbxNormalMap::value_type(GIPair(pGeometry, pNormals->size() + 1), n1)); |
|---|
| 391 | osgToFbxNormMap.insert(OsgToFbxNormalMap::value_type(GIPair(pGeometry, pNormals->size() + 2), n2)); |
|---|
| 392 | |
|---|
| 393 | pNormals->push_back(convertVec3(pFbxNormals->GetDirectArray().GetAt(n0))); |
|---|
| 394 | pNormals->push_back(convertVec3(pFbxNormals->GetDirectArray().GetAt(n1))); |
|---|
| 395 | pNormals->push_back(convertVec3(pFbxNormals->GetDirectArray().GetAt(n2))); |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | if (pTexCoords) |
|---|
| 399 | { |
|---|
| 400 | pTexCoords->push_back(convertVec2(getElement(pFbxUVs, fbxMesh, i, 0, nVertex0))); |
|---|
| 401 | pTexCoords->push_back(convertVec2(getElement(pFbxUVs, fbxMesh, i, j - 1, nVertex - 1))); |
|---|
| 402 | pTexCoords->push_back(convertVec2(getElement(pFbxUVs, fbxMesh, i, j, nVertex))); |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | if (pColors) |
|---|
| 406 | { |
|---|
| 407 | pColors->push_back(convertColor(getElement(pFbxColors, fbxMesh, i, 0, nVertex0))); |
|---|
| 408 | pColors->push_back(convertColor(getElement(pFbxColors, fbxMesh, i, j - 1, nVertex - 1))); |
|---|
| 409 | pColors->push_back(convertColor(getElement(pFbxColors, fbxMesh, i, j, nVertex))); |
|---|
| 410 | } |
|---|
| 411 | } |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | for (int i = 0; i < pGeode->getNumDrawables(); ++i) |
|---|
| 415 | { |
|---|
| 416 | osg::Geometry* pGeometry = pGeode->getDrawable(i)->asGeometry(); |
|---|
| 417 | if (pGeode->getNumDrawables() > 1) |
|---|
| 418 | { |
|---|
| 419 | std::stringstream ss; |
|---|
| 420 | ss << pGeode->getName() << " " << i + 1; |
|---|
| 421 | pGeometry->setName(ss.str()); |
|---|
| 422 | } |
|---|
| 423 | else |
|---|
| 424 | { |
|---|
| 425 | pGeometry->setName(pGeode->getName()); |
|---|
| 426 | } |
|---|
| 427 | |
|---|
| 428 | osg::DrawArrays* pDrawArrays = new osg::DrawArrays( |
|---|
| 429 | GL_TRIANGLES, 0, pGeometry->getVertexArray()->getNumElements()); |
|---|
| 430 | pGeometry->addPrimitiveSet(pDrawArrays); |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | if (geomType == GEOMETRY_RIG) |
|---|
| 434 | { |
|---|
| 435 | typedef std::map<osg::ref_ptr<osg::Geometry>, |
|---|
| 436 | osg::ref_ptr<osgAnimation::RigGeometry> > GeometryRigGeometryMap; |
|---|
| 437 | GeometryRigGeometryMap old2newGeometryMap; |
|---|
| 438 | |
|---|
| 439 | for (int i = 0; i < pGeode->getNumDrawables(); ++i) |
|---|
| 440 | { |
|---|
| 441 | osg::Geometry* pGeometry = pGeode->getDrawable(i)->asGeometry(); |
|---|
| 442 | |
|---|
| 443 | osgAnimation::RigGeometry* pRig = new osgAnimation::RigGeometry; |
|---|
| 444 | pRig->setSourceGeometry(pGeometry); |
|---|
| 445 | pRig->copyFrom(*pGeometry); |
|---|
| 446 | old2newGeometryMap.insert(GeometryRigGeometryMap::value_type( |
|---|
| 447 | pGeometry, pRig)); |
|---|
| 448 | pRig->setDataVariance(osg::Object::DYNAMIC); |
|---|
| 449 | pRig->setUseDisplayList( false ); |
|---|
| 450 | pGeode->setDrawable(i, pRig); |
|---|
| 451 | |
|---|
| 452 | pRig->setInfluenceMap(new osgAnimation::VertexInfluenceMap); |
|---|
| 453 | pGeometry = pRig; |
|---|
| 454 | } |
|---|
| 455 | |
|---|
| 456 | for (int i = 0; i < nDeformerCount; ++i) |
|---|
| 457 | { |
|---|
| 458 | KFbxSkin* pSkin = (KFbxSkin*)fbxMesh->GetDeformer(i, KFbxDeformer::eSKIN); |
|---|
| 459 | int nClusters = pSkin->GetClusterCount(); |
|---|
| 460 | for (int j = 0; j < nClusters; ++j) |
|---|
| 461 | { |
|---|
| 462 | KFbxCluster* pCluster = (KFbxCluster*)pSkin->GetCluster(j); |
|---|
| 463 | //assert(KFbxCluster::eNORMALIZE == pCluster->GetLinkMode()); |
|---|
| 464 | KFbxNode* pBone = pCluster->GetLink(); |
|---|
| 465 | |
|---|
| 466 | KFbxXMatrix transformLink; |
|---|
| 467 | pCluster->GetTransformLinkMatrix(transformLink); |
|---|
| 468 | KFbxXMatrix transformLinkInverse = transformLink.Inverse(); |
|---|
| 469 | const double* pTransformLinkInverse = transformLinkInverse; |
|---|
| 470 | osg::Matrix bindMatrix(pTransformLinkInverse); |
|---|
| 471 | |
|---|
| 472 | int nIndices = pCluster->GetControlPointIndicesCount(); |
|---|
| 473 | int* pIndices = pCluster->GetControlPointIndices(); |
|---|
| 474 | double* pWeights = pCluster->GetControlPointWeights(); |
|---|
| 475 | |
|---|
| 476 | for (int k = 0; k < nIndices; ++k) |
|---|
| 477 | { |
|---|
| 478 | int fbxIndex = pIndices[k]; |
|---|
| 479 | float weight = static_cast<float>(pWeights[k]); |
|---|
| 480 | |
|---|
| 481 | for (FbxToOsgVertexMap::const_iterator it = |
|---|
| 482 | fbxToOsgVertMap.find(fbxIndex); |
|---|
| 483 | it != fbxToOsgVertMap.end() && |
|---|
| 484 | it->first == fbxIndex; ++it) |
|---|
| 485 | { |
|---|
| 486 | GIPair gi = it->second; |
|---|
| 487 | osgAnimation::RigGeometry& rig = |
|---|
| 488 | dynamic_cast<osgAnimation::RigGeometry&>( |
|---|
| 489 | *old2newGeometryMap[gi.first]); |
|---|
| 490 | addBindMatrix(boneBindMatrices, pBone, bindMatrix, &rig); |
|---|
| 491 | osgAnimation::VertexInfluenceMap& vim = |
|---|
| 492 | *rig.getInfluenceMap(); |
|---|
| 493 | osgAnimation::VertexInfluence& vi = |
|---|
| 494 | getVertexInfluence(vim, pBone->GetName()); |
|---|
| 495 | vi.push_back(osgAnimation::VertexIndexWeight( |
|---|
| 496 | gi.second, weight)); |
|---|
| 497 | } |
|---|
| 498 | } |
|---|
| 499 | } |
|---|
| 500 | } |
|---|
| 501 | } |
|---|
| 502 | else if (geomType == GEOMETRY_MORPH) |
|---|
| 503 | { |
|---|
| 504 | for (int i = 0; i < pGeode->getNumDrawables(); ++i) |
|---|
| 505 | { |
|---|
| 506 | osg::Geometry* pGeometry = pGeode->getDrawable(i)->asGeometry(); |
|---|
| 507 | |
|---|
| 508 | osgAnimation::MorphGeometry& morph = dynamic_cast<osgAnimation::MorphGeometry&>(*pGeometry); |
|---|
| 509 | |
|---|
| 510 | pGeode->addUpdateCallback(new osgAnimation::UpdateMorph(morph.getName())); |
|---|
| 511 | |
|---|
| 512 | //read morph geometry |
|---|
| 513 | for (int j = 0; j < nMorphShapeCount; ++j) |
|---|
| 514 | { |
|---|
| 515 | const KFbxGeometryBase* pMorphShape = fbxMesh->GetShape(i); |
|---|
| 516 | |
|---|
| 517 | const KFbxLayerElementNormal* pFbxShapeNormals = 0; |
|---|
| 518 | if (const KFbxLayer* pFbxShapeLayer = pMorphShape->GetLayer(0)) |
|---|
| 519 | { |
|---|
| 520 | pFbxShapeNormals = pFbxShapeLayer->GetNormals(); |
|---|
| 521 | if (!layerElementValid(pFbxShapeNormals)) pFbxShapeNormals = 0; |
|---|
| 522 | } |
|---|
| 523 | |
|---|
| 524 | osg::Geometry* pMorphTarget = new osg::Geometry(morph); |
|---|
| 525 | pMorphTarget->setVertexArray(static_cast<osg::Array*>( |
|---|
| 526 | pMorphTarget->getVertexArray()->clone(osg::CopyOp::DEEP_COPY_ARRAYS))); |
|---|
| 527 | if (pFbxShapeNormals) |
|---|
| 528 | { |
|---|
| 529 | if (osg::Array* pNormals = pMorphTarget->getNormalArray()) |
|---|
| 530 | { |
|---|
| 531 | pMorphTarget->setNormalArray(static_cast<osg::Array*>( |
|---|
| 532 | pNormals->clone(osg::CopyOp::DEEP_COPY_ARRAYS))); |
|---|
| 533 | } |
|---|
| 534 | } |
|---|
| 535 | pMorphTarget->setName(fbxMesh->GetShapeName(j)); |
|---|
| 536 | morph.addMorphTarget(pMorphTarget, 0.0f); |
|---|
| 537 | |
|---|
| 538 | readAnimation(pNode, morph.getName(), pAnimationManager, fbxMesh, j); |
|---|
| 539 | } |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | for (int i = 0; i < nMorphShapeCount; ++i) |
|---|
| 543 | { |
|---|
| 544 | const KFbxGeometryBase* pMorphShape = fbxMesh->GetShape(i); |
|---|
| 545 | |
|---|
| 546 | const KFbxLayerElementNormal* pFbxShapeNormals = 0; |
|---|
| 547 | if (const KFbxLayer* pFbxShapeLayer = pMorphShape->GetLayer(0)) |
|---|
| 548 | { |
|---|
| 549 | pFbxShapeNormals = pFbxShapeLayer->GetNormals(); |
|---|
| 550 | if (!layerElementValid(pFbxShapeNormals)) pFbxShapeNormals = 0; |
|---|
| 551 | } |
|---|
| 552 | |
|---|
| 553 | const KFbxVector4* pControlPoints = pMorphShape->GetControlPoints(); |
|---|
| 554 | int nControlPoints = pMorphShape->GetControlPointsCount(); |
|---|
| 555 | for (int fbxIndex = 0; fbxIndex < nControlPoints; ++fbxIndex) |
|---|
| 556 | { |
|---|
| 557 | osg::Vec3 vPos = convertVec3(pControlPoints[fbxIndex]); |
|---|
| 558 | for (FbxToOsgVertexMap::const_iterator it = |
|---|
| 559 | fbxToOsgVertMap.find(fbxIndex); |
|---|
| 560 | it != fbxToOsgVertMap.end() && |
|---|
| 561 | it->first == fbxIndex; ++it) |
|---|
| 562 | { |
|---|
| 563 | GIPair gi = it->second; |
|---|
| 564 | osgAnimation::MorphGeometry& morphGeom = |
|---|
| 565 | dynamic_cast<osgAnimation::MorphGeometry&>(*gi.first); |
|---|
| 566 | osg::Geometry* pGeometry = morphGeom.getMorphTarget(i).getGeometry(); |
|---|
| 567 | osg::Vec3Array* pVertices = static_cast<osg::Vec3Array*>(pGeometry->getVertexArray()); |
|---|
| 568 | (*pVertices)[gi.second] = vPos; |
|---|
| 569 | |
|---|
| 570 | if (pFbxShapeNormals) |
|---|
| 571 | { |
|---|
| 572 | if (osg::Vec3Array* pNormals = static_cast<osg::Vec3Array*>(pGeometry->getNormalArray())) |
|---|
| 573 | { |
|---|
| 574 | (*pNormals)[gi.second] = convertVec3( |
|---|
| 575 | pFbxShapeNormals->GetDirectArray().GetAt(osgToFbxNormMap[gi])); |
|---|
| 576 | } |
|---|
| 577 | } |
|---|
| 578 | } |
|---|
| 579 | } |
|---|
| 580 | } |
|---|
| 581 | } |
|---|
| 582 | |
|---|
| 583 | KFbxXMatrix fbxGeometricTransform; |
|---|
| 584 | fbxGeometricTransform.SetTRS( |
|---|
| 585 | pNode->GeometricTranslation.Get(), |
|---|
| 586 | pNode->GeometricRotation.Get(), |
|---|
| 587 | pNode->GeometricScaling.Get()); |
|---|
| 588 | const double* pGeometricMat = fbxGeometricTransform; |
|---|
| 589 | osg::Matrix osgGeometricTransform(pGeometricMat); |
|---|
| 590 | |
|---|
| 591 | if (geomType == GEOMETRY_RIG) |
|---|
| 592 | { |
|---|
| 593 | KFbxSkin* pSkin = (KFbxSkin*)fbxMesh->GetDeformer(0, KFbxDeformer::eSKIN); |
|---|
| 594 | if (pSkin->GetClusterCount()) |
|---|
| 595 | { |
|---|
| 596 | KFbxXMatrix fbxTransformMatrix; |
|---|
| 597 | pSkin->GetCluster(0)->GetTransformMatrix(fbxTransformMatrix); |
|---|
| 598 | const double* pTransformMatrix = fbxTransformMatrix; |
|---|
| 599 | osgGeometricTransform.postMult(osg::Matrix(pTransformMatrix)); |
|---|
| 600 | } |
|---|
| 601 | } |
|---|
| 602 | |
|---|
| 603 | osg::Node* pResult = pGeode; |
|---|
| 604 | |
|---|
| 605 | if (!osgGeometricTransform.isIdentity()) |
|---|
| 606 | { |
|---|
| 607 | osg::MatrixTransform* pMatTrans = new osg::MatrixTransform(osgGeometricTransform); |
|---|
| 608 | pMatTrans->addChild(pGeode); |
|---|
| 609 | pResult = pMatTrans; |
|---|
| 610 | } |
|---|
| 611 | |
|---|
| 612 | if (geomType == GEOMETRY_RIG) |
|---|
| 613 | { |
|---|
| 614 | //Add the geometry to the skeleton ancestor of one of the bones. |
|---|
| 615 | KFbxSkin* pSkin = (KFbxSkin*)fbxMesh->GetDeformer(0, KFbxDeformer::eSKIN); |
|---|
| 616 | if (pSkin->GetClusterCount()) |
|---|
| 617 | { |
|---|
| 618 | osgAnimation::Skeleton* pSkeleton = getSkeleton( |
|---|
| 619 | pSkin->GetCluster(0)->GetLink(), fbxSkeletons, skeletonMap); |
|---|
| 620 | pSkeleton->addChild(pResult); |
|---|
| 621 | return osgDB::ReaderWriter::ReadResult::FILE_LOADED; |
|---|
| 622 | } |
|---|
| 623 | } |
|---|
| 624 | |
|---|
| 625 | return osgDB::ReaderWriter::ReadResult(pResult); |
|---|
| 626 | } |
|---|
| 627 | |
|---|
| 628 | osgDB::ReaderWriter::ReadResult readFbxMesh(KFbxSdkManager& pSdkManager, |
|---|
| 629 | KFbxNode* pNode, |
|---|
| 630 | osg::ref_ptr<osgAnimation::AnimationManagerBase>& pAnimationManager, |
|---|
| 631 | std::vector<StateSetContent>& stateSetList, |
|---|
| 632 | BindMatrixMap& boneBindMatrices, |
|---|
| 633 | const std::set<const KFbxNode*>& fbxSkeletons, |
|---|
| 634 | std::map<KFbxNode*, osgAnimation::Skeleton*>& skeletonMap) |
|---|
| 635 | { |
|---|
| 636 | KFbxMesh* lMesh = dynamic_cast<KFbxMesh*>(pNode->GetNodeAttribute()); |
|---|
| 637 | |
|---|
| 638 | if (!lMesh) |
|---|
| 639 | { |
|---|
| 640 | return osgDB::ReaderWriter::ReadResult::ERROR_IN_READING_FILE; |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | return readMesh(pSdkManager, pNode, lMesh, pAnimationManager, stateSetList, |
|---|
| 644 | pNode->GetName(), boneBindMatrices, fbxSkeletons, skeletonMap); |
|---|
| 645 | } |
|---|