This commit is contained in:
songchengjiang 2014-10-21 14:32:32 +08:00
commit a839cc3ae7
2 changed files with 119 additions and 86 deletions

View File

@ -354,11 +354,11 @@ bool Bundle3D::loadAnimationData(const std::string& id, Animation3DData* animati
if (_isBinary) if (_isBinary)
{ {
return loadAnimationDataBinary(animationdata); return loadAnimationDataBinary(id,animationdata);
} }
else else
{ {
return loadAnimationDataJson(animationdata); return loadAnimationDataJson(id,animationdata);
} }
} }
@ -1189,7 +1189,7 @@ bool Bundle3D::loadMaterialDataJson_0_2(MaterialDatas& materialdatas)
return true; return true;
} }
bool Bundle3D::loadAnimationDataJson(Animation3DData* animationdata) bool Bundle3D::loadAnimationDataJson(const std::string& id, Animation3DData* animationdata)
{ {
std::string anim = ""; std::string anim = "";
if (_version == "1.2" || _version == "0.2") if (_version == "1.2" || _version == "0.2")
@ -1198,11 +1198,26 @@ bool Bundle3D::loadAnimationDataJson(Animation3DData* animationdata)
anim = ANIMATIONS; anim = ANIMATIONS;
if (!_jsonReader.HasMember(anim.c_str())) return false; if (!_jsonReader.HasMember(anim.c_str())) return false;
int the_index = -1;
const rapidjson::Value& animation_data_array = _jsonReader[anim.c_str()]; const rapidjson::Value& animation_data_array = _jsonReader[anim.c_str()];
if (animation_data_array.Size()==0) return false; 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<animation_data_array.Size();i++)
{
if(animation_data_array[i][ID].GetString() ==id )
{
the_index = i;
}
}
if(the_index < 0) return false;
}else{
the_index = 0;
}
const rapidjson::Value& animation_data_array_val_0 = animation_data_array[(rapidjson::SizeType)the_index];
animationdata->_totalTime = animation_data_array_val_0[LENGTH].GetDouble(); animationdata->_totalTime = animation_data_array_val_0[LENGTH].GetDouble();
@ -1609,11 +1624,11 @@ bool Bundle3D::loadMaterialDataBinary(MaterialData* materialdata)
return true; return true;
} }
bool Bundle3D::loadAnimationDataBinary(Animation3DData* animationdata) bool Bundle3D::loadAnimationDataBinary(const std::string& id, Animation3DData* animationdata)
{ {
if (!seekToFirstType(BUNDLE_TYPE_ANIMATIONS)) if (!seekToFirstType(BUNDLE_TYPE_ANIMATIONS))
return false; return false;
unsigned int animNum=0; unsigned int animNum = 1;
if( _version == "0.3"|| _version == "0.4") if( _version == "0.3"|| _version == "0.4")
{ {
if (!_binaryReader.read(&animNum)) if (!_binaryReader.read(&animNum))
@ -1622,106 +1637,124 @@ bool Bundle3D::loadAnimationDataBinary(Animation3DData* animationdata)
return false; return false;
} }
} }
std::string id = _binaryReader.readString();
bool has_found =false;
for(unsigned int k = 0; k < animNum ; k++ )
{
animationdata->resetData();
std::string animId = _binaryReader.readString();
if (!_binaryReader.read(&animationdata->_totalTime)) 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))
{ {
CCLOG("warning: Failed to read AnimationData: keyframeNum '%s'.", _path.c_str()); CCLOG("warning: Failed to read AnimationData: totalTime '%s'.", _path.c_str());
return false; 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; CCLOG("warning: Failed to read AnimationData: animNum '%s'.", _path.c_str());
if (!_binaryReader.read(&keytime)) 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; return false;
} }
// transform flag animationdata->_rotationKeys[boneName].reserve(keyframeNum);
unsigned char transformFlag(0); animationdata->_scaleKeys[boneName].reserve(keyframeNum);
if (_version == "0.4") 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; return false;
} }
}
// transform flag
// rotation unsigned char transformFlag(0);
bool hasRotate = true; if (_version == "0.4")
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()); if (!_binaryReader.read(&transformFlag))
return false; {
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())
{
has_found = true;
break;
}
}
if(!has_found)
{
animationdata->resetData();
return false;
} }
return true; return true;
} }
bool Bundle3D::loadNodesJson(NodeDatas& nodedatas) bool Bundle3D::loadNodesJson(NodeDatas& nodedatas)
{ {
if (!_jsonReader.HasMember(NODES)) return false; if (!_jsonReader.HasMember(NODES)) return false;

View File

@ -114,7 +114,7 @@ protected:
bool loadMaterialDataJson(MaterialData* materialdata){return true;} bool loadMaterialDataJson(MaterialData* materialdata){return true;}
bool loadMaterialDataJson_0_1(MaterialData* materialdata){return true;} bool loadMaterialDataJson_0_1(MaterialData* materialdata){return true;}
bool loadMaterialDataJson_0_2(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 * load data in binary
* @param path The c3b file path * @param path The c3b file path
@ -145,7 +145,7 @@ protected:
* load animation data in binary * load animation data in binary
* @param animationdata The animation data pointer * @param animationdata The animation data pointer
*/ */
bool loadAnimationDataBinary(Animation3DData* animationdata); bool loadAnimationDataBinary(const std::string& id,Animation3DData* animationdata);
bool checkIsBone(const std::string& name); bool checkIsBone(const std::string& name);