2011-01-21 15:37:34 +00:00
|
|
|
/*
|
2012-02-03 03:38:30 +00:00
|
|
|
Open Asset Import Library (assimp)
|
2011-01-21 15:37:34 +00:00
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
Copyright (c) 2006-2012, assimp team
|
2011-01-21 15:37:34 +00:00
|
|
|
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.
|
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
* Neither the name of the assimp team, nor the names of its
|
2011-01-21 15:37:34 +00:00
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
2012-02-03 03:38:30 +00:00
|
|
|
written permission of the assimp team.
|
2011-01-21 15:37:34 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2015-04-15 23:00:17 +00:00
|
|
|
|
2011-04-03 11:44:09 +00:00
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_EXPORT
|
2011-07-19 21:41:59 +00:00
|
|
|
#ifndef ASSIMP_BUILD_NO_COLLADA_EXPORTER
|
2011-01-21 15:37:34 +00:00
|
|
|
#include "ColladaExporter.h"
|
|
|
|
|
2013-12-06 12:20:48 +00:00
|
|
|
#include "Bitmap.h"
|
2013-12-05 16:54:12 +00:00
|
|
|
#include "fast_atof.h"
|
2015-04-15 23:00:17 +00:00
|
|
|
#include "SceneCombiner.h"
|
2014-08-23 22:42:47 +00:00
|
|
|
#include "XMLTools.h"
|
2015-04-15 23:00:17 +00:00
|
|
|
#include "../include/assimp/IOSystem.hpp"
|
|
|
|
#include "../include/assimp/Exporter.hpp"
|
|
|
|
#include "../include/assimp/scene.h"
|
|
|
|
|
|
|
|
#include "Exceptional.h"
|
|
|
|
|
|
|
|
#include <boost/scoped_ptr.hpp>
|
2013-10-02 13:48:28 +00:00
|
|
|
#include <ctime>
|
2013-10-02 09:25:04 +00:00
|
|
|
#include <set>
|
|
|
|
|
2011-01-21 15:37:34 +00:00
|
|
|
using namespace Assimp;
|
|
|
|
|
|
|
|
namespace Assimp
|
|
|
|
{
|
|
|
|
|
2011-03-08 16:09:54 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2011-01-21 15:37:34 +00:00
|
|
|
// Worker function for exporting a scene to Collada. Prototyped and registered in Exporter.cpp
|
2015-03-14 13:03:14 +00:00
|
|
|
void ExportSceneCollada(const char* pFile, IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* pProperties)
|
2011-01-21 15:37:34 +00:00
|
|
|
{
|
2013-12-05 16:54:12 +00:00
|
|
|
std::string path = "";
|
2013-12-10 16:21:15 +00:00
|
|
|
std::string file = pFile;
|
2013-12-05 16:54:12 +00:00
|
|
|
|
2013-12-05 17:13:17 +00:00
|
|
|
// We need to test both types of folder separators because pIOSystem->getOsSeparator() is not reliable.
|
|
|
|
// Moreover, the path given by some applications is not even consistent with the OS specific type of separator.
|
|
|
|
const char* end_path = std::max(strrchr(pFile, '\\'), strrchr(pFile, '/'));
|
2013-12-05 16:54:12 +00:00
|
|
|
|
|
|
|
if(end_path != NULL) {
|
|
|
|
path = std::string(pFile, end_path + 1 - pFile);
|
2013-12-10 16:21:15 +00:00
|
|
|
file = file.substr(end_path + 1 - pFile, file.npos);
|
|
|
|
|
|
|
|
std::size_t pos = file.find_last_of('.');
|
|
|
|
if(pos != file.npos) {
|
|
|
|
file = file.substr(0, pos);
|
|
|
|
}
|
2013-12-05 16:54:12 +00:00
|
|
|
}
|
|
|
|
|
2011-01-21 15:37:34 +00:00
|
|
|
// invoke the exporter
|
2013-12-10 16:21:15 +00:00
|
|
|
ColladaExporter iDoTheExportThing( pScene, pIOSystem, path, file);
|
2011-01-21 15:37:34 +00:00
|
|
|
|
2011-03-08 16:09:54 +00:00
|
|
|
// we're still here - export successfully completed. Write result to the given IOSYstem
|
|
|
|
boost::scoped_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
|
2013-06-25 12:09:28 +00:00
|
|
|
if(outfile == NULL) {
|
|
|
|
throw DeadlyExportError("could not open output .dae file: " + std::string(pFile));
|
|
|
|
}
|
2011-03-08 16:09:54 +00:00
|
|
|
|
|
|
|
// XXX maybe use a small wrapper around IOStream that behaves like std::stringstream in order to avoid the extra copy.
|
2011-07-15 21:50:32 +00:00
|
|
|
outfile->Write( iDoTheExportThing.mOutput.str().c_str(), static_cast<size_t>(iDoTheExportThing.mOutput.tellp()),1);
|
2011-01-21 15:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end of namespace Assimp
|
|
|
|
|
|
|
|
|
2014-08-23 22:42:47 +00:00
|
|
|
|
2011-01-21 15:37:34 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor for a specific scene to export
|
2013-12-10 16:21:15 +00:00
|
|
|
ColladaExporter::ColladaExporter( const aiScene* pScene, IOSystem* pIOSystem, const std::string& path, const std::string& file) : mIOSystem(pIOSystem), mPath(path), mFile(file)
|
2011-01-21 15:37:34 +00:00
|
|
|
{
|
2011-07-19 21:41:59 +00:00
|
|
|
// make sure that all formatting happens using the standard, C locale and not the user's current locale
|
|
|
|
mOutput.imbue( std::locale("C") );
|
|
|
|
|
2011-01-21 15:37:34 +00:00
|
|
|
mScene = pScene;
|
2013-10-14 07:59:07 +00:00
|
|
|
mSceneOwned = false;
|
2011-01-21 15:37:34 +00:00
|
|
|
|
|
|
|
// set up strings
|
2011-07-19 21:41:59 +00:00
|
|
|
endstr = "\n";
|
2011-01-21 15:37:34 +00:00
|
|
|
|
|
|
|
// start writing
|
|
|
|
WriteFile();
|
|
|
|
}
|
|
|
|
|
2013-10-14 07:59:07 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor
|
|
|
|
ColladaExporter::~ColladaExporter()
|
|
|
|
{
|
|
|
|
if(mSceneOwned) {
|
|
|
|
delete mScene;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-21 15:37:34 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Starts writing the contents
|
|
|
|
void ColladaExporter::WriteFile()
|
|
|
|
{
|
|
|
|
// write the DTD
|
2014-06-09 13:17:45 +00:00
|
|
|
mOutput << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>" << endstr;
|
2011-01-21 15:37:34 +00:00
|
|
|
// COLLADA element start
|
|
|
|
mOutput << "<COLLADA xmlns=\"http://www.collada.org/2005/11/COLLADASchema\" version=\"1.4.1\">" << endstr;
|
|
|
|
PushTag();
|
|
|
|
|
2013-12-05 16:54:12 +00:00
|
|
|
WriteTextures();
|
2011-01-21 15:37:34 +00:00
|
|
|
WriteHeader();
|
|
|
|
|
2013-10-02 09:25:04 +00:00
|
|
|
WriteMaterials();
|
2011-01-21 15:37:34 +00:00
|
|
|
WriteGeometryLibrary();
|
|
|
|
|
|
|
|
WriteSceneLibrary();
|
|
|
|
|
2013-03-21 18:54:55 +00:00
|
|
|
// useless Collada fu at the end, just in case we haven't had enough indirections, yet.
|
2011-01-21 15:37:34 +00:00
|
|
|
mOutput << startstr << "<scene>" << endstr;
|
|
|
|
PushTag();
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<instance_visual_scene url=\"#" + XMLEscape(mScene->mRootNode->mName.C_Str()) + "\" />" << endstr;
|
2011-01-21 15:37:34 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</scene>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << "</COLLADA>" << endstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes the asset header
|
|
|
|
void ColladaExporter::WriteHeader()
|
|
|
|
{
|
2014-06-05 23:56:54 +00:00
|
|
|
static const float epsilon = 0.00001f;
|
2013-10-09 09:14:09 +00:00
|
|
|
static const aiQuaternion x_rot(aiMatrix3x3(
|
|
|
|
0, -1, 0,
|
|
|
|
1, 0, 0,
|
|
|
|
0, 0, 1));
|
|
|
|
static const aiQuaternion y_rot(aiMatrix3x3(
|
|
|
|
1, 0, 0,
|
|
|
|
0, 1, 0,
|
|
|
|
0, 0, 1));
|
|
|
|
static const aiQuaternion z_rot(aiMatrix3x3(
|
|
|
|
1, 0, 0,
|
|
|
|
0, 0, 1,
|
|
|
|
0, -1, 0));
|
|
|
|
|
2013-10-02 13:48:28 +00:00
|
|
|
static const unsigned int date_nb_chars = 20;
|
|
|
|
char date_str[date_nb_chars];
|
|
|
|
std::time_t date = std::time(NULL);
|
|
|
|
std::strftime(date_str, date_nb_chars, "%Y-%m-%dT%H:%M:%S", std::localtime(&date));
|
|
|
|
|
2013-10-09 09:14:09 +00:00
|
|
|
std::string scene_name = mScene->mRootNode->mName.C_Str();
|
|
|
|
|
2013-10-02 13:48:28 +00:00
|
|
|
aiVector3D scaling;
|
|
|
|
aiQuaternion rotation;
|
|
|
|
aiVector3D position;
|
|
|
|
mScene->mRootNode->mTransformation.Decompose(scaling, rotation, position);
|
2014-06-05 23:56:54 +00:00
|
|
|
rotation.Normalize();
|
2013-10-02 13:48:28 +00:00
|
|
|
|
2013-10-14 07:59:07 +00:00
|
|
|
bool add_root_node = false;
|
|
|
|
|
2013-10-02 13:48:28 +00:00
|
|
|
float scale = 1.0;
|
2013-10-11 14:54:07 +00:00
|
|
|
if(std::abs(scaling.x - scaling.y) <= epsilon && std::abs(scaling.x - scaling.z) <= epsilon && std::abs(scaling.y - scaling.z) <= epsilon) {
|
2013-12-05 16:54:12 +00:00
|
|
|
scale = (float) ((((double) scaling.x) + ((double) scaling.y) + ((double) scaling.z)) / 3.0);
|
2013-10-02 13:48:28 +00:00
|
|
|
} else {
|
2013-10-14 07:59:07 +00:00
|
|
|
add_root_node = true;
|
2013-10-02 13:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string up_axis = "Y_UP";
|
2013-10-11 14:54:07 +00:00
|
|
|
if(rotation.Equal(x_rot, epsilon)) {
|
2013-10-02 13:48:28 +00:00
|
|
|
up_axis = "X_UP";
|
2013-10-11 14:54:07 +00:00
|
|
|
} else if(rotation.Equal(y_rot, epsilon)) {
|
2013-10-09 09:14:09 +00:00
|
|
|
up_axis = "Y_UP";
|
2013-10-11 14:54:07 +00:00
|
|
|
} else if(rotation.Equal(z_rot, epsilon)) {
|
2013-10-02 13:48:28 +00:00
|
|
|
up_axis = "Z_UP";
|
|
|
|
} else {
|
2013-10-14 07:59:07 +00:00
|
|
|
add_root_node = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(! position.Equal(aiVector3D(0, 0, 0))) {
|
|
|
|
add_root_node = true;
|
2013-10-02 13:48:28 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 07:59:07 +00:00
|
|
|
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;
|
2013-10-02 13:48:28 +00:00
|
|
|
}
|
|
|
|
|
2011-01-21 15:37:34 +00:00
|
|
|
mOutput << startstr << "<asset>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<contributor>" << endstr;
|
|
|
|
PushTag();
|
2014-06-19 22:08:11 +00:00
|
|
|
|
|
|
|
aiMetadata* meta = mScene->mRootNode->mMetaData;
|
|
|
|
aiString value;
|
|
|
|
if (!meta || !meta->Get("Author", value))
|
|
|
|
mOutput << startstr << "<author>" << "Assimp" << "</author>" << endstr;
|
|
|
|
else
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<author>" << XMLEscape(value.C_Str()) << "</author>" << endstr;
|
2014-06-19 22:08:11 +00:00
|
|
|
|
|
|
|
if (!meta || !meta->Get("AuthoringTool", value))
|
|
|
|
mOutput << startstr << "<authoring_tool>" << "Assimp Exporter" << "</authoring_tool>" << endstr;
|
|
|
|
else
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<authoring_tool>" << XMLEscape(value.C_Str()) << "</authoring_tool>" << endstr;
|
2014-06-19 22:08:11 +00:00
|
|
|
|
|
|
|
//mOutput << startstr << "<author>" << mScene->author.C_Str() << "</author>" << endstr;
|
|
|
|
//mOutput << startstr << "<authoring_tool>" << mScene->authoringTool.C_Str() << "</authoring_tool>" << endstr;
|
|
|
|
|
2011-01-21 15:37:34 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</contributor>" << endstr;
|
2013-10-02 13:48:28 +00:00
|
|
|
mOutput << startstr << "<created>" << date_str << "</created>" << endstr;
|
|
|
|
mOutput << startstr << "<modified>" << date_str << "</modified>" << endstr;
|
|
|
|
mOutput << startstr << "<unit name=\"meter\" meter=\"" << scale << "\" />" << endstr;
|
|
|
|
mOutput << startstr << "<up_axis>" << up_axis << "</up_axis>" << endstr;
|
2011-01-21 15:37:34 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</asset>" << endstr;
|
|
|
|
}
|
|
|
|
|
2013-12-05 16:54:12 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Write the embedded textures
|
|
|
|
void ColladaExporter::WriteTextures() {
|
|
|
|
static const unsigned int buffer_size = 1024;
|
|
|
|
char str[buffer_size];
|
|
|
|
|
|
|
|
if(mScene->HasTextures()) {
|
|
|
|
for(unsigned int i = 0; i < mScene->mNumTextures; i++) {
|
|
|
|
// It would be great to be able to create a directory in portable standard C++, but it's not the case,
|
|
|
|
// so we just write the textures in the current directory.
|
|
|
|
|
|
|
|
aiTexture* texture = mScene->mTextures[i];
|
|
|
|
|
2013-12-10 16:21:15 +00:00
|
|
|
ASSIMP_itoa10(str, buffer_size, i + 1);
|
2013-12-05 16:54:12 +00:00
|
|
|
|
2013-12-10 16:21:15 +00:00
|
|
|
std::string name = mFile + "_texture_" + (i < 1000 ? "0" : "") + (i < 100 ? "0" : "") + (i < 10 ? "0" : "") + str + "." + ((const char*) texture->achFormatHint);
|
2013-12-05 16:54:12 +00:00
|
|
|
|
|
|
|
boost::scoped_ptr<IOStream> outfile(mIOSystem->Open(mPath + name, "wb"));
|
|
|
|
if(outfile == NULL) {
|
|
|
|
throw DeadlyExportError("could not open output texture file: " + mPath + name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(texture->mHeight == 0) {
|
|
|
|
outfile->Write((void*) texture->pcData, texture->mWidth, 1);
|
|
|
|
} else {
|
2013-12-06 12:20:48 +00:00
|
|
|
Bitmap::Save(texture, outfile.get());
|
2013-12-05 16:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
outfile->Flush();
|
|
|
|
|
|
|
|
textures.insert(std::make_pair(i, name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-14 17:42:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// 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;
|
2012-02-15 23:44:52 +00:00
|
|
|
pSrcMat->GetTexture( pTexture, 0, &texfile, NULL, &uvChannel);
|
2013-12-05 16:54:12 +00:00
|
|
|
|
|
|
|
std::string index_str(texfile.C_Str());
|
|
|
|
|
|
|
|
if(index_str.size() != 0 && index_str[0] == '*')
|
|
|
|
{
|
|
|
|
unsigned int index;
|
|
|
|
|
|
|
|
index_str = index_str.substr(1, std::string::npos);
|
|
|
|
|
|
|
|
try {
|
|
|
|
index = (unsigned int) strtoul10_64(index_str.c_str());
|
|
|
|
} catch(std::exception& error) {
|
|
|
|
throw DeadlyExportError(error.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<unsigned int, std::string>::const_iterator name = textures.find(index);
|
|
|
|
|
|
|
|
if(name != textures.end()) {
|
|
|
|
poSurface.texture = name->second;
|
|
|
|
} else {
|
|
|
|
throw DeadlyExportError("could not find embedded texture at index " + index_str);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
poSurface.texture = texfile.C_Str();
|
|
|
|
}
|
|
|
|
|
2012-02-14 17:42:25 +00:00
|
|
|
poSurface.channel = uvChannel;
|
2013-10-07 13:56:21 +00:00
|
|
|
poSurface.exist = true;
|
2012-02-14 17:42:25 +00:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
if( pKey )
|
2013-10-07 13:56:21 +00:00
|
|
|
poSurface.exist = pSrcMat->Get( pKey, pType, pIndex, poSurface.color) == aiReturn_SUCCESS;
|
2012-02-14 17:42:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes an image entry for the given surface
|
|
|
|
void ColladaExporter::WriteImageEntry( const Surface& pSurface, const std::string& pNameAdd)
|
|
|
|
{
|
|
|
|
if( !pSurface.texture.empty() )
|
|
|
|
{
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<image id=\"" << XMLEscape(pNameAdd) << "\">" << endstr;
|
2012-02-14 17:42:25 +00:00
|
|
|
PushTag();
|
2012-03-08 16:08:46 +00:00
|
|
|
mOutput << startstr << "<init_from>";
|
2014-08-23 22:42:47 +00:00
|
|
|
|
|
|
|
// URL encode image file name first, then XML encode on top
|
|
|
|
std::stringstream imageUrlEncoded;
|
2012-03-09 21:11:42 +00:00
|
|
|
for( std::string::const_iterator it = pSurface.texture.begin(); it != pSurface.texture.end(); ++it )
|
2012-03-08 16:08:46 +00:00
|
|
|
{
|
|
|
|
if( isalnum( *it) || *it == '_' || *it == '.' || *it == '/' || *it == '\\' )
|
2014-08-23 22:42:47 +00:00
|
|
|
imageUrlEncoded << *it;
|
2012-03-08 16:08:46 +00:00
|
|
|
else
|
2014-08-23 22:42:47 +00:00
|
|
|
imageUrlEncoded << '%' << std::hex << size_t( (unsigned char) *it) << std::dec;
|
2012-03-08 16:08:46 +00:00
|
|
|
}
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << XMLEscape(imageUrlEncoded.str());
|
2012-03-08 16:08:46 +00:00
|
|
|
mOutput << "</init_from>" << endstr;
|
2012-02-14 17:42:25 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</image>" << 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)
|
|
|
|
{
|
2013-10-07 13:56:21 +00:00
|
|
|
if(pSurface.exist) {
|
|
|
|
mOutput << startstr << "<" << pTypeName << ">" << endstr;
|
|
|
|
PushTag();
|
|
|
|
if( pSurface.texture.empty() )
|
|
|
|
{
|
|
|
|
mOutput << startstr << "<color sid=\"" << pTypeName << "\">" << pSurface.color.r << " " << pSurface.color.g << " " << pSurface.color.b << " " << pSurface.color.a << "</color>" << endstr;
|
2014-06-07 19:17:31 +00:00
|
|
|
}
|
|
|
|
else
|
2013-10-07 13:56:21 +00:00
|
|
|
{
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<texture texture=\"" << XMLEscape(pImageName) << "\" texcoord=\"CHANNEL" << pSurface.channel << "\" />" << endstr;
|
2013-10-07 13:56:21 +00:00
|
|
|
}
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</" << pTypeName << ">" << endstr;
|
2012-02-14 17:42:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-18 15:20:28 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// 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() )
|
|
|
|
{
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<newparam sid=\"" << XMLEscape(pMatName) << "-" << pTypeName << "-surface\">" << endstr;
|
2012-02-18 15:20:28 +00:00
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<surface type=\"2D\">" << endstr;
|
|
|
|
PushTag();
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<init_from>" << XMLEscape(pMatName) << "-" << pTypeName << "-image</init_from>" << endstr;
|
2012-02-18 15:20:28 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</surface>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</newparam>" << endstr;
|
|
|
|
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<newparam sid=\"" << XMLEscape(pMatName) << "-" << pTypeName << "-sampler\">" << endstr;
|
2012-02-18 15:20:28 +00:00
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<sampler2D>" << endstr;
|
|
|
|
PushTag();
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<source>" << XMLEscape(pMatName) << "-" << pTypeName << "-surface</source>" << endstr;
|
2012-02-18 15:20:28 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</sampler2D>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</newparam>" << endstr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-07 13:56:21 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes a scalar property
|
|
|
|
void ColladaExporter::WriteFloatEntry( const Property& pProperty, const std::string& pTypeName)
|
|
|
|
{
|
|
|
|
if(pProperty.exist) {
|
|
|
|
mOutput << startstr << "<" << pTypeName << ">" << endstr;
|
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<float sid=\"" << pTypeName << "\">" << pProperty.value << "</float>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</" << pTypeName << ">" << endstr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-14 17:42:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes the material setup
|
|
|
|
void ColladaExporter::WriteMaterials()
|
|
|
|
{
|
|
|
|
materials.resize( mScene->mNumMaterials);
|
|
|
|
|
2013-10-02 09:25:04 +00:00
|
|
|
std::set<std::string> material_names;
|
|
|
|
|
2012-02-14 17:42:25 +00:00
|
|
|
/// collect all materials from the scene
|
2012-02-28 09:06:25 +00:00
|
|
|
size_t numTextures = 0;
|
2012-02-14 17:42:25 +00:00
|
|
|
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 )
|
2014-05-31 10:50:07 +00:00
|
|
|
name = "mat";
|
2012-02-18 15:20:28 +00:00
|
|
|
materials[a].name = std::string( "m") + boost::lexical_cast<std::string> (a) + name.C_Str();
|
2013-09-29 19:56:32 +00:00
|
|
|
for( std::string::iterator it = materials[a].name.begin(); it != materials[a].name.end(); ++it ) {
|
2014-08-23 22:42:47 +00:00
|
|
|
// isalnum on MSVC asserts for code points outside [0,255]. Thus prevent unwanted promotion
|
2013-09-29 19:56:32 +00:00
|
|
|
// of char to signed int and take the unsigned char value.
|
2014-05-31 10:50:07 +00:00
|
|
|
if( !isalnum( static_cast<uint8_t>(*it) ) ) {
|
2012-03-08 16:08:46 +00:00
|
|
|
*it = '_';
|
2013-09-29 19:56:32 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-14 17:42:25 +00:00
|
|
|
|
2015-02-13 10:37:40 +00:00
|
|
|
aiShadingMode shading = aiShadingMode_Flat;
|
2013-10-07 13:56:21 +00:00
|
|
|
materials[a].shading_model = "phong";
|
|
|
|
if(mat->Get( AI_MATKEY_SHADING_MODEL, shading) == aiReturn_SUCCESS) {
|
|
|
|
if(shading == aiShadingMode_Phong) {
|
|
|
|
materials[a].shading_model = "phong";
|
|
|
|
} else if(shading == aiShadingMode_Blinn) {
|
|
|
|
materials[a].shading_model = "blinn";
|
|
|
|
} else if(shading == aiShadingMode_NoShading) {
|
|
|
|
materials[a].shading_model = "constant";
|
|
|
|
} else if(shading == aiShadingMode_Gouraud) {
|
|
|
|
materials[a].shading_model = "lambert";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-14 17:42:25 +00:00
|
|
|
ReadMaterialSurface( materials[a].ambient, mat, aiTextureType_AMBIENT, AI_MATKEY_COLOR_AMBIENT);
|
2012-02-28 09:06:25 +00:00
|
|
|
if( !materials[a].ambient.texture.empty() ) numTextures++;
|
2012-02-14 17:42:25 +00:00
|
|
|
ReadMaterialSurface( materials[a].diffuse, mat, aiTextureType_DIFFUSE, AI_MATKEY_COLOR_DIFFUSE);
|
2012-02-28 09:06:25 +00:00
|
|
|
if( !materials[a].diffuse.texture.empty() ) numTextures++;
|
2012-02-14 17:42:25 +00:00
|
|
|
ReadMaterialSurface( materials[a].specular, mat, aiTextureType_SPECULAR, AI_MATKEY_COLOR_SPECULAR);
|
2012-02-28 09:06:25 +00:00
|
|
|
if( !materials[a].specular.texture.empty() ) numTextures++;
|
2012-02-14 17:42:25 +00:00
|
|
|
ReadMaterialSurface( materials[a].emissive, mat, aiTextureType_EMISSIVE, AI_MATKEY_COLOR_EMISSIVE);
|
2012-02-28 09:06:25 +00:00
|
|
|
if( !materials[a].emissive.texture.empty() ) numTextures++;
|
2012-02-14 17:42:25 +00:00
|
|
|
ReadMaterialSurface( materials[a].reflective, mat, aiTextureType_REFLECTION, AI_MATKEY_COLOR_REFLECTIVE);
|
2012-02-28 09:06:25 +00:00
|
|
|
if( !materials[a].reflective.texture.empty() ) numTextures++;
|
2013-10-07 13:56:21 +00:00
|
|
|
ReadMaterialSurface( materials[a].transparent, mat, aiTextureType_OPACITY, AI_MATKEY_COLOR_TRANSPARENT);
|
|
|
|
if( !materials[a].transparent.texture.empty() ) numTextures++;
|
2012-02-15 23:44:52 +00:00
|
|
|
ReadMaterialSurface( materials[a].normal, mat, aiTextureType_NORMALS, NULL, 0, 0);
|
2012-02-28 09:06:25 +00:00
|
|
|
if( !materials[a].normal.texture.empty() ) numTextures++;
|
2012-02-14 17:42:25 +00:00
|
|
|
|
2013-10-07 13:56:21 +00:00
|
|
|
materials[a].shininess.exist = mat->Get( AI_MATKEY_SHININESS, materials[a].shininess.value) == aiReturn_SUCCESS;
|
|
|
|
materials[a].transparency.exist = mat->Get( AI_MATKEY_OPACITY, materials[a].transparency.value) == aiReturn_SUCCESS;
|
|
|
|
materials[a].transparency.value = 1 - materials[a].transparency.value;
|
|
|
|
materials[a].index_refraction.exist = mat->Get( AI_MATKEY_REFRACTI, materials[a].index_refraction.value) == aiReturn_SUCCESS;
|
2012-02-14 17:42:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// output textures if present
|
2012-02-28 09:06:25 +00:00
|
|
|
if( numTextures > 0 )
|
|
|
|
{
|
|
|
|
mOutput << startstr << "<library_images>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
for( std::vector<Material>::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");
|
2013-10-03 16:45:42 +00:00
|
|
|
WriteImageEntry( mat.emissive, mat.name + "-emission-image");
|
2012-02-28 09:06:25 +00:00
|
|
|
WriteImageEntry( mat.reflective, mat.name + "-reflective-image");
|
2013-10-07 13:56:21 +00:00
|
|
|
WriteImageEntry( mat.transparent, mat.name + "-transparent-image");
|
2012-02-28 09:06:25 +00:00
|
|
|
WriteImageEntry( mat.normal, mat.name + "-normal-image");
|
|
|
|
}
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</library_images>" << endstr;
|
2012-02-14 17:42:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// output effects - those are the actual carriers of information
|
2012-02-28 09:06:25 +00:00
|
|
|
if( !materials.empty() )
|
2012-02-14 17:42:25 +00:00
|
|
|
{
|
2012-02-28 09:06:25 +00:00
|
|
|
mOutput << startstr << "<library_effects>" << endstr;
|
2012-02-14 17:42:25 +00:00
|
|
|
PushTag();
|
2012-02-28 09:06:25 +00:00
|
|
|
for( std::vector<Material>::const_iterator it = materials.begin(); it != materials.end(); ++it )
|
|
|
|
{
|
|
|
|
const Material& mat = *it;
|
|
|
|
// this is so ridiculous it must be right
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<effect id=\"" << XMLEscape(mat.name) << "-fx\" name=\"" << XMLEscape(mat.name) << "\">" << endstr;
|
2012-02-28 09:06:25 +00:00
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<profile_COMMON>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
|
|
|
|
// write sampler- and surface params for the texture entries
|
2013-10-03 16:45:42 +00:00
|
|
|
WriteTextureParamEntry( mat.emissive, "emission", mat.name);
|
2012-02-28 09:06:25 +00:00
|
|
|
WriteTextureParamEntry( mat.ambient, "ambient", mat.name);
|
|
|
|
WriteTextureParamEntry( mat.diffuse, "diffuse", mat.name);
|
|
|
|
WriteTextureParamEntry( mat.specular, "specular", mat.name);
|
|
|
|
WriteTextureParamEntry( mat.reflective, "reflective", mat.name);
|
2013-10-07 13:56:21 +00:00
|
|
|
WriteTextureParamEntry( mat.transparent, "transparent", mat.name);
|
|
|
|
WriteTextureParamEntry( mat.normal, "normal", mat.name);
|
2012-02-28 09:06:25 +00:00
|
|
|
|
|
|
|
mOutput << startstr << "<technique sid=\"standard\">" << endstr;
|
|
|
|
PushTag();
|
2013-10-07 13:56:21 +00:00
|
|
|
mOutput << startstr << "<" << mat.shading_model << ">" << endstr;
|
2012-02-28 09:06:25 +00:00
|
|
|
PushTag();
|
|
|
|
|
2013-10-03 16:45:42 +00:00
|
|
|
WriteTextureColorEntry( mat.emissive, "emission", mat.name + "-emission-sampler");
|
2012-02-28 09:06:25 +00:00
|
|
|
WriteTextureColorEntry( mat.ambient, "ambient", mat.name + "-ambient-sampler");
|
|
|
|
WriteTextureColorEntry( mat.diffuse, "diffuse", mat.name + "-diffuse-sampler");
|
|
|
|
WriteTextureColorEntry( mat.specular, "specular", mat.name + "-specular-sampler");
|
2013-10-07 13:56:21 +00:00
|
|
|
WriteFloatEntry(mat.shininess, "shininess");
|
2012-02-28 09:06:25 +00:00
|
|
|
WriteTextureColorEntry( mat.reflective, "reflective", mat.name + "-reflective-sampler");
|
2013-10-07 13:56:21 +00:00
|
|
|
WriteTextureColorEntry( mat.transparent, "transparent", mat.name + "-transparent-sampler");
|
|
|
|
WriteFloatEntry(mat.transparency, "transparency");
|
|
|
|
WriteFloatEntry(mat.index_refraction, "index_of_refraction");
|
2012-02-28 09:06:25 +00:00
|
|
|
|
2013-10-07 13:56:21 +00:00
|
|
|
if(! mat.normal.texture.empty()) {
|
|
|
|
WriteTextureColorEntry( mat.normal, "bump", mat.name + "-normal-sampler");
|
|
|
|
}
|
2012-02-28 09:06:25 +00:00
|
|
|
|
|
|
|
PopTag();
|
2013-10-07 13:56:21 +00:00
|
|
|
mOutput << startstr << "</" << mat.shading_model << ">" << endstr;
|
2012-02-28 09:06:25 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</technique>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</profile_COMMON>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</effect>" << endstr;
|
|
|
|
}
|
2012-02-14 17:42:25 +00:00
|
|
|
PopTag();
|
2012-02-28 09:06:25 +00:00
|
|
|
mOutput << startstr << "</library_effects>" << endstr;
|
2012-02-14 17:42:25 +00:00
|
|
|
|
2012-02-28 09:06:25 +00:00
|
|
|
// write materials - they're just effect references
|
|
|
|
mOutput << startstr << "<library_materials>" << endstr;
|
2012-02-14 17:42:25 +00:00
|
|
|
PushTag();
|
2012-02-28 09:06:25 +00:00
|
|
|
for( std::vector<Material>::const_iterator it = materials.begin(); it != materials.end(); ++it )
|
|
|
|
{
|
|
|
|
const Material& mat = *it;
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<material id=\"" << XMLEscape(mat.name) << "\" name=\"" << mat.name << "\">" << endstr;
|
2012-02-28 09:06:25 +00:00
|
|
|
PushTag();
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<instance_effect url=\"#" << XMLEscape(mat.name) << "-fx\"/>" << endstr;
|
2012-02-28 09:06:25 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</material>" << endstr;
|
|
|
|
}
|
2012-02-14 17:42:25 +00:00
|
|
|
PopTag();
|
2012-02-28 09:06:25 +00:00
|
|
|
mOutput << startstr << "</library_materials>" << endstr;
|
2012-02-14 17:42:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-21 15:37:34 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes the geometry library
|
|
|
|
void ColladaExporter::WriteGeometryLibrary()
|
|
|
|
{
|
|
|
|
mOutput << startstr << "<library_geometries>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
|
|
|
|
for( size_t a = 0; a < mScene->mNumMeshes; ++a)
|
|
|
|
WriteGeometry( a);
|
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</library_geometries>" << endstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes the given mesh
|
|
|
|
void ColladaExporter::WriteGeometry( size_t pIndex)
|
|
|
|
{
|
|
|
|
const aiMesh* mesh = mScene->mMeshes[pIndex];
|
2014-08-23 22:42:47 +00:00
|
|
|
const std::string idstr = GetMeshId( pIndex);
|
|
|
|
const std::string idstrEscaped = XMLEscape(idstr);
|
2011-01-21 15:37:34 +00:00
|
|
|
|
2012-02-17 10:48:25 +00:00
|
|
|
if( mesh->mNumFaces == 0 || mesh->mNumVertices == 0 )
|
|
|
|
return;
|
|
|
|
|
2011-01-21 15:37:34 +00:00
|
|
|
// opening tag
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<geometry id=\"" << idstrEscaped << "\" name=\"" << idstrEscaped << "_name\" >" << endstr;
|
2011-01-21 15:37:34 +00:00
|
|
|
PushTag();
|
|
|
|
|
|
|
|
mOutput << startstr << "<mesh>" << 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<std::string> (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<std::string> (a), FloatType_Color, (float*) mesh->mColors[a], mesh->mNumVertices);
|
|
|
|
}
|
|
|
|
|
|
|
|
// assemble vertex structure
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<vertices id=\"" << idstrEscaped << "-vertices" << "\">" << endstr;
|
2011-01-21 15:37:34 +00:00
|
|
|
PushTag();
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<input semantic=\"POSITION\" source=\"#" << idstrEscaped << "-positions\" />" << endstr;
|
2011-01-21 15:37:34 +00:00
|
|
|
if( mesh->HasNormals() )
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<input semantic=\"NORMAL\" source=\"#" << idstrEscaped << "-normals\" />" << endstr;
|
2011-01-21 15:37:34 +00:00
|
|
|
for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a )
|
|
|
|
{
|
|
|
|
if( mesh->HasTextureCoords( a) )
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<input semantic=\"TEXCOORD\" source=\"#" << idstrEscaped << "-tex" << a << "\" " /*<< "set=\"" << a << "\"" */ << " />" << endstr;
|
2011-01-21 15:37:34 +00:00
|
|
|
}
|
|
|
|
for( size_t a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a )
|
|
|
|
{
|
|
|
|
if( mesh->HasVertexColors( a) )
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<input semantic=\"COLOR\" source=\"#" << idstrEscaped << "-color" << a << "\" " /*<< set=\"" << a << "\"" */ << " />" << endstr;
|
2011-01-21 15:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</vertices>" << endstr;
|
|
|
|
|
2014-05-31 10:50:07 +00:00
|
|
|
// count the number of lines, triangles and polygon meshes
|
|
|
|
int countLines = 0;
|
|
|
|
int countPoly = 0;
|
2011-01-21 15:37:34 +00:00
|
|
|
for( size_t a = 0; a < mesh->mNumFaces; ++a )
|
|
|
|
{
|
2014-05-31 10:50:07 +00:00
|
|
|
if (mesh->mFaces[a].mNumIndices == 2) countLines++;
|
2014-06-10 22:41:18 +00:00
|
|
|
else if (mesh->mFaces[a].mNumIndices >= 3) countPoly++;
|
2014-05-31 10:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// lines
|
|
|
|
if (countLines)
|
|
|
|
{
|
2014-06-09 13:17:45 +00:00
|
|
|
mOutput << startstr << "<lines count=\"" << countLines << "\" material=\"defaultMaterial\">" << endstr;
|
2014-05-31 10:50:07 +00:00
|
|
|
PushTag();
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<input offset=\"0\" semantic=\"VERTEX\" source=\"#" << idstrEscaped << "-vertices\" />" << endstr;
|
2014-05-31 10:50:07 +00:00
|
|
|
mOutput << startstr << "<p>";
|
|
|
|
for( size_t a = 0; a < mesh->mNumFaces; ++a )
|
|
|
|
{
|
|
|
|
const aiFace& face = mesh->mFaces[a];
|
|
|
|
if (face.mNumIndices != 2) continue;
|
|
|
|
for( size_t b = 0; b < face.mNumIndices; ++b )
|
|
|
|
mOutput << face.mIndices[b] << " ";
|
|
|
|
}
|
|
|
|
mOutput << "</p>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</lines>" << endstr;
|
|
|
|
}
|
|
|
|
|
2014-06-10 22:41:18 +00:00
|
|
|
// triangle - dont use it, because compatibility problems
|
2014-05-31 10:50:07 +00:00
|
|
|
|
|
|
|
// polygons
|
|
|
|
if (countPoly)
|
|
|
|
{
|
2014-06-09 13:17:45 +00:00
|
|
|
mOutput << startstr << "<polylist count=\"" << countPoly << "\" material=\"defaultMaterial\">" << endstr;
|
2014-05-31 10:50:07 +00:00
|
|
|
PushTag();
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<input offset=\"0\" semantic=\"VERTEX\" source=\"#" << idstrEscaped << "-vertices\" />" << endstr;
|
2014-05-31 10:50:07 +00:00
|
|
|
|
|
|
|
mOutput << startstr << "<vcount>";
|
|
|
|
for( size_t a = 0; a < mesh->mNumFaces; ++a )
|
|
|
|
{
|
2014-06-10 22:41:18 +00:00
|
|
|
if (mesh->mFaces[a].mNumIndices < 3) continue;
|
2014-05-31 10:50:07 +00:00
|
|
|
mOutput << mesh->mFaces[a].mNumIndices << " ";
|
|
|
|
}
|
|
|
|
mOutput << "</vcount>" << endstr;
|
|
|
|
|
|
|
|
mOutput << startstr << "<p>";
|
|
|
|
for( size_t a = 0; a < mesh->mNumFaces; ++a )
|
|
|
|
{
|
|
|
|
const aiFace& face = mesh->mFaces[a];
|
2014-06-10 22:41:18 +00:00
|
|
|
if (face.mNumIndices < 3) continue;
|
2014-05-31 10:50:07 +00:00
|
|
|
for( size_t b = 0; b < face.mNumIndices; ++b )
|
|
|
|
mOutput << face.mIndices[b] << " ";
|
|
|
|
}
|
|
|
|
mOutput << "</p>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</polylist>" << endstr;
|
2011-01-21 15:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// closing tags
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</mesh>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</geometry>" << 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";
|
|
|
|
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<source id=\"" << XMLEscape(pIdString) << "\" name=\"" << XMLEscape(pIdString) << "\">" << endstr;
|
2011-01-21 15:37:34 +00:00
|
|
|
PushTag();
|
|
|
|
|
|
|
|
// source array
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<float_array id=\"" << XMLEscape(arrayId) << "\" count=\"" << pElementCount * floatsPerElement << "\"> ";
|
2011-01-21 15:37:34 +00:00
|
|
|
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 << "</float_array>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
|
2013-03-21 18:54:55 +00:00
|
|
|
// the usual Collada fun. Let's bloat it even more!
|
2011-01-21 15:37:34 +00:00
|
|
|
mOutput << startstr << "<technique_common>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<accessor count=\"" << pElementCount << "\" offset=\"0\" source=\"#" << arrayId << "\" stride=\"" << floatsPerElement << "\">" << endstr;
|
|
|
|
PushTag();
|
|
|
|
|
|
|
|
switch( pType )
|
|
|
|
{
|
|
|
|
case FloatType_Vector:
|
|
|
|
mOutput << startstr << "<param name=\"X\" type=\"float\" />" << endstr;
|
|
|
|
mOutput << startstr << "<param name=\"Y\" type=\"float\" />" << endstr;
|
|
|
|
mOutput << startstr << "<param name=\"Z\" type=\"float\" />" << endstr;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FloatType_TexCoord2:
|
|
|
|
mOutput << startstr << "<param name=\"S\" type=\"float\" />" << endstr;
|
|
|
|
mOutput << startstr << "<param name=\"T\" type=\"float\" />" << endstr;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FloatType_TexCoord3:
|
|
|
|
mOutput << startstr << "<param name=\"S\" type=\"float\" />" << endstr;
|
|
|
|
mOutput << startstr << "<param name=\"T\" type=\"float\" />" << endstr;
|
|
|
|
mOutput << startstr << "<param name=\"P\" type=\"float\" />" << endstr;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FloatType_Color:
|
|
|
|
mOutput << startstr << "<param name=\"R\" type=\"float\" />" << endstr;
|
|
|
|
mOutput << startstr << "<param name=\"G\" type=\"float\" />" << endstr;
|
|
|
|
mOutput << startstr << "<param name=\"B\" type=\"float\" />" << endstr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</accessor>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</technique_common>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</source>" << endstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes the scene library
|
|
|
|
void ColladaExporter::WriteSceneLibrary()
|
|
|
|
{
|
2014-08-23 22:42:47 +00:00
|
|
|
const std::string scene_name_escaped = XMLEscape(mScene->mRootNode->mName.C_Str());
|
2013-10-02 13:48:28 +00:00
|
|
|
|
2011-01-21 15:37:34 +00:00
|
|
|
mOutput << startstr << "<library_visual_scenes>" << endstr;
|
|
|
|
PushTag();
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<visual_scene id=\"" + scene_name_escaped + "\" name=\"" + scene_name_escaped + "\">" << endstr;
|
2011-01-21 15:37:34 +00:00
|
|
|
PushTag();
|
|
|
|
|
|
|
|
// start recursive write at the root node
|
2013-10-02 13:48:28 +00:00
|
|
|
for( size_t a = 0; a < mScene->mRootNode->mNumChildren; ++a )
|
|
|
|
WriteNode( mScene->mRootNode->mChildren[a]);
|
2011-01-21 15:37:34 +00:00
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</visual_scene>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</library_visual_scenes>" << endstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Recursively writes the given node
|
2014-06-09 13:17:45 +00:00
|
|
|
void ColladaExporter::WriteNode(aiNode* pNode)
|
2011-01-21 15:37:34 +00:00
|
|
|
{
|
2014-06-09 13:17:45 +00:00
|
|
|
// the must have a name
|
|
|
|
if (pNode->mName.length == 0)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Node_" << pNode;
|
|
|
|
pNode->mName.Set(ss.str());
|
|
|
|
}
|
|
|
|
|
2014-08-23 22:42:47 +00:00
|
|
|
const std::string node_name_escaped = XMLEscape(pNode->mName.data);
|
|
|
|
mOutput << startstr << "<node id=\"" << node_name_escaped << "\" name=\"" << node_name_escaped << "\">" << endstr;
|
2013-10-02 13:48:28 +00:00
|
|
|
PushTag();
|
2011-01-21 15:37:34 +00:00
|
|
|
|
2013-10-02 13:48:28 +00:00
|
|
|
// 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 << "<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;
|
2012-02-17 10:48:25 +00:00
|
|
|
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<instance_geometry url=\"#" << XMLEscape(GetMeshId( pNode->mMeshes[a])) << "\">" << endstr;
|
2011-01-21 15:37:34 +00:00
|
|
|
PushTag();
|
2013-10-02 13:48:28 +00:00
|
|
|
mOutput << startstr << "<bind_material>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<technique_common>" << endstr;
|
|
|
|
PushTag();
|
2014-08-23 22:42:47 +00:00
|
|
|
mOutput << startstr << "<instance_material symbol=\"defaultMaterial\" target=\"#" << XMLEscape(materials[mesh->mMaterialIndex].name) << "\" />" << endstr;
|
2013-10-02 09:25:04 +00:00
|
|
|
PopTag();
|
2013-10-02 13:48:28 +00:00
|
|
|
mOutput << startstr << "</technique_common>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</bind_material>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</instance_geometry>" << endstr;
|
2011-01-21 15:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// recurse into subnodes
|
|
|
|
for( size_t a = 0; a < pNode->mNumChildren; ++a )
|
|
|
|
WriteNode( pNode->mChildren[a]);
|
|
|
|
|
2013-10-02 13:48:28 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</node>" << endstr;
|
2011-01-21 15:37:34 +00:00
|
|
|
}
|
2011-04-03 11:44:09 +00:00
|
|
|
|
|
|
|
#endif
|
2011-07-19 21:41:59 +00:00
|
|
|
#endif
|
2011-04-03 11:44:09 +00:00
|
|
|
|