ColladaParser.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /** Defines the parser helper class for the collada loader */
  2. /*
  3. Open Asset Import Library (ASSIMP)
  4. ----------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development Team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the
  9. following conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the ASSIMP team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the ASSIMP Development Team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ----------------------------------------------------------------------
  33. */
  34. #ifndef AI_COLLADAPARSER_H_INC
  35. #define AI_COLLADAPARSER_H_INC
  36. #include "./irrXML/irrXMLWrapper.h"
  37. namespace Assimp
  38. {
  39. /** Parser helper class for the Collada loader. Does all the XML reading and builds internal data structures from it,
  40. * but leaves the resolving of all the references to the loader.
  41. */
  42. class ColladaParser
  43. {
  44. friend class ColladaLoader;
  45. public:
  46. /** Transformation types that can be applied to a node */
  47. enum TransformType
  48. {
  49. TF_LOOKAT,
  50. TF_ROTATE,
  51. TF_TRANSLATE,
  52. TF_SCALE,
  53. TF_SKEW,
  54. TF_MATRIX
  55. };
  56. /** Contains all data for one of the different transformation types */
  57. struct Transform
  58. {
  59. TransformType mType;
  60. float f[16]; ///< Interpretation of data depends on the type of the transformation
  61. };
  62. /** A node in a scene hierarchy */
  63. struct Node
  64. {
  65. std::string mName;
  66. std::string mID;
  67. Node* mParent;
  68. std::vector<Node*> mChildren;
  69. /** Operations in order to calculate the resulting transformation to parent. */
  70. std::vector<Transform> mTransforms;
  71. Node() { mParent = NULL; }
  72. ~Node() { for( std::vector<Node*>::iterator it = mChildren.begin(); it != mChildren.end(); ++it) delete *it; }
  73. };
  74. /** Data source array */
  75. struct Data
  76. {
  77. std::vector<float> mValues;
  78. };
  79. /** Accessor to a data array */
  80. struct Accessor
  81. {
  82. unsigned int mCount; // in number of objects
  83. unsigned int mOffset; // in number of values
  84. unsigned int mStride; // Stride in number of values
  85. std::string mSource; // URL of the source array
  86. };
  87. /** Contains data for a single mesh */
  88. struct Mesh
  89. {
  90. };
  91. protected:
  92. /** Constructor from XML file */
  93. ColladaParser( const std::string& pFile);
  94. /** Destructor */
  95. ~ColladaParser();
  96. /** Reads the contents of the file */
  97. void ReadContents();
  98. /** Reads the structure of the file */
  99. void ReadStructure();
  100. /** Reads asset informations such as coordinate system informations and legal blah */
  101. void ReadAssetInfo();
  102. /** Reads the geometry library contents */
  103. void ReadGeometryLibrary();
  104. /** Reads a mesh from the geometry library */
  105. void ReadGeometry();
  106. /** Reads the library of node hierarchies and scene parts */
  107. void ReadSceneLibrary();
  108. /** Reads a scene node's contents including children and stores it in the given node */
  109. void ReadSceneNode( Node* pNode);
  110. /** Reads a node transformation entry of the given type and adds it to the given node's transformation list. */
  111. void ReadNodeTransformation( Node* pNode, TransformType pType);
  112. /** Reads the collada scene */
  113. void ReadScene();
  114. protected:
  115. /** Aborts the file reading with an exception */
  116. void ThrowException( const std::string& pError) const;
  117. /** Skips all data until the end node of the current element */
  118. void SkipElement();
  119. /** Compares the current xml element name to the given string and returns true if equal */
  120. bool IsElement( const char* pName) const { assert( mReader->getNodeType() == irr::io::EXN_ELEMENT); return strcmp( mReader->getNodeName(), pName) == 0; }
  121. /** Tests for the closing tag of the given element, throws an exception if not found */
  122. void TestClosing( const char* pName);
  123. /** Checks the present element for the presence of the attribute, returns its index or throws an exception if not found */
  124. int GetAttribute( const char* pAttr) const;
  125. /** Returns the index of the named attribute or -1 if not found. Does not throw, therefore useful for optional attributes */
  126. int TestAttribute( const char* pAttr) const;
  127. /** Reads the text contents of an element, throws an exception if not given. Skips leading whitespace. */
  128. const char* GetTextContent();
  129. /** Calculates the resulting transformation fromm all the given transform steps */
  130. aiMatrix4x4 CalculateResultTransform( const std::vector<Transform>& pTransforms) const;
  131. protected:
  132. /** Filename, for a verbose error message */
  133. std::string mFileName;
  134. /** XML reader */
  135. irr::io::IrrXMLReader* mReader;
  136. /** node library: root node of the hierarchy part by ID */
  137. typedef std::map<std::string, Node*> NodeLibrary;
  138. NodeLibrary mNodeLibrary;
  139. /** Pointer to the root node. Don't delete, it just points to one of the nodes in the node library. */
  140. Node* mRootNode;
  141. /** Size unit: how large compared to a meter */
  142. float mUnitSize;
  143. /** Which is the up vector */
  144. enum { UP_X, UP_Y, UP_Z } mUpDirection;
  145. };
  146. } // end of namespace Assimp
  147. #endif // AI_COLLADAPARSER_H_INC