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
14copyright notice, this list of conditions and the
15following disclaimer.
16
17* Redistributions in binary form must reproduce the above
18copyright notice, this list of conditions and the
19following disclaimer in the documentation and/or other
20materials provided with the distribution.
21
22* Neither the name of the assimp team, nor the names of its
23contributors may be used to endorse or promote products
24derived from this software without specific prior
25written 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#ifndef AI_OPENGEX_IMPORTER_H
42#define AI_OPENGEX_IMPORTER_H
43
44#ifndef ASSIMP_BUILD_NO_OPENGEX_IMPORTER
45
46#include "BaseImporter.h"
47#include <assimp/mesh.h>
48
49#include <vector>
50#include <list>
51#include <map>
52#include <memory>
53
54namespace ODDLParser {
55 class DDLNode;
56 struct Context;
57}
58
59struct aiNode;
60struct aiMaterial;
61struct aiCamera;
62struct aiLight;
63
64namespace Assimp {
65namespace OpenGEX {
66
67struct MetricInfo {
68 enum Type {
69 Distance = 0,
70 Angle,
71 Time,
72 Up,
73 Max
74 };
75
76 std::string m_stringValue;
77 float m_floatValue;
78 int m_intValue;
79
80 MetricInfo()
81 : m_stringValue( "" )
82 , m_floatValue( 0.0f )
83 , m_intValue( -1 ) {
84 // empty
85 }
86};
87
88/** @brief This class is used to implement the OpenGEX importer
89 *
90 * See http://opengex.org/OpenGEX.pdf for spec.
91 */
92class OpenGEXImporter : public BaseImporter {
93public:
94 /// The class constructor.
95 OpenGEXImporter();
96
97 /// The class destructor.
98 virtual ~OpenGEXImporter();
99
100 /// BaseImporter override.
101 virtual bool CanRead( const std::string &file, IOSystem *pIOHandler, bool checkSig ) const;
102
103 /// BaseImporter override.
104 virtual void InternReadFile( const std::string &file, aiScene *pScene, IOSystem *pIOHandler );
105
106 /// BaseImporter override.
107 virtual const aiImporterDesc *GetInfo() const;
108
109 /// BaseImporter override.
110 virtual void SetupProperties( const Importer *pImp );
111
112protected:
113 void handleNodes( ODDLParser::DDLNode *node, aiScene *pScene );
114 void handleMetricNode( ODDLParser::DDLNode *node, aiScene *pScene );
115 void handleNameNode( ODDLParser::DDLNode *node, aiScene *pScene );
116 void handleObjectRefNode( ODDLParser::DDLNode *node, aiScene *pScene );
117 void handleMaterialRefNode( ODDLParser::DDLNode *node, aiScene *pScene );
118 void handleGeometryNode( ODDLParser::DDLNode *node, aiScene *pScene );
119 void handleCameraNode( ODDLParser::DDLNode *node, aiScene *pScene );
120 void handleLightNode( ODDLParser::DDLNode *node, aiScene *pScene );
121 void handleGeometryObject( ODDLParser::DDLNode *node, aiScene *pScene );
122 void handleCameraObject( ODDLParser::DDLNode *node, aiScene *pScene );
123 void handleLightObject( ODDLParser::DDLNode *node, aiScene *pScene );
124 void handleTransformNode( ODDLParser::DDLNode *node, aiScene *pScene );
125 void handleMeshNode( ODDLParser::DDLNode *node, aiScene *pScene );
126 void handleVertexArrayNode( ODDLParser::DDLNode *node, aiScene *pScene );
127 void handleIndexArrayNode( ODDLParser::DDLNode *node, aiScene *pScene );
128 void handleMaterialNode( ODDLParser::DDLNode *node, aiScene *pScene );
129 void handleColorNode( ODDLParser::DDLNode *node, aiScene *pScene );
130 void handleTextureNode( ODDLParser::DDLNode *node, aiScene *pScene );
131 void handleParamNode( ODDLParser::DDLNode *node, aiScene *pScene );
132 void handleAttenNode( ODDLParser::DDLNode *node, aiScene *pScene );
133 void copyMeshes( aiScene *pScene );
134 void copyCameras( aiScene *pScene );
135 void copyLights( aiScene *pScene );
136 void copyMaterials( aiScene *pScene );
137 void resolveReferences();
138 void pushNode( aiNode *node, aiScene *pScene );
139 aiNode *popNode();
140 aiNode *top() const;
141 void clearNodeStack();
142 void createNodeTree( aiScene *pScene );
143
144private:
145 struct VertexContainer {
146 size_t m_numVerts;
147 aiVector3D *m_vertices;
148 size_t m_numColors;
149 aiColor4D *m_colors;
150 size_t m_numNormals;
151 aiVector3D *m_normals;
152 size_t m_numUVComps[ AI_MAX_NUMBER_OF_TEXTURECOORDS ];
153 aiVector3D *m_textureCoords[ AI_MAX_NUMBER_OF_TEXTURECOORDS ];
154
155 VertexContainer();
156 ~VertexContainer();
157
158 VertexContainer( const VertexContainer & ) = delete;
159 VertexContainer &operator = ( const VertexContainer & ) = delete;
160 };
161
162 struct RefInfo {
163 enum Type {
164 MeshRef,
165 MaterialRef
166 };
167
168 aiNode *m_node;
169 Type m_type;
170 std::vector<std::string> m_Names;
171
172 RefInfo( aiNode *node, Type type, std::vector<std::string> &names );
173 ~RefInfo();
174
175 RefInfo( const RefInfo & ) = delete;
176 RefInfo &operator = ( const RefInfo & ) = delete;
177 };
178
179 struct ChildInfo {
180 typedef std::list<aiNode*> NodeList;
181 std::list<aiNode*> m_children;
182 };
183 ChildInfo *m_root;
184 typedef std::map<aiNode*, std::unique_ptr<ChildInfo> > NodeChildMap;
185 NodeChildMap m_nodeChildMap;
186
187 std::vector<aiMesh*> m_meshCache;
188 typedef std::map<std::string, size_t> ReferenceMap;
189 std::map<std::string, size_t> m_mesh2refMap;
190 std::map<std::string, size_t> m_material2refMap;
191
192 ODDLParser::Context *m_ctx;
193 MetricInfo m_metrics[ MetricInfo::Max ];
194 aiNode *m_currentNode;
195 VertexContainer m_currentVertices;
196 aiMesh *m_currentMesh;
197 aiMaterial *m_currentMaterial;
198 aiLight *m_currentLight;
199 aiCamera *m_currentCamera;
200 int m_tokenType;
201 std::vector<aiMaterial*> m_materialCache;
202 std::vector<aiCamera*> m_cameraCache;
203 std::vector<aiLight*> m_lightCache;
204 std::vector<aiNode*> m_nodeStack;
205 std::vector<std::unique_ptr<RefInfo> > m_unresolvedRefStack;
206};
207
208} // Namespace OpenGEX
209} // Namespace Assimp
210
211#endif // ASSIMP_BUILD_NO_OPENGEX_IMPORTER
212
213#endif // AI_OPENGEX_IMPORTER_H
214