colladaAppMesh.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _COLLADA_APPMESH_H_
  23. #define _COLLADA_APPMESH_H_
  24. #ifndef _TDICTIONARY_H_
  25. #include "core/util/tDictionary.h"
  26. #endif
  27. #ifndef _APPMESH_H_
  28. #include "ts/loader/appMesh.h"
  29. #endif
  30. #ifndef _TSSHAPELOADER_H_
  31. #include "ts/loader/tsShapeLoader.h"
  32. #endif
  33. #ifndef _COLLADA_APPNODE_H_
  34. #include "ts/collada/colladaAppNode.h"
  35. #endif
  36. #ifndef _COLLADA_EXTENSIONS_H_
  37. #include "ts/collada/colladaExtensions.h"
  38. #endif
  39. //-----------------------------------------------------------------------------
  40. // Torque unifies the vert position, normal and UV values, so that a single index
  41. // uniquely identifies all 3 elements. A triangle then contains just 3 indices,
  42. // and from that we can get the 3 positions, 3 normals and 3 UVs.
  43. //
  44. // for i=1:3
  45. // index = indices[triangle.start + i]
  46. // points[index], normals[index], uvs[index]
  47. //
  48. // Collada does not use unified vertex streams, (each triangle needs 9 indices),
  49. // so this structure is used to map a single VertTuple index to 3 indices into
  50. // the Collada streams. The Collada (and Torque) primitive index is also stored
  51. // because the Collada document may use different streams for different primitives.
  52. //
  53. // For morph geometry, we can use the same array of VertTuples to access the base
  54. // AND all of the target geometries because they MUST have the same topology.
  55. struct VertTuple
  56. {
  57. S32 prim, vertex, normal, color, uv, uv2;
  58. Point3F dataVertex, dataNormal;
  59. ColorI dataColor;
  60. Point2F dataUV, dataUV2;
  61. VertTuple(): prim(-1), vertex(-1), normal(-1), color(-1), uv(-1), uv2(-1) {}
  62. bool operator==(const VertTuple& p) const
  63. {
  64. return dataVertex == p.dataVertex &&
  65. dataColor == p.dataColor &&
  66. dataNormal == p.dataNormal &&
  67. dataUV == p.dataUV &&
  68. dataUV2 == p.dataUV2;
  69. }
  70. };
  71. class ColladaAppMesh : public AppMesh
  72. {
  73. typedef AppMesh Parent;
  74. protected:
  75. class ColladaAppNode* appNode; ///< Pointer to the node that owns this mesh
  76. const domInstance_geometry* instanceGeom;
  77. const domInstance_controller* instanceCtrl;
  78. ColladaExtension_geometry* geomExt; ///< geometry extension
  79. Vector<VertTuple> vertTuples; ///<
  80. Map<StringTableEntry,U32> boundMaterials; ///< Local map of symbols to materials
  81. static bool fixedSizeEnabled; ///< Set to true to fix the detail size to a particular value for all geometry
  82. static S32 fixedSize; ///< The fixed detail size value for all geometry
  83. //-----------------------------------------------------------------------
  84. /// Get the morph controller for this mesh (if any)
  85. const domMorph* getMorph()
  86. {
  87. if (instanceCtrl) {
  88. const domController* ctrl = daeSafeCast<domController>(instanceCtrl->getUrl().getElement());
  89. if (ctrl && ctrl->getSkin())
  90. ctrl = daeSafeCast<domController>(ctrl->getSkin()->getSource().getElement());
  91. return ctrl ? ctrl->getMorph() : NULL;
  92. }
  93. return NULL;
  94. }
  95. S32 addMaterial(const char* symbol);
  96. bool checkGeometryType(const daeElement* element);
  97. void getPrimitives(const domGeometry* geometry);
  98. void getVertexData( const domGeometry* geometry, F32 time, const MatrixF& objOffset,
  99. Vector<Point3F>& points, Vector<Point3F>& norms, Vector<ColorI>& colors,
  100. Vector<Point2F>& uvs, Vector<Point2F>& uv2s, bool appendValues);
  101. void getMorphVertexData( const domMorph* morph, F32 time, const MatrixF& objOffset,
  102. Vector<Point3F>& points, Vector<Point3F>& norms, Vector<ColorI>& colors,
  103. Vector<Point2F>& uvs, Vector<Point2F>& uv2s );
  104. public:
  105. ColladaAppMesh(const domInstance_geometry* instance, ColladaAppNode* node);
  106. ColladaAppMesh(const domInstance_controller* instance, ColladaAppNode* node);
  107. ~ColladaAppMesh()
  108. {
  109. delete geomExt;
  110. }
  111. static void fixDetailSize(bool fixed, S32 size=2)
  112. {
  113. fixedSizeEnabled = fixed;
  114. fixedSize = size;
  115. }
  116. /// Get the name of this mesh
  117. ///
  118. /// @return A string containing the name of this mesh
  119. const char *getName(bool allowFixed=true);
  120. //-----------------------------------------------------------------------
  121. /// Get a floating point property value
  122. ///
  123. /// @param propName Name of the property to get
  124. /// @param defaultVal Reference to variable to hold return value
  125. ///
  126. /// @return True if a value was set, false if not
  127. bool getFloat(const char *propName, F32 &defaultVal)
  128. {
  129. return appNode->getFloat(propName,defaultVal);
  130. }
  131. /// Get an integer property value
  132. ///
  133. /// @param propName Name of the property to get
  134. /// @param defaultVal Reference to variable to hold return value
  135. ///
  136. /// @return True if a value was set, false if not
  137. bool getInt(const char *propName, S32 &defaultVal)
  138. {
  139. return appNode->getInt(propName,defaultVal);
  140. }
  141. /// Get a boolean property value
  142. ///
  143. /// @param propName Name of the property to get
  144. /// @param defaultVal Reference to variable to hold return value
  145. ///
  146. /// @return True if a value was set, false if not
  147. bool getBool(const char *propName, bool &defaultVal)
  148. {
  149. return appNode->getBool(propName,defaultVal);
  150. }
  151. /// Return true if this mesh is a skin
  152. bool isSkin()
  153. {
  154. if (instanceCtrl) {
  155. const domController* ctrl = daeSafeCast<domController>(instanceCtrl->getUrl().getElement());
  156. if (ctrl && ctrl->getSkin() &&
  157. (ctrl->getSkin()->getVertex_weights()->getV()->getValue().getCount() > 0))
  158. return true;
  159. }
  160. return false;
  161. }
  162. /// Get the skin data: bones, vertex weights etc
  163. void lookupSkinData();
  164. /// Check if the mesh visibility is animated
  165. ///
  166. /// @param appSeq Start/end time to check
  167. ///
  168. /// @return True if the mesh visibility is animated, false if not
  169. bool animatesVis(const AppSequence* appSeq);
  170. /// Check if the material used by this mesh is animated
  171. ///
  172. /// @param appSeq Start/end time to check
  173. ///
  174. /// @return True if the material is animated, false if not
  175. bool animatesMatFrame(const AppSequence* appSeq);
  176. /// Check if the mesh is animated
  177. ///
  178. /// @param appSeq Start/end time to check
  179. ///
  180. /// @return True if the mesh is animated, false if not
  181. bool animatesFrame(const AppSequence* appSeq);
  182. /// Generate the vertex, normal and triangle data for the mesh.
  183. ///
  184. /// @param time Time at which to generate the mesh data
  185. /// @param objOffset Transform to apply to the generated data (bounds transform)
  186. void lockMesh(F32 time, const MatrixF& objOffset);
  187. /// Get the transform of this mesh at a certain time
  188. ///
  189. /// @param time Time at which to get the transform
  190. ///
  191. /// @return The mesh transform at the specified time
  192. MatrixF getMeshTransform(F32 time);
  193. /// Get the visibility of this mesh at a certain time
  194. ///
  195. /// @param time Time at which to get visibility info
  196. ///
  197. /// @return Visibility from 0 (invisible) to 1 (opaque)
  198. F32 getVisValue(F32 time);
  199. };
  200. #endif // _COLLADA_APPMESH_H_