1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2016, assimp team
6 All rights reserved.
7 
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
11 
12 * Redistributions of source code must retain the above
13   copyright notice, this list of conditions and the
14   following disclaimer.
15 
16 * Redistributions in binary form must reproduce the above
17   copyright notice, this list of conditions and the
18   following disclaimer in the documentation and/or other
19   materials provided with the distribution.
20 
21 * Neither the name of the assimp team, nor the names of its
22   contributors may be used to endorse or promote products
23   derived from this software without specific prior
24   written permission of the assimp team.
25 
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 
38 ----------------------------------------------------------------------
39 */
40 
41 /** @file  BlenderLoader.h
42  *  @brief Declaration of the Blender 3D (*.blend) importer class.
43  */
44 #ifndef INCLUDED_AI_BLEND_LOADER_H
45 #define INCLUDED_AI_BLEND_LOADER_H
46 
47 #include "BaseImporter.h"
48 #include "LogAux.h"
49 #include <memory>
50 
51 struct aiNode;
52 struct aiMesh;
53 struct aiLight;
54 struct aiCamera;
55 struct aiMaterial;
56 
57 namespace Assimp    {
58 
59     // TinyFormatter.h
60     namespace Formatter {
61         template <typename T,typename TR, typename A> class basic_formatter;
62         typedef class basic_formatter< char, std::char_traits<char>, std::allocator<char> > format;
63     }
64 
65     // BlenderDNA.h
66     namespace Blender {
67         class  FileDatabase;
68         struct ElemBase;
69     }
70 
71     // BlenderScene.h
72     namespace Blender {
73         struct Scene;
74         struct Object;
75         struct Mesh;
76         struct Camera;
77         struct Lamp;
78         struct MTex;
79         struct Image;
80         struct Material;
81     }
82 
83     // BlenderIntermediate.h
84     namespace Blender {
85         struct ConversionData;
86         template <template <typename,typename> class TCLASS, typename T> struct TempArray;
87     }
88 
89     // BlenderModifier.h
90     namespace Blender {
91         class BlenderModifierShowcase;
92         class BlenderModifier;
93     }
94 
95 
96 
97 // -------------------------------------------------------------------------------------------
98 /** Load blenders official binary format. The actual file structure (the `DNA` how they
99  *  call it is outsourced to BlenderDNA.cpp/BlenderDNA.h. This class only performs the
100  *  conversion from intermediate format to aiScene. */
101 // -------------------------------------------------------------------------------------------
102 class BlenderImporter : public BaseImporter, public LogFunctions<BlenderImporter>
103 {
104 public:
105     BlenderImporter();
106     ~BlenderImporter();
107 
108 
109 public:
110 
111     // --------------------
112     bool CanRead( const std::string& pFile,
113         IOSystem* pIOHandler,
114         bool checkSig
115     ) const;
116 
117 protected:
118 
119     // --------------------
120     const aiImporterDesc* GetInfo () const;
121 
122     // --------------------
123     void GetExtensionList(std::set<std::string>& app);
124 
125     // --------------------
126     void SetupProperties(const Importer* pImp);
127 
128     // --------------------
129     void InternReadFile( const std::string& pFile,
130         aiScene* pScene,
131         IOSystem* pIOHandler
132     );
133 
134     // --------------------
135     void ParseBlendFile(Blender::FileDatabase& out,
136         std::shared_ptr<IOStream> stream
137     );
138 
139     // --------------------
140     void ExtractScene(Blender::Scene& out,
141         const Blender::FileDatabase& file
142     );
143 
144     // --------------------
145     void ConvertBlendFile(aiScene* out,
146         const Blender::Scene& in,
147         const Blender::FileDatabase& file
148     );
149 
150 private:
151 
152     // --------------------
153     aiNode* ConvertNode(const Blender::Scene& in,
154         const Blender::Object* obj,
155         Blender::ConversionData& conv_info,
156         const aiMatrix4x4& parentTransform
157     );
158 
159     // --------------------
160     void ConvertMesh(const Blender::Scene& in,
161         const Blender::Object* obj,
162         const Blender::Mesh* mesh,
163         Blender::ConversionData& conv_data,
164         Blender::TempArray<std::vector,aiMesh>& temp
165     );
166 
167     // --------------------
168     aiLight* ConvertLight(const Blender::Scene& in,
169         const Blender::Object* obj,
170         const Blender::Lamp* mesh,
171         Blender::ConversionData& conv_data
172     );
173 
174     // --------------------
175     aiCamera* ConvertCamera(const Blender::Scene& in,
176         const Blender::Object* obj,
177         const Blender::Camera* mesh,
178         Blender::ConversionData& conv_data
179     );
180 
181     // --------------------
182     void BuildDefaultMaterial(
183         Blender::ConversionData& conv_data
184     );
185 
186     void AddBlendParams(
187         aiMaterial* result,
188         const Blender::Material* source
189     );
190 
191     void BuildMaterials(
192         Blender::ConversionData& conv_data
193     );
194 
195     // --------------------
196     void ResolveTexture(
197         aiMaterial* out,
198         const Blender::Material* mat,
199         const Blender::MTex* tex,
200         Blender::ConversionData& conv_data
201     );
202 
203     // --------------------
204     void ResolveImage(
205         aiMaterial* out,
206         const Blender::Material* mat,
207         const Blender::MTex* tex,
208         const Blender::Image* img,
209         Blender::ConversionData& conv_data
210     );
211 
212     void AddSentinelTexture(
213         aiMaterial* out,
214         const Blender::Material* mat,
215         const Blender::MTex* tex,
216         Blender::ConversionData& conv_data
217     );
218 
219 private: // static stuff, mostly logging and error reporting.
220 
221     // --------------------
222     static void CheckActualType(const Blender::ElemBase* dt,
223         const char* check
224     );
225 
226     // --------------------
227     static void NotSupportedObjectType(const Blender::Object* obj,
228         const char* type
229     );
230 
231 
232 private:
233 
234     Blender::BlenderModifierShowcase* modifier_cache;
235 
236 }; // !class BlenderImporter
237 
238 } // end of namespace Assimp
239 #endif // AI_UNREALIMPORTER_H_INC
240