You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

342 line
12 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file ColladaParser.h
  34. * @brief Defines the parser helper class for the collada loader
  35. */
  36. #ifndef AI_COLLADAPARSER_H_INC
  37. #define AI_COLLADAPARSER_H_INC
  38. #include "irrXMLWrapper.h"
  39. #include "ColladaHelper.h"
  40. namespace Assimp
  41. {
  42. // ------------------------------------------------------------------------------------------
  43. /** Parser helper class for the Collada loader.
  44. *
  45. * Does all the XML reading and builds internal data structures from it,
  46. * but leaves the resolving of all the references to the loader.
  47. */
  48. class ColladaParser
  49. {
  50. friend class ColladaLoader;
  51. protected:
  52. /** Constructor from XML file */
  53. ColladaParser( IOSystem* pIOHandler, const std::string& pFile);
  54. /** Destructor */
  55. ~ColladaParser();
  56. /** Reads the contents of the file */
  57. void ReadContents();
  58. /** Reads the structure of the file */
  59. void ReadStructure();
  60. /** Reads asset informations such as coordinate system informations and legal blah */
  61. void ReadAssetInfo();
  62. /** Reads the animation library */
  63. void ReadAnimationLibrary();
  64. /** Reads an animation into the given parent structure */
  65. void ReadAnimation( Collada::Animation* pParent);
  66. /** Reads an animation sampler into the given anim channel */
  67. void ReadAnimationSampler( Collada::AnimationChannel& pChannel);
  68. /** Reads the skeleton controller library */
  69. void ReadControllerLibrary();
  70. /** Reads a controller into the given mesh structure */
  71. void ReadController( Collada::Controller& pController);
  72. /** Reads the joint definitions for the given controller */
  73. void ReadControllerJoints( Collada::Controller& pController);
  74. /** Reads the joint weights for the given controller */
  75. void ReadControllerWeights( Collada::Controller& pController);
  76. /** Reads the image library contents */
  77. void ReadImageLibrary();
  78. /** Reads an image entry into the given image */
  79. void ReadImage( Collada::Image& pImage);
  80. /** Reads the material library */
  81. void ReadMaterialLibrary();
  82. /** Reads a material entry into the given material */
  83. void ReadMaterial( Collada::Material& pMaterial);
  84. /** Reads the camera library */
  85. void ReadCameraLibrary();
  86. /** Reads a camera entry into the given camera */
  87. void ReadCamera( Collada::Camera& pCamera);
  88. /** Reads the light library */
  89. void ReadLightLibrary();
  90. /** Reads a light entry into the given light */
  91. void ReadLight( Collada::Light& pLight);
  92. /** Reads the effect library */
  93. void ReadEffectLibrary();
  94. /** Reads an effect entry into the given effect*/
  95. void ReadEffect( Collada::Effect& pEffect);
  96. /** Reads an COMMON effect profile */
  97. void ReadEffectProfileCommon( Collada::Effect& pEffect);
  98. /** Read sampler properties */
  99. void ReadSamplerProperties( Collada::Sampler& pSampler);
  100. /** Reads an effect entry containing a color or a texture defining that color */
  101. void ReadEffectColor( aiColor4D& pColor, Collada::Sampler& pSampler);
  102. /** Reads an effect entry containing a float */
  103. void ReadEffectFloat( float& pFloat);
  104. /** Reads an effect parameter specification of any kind */
  105. void ReadEffectParam( Collada::EffectParam& pParam);
  106. /** Reads the geometry library contents */
  107. void ReadGeometryLibrary();
  108. /** Reads a geometry from the geometry library. */
  109. void ReadGeometry( Collada::Mesh* pMesh);
  110. /** Reads a mesh from the geometry library */
  111. void ReadMesh( Collada::Mesh* pMesh);
  112. /** Reads a source element - a combination of raw data and an accessor defining
  113. * things that should not be redefinable. Yes, that's another rant.
  114. */
  115. void ReadSource();
  116. /** Reads a data array holding a number of elements, and stores it in the global library.
  117. * Currently supported are array of floats and arrays of strings.
  118. */
  119. void ReadDataArray();
  120. /** Reads an accessor and stores it in the global library under the given ID -
  121. * accessors use the ID of the parent <source> element
  122. */
  123. void ReadAccessor( const std::string& pID);
  124. /** Reads input declarations of per-vertex mesh data into the given mesh */
  125. void ReadVertexData( Collada::Mesh* pMesh);
  126. /** Reads input declarations of per-index mesh data into the given mesh */
  127. void ReadIndexData( Collada::Mesh* pMesh);
  128. /** Reads a single input channel element and stores it in the given array, if valid */
  129. void ReadInputChannel( std::vector<Collada::InputChannel>& poChannels);
  130. /** Reads a <p> primitive index list and assembles the mesh data into the given mesh */
  131. void ReadPrimitives( Collada::Mesh* pMesh, std::vector<Collada::InputChannel>& pPerIndexChannels,
  132. size_t pNumPrimitives, const std::vector<size_t>& pVCount, Collada::PrimitiveType pPrimType);
  133. /** Extracts a single object from an input channel and stores it in the appropriate mesh data array */
  134. void ExtractDataObjectFromChannel( const Collada::InputChannel& pInput, size_t pLocalIndex, Collada::Mesh* pMesh);
  135. /** Reads the library of node hierarchies and scene parts */
  136. void ReadSceneLibrary();
  137. /** Reads a scene node's contents including children and stores it in the given node */
  138. void ReadSceneNode( Collada::Node* pNode);
  139. /** Reads a node transformation entry of the given type and adds it to the given node's transformation list. */
  140. void ReadNodeTransformation( Collada::Node* pNode, Collada::TransformType pType);
  141. /** Reads a mesh reference in a node and adds it to the node's mesh list */
  142. void ReadNodeGeometry( Collada::Node* pNode);
  143. /** Reads the collada scene */
  144. void ReadScene();
  145. // Processes bind_vertex_input and bind elements
  146. void ReadMaterialVertexInputBinding( Collada::SemanticMappingTable& tbl);
  147. protected:
  148. /** Aborts the file reading with an exception */
  149. void ThrowException( const std::string& pError) const;
  150. /** Skips all data until the end node of the current element */
  151. void SkipElement();
  152. /** Skips all data until the end node of the given element */
  153. void SkipElement( const char* pElement);
  154. /** Compares the current xml element name to the given string and returns true if equal */
  155. bool IsElement( const char* pName) const;
  156. /** Tests for the opening tag of the given element, throws an exception if not found */
  157. void TestOpening( const char* pName);
  158. /** Tests for the closing tag of the given element, throws an exception if not found */
  159. void TestClosing( const char* pName);
  160. /** Checks the present element for the presence of the attribute, returns its index
  161. or throws an exception if not found */
  162. int GetAttribute( const char* pAttr) const;
  163. /** Returns the index of the named attribute or -1 if not found. Does not throw,
  164. therefore useful for optional attributes */
  165. int TestAttribute( const char* pAttr) const;
  166. /** Reads the text contents of an element, throws an exception if not given.
  167. Skips leading whitespace. */
  168. const char* GetTextContent();
  169. /** Reads the text contents of an element, returns NULL if not given.
  170. Skips leading whitespace. */
  171. const char* TestTextContent();
  172. /** Reads a single bool from current text content */
  173. bool ReadBoolFromTextContent();
  174. /** Reads a single float from current text content */
  175. float ReadFloatFromTextContent();
  176. /** Calculates the resulting transformation from all the given transform steps */
  177. aiMatrix4x4 CalculateResultTransform( const std::vector<Collada::Transform>& pTransforms) const;
  178. /** Determines the input data type for the given semantic string */
  179. Collada::InputType GetTypeForSemantic( const std::string& pSemantic);
  180. /** Finds the item in the given library by its reference, throws if not found */
  181. template <typename Type> const Type& ResolveLibraryReference(
  182. const std::map<std::string, Type>& pLibrary, const std::string& pURL) const;
  183. protected:
  184. /** Filename, for a verbose error message */
  185. std::string mFileName;
  186. /** XML reader, member for everyday use */
  187. irr::io::IrrXMLReader* mReader;
  188. /** All data arrays found in the file by ID. Might be referred to by actually
  189. everyone. Collada, you are a steaming pile of indirection. */
  190. typedef std::map<std::string, Collada::Data> DataLibrary;
  191. DataLibrary mDataLibrary;
  192. /** Same for accessors which define how the data in a data array is accessed. */
  193. typedef std::map<std::string, Collada::Accessor> AccessorLibrary;
  194. AccessorLibrary mAccessorLibrary;
  195. /** Mesh library: mesh by ID */
  196. typedef std::map<std::string, Collada::Mesh*> MeshLibrary;
  197. MeshLibrary mMeshLibrary;
  198. /** node library: root node of the hierarchy part by ID */
  199. typedef std::map<std::string, Collada::Node*> NodeLibrary;
  200. NodeLibrary mNodeLibrary;
  201. /** Image library: stores texture properties by ID */
  202. typedef std::map<std::string, Collada::Image> ImageLibrary;
  203. ImageLibrary mImageLibrary;
  204. /** Effect library: surface attributes by ID */
  205. typedef std::map<std::string, Collada::Effect> EffectLibrary;
  206. EffectLibrary mEffectLibrary;
  207. /** Material library: surface material by ID */
  208. typedef std::map<std::string, Collada::Material> MaterialLibrary;
  209. MaterialLibrary mMaterialLibrary;
  210. /** Light library: surface light by ID */
  211. typedef std::map<std::string, Collada::Light> LightLibrary;
  212. LightLibrary mLightLibrary;
  213. /** Camera library: surface material by ID */
  214. typedef std::map<std::string, Collada::Camera> CameraLibrary;
  215. CameraLibrary mCameraLibrary;
  216. /** Controller library: joint controllers by ID */
  217. typedef std::map<std::string, Collada::Controller> ControllerLibrary;
  218. ControllerLibrary mControllerLibrary;
  219. /** Pointer to the root node. Don't delete, it just points to one of
  220. the nodes in the node library. */
  221. Collada::Node* mRootNode;
  222. /** Root animation container */
  223. Collada::Animation mAnims;
  224. /** Size unit: how large compared to a meter */
  225. float mUnitSize;
  226. /** Which is the up vector */
  227. enum { UP_X, UP_Y, UP_Z } mUpDirection;
  228. /** Collada file format version */
  229. Collada::FormatVersion mFormat;
  230. };
  231. // ------------------------------------------------------------------------------------------------
  232. // Check for element match
  233. inline bool ColladaParser::IsElement( const char* pName) const
  234. {
  235. ai_assert( mReader->getNodeType() == irr::io::EXN_ELEMENT);
  236. return ::strcmp( mReader->getNodeName(), pName) == 0;
  237. }
  238. // ------------------------------------------------------------------------------------------------
  239. // Finds the item in the given library by its reference, throws if not found
  240. template <typename Type>
  241. const Type& ColladaParser::ResolveLibraryReference( const std::map<std::string, Type>& pLibrary, const std::string& pURL) const
  242. {
  243. typename std::map<std::string, Type>::const_iterator it = pLibrary.find( pURL);
  244. if( it == pLibrary.end())
  245. ThrowException( boost::str( boost::format( "Unable to resolve library reference \"%s\".") % pURL));
  246. return it->second;
  247. }
  248. } // end of namespace Assimp
  249. #endif // AI_COLLADAPARSER_H_INC