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