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 ColladaExporter.h
42  * Declares the exporter class to write a scene to a Collada file
43  */
44 #ifndef AI_COLLADAEXPORTER_H_INC
45 #define AI_COLLADAEXPORTER_H_INC
46 
47 #include <assimp/ai_assert.h>
48 #include <assimp/material.h>
49 #include <assimp/mesh.h>
50 #include <assimp/light.h>
51 #include <assimp/Exporter.hpp>
52 #include <sstream>
53 #include <vector>
54 #include <map>
55 
56 struct aiScene;
57 struct aiNode;
58 
59 namespace Assimp
60 {
61 
62 /// Helper class to export a given scene to a Collada file. Just for my personal
63 /// comfort when implementing it.
64 class ColladaExporter
65 {
66 public:
67     /// Constructor for a specific scene to export
68     ColladaExporter( const aiScene* pScene, IOSystem* pIOSystem, const std::string& path, const std::string& file);
69 
70     /// Destructor
71     virtual ~ColladaExporter();
72 
73 protected:
74     /// Starts writing the contents
75     void WriteFile();
76 
77     /// Writes the asset header
78     void WriteHeader();
79 
80     /// Writes the embedded textures
81     void WriteTextures();
82 
83     /// Writes the material setup
84     void WriteMaterials();
85 
86     /// Writes the cameras library
87     void WriteCamerasLibrary();
88 
89     // Write a camera entry
90     void WriteCamera(size_t pIndex);
91 
92     /// Writes the cameras library
93     void WriteLightsLibrary();
94 
95     // Write a camera entry
96     void WriteLight(size_t pIndex);
97     void WritePointLight(const aiLight *const light);
98     void WriteDirectionalLight(const aiLight *const light);
99     void WriteSpotLight(const aiLight *const light);
100     void WriteAmbienttLight(const aiLight *const light);
101 
102     /// Writes the geometry library
103     void WriteGeometryLibrary();
104 
105     /// Writes the given mesh
106     void WriteGeometry( size_t pIndex);
107 
108     enum FloatDataType { FloatType_Vector, FloatType_TexCoord2, FloatType_TexCoord3, FloatType_Color };
109 
110     /// Writes a float array of the given type
111     void WriteFloatArray( const std::string& pIdString, FloatDataType pType, const float* pData, size_t pElementCount);
112 
113     /// Writes the scene library
114     void WriteSceneLibrary();
115 
116     /// Recursively writes the given node
117     void WriteNode( const aiScene* scene, aiNode* pNode);
118 
119     /// Enters a new xml element, which increases the indentation
PushTag()120     void PushTag() { startstr.append( "  "); }
121     /// Leaves an element, decreasing the indentation
PopTag()122     void PopTag() { ai_assert( startstr.length() > 1); startstr.erase( startstr.length() - 2); }
123 
124     /// Creates a mesh ID for the given mesh
GetMeshId(size_t pIndex)125     std::string GetMeshId( size_t pIndex) const { return std::string( "meshId" ) + std::to_string(pIndex); }
126 
127 public:
128     /// Stringstream to write all output into
129     std::stringstream mOutput;
130 
131 protected:
132     /// The IOSystem for output
133     IOSystem* mIOSystem;
134 
135     /// Path of the directory where the scene will be exported
136     const std::string mPath;
137 
138     /// Name of the file (without extension) where the scene will be exported
139     const std::string mFile;
140 
141     /// The scene to be written
142     const aiScene* mScene;
143     bool mSceneOwned;
144 
145     /// current line start string, contains the current indentation for simple stream insertion
146     std::string startstr;
147     /// current line end string for simple stream insertion
148     std::string endstr;
149 
150   // pair of color and texture - texture precedences color
151   struct Surface
152   {
153     bool exist;
154     aiColor4D color;
155     std::string texture;
156     size_t channel;
SurfaceSurface157     Surface() { exist = false; channel = 0; }
158   };
159 
160   struct Property
161   {
162     bool exist;
163      float value;
PropertyProperty164      Property()
165          : exist(false)
166          , value(0.0f)
167      {}
168   };
169 
170   // summarize a material in an convinient way.
171   struct Material
172   {
173     std::string name;
174     std::string shading_model;
175     Surface ambient, diffuse, specular, emissive, reflective, transparent, normal;
176     Property shininess, transparency, index_refraction;
177 
MaterialMaterial178     Material() {}
179   };
180 
181   std::vector<Material> materials;
182 
183   std::map<unsigned int, std::string> textures;
184 
185 protected:
186   /// Dammit C++ - y u no compile two-pass? No I have to add all methods below the struct definitions
187   /// Reads a single surface entry from the given material keys
188   void ReadMaterialSurface( Surface& poSurface, const aiMaterial* pSrcMat, aiTextureType pTexture, const char* pKey, size_t pType, size_t pIndex);
189   /// Writes an image entry for the given surface
190   void WriteImageEntry( const Surface& pSurface, const std::string& pNameAdd);
191   /// Writes the two parameters necessary for referencing a texture in an effect entry
192   void WriteTextureParamEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pMatName);
193   /// Writes a color-or-texture entry into an effect definition
194   void WriteTextureColorEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pImageName);
195   /// Writes a scalar property
196   void WriteFloatEntry( const Property& pProperty, const std::string& pTypeName);
197 };
198 
199 }
200 
201 #endif // !! AI_COLLADAEXPORTER_H_INC
202