Improving ColladaExporter to allow non-destructive cycles of import/export

pull/261/head
Léo Terziman 2013-10-02 11:25:04 +02:00
parent bbfd56046b
commit b4b39e026d
1 changed files with 51 additions and 35 deletions

View File

@ -44,6 +44,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef ASSIMP_BUILD_NO_COLLADA_EXPORTER #ifndef ASSIMP_BUILD_NO_COLLADA_EXPORTER
#include "ColladaExporter.h" #include "ColladaExporter.h"
#include <set>
using namespace Assimp; using namespace Assimp;
namespace Assimp namespace Assimp
@ -97,7 +99,7 @@ void ColladaExporter::WriteFile()
WriteHeader(); WriteHeader();
WriteMaterials(); WriteMaterials();
WriteGeometryLibrary(); WriteGeometryLibrary();
WriteSceneLibrary(); WriteSceneLibrary();
@ -225,6 +227,8 @@ void ColladaExporter::WriteMaterials()
{ {
materials.resize( mScene->mNumMaterials); materials.resize( mScene->mNumMaterials);
std::set<std::string> material_names;
/// collect all materials from the scene /// collect all materials from the scene
size_t numTextures = 0; size_t numTextures = 0;
for( size_t a = 0; a < mScene->mNumMaterials; ++a ) for( size_t a = 0; a < mScene->mNumMaterials; ++a )
@ -234,7 +238,12 @@ void ColladaExporter::WriteMaterials()
aiString name; aiString name;
if( mat->Get( AI_MATKEY_NAME, name) != aiReturn_SUCCESS ) if( mat->Get( AI_MATKEY_NAME, name) != aiReturn_SUCCESS )
name = "mat"; name = "mat";
materials[a].name = std::string( "m") + boost::lexical_cast<std::string> (a) + name.C_Str(); if(material_names.find(name.C_Str()) != material_names.end()) {
materials[a].name = std::string( "m") + boost::lexical_cast<std::string> (a) + "_" + name.C_Str();
material_names.insert(materials[a].name);
} else {
materials[a].name = name.C_Str();
}
for( std::string::iterator it = materials[a].name.begin(); it != materials[a].name.end(); ++it ) for( std::string::iterator it = materials[a].name.begin(); it != materials[a].name.end(); ++it )
if( !isalnum( *it) ) if( !isalnum( *it) )
*it = '_'; *it = '_';
@ -560,48 +569,55 @@ void ColladaExporter::WriteSceneLibrary()
// Recursively writes the given node // Recursively writes the given node
void ColladaExporter::WriteNode( const aiNode* pNode) void ColladaExporter::WriteNode( const aiNode* pNode)
{ {
mOutput << startstr << "<node id=\"" << pNode->mName.data << "\" name=\"" << pNode->mName.data << "\">" << endstr; std::string name(pNode->mName.C_Str());
PushTag(); std::transform(name.begin(), name.end(), name.begin(), ::tolower);
// write transformation - we can directly put the matrix there if(name.compare("myscene") != 0) {
// TODO: (thom) decompose into scale - rot - quad to allow adressing it by animations afterwards mOutput << startstr << "<node id=\"" << pNode->mName.data << "\" name=\"" << pNode->mName.data << "\">" << endstr;
const aiMatrix4x4& mat = pNode->mTransformation;
mOutput << startstr << "<matrix>";
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 << "</matrix>" << 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 << "<instance_geometry url=\"#" << GetMeshId( pNode->mMeshes[a]) << "\">" << endstr;
PushTag(); PushTag();
mOutput << startstr << "<bind_material>" << endstr;
PushTag(); // write transformation - we can directly put the matrix there
mOutput << startstr << "<technique_common>" << endstr; // TODO: (thom) decompose into scale - rot - quad to allow adressing it by animations afterwards
PushTag(); const aiMatrix4x4& mat = pNode->mTransformation;
mOutput << startstr << "<instance_material symbol=\"theresonlyone\" target=\"#" << materials[mesh->mMaterialIndex].name << "\" />" << endstr; mOutput << startstr << "<matrix>";
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 << "</matrix>" << 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 << "<instance_geometry url=\"#" << GetMeshId( pNode->mMeshes[a]) << "\">" << endstr;
PushTag();
mOutput << startstr << "<bind_material>" << endstr;
PushTag();
mOutput << startstr << "<technique_common>" << endstr;
PushTag();
mOutput << startstr << "<instance_material symbol=\"theresonlyone\" target=\"#" << materials[mesh->mMaterialIndex].name << "\" />" << endstr;
PopTag();
mOutput << startstr << "</technique_common>" << endstr;
PopTag(); PopTag();
mOutput << startstr << "</technique_common>" << endstr; mOutput << startstr << "</bind_material>" << endstr;
PopTag(); PopTag();
mOutput << startstr << "</bind_material>" << endstr; mOutput << startstr << "</instance_geometry>" << endstr;
PopTag(); }
mOutput << startstr << "</instance_geometry>" << endstr;
} }
// recurse into subnodes // recurse into subnodes
for( size_t a = 0; a < pNode->mNumChildren; ++a ) for( size_t a = 0; a < pNode->mNumChildren; ++a )
WriteNode( pNode->mChildren[a]); WriteNode( pNode->mChildren[a]);
PopTag(); if(name.compare("myscene") != 0) {
mOutput << startstr << "</node>" << endstr; PopTag();
mOutput << startstr << "</node>" << endstr;
}
} }
#endif #endif