tsShapeLoader.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 _TSSHAPE_LOADER_H_
  23. #define _TSSHAPE_LOADER_H_
  24. #ifndef _MMATH_H_
  25. #include "math/mMath.h"
  26. #endif
  27. #ifndef _TVECTOR_H_
  28. #include "core/util/tVector.h"
  29. #endif
  30. #ifndef _TSSHAPE_H_
  31. #include "ts/tsShape.h"
  32. #endif
  33. #ifndef _APPNODE_H_
  34. #include "ts/loader/appNode.h"
  35. #endif
  36. #ifndef _APPMESH_H_
  37. #include "ts/loader/appMesh.h"
  38. #endif
  39. #ifndef _APPSEQUENCE_H_
  40. #include "ts/loader/appSequence.h"
  41. #endif
  42. class TSShapeLoader
  43. {
  44. // Supported Format List
  45. protected:
  46. struct ShapeFormat
  47. {
  48. String mName;
  49. String mExtension;
  50. };
  51. static Vector<ShapeFormat> smFormats;
  52. public:
  53. static void addFormat(String name, String extension);
  54. static String getFormatExtensions();
  55. static String getFormatFilters();
  56. static bool isSupportedFormat(String extension);
  57. public:
  58. enum eLoadPhases
  59. {
  60. Load_ReadFile = 0,
  61. Load_ParseFile,
  62. Load_ExternalRefs,
  63. Load_EnumerateScene,
  64. Load_GenerateSubshapes,
  65. Load_GenerateObjects,
  66. Load_GenerateDefaultStates,
  67. Load_GenerateSkins,
  68. Load_GenerateMaterials,
  69. Load_GenerateSequences,
  70. Load_InitShape,
  71. NumLoadPhases,
  72. Load_Complete = NumLoadPhases
  73. };
  74. static void updateProgress(S32 major, const char* msg, S32 numMinor=0, S32 minor=0);
  75. protected:
  76. struct Subshape
  77. {
  78. Vector<AppNode*> branches; ///< Shape branches
  79. Vector<AppMesh*> objMeshes; ///< Object meshes for this subshape
  80. Vector<S32> objNodes; ///< AppNode indices with objects attached
  81. ~Subshape()
  82. {
  83. // Delete children
  84. for (S32 i = 0; i < branches.size(); i++)
  85. delete branches[i];
  86. }
  87. };
  88. public:
  89. static const F32 DefaultTime;
  90. static const F64 MinFrameRate;
  91. static const F64 MaxFrameRate;
  92. static const F64 AppGroundFrameRate;
  93. protected:
  94. // Variables used during loading that must be held until the shape is deleted
  95. TSShape* shape;
  96. Vector<AppMesh*> appMeshes;
  97. // Variables used during loading, but that can be discarded afterwards
  98. static Torque::Path shapePath;
  99. AppNode* boundsNode;
  100. Vector<AppNode*> appNodes; ///< Nodes in the loaded shape
  101. Vector<AppSequence*> appSequences;
  102. Vector<Subshape*> subshapes;
  103. Vector<QuatF*> nodeRotCache;
  104. Vector<Point3F*> nodeTransCache;
  105. Vector<QuatF*> nodeScaleRotCache;
  106. Vector<Point3F*> nodeScaleCache;
  107. //--------------------------------------------------------------------------
  108. // Collect the nodes, objects and sequences for the scene
  109. virtual void enumerateScene() = 0;
  110. bool processNode(AppNode* node);
  111. virtual bool ignoreNode(const String& name) { return false; }
  112. virtual bool ignoreMesh(const String& name) { return false; }
  113. void addSkin(AppMesh* mesh);
  114. void addDetailMesh(AppMesh* mesh);
  115. void addSubshape(AppNode* node);
  116. void addObject(AppMesh* mesh, S32 nodeIndex, S32 subShapeNum);
  117. // Node transform methods
  118. MatrixF getLocalNodeMatrix(AppNode* node, F32 t);
  119. void generateNodeTransform(AppNode* node, F32 t, bool blend, F32 referenceTime,
  120. QuatF& rot, Point3F& trans, QuatF& srot, Point3F& scale);
  121. virtual void computeBounds(Box3F& bounds);
  122. // Create objects, materials and sequences
  123. void recurseSubshape(AppNode* appNode, S32 parentIndex, bool recurseChildren);
  124. void generateSubshapes();
  125. void generateObjects();
  126. void generateSkins();
  127. void generateDefaultStates();
  128. void generateObjectState(TSShape::Object& obj, F32 t, bool addFrame, bool addMatFrame);
  129. void generateFrame(TSShape::Object& obj, F32 t, bool addFrame, bool addMatFrame);
  130. void generateMaterialList();
  131. void generateSequences();
  132. // Determine what is actually animated in the sequence
  133. void setNodeMembership(TSShape::Sequence& seq, const AppSequence* appSeq);
  134. void setRotationMembership(TSShape::Sequence& seq);
  135. void setTranslationMembership(TSShape::Sequence& seq);
  136. void setScaleMembership(TSShape::Sequence& seq);
  137. void setObjectMembership(TSShape::Sequence& seq, const AppSequence* appSeq);
  138. // Manage a cache of all node transform elements for the sequence
  139. void clearNodeTransformCache();
  140. void fillNodeTransformCache(TSShape::Sequence& seq, const AppSequence* appSeq);
  141. // Add node transform elements
  142. void addNodeRotation(QuatF& rot, bool defaultVal);
  143. void addNodeTranslation(Point3F& trans, bool defaultVal);
  144. void addNodeUniformScale(F32 scale);
  145. void addNodeAlignedScale(Point3F& scale);
  146. void addNodeArbitraryScale(QuatF& qrot, Point3F& scale);
  147. // Generate animation data
  148. void generateNodeAnimation(TSShape::Sequence& seq);
  149. void generateObjectAnimation(TSShape::Sequence& seq, const AppSequence* appSeq);
  150. void generateGroundAnimation(TSShape::Sequence& seq, const AppSequence* appSeq);
  151. void generateFrameTriggers(TSShape::Sequence& seq, const AppSequence* appSeq);
  152. // Shape construction
  153. void sortDetails();
  154. void install();
  155. public:
  156. TSShapeLoader() : boundsNode(0), shape(NULL) { }
  157. virtual ~TSShapeLoader();
  158. static const Torque::Path& getShapePath() { return shapePath; }
  159. static void zapScale(MatrixF& mat);
  160. TSShape* generateShape(const Torque::Path& path);
  161. };
  162. #endif // _TSSHAPE_LOADER_H_