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