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

📄 loader.cpp

📁 Cal3D实现虚拟角色 Cal3D实现虚拟角色
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//****************************************************************************//// loader.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/loader.h"#include "cal3d/error.h"#include "cal3d/matrix.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"#include "cal3d/streamsource.h"#include "cal3d/buffersource.h"using namespace cal3d;int CalLoader::loadingMode; /*****************************************************************************//** Sets optional flags which affect how the model is loaded into memory.  *  * This function sets the loading mode for all future loader calls.  *  * @param flags A boolean OR of any of the following flags  *         \li LOADER_ROTATE_X_AXIS will rotate the mesh 90 degrees about the X axis,  *             which has the effect of swapping Y/Z coordinates.  *         \li LOADER_INVERT_V_COORD will substitute (1-v) for any v texture coordinate  *             to eliminate the need for texture inversion after export.  *  *****************************************************************************/void CalLoader::setLoadingMode(int flags){    loadingMode = flags;} /*****************************************************************************//** Loads a core animation instance.  *  * This function loads a core animation instance from a file.  *  * @param strFilename The file to load the core animation instance from.  *  * @return One of the following values:  *         \li a pointer to the core animation  *         \li \b 0 if an error happened  *****************************************************************************/CalCoreAnimationPtr CalLoader::loadCoreAnimation(const std::string& strFilename, CalCoreSkeleton *skel){  if(strFilename.size()>= 3 && stricmp(strFilename.substr(strFilename.size()-3,3).c_str(),Cal::ANIMATION_XMLFILE_MAGIC)==0)    return loadXmlCoreAnimation(strFilename, skel);  // open the file  std::ifstream file(strFilename.c_str(), std::ios::in | std::ios::binary);  //make sure it was opened properly  if(!file)  {    CalError::setLastError(CalError::FILE_NOT_FOUND, __FILE__, __LINE__, strFilename);    return 0;  }  //make a new stream data source and use it to load the animation  CalStreamSource streamSrc( file );    CalCoreAnimationPtr coreanim = loadCoreAnimation( streamSrc,skel );  if(coreanim) coreanim->setFilename( strFilename );  //close the file  file.close();  return coreanim;} /*****************************************************************************//** Loads a core material instance.  *  * This function loads a core material instance from a file.  *  * @param strFilename The file to load the core material instance from.  *  * @return One of the following values:  *         \li a pointer to the core material  *         \li \b 0 if an error happened  *****************************************************************************/CalCoreMaterialPtr CalLoader::loadCoreMaterial(const std::string& strFilename){  if(strFilename.size()>= 3 && stricmp(strFilename.substr(strFilename.size()-3,3).c_str(),Cal::MATERIAL_XMLFILE_MAGIC)==0)    return loadXmlCoreMaterial(strFilename);  // open the file  std::ifstream file;  file.open(strFilename.c_str(), std::ios::in | std::ios::binary);  // make sure it opened properly  if(!file)  {    CalError::setLastError(CalError::FILE_NOT_FOUND, __FILE__, __LINE__, strFilename);    return 0;  }  //make a new stream data source and use it to load the material  CalStreamSource streamSrc( file );    CalCoreMaterialPtr coremat = loadCoreMaterial( streamSrc );  if(coremat) coremat->setFilename( strFilename );  //close the file  file.close();  return coremat;} /*****************************************************************************//** Loads a core mesh instance.  *  * This function loads a core mesh instance from a file.  *  * @param strFilename The file to load the core mesh instance from.  *  * @return One of the following values:  *         \li a pointer to the core mesh  *         \li \b 0 if an error happened  *****************************************************************************/CalCoreMeshPtr CalLoader::loadCoreMesh(const std::string& strFilename){  if(strFilename.size()>= 3 && stricmp(strFilename.substr(strFilename.size()-3,3).c_str(),Cal::MESH_XMLFILE_MAGIC)==0)    return loadXmlCoreMesh(strFilename);  // open the file  std::ifstream file;  file.open(strFilename.c_str(), std::ios::in | std::ios::binary);  // make sure it opened properly  if(!file)  {    CalError::setLastError(CalError::FILE_NOT_FOUND, __FILE__, __LINE__, strFilename);    return 0;  }  //make a new stream data source and use it to load the mesh  CalStreamSource streamSrc( file );    CalCoreMeshPtr coremesh = loadCoreMesh( streamSrc );  if(coremesh) coremesh->setFilename( strFilename );  //close the file  file.close();  return coremesh;} /*****************************************************************************//** Loads a core skeleton instance.  *  * This function loads a core skeleton instance from a file.  *  * @param strFilename The file to load the core skeleton instance from.  *  * @return One of the following values:  *         \li a pointer to the core skeleton  *         \li \b 0 if an error happened  *****************************************************************************/CalCoreSkeletonPtr CalLoader::loadCoreSkeleton(const std::string& strFilename){  if(strFilename.size()>= 3 && stricmp(strFilename.substr(strFilename.size()-3,3).c_str(),Cal::SKELETON_XMLFILE_MAGIC)==0)    return loadXmlCoreSkeleton(strFilename);  // open the file  std::ifstream file;  file.open(strFilename.c_str(), std::ios::in | std::ios::binary);  //make sure it opened properly  if(!file)  {    CalError::setLastError(CalError::FILE_NOT_FOUND, __FILE__, __LINE__, strFilename);    return 0;  }  //make a new stream data source and use it to load the skeleton  CalStreamSource streamSrc( file );    CalCoreSkeletonPtr coreskeleton = loadCoreSkeleton( streamSrc );  //close the file  file.close();  return coreskeleton;} /*****************************************************************************//** Loads a core animation instance.  *  * This function loads a core animation instance from an input stream.  *  * @param inputStream The stream to load the core animation instance from. This  *                    stream should be initialized and ready to be read from.  *  * @return One of the following values:  *         \li a pointer to the core animation  *         \li \b 0 if an error happened  *****************************************************************************/CalCoreAnimationPtr CalLoader::loadCoreAnimation(std::istream& inputStream, CalCoreSkeleton *skel ){   //Create a new istream data source and pass it on   CalStreamSource streamSrc(inputStream);   return loadCoreAnimation(streamSrc, skel);} /*****************************************************************************//** Loads a core material instance.  *  * This function loads a core material instance from an input stream.  *  * @param inputStream The stream to load the core material instance from. This  *                    stream should be initialized and ready to be read from.  *  * @return One of the following values:  *         \li a pointer to the core material  *         \li \b 0 if an error happened  *****************************************************************************/CalCoreMaterialPtr CalLoader::loadCoreMaterial(std::istream& inputStream){   //Create a new istream data source and pass it on   CalStreamSource streamSrc(inputStream);   return loadCoreMaterial(streamSrc);} /*****************************************************************************//** Loads a core mesh instance.  *  * This function loads a core mesh instance from an input stream.  *  * @param inputStream The stream to load the core mesh instance from. This  *                    stream should be initialized and ready to be read from.  *  * @return One of the following values:  *         \li a pointer to the core mesh  *         \li \b 0 if an error happened  *****************************************************************************/CalCoreMeshPtr CalLoader::loadCoreMesh(std::istream& inputStream){   //Create a new istream data source and pass it on   CalStreamSource streamSrc(inputStream);   return loadCoreMesh(streamSrc);} /*****************************************************************************//** Loads a core skeleton instance.  *  * This function loads a core skeleton instance from an input stream.  *  * @param inputStream The stream to load the core skeleton instance from. This  *                    stream should be initialized and ready to be read from.  *  * @return One of the following values:  *         \li a pointer to the core skeleton  *         \li \b 0 if an error happened  *****************************************************************************/CalCoreSkeletonPtr CalLoader::loadCoreSkeleton(std::istream& inputStream){   //Create a new istream data source and pass it on   CalStreamSource streamSrc(inputStream);   return loadCoreSkeleton(streamSrc);} /*****************************************************************************//** Loads a core animation instance.  *  * This function loads a core animation instance from a memory buffer.  *  * @param inputBuffer The memory buffer to load the core animation instance   *                    from. This buffer should be initialized and ready to   *                    be read from.  *  * @return One of the following values:  *         \li a pointer to the core animation  *         \li \b 0 if an error happened  *****************************************************************************/CalCoreAnimationPtr CalLoader::loadCoreAnimation(void* inputBuffer, CalCoreSkeleton *skel){   //Create a new buffer data source and pass it on   CalBufferSource bufferSrc(inputBuffer);   return loadCoreAnimation(bufferSrc,skel);} /*****************************************************************************//** Loads a core material instance.  *  * This function loads a core material instance from a memory buffer.  *  * @param inputBuffer The memory buffer to load the core material instance   *                    from. This buffer should be initialized and ready to   *                    be read from.  *  * @return One of the following values:  *         \li a pointer to the core material  *         \li \b 0 if an error happened  *****************************************************************************/CalCoreMaterialPtr CalLoader::loadCoreMaterial(void* inputBuffer){   //Create a new buffer data source and pass it on   CalBufferSource bufferSrc(inputBuffer);   return loadCoreMaterial(bufferSrc);} /*****************************************************************************//** Loads a core mesh instance.  *  * This function loads a core mesh instance from a memory buffer.  *  * @param inputBuffer The memory buffer to load the core mesh instance from.   *                    This buffer should be initialized and ready to be   *                    read from.  *  * @return One of the following values:  *         \li a pointer to the core mesh  *         \li \b 0 if an error happened  *****************************************************************************/CalCoreMeshPtr CalLoader::loadCoreMesh(void* inputBuffer){   //Create a new buffer data source and pass it on   CalBufferSource bufferSrc(inputBuffer);   return loadCoreMesh(bufferSrc);} /*****************************************************************************//** Loads a core skeleton instance.  *  * This function loads a core skeleton instance from a memory buffer.  *  * @param inputBuffer The memory buffer to load the core skeleton instance   *                    from. This buffer should be initialized and ready to   *                    be read from.  *  * @return One of the following values:  *         \li a pointer to the core skeleton  *         \li \b 0 if an error happened  *****************************************************************************/CalCoreSkeletonPtr CalLoader::loadCoreSkeleton(void* inputBuffer){   //Create a new buffer data source and pass it on   CalBufferSource bufferSrc(inputBuffer);   return loadCoreSkeleton(bufferSrc);} /*****************************************************************************//** Loads a core animation instance.  *  * This function loads a core animation instance from a data source.  *  * @param dataSrc The data source to load the core animation instance from.  *  * @return One of the following values:  *         \li a pointer to the core animation  *         \li \b 0 if an error happened  *****************************************************************************/CalCoreAnimationPtr CalLoader::loadCoreAnimation(CalDataSource& dataSrc, CalCoreSkeleton *skel){  // check if this is a valid file  char magic[4];  if(!dataSrc.readBytes(&magic[0], 4) || (memcmp(&magic[0], Cal::ANIMATION_FILE_MAGIC, 4) != 0))  {    CalError::setLastError(CalError::INVALID_FILE_FORMAT, __FILE__, __LINE__);    return 0;  }  // check if the version is compatible with the library  int version;  if(!dataSrc.readInteger(version) || (version < Cal::EARLIEST_COMPATIBLE_FILE_VERSION) || (version > Cal::CURRENT_FILE_VERSION))  {    CalError::setLastError(CalError::INCOMPATIBLE_FILE_VERSION, __FILE__, __LINE__);    return 0;  }  // allocate a new core animation instance  CalCoreAnimationPtr pCoreAnimation(new CalCoreAnimation);  if(!pCoreAnimation)  {    CalError::setLastError(CalError::MEMORY_ALLOCATION_FAILED, __FILE__, __LINE__);    return 0;  }  // get the duration of the core animation  float duration;  if(!dataSrc.readFloat(duration))  {    CalError::setLastError(CalError::INVALID_FILE_FORMAT, __FILE__, __LINE__);    return 0;  }  // check for a valid duration  if(duration <= 0.0f)  {    CalError::setLastError(CalError::INVALID_ANIMATION_DURATION, __FILE__, __LINE__);    return 0;  }

⌨️ 快捷键说明

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