/* Open Asset Import Library (assimp) ---------------------------------------------------------------------- Copyright (c) 2006-2012, assimp team All rights reserved. Redistribution and use of this software in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the assimp team, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission of the assimp team. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ---------------------------------------------------------------------- */ #include "AssimpPCH.h" #ifndef ASSIMP_BUILD_NO_EXPORT #ifndef ASSIMP_BUILD_NO_COLLADA_EXPORTER #include "ColladaExporter.h" using namespace Assimp; namespace Assimp { // ------------------------------------------------------------------------------------------------ // Worker function for exporting a scene to Collada. Prototyped and registered in Exporter.cpp void ExportSceneCollada(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene) { // invoke the exporter ColladaExporter iDoTheExportThing( pScene); // we're still here - export successfully completed. Write result to the given IOSYstem boost::scoped_ptr outfile (pIOSystem->Open(pFile,"wt")); if(outfile == NULL) { throw DeadlyExportError("could not open output .dae file: " + std::string(pFile)); } // XXX maybe use a small wrapper around IOStream that behaves like std::stringstream in order to avoid the extra copy. outfile->Write( iDoTheExportThing.mOutput.str().c_str(), static_cast(iDoTheExportThing.mOutput.tellp()),1); } } // end of namespace Assimp // ------------------------------------------------------------------------------------------------ // Constructor for a specific scene to export ColladaExporter::ColladaExporter( const aiScene* pScene) { // make sure that all formatting happens using the standard, C locale and not the user's current locale mOutput.imbue( std::locale("C") ); mScene = pScene; // set up strings endstr = "\n"; // start writing WriteFile(); } // ------------------------------------------------------------------------------------------------ // Starts writing the contents void ColladaExporter::WriteFile() { // write the DTD mOutput << "" << endstr; // COLLADA element start mOutput << "" << endstr; PushTag(); WriteHeader(); WriteMaterials(); WriteGeometryLibrary(); WriteSceneLibrary(); // useless Collada fu at the end, just in case we haven't had enough indirections, yet. mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; PopTag(); mOutput << "" << endstr; } // ------------------------------------------------------------------------------------------------ // Writes the asset header void ColladaExporter::WriteHeader() { // Dummy stuff. Nobody actually cares for it anyways mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "Someone" << endstr; mOutput << startstr << "Assimp Collada Exporter" << endstr; PopTag(); mOutput << startstr << "" << endstr; mOutput << startstr << "2000-01-01T23:59:59" << endstr; mOutput << startstr << "2000-01-01T23:59:59" << endstr; mOutput << startstr << "" << endstr; mOutput << startstr << "Y_UP" << endstr; PopTag(); mOutput << startstr << "" << endstr; } // ------------------------------------------------------------------------------------------------ // Reads a single surface entry from the given material keys void ColladaExporter::ReadMaterialSurface( Surface& poSurface, const aiMaterial* pSrcMat, aiTextureType pTexture, const char* pKey, size_t pType, size_t pIndex) { if( pSrcMat->GetTextureCount( pTexture) > 0 ) { aiString texfile; unsigned int uvChannel = 0; pSrcMat->GetTexture( pTexture, 0, &texfile, NULL, &uvChannel); poSurface.texture = texfile.C_Str(); poSurface.channel = uvChannel; } else { if( pKey ) pSrcMat->Get( pKey, pType, pIndex, poSurface.color); } } // ------------------------------------------------------------------------------------------------ // Writes an image entry for the given surface void ColladaExporter::WriteImageEntry( const Surface& pSurface, const std::string& pNameAdd) { if( !pSurface.texture.empty() ) { mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << ""; for( std::string::const_iterator it = pSurface.texture.begin(); it != pSurface.texture.end(); ++it ) { if( isalnum( *it) || *it == '_' || *it == '.' || *it == '/' || *it == '\\' ) mOutput << *it; else mOutput << '%' << std::hex << size_t( (unsigned char) *it) << std::dec; } mOutput << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; } } // ------------------------------------------------------------------------------------------------ // Writes a color-or-texture entry into an effect definition void ColladaExporter::WriteTextureColorEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pImageName) { mOutput << startstr << "<" << pTypeName << ">" << endstr; PushTag(); if( pSurface.texture.empty() ) { mOutput << startstr << "" << pSurface.color.r << " " << pSurface.color.g << " " << pSurface.color.b << " " << pSurface.color.a << "" << endstr; } else { mOutput << startstr << "" << endstr; } PopTag(); mOutput << startstr << "" << endstr; } // ------------------------------------------------------------------------------------------------ // Writes the two parameters necessary for referencing a texture in an effect entry void ColladaExporter::WriteTextureParamEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pMatName) { // if surface is a texture, write out the sampler and the surface parameters necessary to reference the texture if( !pSurface.texture.empty() ) { mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << pMatName << "-" << pTypeName << "-image" << endstr; PopTag(); mOutput << startstr << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << pMatName << "-" << pTypeName << "-surface" << endstr; PopTag(); mOutput << startstr << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; } } // ------------------------------------------------------------------------------------------------ // Writes the material setup void ColladaExporter::WriteMaterials() { materials.resize( mScene->mNumMaterials); /// collect all materials from the scene size_t numTextures = 0; for( size_t a = 0; a < mScene->mNumMaterials; ++a ) { const aiMaterial* mat = mScene->mMaterials[a]; aiString name; if( mat->Get( AI_MATKEY_NAME, name) != aiReturn_SUCCESS ) name = "mat"; materials[a].name = std::string( "m") + boost::lexical_cast (a) + name.C_Str(); for( std::string::iterator it = materials[a].name.begin(); it != materials[a].name.end(); ++it ) { // isalnum on MSVC asserts for code points in [0,255]. Thus prevent unwanted promotion // of char to signed int and take the unsigned char value. if( !isalnum( static_cast(*it) ) ) { *it = '_'; } } ReadMaterialSurface( materials[a].ambient, mat, aiTextureType_AMBIENT, AI_MATKEY_COLOR_AMBIENT); if( !materials[a].ambient.texture.empty() ) numTextures++; ReadMaterialSurface( materials[a].diffuse, mat, aiTextureType_DIFFUSE, AI_MATKEY_COLOR_DIFFUSE); if( !materials[a].diffuse.texture.empty() ) numTextures++; ReadMaterialSurface( materials[a].specular, mat, aiTextureType_SPECULAR, AI_MATKEY_COLOR_SPECULAR); if( !materials[a].specular.texture.empty() ) numTextures++; ReadMaterialSurface( materials[a].emissive, mat, aiTextureType_EMISSIVE, AI_MATKEY_COLOR_EMISSIVE); if( !materials[a].emissive.texture.empty() ) numTextures++; ReadMaterialSurface( materials[a].reflective, mat, aiTextureType_REFLECTION, AI_MATKEY_COLOR_REFLECTIVE); if( !materials[a].reflective.texture.empty() ) numTextures++; ReadMaterialSurface( materials[a].normal, mat, aiTextureType_NORMALS, NULL, 0, 0); if( !materials[a].normal.texture.empty() ) numTextures++; mat->Get( AI_MATKEY_SHININESS, materials[a].shininess); } // output textures if present if( numTextures > 0 ) { mOutput << startstr << "" << endstr; PushTag(); for( std::vector::const_iterator it = materials.begin(); it != materials.end(); ++it ) { const Material& mat = *it; WriteImageEntry( mat.ambient, mat.name + "-ambient-image"); WriteImageEntry( mat.diffuse, mat.name + "-diffuse-image"); WriteImageEntry( mat.specular, mat.name + "-specular-image"); WriteImageEntry( mat.emissive, mat.name + "-emissive-image"); WriteImageEntry( mat.reflective, mat.name + "-reflective-image"); WriteImageEntry( mat.normal, mat.name + "-normal-image"); } PopTag(); mOutput << startstr << "" << endstr; } // output effects - those are the actual carriers of information if( !materials.empty() ) { mOutput << startstr << "" << endstr; PushTag(); for( std::vector::const_iterator it = materials.begin(); it != materials.end(); ++it ) { const Material& mat = *it; // this is so ridiculous it must be right mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; PushTag(); // write sampler- and surface params for the texture entries WriteTextureParamEntry( mat.emissive, "emissive", mat.name); WriteTextureParamEntry( mat.ambient, "ambient", mat.name); WriteTextureParamEntry( mat.diffuse, "diffuse", mat.name); WriteTextureParamEntry( mat.specular, "specular", mat.name); WriteTextureParamEntry( mat.reflective, "reflective", mat.name); mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; PushTag(); WriteTextureColorEntry( mat.emissive, "emission", mat.name + "-emissive-sampler"); WriteTextureColorEntry( mat.ambient, "ambient", mat.name + "-ambient-sampler"); WriteTextureColorEntry( mat.diffuse, "diffuse", mat.name + "-diffuse-sampler"); WriteTextureColorEntry( mat.specular, "specular", mat.name + "-specular-sampler"); mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << mat.shininess << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; WriteTextureColorEntry( mat.reflective, "reflective", mat.name + "-reflective-sampler"); // deactivated because the Collada spec PHONG model does not allow other textures. // if( !mat.normal.texture.empty() ) // WriteTextureColorEntry( mat.normal, "bump", mat.name + "-normal-sampler"); PopTag(); mOutput << startstr << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; } PopTag(); mOutput << startstr << "" << endstr; // write materials - they're just effect references mOutput << startstr << "" << endstr; PushTag(); for( std::vector::const_iterator it = materials.begin(); it != materials.end(); ++it ) { const Material& mat = *it; mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; } PopTag(); mOutput << startstr << "" << endstr; } } // ------------------------------------------------------------------------------------------------ // Writes the geometry library void ColladaExporter::WriteGeometryLibrary() { mOutput << startstr << "" << endstr; PushTag(); for( size_t a = 0; a < mScene->mNumMeshes; ++a) WriteGeometry( a); PopTag(); mOutput << startstr << "" << endstr; } // ------------------------------------------------------------------------------------------------ // Writes the given mesh void ColladaExporter::WriteGeometry( size_t pIndex) { const aiMesh* mesh = mScene->mMeshes[pIndex]; std::string idstr = GetMeshId( pIndex); if( mesh->mNumFaces == 0 || mesh->mNumVertices == 0 ) return; // opening tag mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; PushTag(); // Positions WriteFloatArray( idstr + "-positions", FloatType_Vector, (float*) mesh->mVertices, mesh->mNumVertices); // Normals, if any if( mesh->HasNormals() ) WriteFloatArray( idstr + "-normals", FloatType_Vector, (float*) mesh->mNormals, mesh->mNumVertices); // texture coords for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a) { if( mesh->HasTextureCoords( a) ) { WriteFloatArray( idstr + "-tex" + boost::lexical_cast (a), mesh->mNumUVComponents[a] == 3 ? FloatType_TexCoord3 : FloatType_TexCoord2, (float*) mesh->mTextureCoords[a], mesh->mNumVertices); } } // vertex colors for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a) { if( mesh->HasVertexColors( a) ) WriteFloatArray( idstr + "-color" + boost::lexical_cast (a), FloatType_Color, (float*) mesh->mColors[a], mesh->mNumVertices); } // assemble vertex structure mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; if( mesh->HasNormals() ) mOutput << startstr << "" << endstr; for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a ) { if( mesh->HasTextureCoords( a) ) mOutput << startstr << "" << endstr; } for( size_t a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a ) { if( mesh->HasVertexColors( a) ) mOutput << startstr << "" << endstr; } PopTag(); mOutput << startstr << "" << endstr; // write face setup mOutput << startstr << "mNumFaces << "\" material=\"theresonlyone\">" << endstr; PushTag(); mOutput << startstr << "" << endstr; mOutput << startstr << ""; for( size_t a = 0; a < mesh->mNumFaces; ++a ) mOutput << mesh->mFaces[a].mNumIndices << " "; mOutput << "" << endstr; mOutput << startstr << "

"; for( size_t a = 0; a < mesh->mNumFaces; ++a ) { const aiFace& face = mesh->mFaces[a]; for( size_t b = 0; b < face.mNumIndices; ++b ) mOutput << face.mIndices[b] << " "; } mOutput << "

" << endstr; PopTag(); mOutput << startstr << "
" << endstr; // closing tags PopTag(); mOutput << startstr << "
" << endstr; PopTag(); mOutput << startstr << "
" << endstr; } // ------------------------------------------------------------------------------------------------ // Writes a float array of the given type void ColladaExporter::WriteFloatArray( const std::string& pIdString, FloatDataType pType, const float* pData, size_t pElementCount) { size_t floatsPerElement = 0; switch( pType ) { case FloatType_Vector: floatsPerElement = 3; break; case FloatType_TexCoord2: floatsPerElement = 2; break; case FloatType_TexCoord3: floatsPerElement = 3; break; case FloatType_Color: floatsPerElement = 3; break; default: return; } std::string arrayId = pIdString + "-array"; mOutput << startstr << "" << endstr; PushTag(); // source array mOutput << startstr << " "; PushTag(); if( pType == FloatType_TexCoord2 ) { for( size_t a = 0; a < pElementCount; ++a ) { mOutput << pData[a*3+0] << " "; mOutput << pData[a*3+1] << " "; } } else if( pType == FloatType_Color ) { for( size_t a = 0; a < pElementCount; ++a ) { mOutput << pData[a*4+0] << " "; mOutput << pData[a*4+1] << " "; mOutput << pData[a*4+2] << " "; } } else { for( size_t a = 0; a < pElementCount * floatsPerElement; ++a ) mOutput << pData[a] << " "; } mOutput << "" << endstr; PopTag(); // the usual Collada fun. Let's bloat it even more! mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; PushTag(); switch( pType ) { case FloatType_Vector: mOutput << startstr << "" << endstr; mOutput << startstr << "" << endstr; mOutput << startstr << "" << endstr; break; case FloatType_TexCoord2: mOutput << startstr << "" << endstr; mOutput << startstr << "" << endstr; break; case FloatType_TexCoord3: mOutput << startstr << "" << endstr; mOutput << startstr << "" << endstr; mOutput << startstr << "" << endstr; break; case FloatType_Color: mOutput << startstr << "" << endstr; mOutput << startstr << "" << endstr; mOutput << startstr << "" << endstr; break; } PopTag(); mOutput << startstr << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; } // ------------------------------------------------------------------------------------------------ // Writes the scene library void ColladaExporter::WriteSceneLibrary() { mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; PushTag(); // start recursive write at the root node WriteNode( mScene->mRootNode); PopTag(); mOutput << startstr << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; } // ------------------------------------------------------------------------------------------------ // Recursively writes the given node void ColladaExporter::WriteNode( const aiNode* pNode) { mOutput << startstr << "mName.data << "\" name=\"" << pNode->mName.data << "\">" << endstr; PushTag(); // write transformation - we can directly put the matrix there // TODO: (thom) decompose into scale - rot - quad to allow adressing it by animations afterwards const aiMatrix4x4& mat = pNode->mTransformation; mOutput << startstr << ""; mOutput << mat.a1 << " " << mat.a2 << " " << mat.a3 << " " << mat.a4 << " "; mOutput << mat.b1 << " " << mat.b2 << " " << mat.b3 << " " << mat.b4 << " "; mOutput << mat.c1 << " " << mat.c2 << " " << mat.c3 << " " << mat.c4 << " "; mOutput << mat.d1 << " " << mat.d2 << " " << mat.d3 << " " << mat.d4; mOutput << "" << endstr; // instance every geometry for( size_t a = 0; a < pNode->mNumMeshes; ++a ) { const aiMesh* mesh = mScene->mMeshes[pNode->mMeshes[a]]; // do not instanciate mesh if empty. I wonder how this could happen if( mesh->mNumFaces == 0 || mesh->mNumVertices == 0 ) continue; mOutput << startstr << "mMeshes[a]) << "\">" << endstr; PushTag(); mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "mMaterialIndex].name << "\" />" << endstr; PopTag(); mOutput << startstr << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; } // recurse into subnodes for( size_t a = 0; a < pNode->mNumChildren; ++a ) WriteNode( pNode->mChildren[a]); PopTag(); mOutput << startstr << "" << endstr; } #endif #endif