Improving ColladaExporter to support scenes with only one node

pull/261/head
Léo Terziman 2013-10-14 09:59:07 +02:00
parent 365b3aa412
commit 8ba5fa33af
2 changed files with 47 additions and 5 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 "SceneCombiner.h"
#include <ctime> #include <ctime>
#include <set> #include <set>
@ -80,6 +82,7 @@ ColladaExporter::ColladaExporter( const aiScene* pScene)
mOutput.imbue( std::locale("C") ); mOutput.imbue( std::locale("C") );
mScene = pScene; mScene = pScene;
mSceneOwned = false;
// set up strings // set up strings
endstr = "\n"; endstr = "\n";
@ -88,6 +91,15 @@ ColladaExporter::ColladaExporter( const aiScene* pScene)
WriteFile(); WriteFile();
} }
// ------------------------------------------------------------------------------------------------
// Destructor
ColladaExporter::~ColladaExporter()
{
if(mSceneOwned) {
delete mScene;
}
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Starts writing the contents // Starts writing the contents
void ColladaExporter::WriteFile() void ColladaExporter::WriteFile()
@ -145,11 +157,13 @@ void ColladaExporter::WriteHeader()
aiVector3D position; aiVector3D position;
mScene->mRootNode->mTransformation.Decompose(scaling, rotation, position); mScene->mRootNode->mTransformation.Decompose(scaling, rotation, position);
bool add_root_node = false;
float scale = 1.0; float scale = 1.0;
if(std::abs(scaling.x - scaling.y) <= epsilon && std::abs(scaling.x - scaling.z) <= epsilon && std::abs(scaling.y - scaling.z) <= epsilon) { if(std::abs(scaling.x - scaling.y) <= epsilon && std::abs(scaling.x - scaling.z) <= epsilon && std::abs(scaling.y - scaling.z) <= epsilon) {
scale = scaling.x; scale = scaling.x;
} else { } else {
DefaultLogger::get()->warn("Collada: Unable to compute the global scale of the scene " + scene_name); add_root_node = true;
} }
std::string up_axis = "Y_UP"; std::string up_axis = "Y_UP";
@ -160,11 +174,35 @@ void ColladaExporter::WriteHeader()
} else if(rotation.Equal(z_rot, epsilon)) { } else if(rotation.Equal(z_rot, epsilon)) {
up_axis = "Z_UP"; up_axis = "Z_UP";
} else { } else {
DefaultLogger::get()->warn("Collada: Unable to compute the up axis of the scene " + scene_name); add_root_node = true;
} }
if(position.x != 0 || position.y != 0 || position.z != 0) { if(! position.Equal(aiVector3D(0, 0, 0))) {
DefaultLogger::get()->warn("Collada: Unable to keep the global position of the scene " + scene_name); add_root_node = true;
}
if(mScene->mRootNode->mNumChildren == 0) {
add_root_node = true;
}
if(add_root_node) {
aiScene* scene;
SceneCombiner::CopyScene(&scene, mScene);
aiNode* root = new aiNode("Scene");
root->mNumChildren = 1;
root->mChildren = new aiNode*[root->mNumChildren];
root->mChildren[0] = scene->mRootNode;
scene->mRootNode->mParent = root;
scene->mRootNode = root;
mScene = scene;
mSceneOwned = true;
up_axis = "Y_UP";
scale = 1.0;
} }
mOutput << startstr << "<asset>" << endstr; mOutput << startstr << "<asset>" << endstr;

View File

@ -61,6 +61,9 @@ public:
/// Constructor for a specific scene to export /// Constructor for a specific scene to export
ColladaExporter( const aiScene* pScene); ColladaExporter( const aiScene* pScene);
/// Destructor
virtual ~ColladaExporter();
protected: protected:
/// Starts writing the contents /// Starts writing the contents
void WriteFile(); void WriteFile();
@ -103,6 +106,7 @@ public:
protected: protected:
/// The scene to be written /// The scene to be written
const aiScene* mScene; const aiScene* mScene;
bool mSceneOwned;
/// current line start string, contains the current indentation for simple stream insertion /// current line start string, contains the current indentation for simple stream insertion
std::string startstr; std::string startstr;
@ -150,7 +154,7 @@ protected:
/// Writes a color-or-texture entry into an effect definition /// Writes a color-or-texture entry into an effect definition
void WriteTextureColorEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pImageName); void WriteTextureColorEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pImageName);
/// Writes a scalar property /// Writes a scalar property
void ColladaExporter::WriteFloatEntry( const Property& pProperty, const std::string& pTypeName); void WriteFloatEntry( const Property& pProperty, const std::string& pTypeName);
}; };
} }