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