/COLLADASaxFrameworkLoader/include/COLLADASaxFWLSource.h

https://bitbucket.org/ZhangJingGuo/opencollada · C Header · 291 lines · 124 code · 53 blank · 114 comment · 10 complexity · 782a4a1bd5173a47fdcec0cd09f1a6bc MD5 · raw file

  1. /*
  2. Copyright (c) 2008-2009 NetAllied Systems GmbH
  3. This file is part of COLLADASaxFrameworkLoader.
  4. Licensed under the MIT Open Source License,
  5. for details please see LICENSE file or the website
  6. http://www.opensource.org/licenses/mit-license.php
  7. */
  8. #ifndef __COLLADASAXFWL_SOURCE_H__
  9. #define __COLLADASAXFWL_SOURCE_H__
  10. #include "COLLADASaxFWLPrerequisites.h"
  11. #include "COLLADASaxFWLTechniqueCommon.h"
  12. #include "COLLADASaxFWLArrayElement.h"
  13. #include "COLLADASaxFWLInputUnshared.h"
  14. #include "COLLADAFWObject.h"
  15. #include "COLLADAFWArray.h"
  16. #include "COLLADAFWMeshPrimitive.h"
  17. namespace COLLADASaxFWL
  18. {
  19. /**
  20. * Base source class for hold source element pointers in an array.
  21. */
  22. class SourceBase
  23. {
  24. public:
  25. enum DataType
  26. {
  27. DATA_TYPE_INT,
  28. DATA_TYPE_LONG64,
  29. DATA_TYPE_FLOAT,
  30. DATA_TYPE_DOUBLE,
  31. #ifdef COLLADASAXFWL_REAL_IS_FLOAT
  32. DATA_TYPE_REAL = DATA_TYPE_FLOAT,
  33. #else
  34. DATA_TYPE_REAL = DATA_TYPE_DOUBLE,
  35. #endif
  36. DATA_TYPE_BOOL,
  37. DATA_TYPE_NAME,
  38. DATA_TYPE_IDREF,
  39. DATA_TYPE_INTERPOLATIONTYPE,
  40. DATA_TYPE_UNKNOWN
  41. };
  42. /** Describes one parameter in a COLLADA accessor.*/
  43. struct AccessorParameter
  44. {
  45. String name;
  46. String type;
  47. bool operator==( const SourceBase::AccessorParameter& rhs) const
  48. {
  49. return ( name == rhs.name) && ( type == rhs.type );
  50. }
  51. bool operator!=( const SourceBase::AccessorParameter& rhs) const
  52. {
  53. return ( name != rhs.name) || ( type != rhs.type );
  54. }
  55. };
  56. /** List of all parameters of an accessor, defining the meaning of the values in an array.*/
  57. typedef std::vector< AccessorParameter > Accessor;
  58. private:
  59. /**
  60. * A text string containing the unique identifier of the element. This value must be unique
  61. * within the instance document. Required.
  62. */
  63. String mId;
  64. /**
  65. * The number of values that are to be considered a unit during each access to the array.
  66. * The default is 1, indicating that a single value is accessed. Optional.
  67. */
  68. unsigned long long mStride;
  69. /**
  70. * This member will be used, if multiple source elements with the same input semantic are
  71. * referenced in the current mesh.
  72. */
  73. size_t mInitialIndex;
  74. /**
  75. * Flags, if the source element is already loaded into the framework. A source element
  76. * can be referenced from the same input element in multiple primitive elements or from
  77. * different input elements (NORMALS, COLOR, TEXCOORD, ...). It should be loaded only once
  78. * from every input element.
  79. */
  80. COLLADAFW::ArrayPrimitiveType<InputSemantic::Semantic> mLoadedInputElements;
  81. /** The accessor of the source.*/
  82. Accessor mAccessor;
  83. public:
  84. /** Constructor. */
  85. SourceBase ( )
  86. : mId()
  87. , mStride(0)
  88. , mInitialIndex (0)
  89. , mLoadedInputElements ( COLLADAFW::MeshPrimitiveArray::OWNER )
  90. {}
  91. /** Destructor. */
  92. virtual ~SourceBase ()
  93. {
  94. mLoadedInputElements.clear();
  95. }
  96. /**
  97. * The value type of the current values.
  98. */
  99. virtual DataType getDataType ()const =0;
  100. /**
  101. * Adds the current input element in the list of already loaded input elements.
  102. * Returns true, if the input element was not already in the list and was successfully added.
  103. * A source element can be referenced from the same input element in multiple primitive
  104. * elements or from different input elements (NORMALS, COLOR, TEXCOORD, ...). It should be
  105. * loaded only once from every input element.
  106. */
  107. bool addLoadedInputElement ( const InputSemantic::Semantic& semantic )
  108. {
  109. if ( !isLoadedInputElement ( semantic ) )
  110. {
  111. mLoadedInputElements.append ( semantic );
  112. return true;
  113. }
  114. return false;
  115. }
  116. /**
  117. * Checks if the current input element is already in the list of loaded input elements.
  118. * Returns true, if the input element was not already in the list and was successfully added.
  119. * A source element can be referenced from the same input element in multiple primitive
  120. * elements or from different input elements (NORMALS, COLOR, TEXCOORD, ...). It should be
  121. * loaded only once from every input element.
  122. */
  123. bool isLoadedInputElement ( const InputSemantic::Semantic& semantic )
  124. {
  125. const size_t numLoadedInputElements = mLoadedInputElements.getCount ();
  126. for ( size_t i=0; i<numLoadedInputElements; ++i )
  127. {
  128. InputSemantic::Semantic& currentSemantic = mLoadedInputElements [i];
  129. if ( currentSemantic == semantic ) return true;
  130. }
  131. return false;
  132. }
  133. /**
  134. * A text string containing the unique identifier of the element.
  135. * This value must be unique within the instance document. Required.
  136. * @return const String& The unique identifier of the element.
  137. */
  138. const String& getId () const { return mId; }
  139. /**
  140. * A text string containing the unique identifier of the element.
  141. * This value must be unique within the instance document. Required.
  142. * @param val The unique identifier of the element.
  143. */
  144. void setId ( const String& val ) { mId = val; }
  145. /**
  146. * The number of values that are to be considered a unit during each access to the array.
  147. * The default is 1, indicating that a single value is accessed. Optional.
  148. */
  149. unsigned long long getStride () const { return mStride; }
  150. /**
  151. * The number of values that are to be considered a unit during each access to the array.
  152. * The default is 1, indicating that a single value is accessed. Optional.
  153. */
  154. void setStride ( unsigned long long Stride ) { mStride = Stride; }
  155. /**
  156. * This member will be used, if multiple source elements with the same input semantic are
  157. * referenced in the current mesh.
  158. * The sources of semantic "POSITION" and "NORMAL" will be concated to one source and
  159. * and one indices array.
  160. */
  161. size_t getInitialIndex () const { return mInitialIndex; }
  162. /**
  163. * This member will be used, if multiple source elements with the same input semantic are
  164. * referenced in the current mesh.
  165. * The sources of semantic "POSITION" and "NORMAL" will be concated to one source and
  166. * and one indices array.
  167. */
  168. void setInitialIndex ( size_t InitialIndex ) { mInitialIndex = InitialIndex; }
  169. /** Appends an accessor parameter to the source's accessor.*/
  170. void appendAccessorParameter( const AccessorParameter& parameter ) { mAccessor.push_back( parameter ); }
  171. /** Returns the accessor.*/
  172. const Accessor& getAccessor() const { return mAccessor; }
  173. };
  174. /**
  175. * Declares a data repository that provides values according to the
  176. * semantics of an <input> element that refers to it.
  177. * A data source is a well-known source of information that can be accessed
  178. * through an established communication channel.
  179. * The data source provides access methods to the information. These access
  180. * methods implement various techniques according to the representation of
  181. * the information. The information may be stored locally as an array of
  182. * data or a program that generates the data.
  183. */
  184. template < class ArrayType, SourceBase::DataType dataType >
  185. class Source : public SourceBase
  186. {
  187. private:
  188. /**
  189. * Stores a homogeneous array of values.
  190. */
  191. ArrayType mArrayElement;
  192. public:
  193. /** Constructor. */
  194. Source () : SourceBase () {}
  195. /** Destructor. */
  196. virtual ~Source ()
  197. {
  198. }
  199. /**
  200. * The value type of the current values. All other arrays are empty!
  201. */
  202. DataType getDataType () const { return dataType; }
  203. /**
  204. * Stores a homogeneous array of values.
  205. * The class contains members for all valid array element types,
  206. * but there is always just one, which is initialized.
  207. * @return const IntArrayElement The array element with the values array.
  208. */
  209. ArrayType& getArrayElement ()
  210. {
  211. return mArrayElement;
  212. }
  213. /**
  214. * Stores a homogeneous array of values.
  215. * The class contains members for all valid array element types,
  216. * but there is always just one, which is initialized.
  217. * @return const IntArrayElement The array element with the values array.
  218. */
  219. const ArrayType& getArrayElement() const
  220. {
  221. return mArrayElement;
  222. }
  223. };
  224. typedef Source < DoubleArrayElement, SourceBase::DATA_TYPE_DOUBLE > DoubleSource;
  225. typedef Source < FloatArrayElement, SourceBase::DATA_TYPE_FLOAT > FloatSource;
  226. typedef Source < IntArrayElement, SourceBase::DATA_TYPE_INT > IntSource;
  227. typedef Source < Long64ArrayElement, SourceBase::DATA_TYPE_LONG64 > Long64Source;
  228. typedef Source < BoolArrayElement, SourceBase::DATA_TYPE_BOOL > BoolSource;
  229. #ifdef COLLADASAXFWL_REAL_IS_FLOAT
  230. typedef FloatSource RealSource;
  231. #else
  232. typedef DoubleSource RealSource;
  233. #endif
  234. typedef COLLADAFW::ArrayPrimitiveType<SourceBase*> SourceArray;
  235. } // namespace COLLADASaxFWL
  236. #endif // __COLLADASAXFWL_SOURCE_H__