tsShapeLoader.h 6.3 KB

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