📄 saver.cpp
字号:
//****************************************************************************//// saver.cpp //// Copyright (C) 2001, 2002 Bruno 'Beosil' Heidelberger ////****************************************************************************//// This library is free software; you can redistribute it and/or modify it //// under the terms of the GNU Lesser General Public License as published by //// the Free Software Foundation; either version 2.1 of the License, or (at //// your option) any later version. ////****************************************************************************//#ifdef HAVE_CONFIG_H#include "config.h"#endif//****************************************************************************//// Includes ////****************************************************************************//#include "cal3d/saver.h"#include "cal3d/error.h"#include "cal3d/vector.h"#include "cal3d/quaternion.h"#include "cal3d/coremodel.h"#include "cal3d/coreskeleton.h"#include "cal3d/corebone.h"#include "cal3d/coreanimation.h"#include "cal3d/coremesh.h"#include "cal3d/coresubmesh.h"#include "cal3d/corematerial.h"#include "cal3d/corekeyframe.h"#include "cal3d/coretrack.h"#include "cal3d/tinyxml.h"using namespace cal3d; /*****************************************************************************//** Saves a core animation instance. * * This function saves a core animation instance to a 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 * be saved. * * @return One of the following values: * \li \b true if successful * \li \b false if an error happend *****************************************************************************/bool CalSaver::saveCoreAnimation(const std::string& strFilename, CalCoreAnimation *pCoreAnimation){ if(strFilename.size()>= 3 && stricmp(strFilename.substr(strFilename.size()-3,3).c_str(),Cal::ANIMATION_XMLFILE_MAGIC)==0) return saveXmlCoreAnimation(strFilename, pCoreAnimation); // 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::ANIMATION_FILE_MAGIC, sizeof(Cal::ANIMATION_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 duration of the core animation if(!CalPlatform::writeFloat(file, pCoreAnimation->getDuration())) { CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename); return false; } // write the number of tracks if(!CalPlatform::writeInteger(file, pCoreAnimation->getTrackCount())) { CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename); return 0; } // get core track list std::list<CalCoreTrack *>& listCoreTrack = pCoreAnimation->getListCoreTrack(); // write all core bones std::list<CalCoreTrack *>::iterator iteratorCoreTrack; for(iteratorCoreTrack = listCoreTrack.begin(); iteratorCoreTrack != listCoreTrack.end(); ++iteratorCoreTrack) { // save core track if(!saveCoreTrack(file, strFilename, *iteratorCoreTrack)) { return false; } } // explicitly close the file file.close(); pCoreAnimation->setFilename(strFilename); return true;} /*****************************************************************************//** Saves a core bone instance. * * This function saves a core bone instance to a file stream. * * @param file The file stream to save the core bone instance to. * @param strFilename The name of the file stream. * @param pCoreBone A pointer to the core bone 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::saveCoreBones(std::ofstream& file, const std::string& strFilename, CalCoreBone *pCoreBone){ if(!file) { CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__, strFilename); return false; } // write the name of the bone if(!CalPlatform::writeString(file, pCoreBone->getName())) { CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename); return false; } // write the translation of the bone const CalVector& translation = pCoreBone->getTranslation(); CalPlatform::writeFloat(file, translation[0]); CalPlatform::writeFloat(file, translation[1]); CalPlatform::writeFloat(file, translation[2]); // write the rotation of the bone const CalQuaternion& rotation = pCoreBone->getRotation(); CalPlatform::writeFloat(file, rotation[0]); CalPlatform::writeFloat(file, rotation[1]); CalPlatform::writeFloat(file, rotation[2]); CalPlatform::writeFloat(file, rotation[3]); // write the translation of the bone const CalVector& translationBoneSpace = pCoreBone->getTranslationBoneSpace(); CalPlatform::writeFloat(file, translationBoneSpace[0]); CalPlatform::writeFloat(file, translationBoneSpace[1]); CalPlatform::writeFloat(file, translationBoneSpace[2]); // write the rotation of the bone const CalQuaternion& rotationBoneSpace = pCoreBone->getRotationBoneSpace(); CalPlatform::writeFloat(file, rotationBoneSpace[0]); CalPlatform::writeFloat(file, rotationBoneSpace[1]); CalPlatform::writeFloat(file, rotationBoneSpace[2]); CalPlatform::writeFloat(file, rotationBoneSpace[3]); // write the parent bone id if(!CalPlatform::writeInteger(file, pCoreBone->getParentId())) { CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename); return false; } // get children list std::list<int>& listChildId = pCoreBone->getListChildId(); // write the number of children if(!CalPlatform::writeInteger(file, listChildId.size())) { CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename); return false; } // write all children ids std::list<int>::iterator iteratorChildId; for(iteratorChildId = listChildId.begin(); iteratorChildId != listChildId.end(); ++iteratorChildId) { // write the child id if(!CalPlatform::writeInteger(file, *iteratorChildId)) { CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename); return false; } } return true;} /*****************************************************************************//** Saves a core keyframe instance. * * This function saves a core keyframe instance to a file stream. * * @param file The file stream to save the core keyframe instance to. * @param strFilename The name of the file stream. * @param pCoreKeyframe A pointer to the core keyframe 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::saveCoreKeyframe(std::ofstream& file, const std::string& strFilename, CalCoreKeyframe *pCoreKeyframe){ if(!file) { CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__, strFilename); return false; } // write the time of the keyframe CalPlatform::writeFloat(file, pCoreKeyframe->getTime()); // write the translation of the keyframe const CalVector& translation = pCoreKeyframe->getTranslation(); CalPlatform::writeFloat(file, translation[0]); CalPlatform::writeFloat(file, translation[1]); CalPlatform::writeFloat(file, translation[2]); // write the rotation of the keyframe const CalQuaternion& rotation = pCoreKeyframe->getRotation(); CalPlatform::writeFloat(file, rotation[0]); CalPlatform::writeFloat(file, rotation[1]); CalPlatform::writeFloat(file, rotation[2]); CalPlatform::writeFloat(file, rotation[3]); // check if an error happend if(!file) { CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename); return false; } return true;} /*****************************************************************************//** Saves a core material instance. * * This function saves a core material instance to a file. * * @param strFilename The name of the file to save the core material instance * to. * @param pCoreMaterial A pointer to the core material 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::saveCoreMaterial(const std::string& strFilename, CalCoreMaterial *pCoreMaterial){ if(strFilename.size()>= 3 && stricmp(strFilename.substr(strFilename.size()-3,3).c_str(),Cal::MATERIAL_XMLFILE_MAGIC)==0) return saveXmlCoreMaterial(strFilename, pCoreMaterial); // 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::MATERIAL_FILE_MAGIC, sizeof(Cal::MATERIAL_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 ambient color CalCoreMaterial::Color ambientColor; ambientColor = pCoreMaterial->getAmbientColor(); CalPlatform::writeBytes(file, &ambientColor, sizeof(ambientColor)); // write the diffuse color CalCoreMaterial::Color diffusetColor; diffusetColor = pCoreMaterial->getDiffuseColor(); CalPlatform::writeBytes(file, &diffusetColor, sizeof(diffusetColor)); // write the specular color CalCoreMaterial::Color specularColor; specularColor = pCoreMaterial->getSpecularColor(); CalPlatform::writeBytes(file, &specularColor, sizeof(specularColor)); // write the shininess factor CalPlatform::writeFloat(file, pCoreMaterial->getShininess()); // check if an error happend if(!file) { CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename); return false; } // get the map vector std::vector<CalCoreMaterial::Map>& vectorMap = pCoreMaterial->getVectorMap(); // write the number of maps if(!CalPlatform::writeInteger(file, vectorMap.size())) { CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename); return false; } // write all maps int mapId; for(mapId = 0; mapId < (int)vectorMap.size(); ++mapId) { CalCoreMaterial::Map& map = vectorMap[mapId]; // write the filename of the map if(!CalPlatform::writeString(file, map.strFilename)) { CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename); return false; } } // explicitly close the file file.close(); pCoreMaterial->setFilename(strFilename); return true;} /*****************************************************************************//** Saves a core mesh instance. * * This function saves a core mesh instance to a file. * * @param strFilename The name of the file to save the core mesh instance to. * @param pCoreMesh A pointer to the core mesh 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::saveCoreMesh(const std::string& strFilename, CalCoreMesh *pCoreMesh){ if(strFilename.size()>= 3 && stricmp(strFilename.substr(strFilename.size()-3,3).c_str(),Cal::MESH_XMLFILE_MAGIC)==0) return saveXmlCoreMesh(strFilename, pCoreMesh); // 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::MESH_FILE_MAGIC, sizeof(Cal::MESH_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; } // get the submesh vector std::vector<CalCoreSubmesh *>& vectorCoreSubmesh = pCoreMesh->getVectorCoreSubmesh(); // write the number of submeshes if(!CalPlatform::writeInteger(file, vectorCoreSubmesh.size())) { CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename); return false; } // write all core submeshes int submeshId; for(submeshId = 0; submeshId < (int)vectorCoreSubmesh.size(); ++submeshId) { // write the core submesh if(!saveCoreSubmesh(file, strFilename, vectorCoreSubmesh[submeshId])) { return false; } } // explicitly close the file file.close(); pCoreMesh->setFilename(strFilename); return true;} /*****************************************************************************//** Saves a core skeleton instance. * * This function saves a core skeleton instance to a file. * * @param strFilename The name of the file to save the core skeleton instance * to.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -