From 8f7d2028169e2418d5214c523e5e03cd9030e4b9 Mon Sep 17 00:00:00 2001 From: tangziwen Date: Mon, 20 Oct 2014 19:22:00 +0800 Subject: [PATCH 1/3] fix animation3D ID bug --- cocos/3d/CCBundle3D.cpp | 191 +++++++++++++++++++++++----------------- cocos/3d/CCBundle3D.h | 4 +- 2 files changed, 110 insertions(+), 85 deletions(-) diff --git a/cocos/3d/CCBundle3D.cpp b/cocos/3d/CCBundle3D.cpp index 427802b40d..becad6f188 100644 --- a/cocos/3d/CCBundle3D.cpp +++ b/cocos/3d/CCBundle3D.cpp @@ -354,11 +354,11 @@ bool Bundle3D::loadAnimationData(const std::string& id, Animation3DData* animati if (_isBinary) { - return loadAnimationDataBinary(animationdata); + return loadAnimationDataBinary(id,animationdata); } else { - return loadAnimationDataJson(animationdata); + return loadAnimationDataJson(id,animationdata); } } @@ -1189,7 +1189,7 @@ bool Bundle3D::loadMaterialDataJson_0_2(MaterialDatas& materialdatas) return true; } -bool Bundle3D::loadAnimationDataJson(Animation3DData* animationdata) +bool Bundle3D::loadAnimationDataJson(const std::string& id, Animation3DData* animationdata) { std::string anim = ""; if (_version == "1.2" || _version == "0.2") @@ -1198,11 +1198,26 @@ bool Bundle3D::loadAnimationDataJson(Animation3DData* animationdata) anim = ANIMATIONS; if (!_jsonReader.HasMember(anim.c_str())) return false; - + int the_index = -1; const rapidjson::Value& animation_data_array = _jsonReader[anim.c_str()]; + if (animation_data_array.Size()==0) return false; - const rapidjson::Value& animation_data_array_val_0 = animation_data_array[(rapidjson::SizeType)0]; + if(!id.empty()) + { + for(int i=0 ;i_totalTime = animation_data_array_val_0[LENGTH].GetDouble(); @@ -1609,7 +1624,7 @@ bool Bundle3D::loadMaterialDataBinary(MaterialData* materialdata) return true; } -bool Bundle3D::loadAnimationDataBinary(Animation3DData* animationdata) +bool Bundle3D::loadAnimationDataBinary(const std::string& id, Animation3DData* animationdata) { if (!seekToFirstType(BUNDLE_TYPE_ANIMATIONS)) return false; @@ -1622,106 +1637,116 @@ bool Bundle3D::loadAnimationDataBinary(Animation3DData* animationdata) return false; } } - std::string id = _binaryReader.readString(); + for( int i = 0; i < animNum ; i++ ) + { + animationdata->resetData(); + std::string animId = _binaryReader.readString(); - if (!_binaryReader.read(&animationdata->_totalTime)) - { - CCLOG("warning: Failed to read AnimationData: totalTime '%s'.", _path.c_str()); - return false; - } - - unsigned int nodeAnimationNum; - if (!_binaryReader.read(&nodeAnimationNum)) - { - CCLOG("warning: Failed to read AnimationData: animNum '%s'.", _path.c_str()); - return false; - } - for (unsigned int i = 0; i < nodeAnimationNum; ++i) - { - std::string boneName = _binaryReader.readString(); - unsigned int keyframeNum; - if (!_binaryReader.read(&keyframeNum)) + if (!_binaryReader.read(&animationdata->_totalTime)) { - CCLOG("warning: Failed to read AnimationData: keyframeNum '%s'.", _path.c_str()); + CCLOG("warning: Failed to read AnimationData: totalTime '%s'.", _path.c_str()); return false; } - - animationdata->_rotationKeys[boneName].reserve(keyframeNum); - animationdata->_scaleKeys[boneName].reserve(keyframeNum); - animationdata->_translationKeys[boneName].reserve(keyframeNum); - for (unsigned int j = 0; j < keyframeNum; ++j) + unsigned int nodeAnimationNum; + if (!_binaryReader.read(&nodeAnimationNum)) { - float keytime; - if (!_binaryReader.read(&keytime)) + CCLOG("warning: Failed to read AnimationData: animNum '%s'.", _path.c_str()); + return false; + } + for (unsigned int i = 0; i < nodeAnimationNum; ++i) + { + std::string boneName = _binaryReader.readString(); + unsigned int keyframeNum; + if (!_binaryReader.read(&keyframeNum)) { - CCLOG("warning: Failed to read AnimationData: keytime '%s'.", _path.c_str()); + CCLOG("warning: Failed to read AnimationData: keyframeNum '%s'.", _path.c_str()); return false; } - // transform flag - unsigned char transformFlag(0); - if (_version == "0.4") + animationdata->_rotationKeys[boneName].reserve(keyframeNum); + animationdata->_scaleKeys[boneName].reserve(keyframeNum); + animationdata->_translationKeys[boneName].reserve(keyframeNum); + + for (unsigned int j = 0; j < keyframeNum; ++j) { - if (!_binaryReader.read(&transformFlag)) + float keytime; + if (!_binaryReader.read(&keytime)) { - CCLOG("warning: Failed to read AnimationData: transformFlag '%s'.", _path.c_str()); + CCLOG("warning: Failed to read AnimationData: keytime '%s'.", _path.c_str()); return false; } - } - - // rotation - bool hasRotate = true; - if (_version == "0.4") - hasRotate = transformFlag & 0x01; - - if (hasRotate) - { - Quaternion rotate; - if (_binaryReader.read(&rotate, 4, 4) != 4) + + // transform flag + unsigned char transformFlag(0); + if (_version == "0.4") { - CCLOG("warning: Failed to read AnimationData: rotate '%s'.", _path.c_str()); - return false; + if (!_binaryReader.read(&transformFlag)) + { + CCLOG("warning: Failed to read AnimationData: transformFlag '%s'.", _path.c_str()); + return false; + } + } + + // rotation + bool hasRotate = true; + if (_version == "0.4") + hasRotate = transformFlag & 0x01; + + if (hasRotate) + { + Quaternion rotate; + if (_binaryReader.read(&rotate, 4, 4) != 4) + { + CCLOG("warning: Failed to read AnimationData: rotate '%s'.", _path.c_str()); + return false; + } + animationdata->_rotationKeys[boneName].push_back(Animation3DData::QuatKey(keytime, rotate)); + } + + // scale + bool hasScale = true; + if (_version == "0.4") + hasScale = (transformFlag >> 1) & 0x01; + + if (hasScale) + { + Vec3 scale; + if (_binaryReader.read(&scale, 4, 3) != 3) + { + CCLOG("warning: Failed to read AnimationData: scale '%s'.", _path.c_str()); + return false; + } + animationdata->_scaleKeys[boneName].push_back(Animation3DData::Vec3Key(keytime, scale)); + } + + // translation + bool hasTranslation = true; + if (_version == "0.4") + hasTranslation = (transformFlag >> 2) & 0x01; + + if (hasTranslation) + { + Vec3 position; + if (_binaryReader.read(&position, 4, 3) != 3) + { + CCLOG("warning: Failed to read AnimationData: position '%s'.", _path.c_str()); + return false; + } + animationdata->_translationKeys[boneName].push_back(Animation3DData::Vec3Key(keytime, position)); } - animationdata->_rotationKeys[boneName].push_back(Animation3DData::QuatKey(keytime, rotate)); } - // scale - bool hasScale = true; - if (_version == "0.4") - hasScale = (transformFlag >> 1) & 0x01; - - if (hasScale) - { - Vec3 scale; - if (_binaryReader.read(&scale, 4, 3) != 3) - { - CCLOG("warning: Failed to read AnimationData: scale '%s'.", _path.c_str()); - return false; - } - animationdata->_scaleKeys[boneName].push_back(Animation3DData::Vec3Key(keytime, scale)); - } - - // translation - bool hasTranslation = true; - if (_version == "0.4") - hasTranslation = (transformFlag >> 2) & 0x01; - - if (hasTranslation) - { - Vec3 position; - if (_binaryReader.read(&position, 4, 3) != 3) - { - CCLOG("warning: Failed to read AnimationData: position '%s'.", _path.c_str()); - return false; - } - animationdata->_translationKeys[boneName].push_back(Animation3DData::Vec3Key(keytime, position)); - } } + if( id == animId || id.empty()) + { + break; + } } return true; } + bool Bundle3D::loadNodesJson(NodeDatas& nodedatas) { if (!_jsonReader.HasMember(NODES)) return false; diff --git a/cocos/3d/CCBundle3D.h b/cocos/3d/CCBundle3D.h index 7512cf8b3a..6f1c1a8bee 100644 --- a/cocos/3d/CCBundle3D.h +++ b/cocos/3d/CCBundle3D.h @@ -114,7 +114,7 @@ protected: bool loadMaterialDataJson(MaterialData* materialdata){return true;} bool loadMaterialDataJson_0_1(MaterialData* materialdata){return true;} bool loadMaterialDataJson_0_2(MaterialData* materialdata){return true;} - bool loadAnimationDataJson(Animation3DData* animationdata); + bool loadAnimationDataJson(const std::string& id,Animation3DData* animationdata); /** * load data in binary * @param path The c3b file path @@ -145,7 +145,7 @@ protected: * load animation data in binary * @param animationdata The animation data pointer */ - bool loadAnimationDataBinary(Animation3DData* animationdata); + bool loadAnimationDataBinary(const std::string& id,Animation3DData* animationdata); bool checkIsBone(const std::string& name); From 61a6fda1c08f3b359781f3b92a65dff7c0539417 Mon Sep 17 00:00:00 2001 From: tangziwen Date: Tue, 21 Oct 2014 11:43:28 +0800 Subject: [PATCH 2/3] fix some bugs when loading Animation3D from binary file --- cocos/3d/CCBundle3D.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cocos/3d/CCBundle3D.cpp b/cocos/3d/CCBundle3D.cpp index becad6f188..f99c8df4a6 100644 --- a/cocos/3d/CCBundle3D.cpp +++ b/cocos/3d/CCBundle3D.cpp @@ -1637,7 +1637,12 @@ bool Bundle3D::loadAnimationDataBinary(const std::string& id, Animation3DData* a return false; } } - for( int i = 0; i < animNum ; i++ ) + else + { + animNum = 1; + } + bool has_found =false; + for( int k = 0; k < animNum ; k++ ) { animationdata->resetData(); std::string animId = _binaryReader.readString(); @@ -1740,9 +1745,14 @@ bool Bundle3D::loadAnimationDataBinary(const std::string& id, Animation3DData* a } if( id == animId || id.empty()) { + has_found = true; break; } } + if(!id.empty() && !has_found) + { + return false; + } return true; } From 25409b441aae17e5d4aa2d0d82bc12606c40a608 Mon Sep 17 00:00:00 2001 From: yangxiao Date: Tue, 21 Oct 2014 11:59:03 +0800 Subject: [PATCH 3/3] adjust code --- cocos/3d/CCBundle3D.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/cocos/3d/CCBundle3D.cpp b/cocos/3d/CCBundle3D.cpp index f99c8df4a6..a80eb35819 100644 --- a/cocos/3d/CCBundle3D.cpp +++ b/cocos/3d/CCBundle3D.cpp @@ -1628,7 +1628,7 @@ bool Bundle3D::loadAnimationDataBinary(const std::string& id, Animation3DData* a { if (!seekToFirstType(BUNDLE_TYPE_ANIMATIONS)) return false; - unsigned int animNum=0; + unsigned int animNum = 1; if( _version == "0.3"|| _version == "0.4") { if (!_binaryReader.read(&animNum)) @@ -1637,12 +1637,9 @@ bool Bundle3D::loadAnimationDataBinary(const std::string& id, Animation3DData* a return false; } } - else - { - animNum = 1; - } + bool has_found =false; - for( int k = 0; k < animNum ; k++ ) + for(unsigned int k = 0; k < animNum ; k++ ) { animationdata->resetData(); std::string animId = _binaryReader.readString(); @@ -1749,8 +1746,9 @@ bool Bundle3D::loadAnimationDataBinary(const std::string& id, Animation3DData* a break; } } - if(!id.empty() && !has_found) + if(!has_found) { + animationdata->resetData(); return false; } return true;