⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 saver.cpp

📁 Cal3D实现虚拟角色 Cal3D实现虚拟角色
💻 CPP
📖 第 1 页 / 共 3 页
字号:
  * @param pCoreSkeleton A pointer to the core skeleton instance that should be  *                      saved.  *  * @return One of the following values:  *         \li \b true if successful  *         \li \b false if an error happend  *****************************************************************************/bool CalSaver::saveCoreSkeleton(const std::string& strFilename, CalCoreSkeleton *pCoreSkeleton){  if(strFilename.size()>= 3 && stricmp(strFilename.substr(strFilename.size()-3,3).c_str(),Cal::SKELETON_XMLFILE_MAGIC)==0)    return saveXmlCoreSkeleton(strFilename, pCoreSkeleton);	  // open the file  std::ofstream file;  file.open(strFilename.c_str(), std::ios::out | std::ios::binary);  if(!file)  {    CalError::setLastError(CalError::FILE_CREATION_FAILED, __FILE__, __LINE__, strFilename);    return false;  }  // write magic tag  if(!CalPlatform::writeBytes(file, &Cal::SKELETON_FILE_MAGIC, sizeof(Cal::SKELETON_FILE_MAGIC)))  {    CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename);    return false;  }  // write version info  if(!CalPlatform::writeInteger(file, Cal::CURRENT_FILE_VERSION))  {    CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename);    return false;  }  // write the number of bones  if(!CalPlatform::writeInteger(file, pCoreSkeleton->getVectorCoreBone().size()))  {    CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename);    return false;  }  // write all core bones  int boneId;  for(boneId = 0; boneId < (int)pCoreSkeleton->getVectorCoreBone().size(); ++boneId)  {    // write the core bone    if(!saveCoreBones(file, strFilename, pCoreSkeleton->getCoreBone(boneId)))    {      return false;    }  }  // explicitly close the file  file.close();  return true;} /*****************************************************************************//** Saves a core submesh instance.  *  * This function saves a core submesh instance to a file stream.  *  * @param file The file stream to save the core submesh instance to.  * @param strFilename The name of the file stream.  * @param pCoreSubmesh A pointer to the core submesh instance that should be  *                     saved.  *  * @return One of the following values:  *         \li \b true if successful  *         \li \b false if an error happend  *****************************************************************************/bool CalSaver::saveCoreSubmesh(std::ofstream& file, const std::string& strFilename, CalCoreSubmesh *pCoreSubmesh){  if(!file)  {    CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__, strFilename);    return false;  }  // write the core material thread id  if(!CalPlatform::writeInteger(file, pCoreSubmesh->getCoreMaterialThreadId()))  {    CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename);    return false;  }  // get the vertex, face, physical property and spring vector  std::vector<CalCoreSubmesh::Vertex>& vectorVertex = pCoreSubmesh->getVectorVertex();  std::vector<CalCoreSubmesh::Face>& vectorFace = pCoreSubmesh->getVectorFace();  std::vector<CalCoreSubmesh::PhysicalProperty>& vectorPhysicalProperty = pCoreSubmesh->getVectorPhysicalProperty();  std::vector<CalCoreSubmesh::Spring>& vectorSpring = pCoreSubmesh->getVectorSpring();  // write the number of vertices, faces, level-of-details and springs  CalPlatform::writeInteger(file, vectorVertex.size());  CalPlatform::writeInteger(file, vectorFace.size());  CalPlatform::writeInteger(file, pCoreSubmesh->getLodCount());  CalPlatform::writeInteger(file, pCoreSubmesh->getSpringCount());  // get the texture coordinate vector vector  std::vector<std::vector<CalCoreSubmesh::TextureCoordinate> >& vectorvectorTextureCoordinate = pCoreSubmesh->getVectorVectorTextureCoordinate();  // write the number of texture coordinates per vertex  CalPlatform::writeInteger(file, vectorvectorTextureCoordinate.size());  // check if an error happend  if(!file)  {    CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename);    return false;  }  // write all vertices  int vertexId;  for(vertexId = 0; vertexId < (int)vectorVertex.size(); ++vertexId)  {    CalCoreSubmesh::Vertex& vertex = vectorVertex[vertexId];    // write the vertex data    CalPlatform::writeFloat(file, vertex.position.x);    CalPlatform::writeFloat(file, vertex.position.y);    CalPlatform::writeFloat(file, vertex.position.z);    CalPlatform::writeFloat(file, vertex.normal.x);    CalPlatform::writeFloat(file, vertex.normal.y);    CalPlatform::writeFloat(file, vertex.normal.z);    CalPlatform::writeInteger(file, vertex.collapseId);    CalPlatform::writeInteger(file, vertex.faceCollapseCount);    // write all texture coordinates of this vertex    int textureCoordinateId;    for(textureCoordinateId = 0; textureCoordinateId < (int)vectorvectorTextureCoordinate.size(); ++textureCoordinateId)    {      CalCoreSubmesh::TextureCoordinate& textureCoordinate = vectorvectorTextureCoordinate[textureCoordinateId][vertexId];      // write the influence data      CalPlatform::writeFloat(file, textureCoordinate.u);      CalPlatform::writeFloat(file, textureCoordinate.v);      // check if an error happend      if(!file)      {        CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename);        return false;      }    }    // write the number of influences    if(!CalPlatform::writeInteger(file, vertex.vectorInfluence.size()))    {      CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename);      return false;    }     // write all influences of this vertex    int influenceId;    for(influenceId = 0; influenceId < (int)vertex.vectorInfluence.size(); ++influenceId)    {      CalCoreSubmesh::Influence& influence = vertex.vectorInfluence[influenceId];      // write the influence data      CalPlatform::writeInteger(file, influence.boneId);      CalPlatform::writeFloat(file, influence.weight);      // check if an error happend      if(!file)      {        CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename);        return false;      }    }    // save the physical property of the vertex if there are springs in the core submesh    if(pCoreSubmesh->getSpringCount() > 0)    {      // write the physical property of this vertex if there are springs in the core submesh      CalCoreSubmesh::PhysicalProperty& physicalProperty = vectorPhysicalProperty[vertexId];      // write the physical property data      CalPlatform::writeFloat(file, physicalProperty.weight);      // check if an error happend      if(!file)      {        CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename);        return false;      }    }  }  // write all springs  int springId;  for(springId = 0; springId < (int)pCoreSubmesh->getSpringCount(); ++springId)  {    CalCoreSubmesh::Spring& spring = vectorSpring[springId];    // write the spring data    CalPlatform::writeInteger(file, spring.vertexId[0]);    CalPlatform::writeInteger(file, spring.vertexId[1]);    CalPlatform::writeFloat(file, spring.springCoefficient);    CalPlatform::writeFloat(file, spring.idleLength);    // check if an error happend    if(!file)    {      CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename);      return false;    }  }  // write all faces  int faceId;  for(faceId = 0; faceId < (int)vectorFace.size(); ++faceId)  {    CalCoreSubmesh::Face& face = vectorFace[faceId];    // write the face data    CalPlatform::writeInteger(file, face.vertexId[0]);    CalPlatform::writeInteger(file, face.vertexId[1]);    CalPlatform::writeInteger(file, face.vertexId[2]);    // check if an error happend    if(!file)    {      CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename);      return false;    }  }  return true;} /*****************************************************************************//** Saves a core track instance.  *  * This function saves a core track instance to a file stream.  *  * @param file The file stream to save the core track instance to.  * @param strFilename The name of the file stream.  * @param pCoreTrack A pointer to the core track instance that should be saved.  *  * @return One of the following values:  *         \li \b true if successful  *         \li \b false if an error happend  *****************************************************************************/bool CalSaver::saveCoreTrack(std::ofstream& file, const std::string& strFilename, CalCoreTrack *pCoreTrack){  if(!file)  {    CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__, strFilename);    return false;  }  // write the bone id  if(!CalPlatform::writeInteger(file, pCoreTrack->getCoreBoneId()))  {    CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename);    return false;  }  // write the number of keyframes  if(!CalPlatform::writeInteger(file, pCoreTrack->getCoreKeyframeCount()))  {    CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename);    return false;  }  // save all core keyframes  for(int i = 0; i < pCoreTrack->getCoreKeyframeCount(); ++i)  {    // save the core keyframe		bool res = saveCoreKeyframe(file, strFilename, pCoreTrack->getCoreKeyframe(i));		if (!res) {      return false;    }  }  return true;} /*****************************************************************************//** Saves a core skeleton instance to a XML file  *  * This function saves a core skeleton instance to a XML file.  *  * @param strFilename The name of the file to save the core skeleton instance  *                    to.  * @param pCoreSkeleton A pointer to the core skeleton instance that should be  *                      saved.  *  * @return One of the following values:  *         \li \b true if successful  *         \li \b false if an error happend  *****************************************************************************/bool CalSaver::saveXmlCoreSkeleton(const std::string& strFilename, CalCoreSkeleton *pCoreSkeleton){  std::stringstream str;  TiXmlDocument doc(strFilename);    TiXmlElement skeleton("SKELETON");  //skeleton.SetAttribute("MAGIC",Cal::SKELETON_XMLFILE_MAGIC);  skeleton.SetAttribute("VERSION",Cal::LIBRARY_VERSION);  skeleton.SetAttribute("NUMBONES",pCoreSkeleton->getVectorCoreBone().size());    int boneId;  for(boneId = 0; boneId < (int)pCoreSkeleton->getVectorCoreBone().size(); ++boneId)  {	  CalCoreBone* pCoreBone=pCoreSkeleton->getCoreBone(boneId);	  TiXmlElement bone("BONE");	  	  bone.SetAttribute("ID",boneId);	  bone.SetAttribute("NAME",pCoreBone->getName());	  bone.SetAttribute("NUMCHILDS",pCoreBone->getListChildId().size());	  TiXmlElement translation("TRANSLATION");	  const CalVector& translationVector = pCoreBone->getTranslation();            str.str("");      str << translationVector.x << " "     	  << translationVector.y << " "	      << translationVector.z;        TiXmlText translationdata(str.str());        translation.InsertEndChild(translationdata);      bone.InsertEndChild(translation);	  TiXmlElement rotation("ROTATION");	  const CalQuaternion& rotationQuad = pCoreBone->getRotation();              str.str("");      str << rotationQuad.x << " "     	  << rotationQuad.y << " "		  << rotationQuad.z << " "	      << rotationQuad.w;        TiXmlText rotationdata(str.str());      rotation.InsertEndChild(rotationdata);      bone.InsertEndChild(rotation);	  TiXmlElement localtranslation("LOCALTRANSLATION");	  const CalVector& localtranslationVector = pCoreBone->getTranslationBoneSpace();            str.str("");      str << localtranslationVector.x << " "     	  << localtranslationVector.y << " "	      << localtranslationVector.z;        TiXmlText localtranslationdata(str.str());       localtranslation.InsertEndChild(localtranslationdata);      bone.InsertEndChild(localtranslation);	  TiXmlElement localrotation("LOCALROTATION");	  const CalQuaternion& localrotationQuad = pCoreBone->getRotationBoneSpace();              str.str("");      str << localrotationQuad.x << " "     	  << localrotationQuad.y << " "		  << localrotationQuad.z << " "	      << localrotationQuad.w;        TiXmlText localrotationdata(str.str());      localrotation.InsertEndChild(localrotationdata);      bone.InsertEndChild(localrotation);	  TiXmlElement parent("PARENTID");	  str.str("");      str << pCoreBone->getParentId();	  TiXmlText parentid(str.str());	  parent.InsertEndChild(parentid);      bone.InsertEndChild(parent);	  // get children list      std::list<int>& listChildId = pCoreBone->getListChildId();	  // write all children ids      std::list<int>::iterator iteratorChildId;      for(iteratorChildId = listChildId.begin(); iteratorChildId != listChildId.end(); ++iteratorChildId)	  {		  TiXmlElement child("CHILDID");		  str.str("");		  //int id=*iteratorChildId;          str << *iteratorChildId;	      TiXmlText childid(str.str());		  child.InsertEndChild(childid);          bone.InsertEndChild(child);	  }	  skeleton.InsertEndChild(bone);  }  doc.InsertEndChild(skeleton);    if(!doc.SaveFile())  {	CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename);    return false;  }  return true;} /*****************************************************************************//** Saves a core animation instance in a XML file.  *  * This function saves a core animation instance to a XML file.  *  * @param strFilename The name of the file to save the core animation instance  *                    to.  * @param pCoreAnimation A pointer to the core animation instance that should

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -