OptimizeMeshes.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file OptimizeMeshes.h
  34. * @brief Declares a post processing step to join meshes, if possible
  35. */
  36. #ifndef AI_OPTIMIZEMESHESPROCESS_H_INC
  37. #define AI_OPTIMIZEMESHESPROCESS_H_INC
  38. #include "Common/BaseProcess.h"
  39. #include <assimp/types.h>
  40. #include <vector>
  41. struct aiMesh;
  42. struct aiNode;
  43. class OptimizeMeshesProcessTest;
  44. namespace Assimp {
  45. // ---------------------------------------------------------------------------
  46. /** @brief Postprocessing step to optimize mesh usage
  47. *
  48. * The implementation looks for meshes that could be joined and joins them.
  49. * Usually this will reduce the number of drawcalls.
  50. *
  51. * @note Instanced meshes are currently not processed.
  52. */
  53. class OptimizeMeshesProcess : public BaseProcess {
  54. public:
  55. /// @brief The class constructor.
  56. OptimizeMeshesProcess();
  57. /// @brief The class destructor.
  58. ~OptimizeMeshesProcess();
  59. /** @brief Internal utility to store additional mesh info
  60. */
  61. struct MeshInfo {
  62. MeshInfo() AI_NO_EXCEPT
  63. : instance_cnt(0)
  64. , vertex_format(0)
  65. , output_id(0xffffffff) {
  66. // empty
  67. }
  68. //! Number of times this mesh is referenced
  69. unsigned int instance_cnt;
  70. //! Vertex format id
  71. unsigned int vertex_format;
  72. //! Output ID
  73. unsigned int output_id;
  74. };
  75. public:
  76. // -------------------------------------------------------------------
  77. bool IsActive( unsigned int pFlags) const;
  78. // -------------------------------------------------------------------
  79. void Execute( aiScene* pScene);
  80. // -------------------------------------------------------------------
  81. void SetupProperties(const Importer* pImp);
  82. // -------------------------------------------------------------------
  83. /** @brief Specify whether you want meshes with different
  84. * primitive types to be merged as well.
  85. *
  86. * IsActive() sets this property automatically to true if the
  87. * aiProcess_SortByPType flag is found.
  88. */
  89. void EnablePrimitiveTypeSorting(bool enable) {
  90. pts = enable;
  91. }
  92. // Getter
  93. bool IsPrimitiveTypeSortingEnabled () const {
  94. return pts;
  95. }
  96. // -------------------------------------------------------------------
  97. /** @brief Specify a maximum size of a single output mesh.
  98. *
  99. * If a single input mesh already exceeds this limit, it won't
  100. * be split.
  101. * @param verts Maximum number of vertices per mesh
  102. * @param faces Maximum number of faces per mesh
  103. */
  104. void SetPreferredMeshSizeLimit (unsigned int verts, unsigned int faces)
  105. {
  106. max_verts = verts;
  107. max_faces = faces;
  108. }
  109. protected:
  110. // -------------------------------------------------------------------
  111. /** @brief Do the actual optimization on all meshes of this node
  112. * @param pNode Node we're working with
  113. */
  114. void ProcessNode( aiNode* pNode);
  115. // -------------------------------------------------------------------
  116. /** @brief Returns true if b can be joined with a
  117. *
  118. * @param verts Number of output verts up to now
  119. * @param faces Number of output faces up to now
  120. */
  121. bool CanJoin ( unsigned int a, unsigned int b,
  122. unsigned int verts, unsigned int faces );
  123. // -------------------------------------------------------------------
  124. /** @brief Find instanced meshes, for the moment we're excluding
  125. * them from all optimizations
  126. */
  127. void FindInstancedMeshes (aiNode* pNode);
  128. private:
  129. //! Scene we're working with
  130. aiScene* mScene;
  131. //! Per mesh info
  132. std::vector<MeshInfo> meshes;
  133. //! Output meshes
  134. std::vector<aiMesh*> output;
  135. //! @see EnablePrimitiveTypeSorting
  136. mutable bool pts;
  137. //! @see SetPreferredMeshSizeLimit
  138. mutable unsigned int max_verts,max_faces;
  139. //! Temporary storage
  140. std::vector<aiMesh*> merge_list;
  141. };
  142. } // end of namespace Assimp
  143. #endif // AI_CALCTANGENTSPROCESS_H_INC