1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2021, assimp team
6 
7 
8 All rights reserved.
9 
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the
12 following conditions are met:
13 
14 * Redistributions of source code must retain the above
15   copyright notice, this list of conditions and the
16   following disclaimer.
17 
18 * Redistributions in binary form must reproduce the above
19   copyright notice, this list of conditions and the
20   following disclaimer in the documentation and/or other
21   materials provided with the distribution.
22 
23 * Neither the name of the assimp team, nor the names of its
24   contributors may be used to endorse or promote products
25   derived from this software without specific prior
26   written permission of the assimp team.
27 
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 
40 ----------------------------------------------------------------------
41 */
42 
43 /** @file Defines the helper data structures for importing XFiles */
44 #ifndef AI_XFILEHELPER_H_INC
45 #define AI_XFILEHELPER_H_INC
46 
47 #include <cstdint>
48 #include <string>
49 #include <vector>
50 
51 #include <assimp/anim.h>
52 #include <assimp/mesh.h>
53 #include <assimp/quaternion.h>
54 #include <assimp/types.h>
55 
56 namespace Assimp {
57 namespace XFile {
58 
59 /** Helper structure representing a XFile mesh face */
60 struct Face {
61     std::vector<unsigned int> mIndices;
62 };
63 
64 /** Helper structure representing a texture filename inside a material and its potential source */
65 struct TexEntry {
66     std::string mName;
67     bool mIsNormalMap; // true if the texname was specified in a NormalmapFilename tag
68 
TexEntryTexEntry69     TexEntry() AI_NO_EXCEPT :
70             mName(),
71             mIsNormalMap(false) {
72         // empty
73     }
74     TexEntry(const std::string &pName, bool pIsNormalMap = false) :
mNameTexEntry75             mName(pName), mIsNormalMap(pIsNormalMap) {
76         // empty
77     }
78 };
79 
80 /** Helper structure representing a XFile material */
81 struct Material {
82     std::string mName;
83     bool mIsReference; // if true, mName holds a name by which the actual material can be found in the material list
84     aiColor4D mDiffuse;
85     ai_real mSpecularExponent;
86     aiColor3D mSpecular;
87     aiColor3D mEmissive;
88     std::vector<TexEntry> mTextures;
89     size_t sceneIndex; ///< the index under which it was stored in the scene's material list
90 
MaterialMaterial91     Material() AI_NO_EXCEPT :
92             mIsReference(false),
93             mSpecularExponent(),
94             sceneIndex(SIZE_MAX) {
95         // empty
96     }
97 };
98 
99 /** Helper structure to represent a bone weight */
100 struct BoneWeight {
101     unsigned int mVertex;
102     ai_real mWeight;
103 };
104 
105 /** Helper structure to represent a bone in a mesh */
106 struct Bone {
107     std::string mName;
108     std::vector<BoneWeight> mWeights;
109     aiMatrix4x4 mOffsetMatrix;
110 };
111 
112 /** Helper structure to represent an XFile mesh */
113 struct Mesh {
114     std::string mName;
115     std::vector<aiVector3D> mPositions;
116     std::vector<Face> mPosFaces;
117     std::vector<aiVector3D> mNormals;
118     std::vector<Face> mNormFaces;
119     unsigned int mNumTextures;
120     std::vector<aiVector2D> mTexCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
121     unsigned int mNumColorSets;
122     std::vector<aiColor4D> mColors[AI_MAX_NUMBER_OF_COLOR_SETS];
123 
124     std::vector<unsigned int> mFaceMaterials;
125     std::vector<Material> mMaterials;
126 
127     std::vector<Bone> mBones;
128 
129     explicit Mesh(const std::string &pName = std::string()) AI_NO_EXCEPT
mNameMesh130             : mName(pName),
131               mPositions(),
132               mPosFaces(),
133               mNormals(),
134               mNormFaces(),
135               mNumTextures(0),
136               mTexCoords{},
137               mNumColorSets(0),
138               mColors{},
139               mFaceMaterials(),
140               mMaterials(),
141               mBones() {
142         // empty
143     }
144 };
145 
146 /** Helper structure to represent a XFile frame */
147 struct Node {
148     std::string mName;
149     aiMatrix4x4 mTrafoMatrix;
150     Node *mParent;
151     std::vector<Node *> mChildren;
152     std::vector<Mesh *> mMeshes;
153 
NodeNode154     Node() AI_NO_EXCEPT
155             : mName(),
156               mTrafoMatrix(),
157               mParent(nullptr),
158               mChildren(),
159               mMeshes() {
160         // empty
161     }
NodeNode162     explicit Node(Node *pParent) :
163             mName(), mTrafoMatrix(), mParent(pParent), mChildren(), mMeshes() {
164         // empty
165     }
166 
~NodeNode167     ~Node() {
168         for (unsigned int a = 0; a < mChildren.size(); ++a) {
169             delete mChildren[a];
170         }
171         for (unsigned int a = 0; a < mMeshes.size(); ++a) {
172             delete mMeshes[a];
173         }
174     }
175 };
176 
177 struct MatrixKey {
178     double mTime;
179     aiMatrix4x4 mMatrix;
180 };
181 
182 /** Helper structure representing a single animated bone in a XFile */
183 struct AnimBone {
184     std::string mBoneName;
185     std::vector<aiVectorKey> mPosKeys; // either three separate key sequences for position, rotation, scaling
186     std::vector<aiQuatKey> mRotKeys;
187     std::vector<aiVectorKey> mScaleKeys;
188     std::vector<MatrixKey> mTrafoKeys; // or a combined key sequence of transformation matrices.
189 };
190 
191 /** Helper structure to represent an animation set in a XFile */
192 struct Animation {
193     std::string mName;
194     std::vector<AnimBone *> mAnims;
195 
~AnimationAnimation196     ~Animation() {
197         for (unsigned int a = 0; a < mAnims.size(); a++)
198             delete mAnims[a];
199     }
200 };
201 
202 /** Helper structure analogue to aiScene */
203 struct Scene {
204     Node *mRootNode;
205 
206     std::vector<Mesh *> mGlobalMeshes; // global meshes found outside of any frames
207     std::vector<Material> mGlobalMaterials; // global materials found outside of any meshes.
208 
209     std::vector<Animation *> mAnims;
210     unsigned int mAnimTicksPerSecond;
211 
SceneScene212     Scene() AI_NO_EXCEPT
213             : mRootNode(nullptr),
214               mGlobalMeshes(),
215               mGlobalMaterials(),
216               mAnimTicksPerSecond(0) {
217         // empty
218     }
~SceneScene219     ~Scene() {
220         delete mRootNode;
221         mRootNode = nullptr;
222         for (unsigned int a = 0; a < mGlobalMeshes.size(); ++a) {
223             delete mGlobalMeshes[a];
224         }
225         for (unsigned int a = 0; a < mAnims.size(); ++a) {
226             delete mAnims[a];
227         }
228     }
229 };
230 
231 } // end of namespace XFile
232 } // end of namespace Assimp
233 
234 #endif // AI_XFILEHELPER_H_INC
235