2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2022-01-10 20:13:43 +00:00
|
|
|
Copyright (c) 2006-2022, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2017-05-09 17:57:36 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
2015-05-19 03:48:29 +00:00
|
|
|
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.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-05-19 03:48:29 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
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
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_EXPORT
|
|
|
|
#ifndef ASSIMP_BUILD_NO_COLLADA_EXPORTER
|
|
|
|
|
2017-02-22 16:20:26 +00:00
|
|
|
#include "ColladaExporter.h"
|
2020-05-01 10:39:43 +00:00
|
|
|
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/Bitmap.h>
|
2020-04-29 16:17:46 +00:00
|
|
|
#include <assimp/ColladaMetaData.h>
|
2020-04-28 16:46:07 +00:00
|
|
|
#include <assimp/DefaultIOSystem.h>
|
2020-05-01 10:39:43 +00:00
|
|
|
#include <assimp/Exceptional.h>
|
2019-09-15 17:25:37 +00:00
|
|
|
#include <assimp/MathFunctions.h>
|
2017-06-21 13:24:06 +00:00
|
|
|
#include <assimp/SceneCombiner.h>
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/StringUtils.h>
|
|
|
|
#include <assimp/XMLTools.h>
|
2020-04-28 16:46:07 +00:00
|
|
|
#include <assimp/commonMetaData.h>
|
|
|
|
#include <assimp/fast_atof.h>
|
2016-06-06 20:04:29 +00:00
|
|
|
#include <assimp/scene.h>
|
2020-04-28 16:46:07 +00:00
|
|
|
#include <assimp/Exporter.hpp>
|
|
|
|
#include <assimp/IOSystem.hpp>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
#include <ctime>
|
2020-04-28 16:46:07 +00:00
|
|
|
#include <memory>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2019-04-24 07:03:59 +00:00
|
|
|
namespace Assimp {
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Worker function for exporting a scene to Collada. Prototyped and registered in Exporter.cpp
|
2020-04-28 16:46:07 +00:00
|
|
|
void ExportSceneCollada(const char *pFile, IOSystem *pIOSystem, const aiScene *pScene, const ExportProperties * /*pProperties*/) {
|
2015-05-19 03:57:13 +00:00
|
|
|
std::string path = DefaultIOSystem::absolutePath(std::string(pFile));
|
|
|
|
std::string file = DefaultIOSystem::completeBaseName(std::string(pFile));
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// invoke the exporter
|
2020-04-28 16:46:07 +00:00
|
|
|
ColladaExporter iDoTheExportThing(pScene, pIOSystem, path, file);
|
|
|
|
|
2017-10-09 13:47:17 +00:00
|
|
|
if (iDoTheExportThing.mOutput.fail()) {
|
|
|
|
throw DeadlyExportError("output data creation failed. Most likely the file became too large: " + std::string(pFile));
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// we're still here - export successfully completed. Write result to the given IOSYstem
|
2020-04-28 16:46:07 +00:00
|
|
|
std::unique_ptr<IOStream> outfile(pIOSystem->Open(pFile, "wt"));
|
2020-05-01 10:39:43 +00:00
|
|
|
if (outfile == nullptr) {
|
2015-05-19 03:57:13 +00:00
|
|
|
throw DeadlyExportError("could not open output .dae file: " + std::string(pFile));
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// XXX maybe use a small wrapper around IOStream that behaves like std::stringstream in order to avoid the extra copy.
|
2020-04-28 16:46:07 +00:00
|
|
|
outfile->Write(iDoTheExportThing.mOutput.str().c_str(), static_cast<size_t>(iDoTheExportThing.mOutput.tellp()), 1);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 23:32:53 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2019-10-02 17:18:48 +00:00
|
|
|
// Encodes a string into a valid XML ID using the xsd:ID schema qualifications.
|
2020-04-28 16:46:07 +00:00
|
|
|
static const std::string XMLIDEncode(const std::string &name) {
|
2019-10-01 23:32:53 +00:00
|
|
|
const char XML_ID_CHARS[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-.";
|
|
|
|
const unsigned int XML_ID_CHARS_COUNT = sizeof(XML_ID_CHARS) / sizeof(char);
|
|
|
|
|
2019-10-19 10:08:57 +00:00
|
|
|
if (name.length() == 0) {
|
2019-10-01 23:32:53 +00:00
|
|
|
return name;
|
2019-10-19 10:08:57 +00:00
|
|
|
}
|
2019-10-01 23:32:53 +00:00
|
|
|
|
|
|
|
std::stringstream idEncoded;
|
|
|
|
|
|
|
|
// xsd:ID must start with letter or underscore
|
2019-10-19 10:08:57 +00:00
|
|
|
if (!((name[0] >= 'A' && name[0] <= 'z') || name[0] == '_')) {
|
2019-10-01 23:32:53 +00:00
|
|
|
idEncoded << '_';
|
2019-10-19 10:08:57 +00:00
|
|
|
}
|
2019-10-01 23:32:53 +00:00
|
|
|
|
2019-10-19 10:08:57 +00:00
|
|
|
for (std::string::const_iterator it = name.begin(); it != name.end(); ++it) {
|
2019-10-01 23:32:53 +00:00
|
|
|
// xsd:ID can only contain letters, digits, underscores, hyphens and periods
|
2019-10-19 10:08:57 +00:00
|
|
|
if (strchr(XML_ID_CHARS, *it) != nullptr) {
|
2019-10-01 23:32:53 +00:00
|
|
|
idEncoded << *it;
|
2019-10-19 10:08:57 +00:00
|
|
|
} else {
|
2020-04-29 16:17:46 +00:00
|
|
|
// Select placeholder character based on invalid character to reduce ID collisions
|
2019-10-01 23:32:53 +00:00
|
|
|
idEncoded << XML_ID_CHARS[(*it) % XML_ID_CHARS_COUNT];
|
2019-10-19 10:08:57 +00:00
|
|
|
}
|
2019-10-01 23:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return idEncoded.str();
|
|
|
|
}
|
|
|
|
|
2020-05-05 09:16:13 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Helper functions to create unique ids
|
|
|
|
inline bool IsUniqueId(const std::unordered_set<std::string> &idSet, const std::string &idStr) {
|
|
|
|
return (idSet.find(idStr) == idSet.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string MakeUniqueId(const std::unordered_set<std::string> &idSet, const std::string &idPrefix, const std::string &postfix) {
|
|
|
|
std::string result(idPrefix + postfix);
|
|
|
|
if (!IsUniqueId(idSet, result)) {
|
|
|
|
// Select a number to append
|
|
|
|
size_t idnum = 1;
|
|
|
|
do {
|
2021-03-09 20:08:28 +00:00
|
|
|
result = idPrefix + '_' + ai_to_string(idnum) + postfix;
|
2020-05-05 09:16:13 +00:00
|
|
|
++idnum;
|
|
|
|
} while (!IsUniqueId(idSet, result));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor for a specific scene to export
|
2020-04-28 16:46:07 +00:00
|
|
|
ColladaExporter::ColladaExporter(const aiScene *pScene, IOSystem *pIOSystem, const std::string &path, const std::string &file) :
|
|
|
|
mIOSystem(pIOSystem),
|
|
|
|
mPath(path),
|
2020-05-05 09:16:13 +00:00
|
|
|
mFile(file),
|
|
|
|
mScene(pScene),
|
|
|
|
endstr("\n") {
|
2015-05-19 03:57:13 +00:00
|
|
|
// make sure that all formatting happens using the standard, C locale and not the user's current locale
|
2020-04-28 16:46:07 +00:00
|
|
|
mOutput.imbue(std::locale("C"));
|
2019-06-20 17:11:11 +00:00
|
|
|
mOutput.precision(ASSIMP_AI_REAL_TEXT_PRECISION);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2017-10-16 16:51:25 +00:00
|
|
|
// start writing the file
|
2015-05-19 03:57:13 +00:00
|
|
|
WriteFile();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor
|
2019-04-24 07:03:59 +00:00
|
|
|
ColladaExporter::~ColladaExporter() {
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Starts writing the contents
|
2019-04-24 07:03:59 +00:00
|
|
|
void ColladaExporter::WriteFile() {
|
2015-05-19 03:57:13 +00:00
|
|
|
// write the DTD
|
|
|
|
mOutput << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>" << endstr;
|
|
|
|
// COLLADA element start
|
|
|
|
mOutput << "<COLLADA xmlns=\"http://www.collada.org/2005/11/COLLADASchema\" version=\"1.4.1\">" << endstr;
|
|
|
|
PushTag();
|
|
|
|
|
|
|
|
WriteTextures();
|
|
|
|
WriteHeader();
|
|
|
|
|
2020-04-30 17:28:06 +00:00
|
|
|
// Add node names to the unique id database first so they are most likely to use their names as unique ids
|
|
|
|
CreateNodeIds(mScene->mRootNode);
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
WriteCamerasLibrary();
|
|
|
|
WriteLightsLibrary();
|
|
|
|
WriteMaterials();
|
|
|
|
WriteGeometryLibrary();
|
2017-03-21 08:21:49 +00:00
|
|
|
WriteControllerLibrary();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
WriteSceneLibrary();
|
2020-04-28 16:46:07 +00:00
|
|
|
|
|
|
|
// customized, Writes the animation library
|
|
|
|
WriteAnimationsLibrary();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-05-05 09:16:13 +00:00
|
|
|
// instantiate the scene(s)
|
|
|
|
// For Assimp there will only ever be one
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<scene>" << endstr;
|
|
|
|
PushTag();
|
2020-05-05 09:16:13 +00:00
|
|
|
mOutput << startstr << "<instance_visual_scene url=\"#" + mSceneId + "\" />" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</scene>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << "</COLLADA>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes the asset header
|
2019-04-24 07:03:59 +00:00
|
|
|
void ColladaExporter::WriteHeader() {
|
2019-09-15 17:25:37 +00:00
|
|
|
static const ai_real epsilon = Math::getEpsilon<ai_real>();
|
2015-05-19 03:57:13 +00:00
|
|
|
static const aiQuaternion x_rot(aiMatrix3x3(
|
2020-04-28 16:46:07 +00:00
|
|
|
0, -1, 0,
|
|
|
|
1, 0, 0,
|
|
|
|
0, 0, 1));
|
2015-05-19 03:57:13 +00:00
|
|
|
static const aiQuaternion y_rot(aiMatrix3x3(
|
2020-04-28 16:46:07 +00:00
|
|
|
1, 0, 0,
|
|
|
|
0, 1, 0,
|
|
|
|
0, 0, 1));
|
2015-05-19 03:57:13 +00:00
|
|
|
static const aiQuaternion z_rot(aiMatrix3x3(
|
2020-04-28 16:46:07 +00:00
|
|
|
1, 0, 0,
|
|
|
|
0, 0, 1,
|
|
|
|
0, -1, 0));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
static const unsigned int date_nb_chars = 20;
|
|
|
|
char date_str[date_nb_chars];
|
2020-05-01 10:39:43 +00:00
|
|
|
std::time_t date = std::time(nullptr);
|
2015-05-19 03:57:13 +00:00
|
|
|
std::strftime(date_str, date_nb_chars, "%Y-%m-%dT%H:%M:%S", std::localtime(&date));
|
|
|
|
|
|
|
|
aiVector3D scaling;
|
|
|
|
aiQuaternion rotation;
|
|
|
|
aiVector3D position;
|
|
|
|
mScene->mRootNode->mTransformation.Decompose(scaling, rotation, position);
|
|
|
|
rotation.Normalize();
|
|
|
|
|
2020-05-05 09:16:13 +00:00
|
|
|
mAdd_root_node = false;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2016-07-16 02:14:36 +00:00
|
|
|
ai_real scale = 1.0;
|
2020-04-28 16:46: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) {
|
|
|
|
scale = (ai_real)((((double)scaling.x) + ((double)scaling.y) + ((double)scaling.z)) / 3.0);
|
2015-05-19 03:57:13 +00:00
|
|
|
} else {
|
2020-05-05 09:16:13 +00:00
|
|
|
mAdd_root_node = true;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string up_axis = "Y_UP";
|
2020-04-28 16:46:07 +00:00
|
|
|
if (rotation.Equal(x_rot, epsilon)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
up_axis = "X_UP";
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (rotation.Equal(y_rot, epsilon)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
up_axis = "Y_UP";
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (rotation.Equal(z_rot, epsilon)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
up_axis = "Z_UP";
|
|
|
|
} else {
|
2020-05-05 09:16:13 +00:00
|
|
|
mAdd_root_node = true;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
if (!position.Equal(aiVector3D(0, 0, 0))) {
|
2020-05-05 09:16:13 +00:00
|
|
|
mAdd_root_node = true;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 16:47:09 +00:00
|
|
|
// Assimp root nodes can have meshes, Collada Scenes cannot
|
|
|
|
if (mScene->mRootNode->mNumChildren == 0 || mScene->mRootNode->mMeshes != 0) {
|
2020-05-05 09:16:13 +00:00
|
|
|
mAdd_root_node = true;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 09:16:13 +00:00
|
|
|
if (mAdd_root_node) {
|
2015-05-19 03:57:13 +00:00
|
|
|
up_axis = "Y_UP";
|
|
|
|
scale = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mOutput << startstr << "<asset>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<contributor>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
|
2019-04-09 15:28:15 +00:00
|
|
|
// If no Scene metadata, use root node metadata
|
2020-04-28 16:46:07 +00:00
|
|
|
aiMetadata *meta = mScene->mMetaData;
|
2019-04-24 07:03:59 +00:00
|
|
|
if (nullptr == meta) {
|
2019-04-09 15:28:15 +00:00
|
|
|
meta = mScene->mRootNode->mMetaData;
|
2019-04-24 07:03:59 +00:00
|
|
|
}
|
2019-04-09 15:28:15 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
aiString value;
|
2019-04-24 07:03:59 +00:00
|
|
|
if (!meta || !meta->Get("Author", value)) {
|
2020-04-28 16:46:07 +00:00
|
|
|
mOutput << startstr << "<author>"
|
|
|
|
<< "Assimp"
|
|
|
|
<< "</author>" << endstr;
|
2019-04-24 07:03:59 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<author>" << XMLEscape(value.C_Str()) << "</author>" << endstr;
|
2019-04-24 07:03:59 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-12-09 14:05:41 +00:00
|
|
|
if (nullptr == meta || !meta->Get(AI_METADATA_SOURCE_GENERATOR, value)) {
|
2020-04-28 16:46:07 +00:00
|
|
|
mOutput << startstr << "<authoring_tool>"
|
|
|
|
<< "Assimp Exporter"
|
|
|
|
<< "</authoring_tool>" << endstr;
|
2019-04-24 07:03:59 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<authoring_tool>" << XMLEscape(value.C_Str()) << "</authoring_tool>" << endstr;
|
2019-04-24 07:03:59 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-04-24 07:03:59 +00:00
|
|
|
if (meta) {
|
|
|
|
if (meta->Get("Comments", value)) {
|
2019-04-09 15:28:15 +00:00
|
|
|
mOutput << startstr << "<comments>" << XMLEscape(value.C_Str()) << "</comments>" << endstr;
|
2019-04-24 07:03:59 +00:00
|
|
|
}
|
2019-12-09 14:05:41 +00:00
|
|
|
if (meta->Get(AI_METADATA_SOURCE_COPYRIGHT, value)) {
|
2019-04-09 15:28:15 +00:00
|
|
|
mOutput << startstr << "<copyright>" << XMLEscape(value.C_Str()) << "</copyright>" << endstr;
|
2019-04-24 07:03:59 +00:00
|
|
|
}
|
|
|
|
if (meta->Get("SourceData", value)) {
|
2019-04-09 15:28:15 +00:00
|
|
|
mOutput << startstr << "<source_data>" << XMLEscape(value.C_Str()) << "</source_data>" << endstr;
|
2019-04-24 07:03:59 +00:00
|
|
|
}
|
2019-04-09 15:28:15 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</contributor>" << endstr;
|
2019-04-09 15:28:15 +00:00
|
|
|
|
2019-04-24 07:03:59 +00:00
|
|
|
if (nullptr == meta || !meta->Get("Created", value)) {
|
2019-04-09 15:28:15 +00:00
|
|
|
mOutput << startstr << "<created>" << date_str << "</created>" << endstr;
|
2019-04-24 07:03:59 +00:00
|
|
|
} else {
|
2019-04-09 15:28:15 +00:00
|
|
|
mOutput << startstr << "<created>" << XMLEscape(value.C_Str()) << "</created>" << endstr;
|
2019-04-24 07:03:59 +00:00
|
|
|
}
|
2019-04-09 15:28:15 +00:00
|
|
|
|
|
|
|
// Modified date is always the date saved
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<modified>" << date_str << "</modified>" << endstr;
|
2019-04-09 15:28:15 +00:00
|
|
|
|
2019-04-24 07:03:59 +00:00
|
|
|
if (meta) {
|
|
|
|
if (meta->Get("Keywords", value)) {
|
2019-04-09 15:28:15 +00:00
|
|
|
mOutput << startstr << "<keywords>" << XMLEscape(value.C_Str()) << "</keywords>" << endstr;
|
2019-04-24 07:03:59 +00:00
|
|
|
}
|
|
|
|
if (meta->Get("Revision", value)) {
|
2019-04-09 15:28:15 +00:00
|
|
|
mOutput << startstr << "<revision>" << XMLEscape(value.C_Str()) << "</revision>" << endstr;
|
2019-04-24 07:03:59 +00:00
|
|
|
}
|
|
|
|
if (meta->Get("Subject", value)) {
|
2019-04-09 15:28:15 +00:00
|
|
|
mOutput << startstr << "<subject>" << XMLEscape(value.C_Str()) << "</subject>" << endstr;
|
2019-04-24 07:03:59 +00:00
|
|
|
}
|
|
|
|
if (meta->Get("Title", value)) {
|
2019-04-09 15:28:15 +00:00
|
|
|
mOutput << startstr << "<title>" << XMLEscape(value.C_Str()) << "</title>" << endstr;
|
2019-04-24 07:03:59 +00:00
|
|
|
}
|
2019-04-09 15:28:15 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<unit name=\"meter\" meter=\"" << scale << "\" />" << endstr;
|
|
|
|
mOutput << startstr << "<up_axis>" << up_axis << "</up_axis>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</asset>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Write the embedded textures
|
|
|
|
void ColladaExporter::WriteTextures() {
|
2015-05-19 03:57:13 +00:00
|
|
|
static const unsigned int buffer_size = 1024;
|
|
|
|
char str[buffer_size];
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2019-04-24 07:03:59 +00:00
|
|
|
if (mScene->HasTextures()) {
|
2020-04-28 16:46:07 +00:00
|
|
|
for (unsigned int i = 0; i < mScene->mNumTextures; i++) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// 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.
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
aiTexture *texture = mScene->mTextures[i];
|
|
|
|
if (nullptr == texture) {
|
2019-04-24 07:03:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
ASSIMP_itoa10(str, buffer_size, i + 1);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
std::string name = mFile + "_texture_" + (i < 1000 ? "0" : "") + (i < 100 ? "0" : "") + (i < 10 ? "0" : "") + str + "." + ((const char *)texture->achFormatHint);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2019-09-18 15:54:30 +00:00
|
|
|
std::unique_ptr<IOStream> outfile(mIOSystem->Open(mPath + mIOSystem->getOsSeparator() + name, "wb"));
|
2020-05-01 10:39:43 +00:00
|
|
|
if (outfile == nullptr) {
|
2015-05-19 03:57:13 +00:00
|
|
|
throw DeadlyExportError("could not open output texture file: " + mPath + name);
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
if (texture->mHeight == 0) {
|
|
|
|
outfile->Write((void *)texture->pcData, texture->mWidth, 1);
|
2015-05-19 03:57:13 +00:00
|
|
|
} else {
|
|
|
|
Bitmap::Save(texture, outfile.get());
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
outfile->Flush();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
textures.insert(std::make_pair(i, name));
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Write the embedded textures
|
|
|
|
void ColladaExporter::WriteCamerasLibrary() {
|
2020-04-28 16:46:07 +00:00
|
|
|
if (mScene->HasCameras()) {
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<library_cameras>" << endstr;
|
|
|
|
PushTag();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t a = 0; a < mScene->mNumCameras; ++a)
|
|
|
|
WriteCamera(a);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</library_cameras>" << endstr;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaExporter::WriteCamera(size_t pIndex) {
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
const aiCamera *cam = mScene->mCameras[pIndex];
|
2020-04-30 17:28:06 +00:00
|
|
|
const std::string cameraId = GetObjectUniqueId(AiObjectType::Camera, pIndex);
|
|
|
|
const std::string cameraName = GetObjectName(AiObjectType::Camera, pIndex);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-05-01 10:13:38 +00:00
|
|
|
mOutput << startstr << "<camera id=\"" << cameraId << "\" name=\"" << cameraName << "\" >" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<optics>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<technique_common>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
//assimp doesn't support the import of orthographic cameras! se we write
|
|
|
|
//always perspective
|
|
|
|
mOutput << startstr << "<perspective>" << endstr;
|
|
|
|
PushTag();
|
2020-04-28 16:46:07 +00:00
|
|
|
mOutput << startstr << "<xfov sid=\"xfov\">" << AI_RAD_TO_DEG(cam->mHorizontalFOV)
|
|
|
|
<< "</xfov>" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<aspect_ratio>"
|
2020-04-28 16:46:07 +00:00
|
|
|
<< cam->mAspect
|
|
|
|
<< "</aspect_ratio>" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<znear sid=\"znear\">"
|
2020-04-28 16:46:07 +00:00
|
|
|
<< cam->mClipPlaneNear
|
|
|
|
<< "</znear>" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<zfar sid=\"zfar\">"
|
2020-04-28 16:46:07 +00:00
|
|
|
<< cam->mClipPlaneFar
|
|
|
|
<< "</zfar>" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</perspective>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</technique_common>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</optics>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</camera>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Write the embedded textures
|
|
|
|
void ColladaExporter::WriteLightsLibrary() {
|
2020-04-28 16:46:07 +00:00
|
|
|
if (mScene->HasLights()) {
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<library_lights>" << endstr;
|
|
|
|
PushTag();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t a = 0; a < mScene->mNumLights; ++a)
|
|
|
|
WriteLight(a);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</library_lights>" << endstr;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaExporter::WriteLight(size_t pIndex) {
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
const aiLight *light = mScene->mLights[pIndex];
|
2020-04-30 17:28:06 +00:00
|
|
|
const std::string lightId = GetObjectUniqueId(AiObjectType::Light, pIndex);
|
|
|
|
const std::string lightName = GetObjectName(AiObjectType::Light, pIndex);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-05-01 10:13:38 +00:00
|
|
|
mOutput << startstr << "<light id=\"" << lightId << "\" name=\""
|
2019-10-01 23:32:53 +00:00
|
|
|
<< lightName << "\" >" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<technique_common>" << endstr;
|
|
|
|
PushTag();
|
2020-04-28 16:46:07 +00:00
|
|
|
switch (light->mType) {
|
|
|
|
case aiLightSource_AMBIENT:
|
|
|
|
WriteAmbienttLight(light);
|
|
|
|
break;
|
|
|
|
case aiLightSource_DIRECTIONAL:
|
|
|
|
WriteDirectionalLight(light);
|
|
|
|
break;
|
|
|
|
case aiLightSource_POINT:
|
|
|
|
WritePointLight(light);
|
|
|
|
break;
|
|
|
|
case aiLightSource_SPOT:
|
|
|
|
WriteSpotLight(light);
|
|
|
|
break;
|
|
|
|
case aiLightSource_AREA:
|
|
|
|
case aiLightSource_UNDEFINED:
|
|
|
|
case _aiLightSource_Force32Bit:
|
|
|
|
break;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</technique_common>" << endstr;
|
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</light>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaExporter::WritePointLight(const aiLight *const light) {
|
|
|
|
const aiColor3D &color = light->mColorDiffuse;
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<point>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<color sid=\"color\">"
|
2020-04-28 16:46:07 +00:00
|
|
|
<< color.r << " " << color.g << " " << color.b
|
|
|
|
<< "</color>" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<constant_attenuation>"
|
2020-04-28 16:46:07 +00:00
|
|
|
<< light->mAttenuationConstant
|
|
|
|
<< "</constant_attenuation>" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<linear_attenuation>"
|
2020-04-28 16:46:07 +00:00
|
|
|
<< light->mAttenuationLinear
|
|
|
|
<< "</linear_attenuation>" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<quadratic_attenuation>"
|
2020-04-28 16:46:07 +00:00
|
|
|
<< light->mAttenuationQuadratic
|
|
|
|
<< "</quadratic_attenuation>" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</point>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2019-04-24 07:03:59 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaExporter::WriteDirectionalLight(const aiLight *const light) {
|
|
|
|
const aiColor3D &color = light->mColorDiffuse;
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<directional>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<color sid=\"color\">"
|
2020-04-28 16:46:07 +00:00
|
|
|
<< color.r << " " << color.g << " " << color.b
|
|
|
|
<< "</color>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</directional>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2019-04-24 07:03:59 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaExporter::WriteSpotLight(const aiLight *const light) {
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
const aiColor3D &color = light->mColorDiffuse;
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<spot>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<color sid=\"color\">"
|
2020-04-28 16:46:07 +00:00
|
|
|
<< color.r << " " << color.g << " " << color.b
|
|
|
|
<< "</color>" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<constant_attenuation>"
|
2020-04-28 16:46:07 +00:00
|
|
|
<< light->mAttenuationConstant
|
|
|
|
<< "</constant_attenuation>" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<linear_attenuation>"
|
2020-04-28 16:46:07 +00:00
|
|
|
<< light->mAttenuationLinear
|
|
|
|
<< "</linear_attenuation>" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<quadratic_attenuation>"
|
2020-04-28 16:46:07 +00:00
|
|
|
<< light->mAttenuationQuadratic
|
|
|
|
<< "</quadratic_attenuation>" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
/*
|
|
|
|
out->mAngleOuterCone = AI_DEG_TO_RAD (std::acos(std::pow(0.1f,1.f/srcLight->mFalloffExponent))+
|
|
|
|
srcLight->mFalloffAngle);
|
|
|
|
*/
|
|
|
|
|
2016-07-16 02:14:36 +00:00
|
|
|
const ai_real fallOffAngle = AI_RAD_TO_DEG(light->mAngleInnerCone);
|
2020-04-28 16:46:07 +00:00
|
|
|
mOutput << startstr << "<falloff_angle sid=\"fall_off_angle\">"
|
|
|
|
<< fallOffAngle
|
|
|
|
<< "</falloff_angle>" << endstr;
|
|
|
|
double temp = light->mAngleOuterCone - light->mAngleInnerCone;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
temp = std::cos(temp);
|
2020-04-28 16:46:07 +00:00
|
|
|
temp = std::log(temp) / std::log(0.1);
|
|
|
|
temp = 1 / temp;
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<falloff_exponent sid=\"fall_off_exponent\">"
|
2020-04-28 16:46:07 +00:00
|
|
|
<< temp
|
|
|
|
<< "</falloff_exponent>" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</spot>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaExporter::WriteAmbienttLight(const aiLight *const light) {
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
const aiColor3D &color = light->mColorAmbient;
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<ambient>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<color sid=\"color\">"
|
2020-04-28 16:46:07 +00:00
|
|
|
<< color.r << " " << color.g << " " << color.b
|
|
|
|
<< "</color>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</ambient>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads a single surface entry from the given material keys
|
2020-04-30 17:28:06 +00:00
|
|
|
bool ColladaExporter::ReadMaterialSurface(Surface &poSurface, const aiMaterial &pSrcMat, aiTextureType pTexture, const char *pKey, size_t pType, size_t pIndex) {
|
|
|
|
if (pSrcMat.GetTextureCount(pTexture) > 0) {
|
2020-04-28 16:46:07 +00:00
|
|
|
aiString texfile;
|
|
|
|
unsigned int uvChannel = 0;
|
2020-05-01 10:39:43 +00:00
|
|
|
pSrcMat.GetTexture(pTexture, 0, &texfile, nullptr, &uvChannel);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
std::string index_str(texfile.C_Str());
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
if (index_str.size() != 0 && index_str[0] == '*') {
|
|
|
|
unsigned int index;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
index_str = index_str.substr(1, std::string::npos);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
try {
|
2020-10-02 12:58:55 +00:00
|
|
|
index = (unsigned int)strtoul10_64<DeadlyExportError>(index_str.c_str());
|
2020-04-28 16:46:07 +00:00
|
|
|
} catch (std::exception &error) {
|
|
|
|
throw DeadlyExportError(error.what());
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
std::map<unsigned int, std::string>::const_iterator name = textures.find(index);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
if (name != textures.end()) {
|
|
|
|
poSurface.texture = name->second;
|
|
|
|
} else {
|
|
|
|
throw DeadlyExportError("could not find embedded texture at index " + index_str);
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
} else {
|
2020-04-28 16:46:07 +00:00
|
|
|
poSurface.texture = texfile.C_Str();
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
|
|
|
|
poSurface.channel = uvChannel;
|
|
|
|
poSurface.exist = true;
|
2019-04-24 07:03:59 +00:00
|
|
|
} else {
|
2020-04-28 16:46:07 +00:00
|
|
|
if (pKey)
|
2020-04-30 17:28:06 +00:00
|
|
|
poSurface.exist = pSrcMat.Get(pKey, static_cast<unsigned int>(pType), static_cast<unsigned int>(pIndex), poSurface.color) == aiReturn_SUCCESS;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2020-04-30 17:28:06 +00:00
|
|
|
return poSurface.exist;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2015-12-31 13:41:53 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reimplementation of isalnum(,C locale), because AppVeyor does not see standard version.
|
2019-04-24 07:03:59 +00:00
|
|
|
static bool isalnum_C(char c) {
|
2020-04-28 16:46:07 +00:00
|
|
|
return (nullptr != strchr("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", c));
|
2015-12-31 13:41:53 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes an image entry for the given surface
|
2020-04-30 17:28:06 +00:00
|
|
|
void ColladaExporter::WriteImageEntry(const Surface &pSurface, const std::string &imageId) {
|
2020-04-28 16:46:07 +00:00
|
|
|
if (!pSurface.texture.empty()) {
|
2020-04-30 17:28:06 +00:00
|
|
|
mOutput << startstr << "<image id=\"" << imageId << "\">" << endstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<init_from>";
|
|
|
|
|
|
|
|
// URL encode image file name first, then XML encode on top
|
|
|
|
std::stringstream imageUrlEncoded;
|
|
|
|
for (std::string::const_iterator it = pSurface.texture.begin(); it != pSurface.texture.end(); ++it) {
|
|
|
|
if (isalnum_C((unsigned char)*it) || *it == ':' || *it == '_' || *it == '-' || *it == '.' || *it == '/' || *it == '\\')
|
|
|
|
imageUrlEncoded << *it;
|
|
|
|
else
|
|
|
|
imageUrlEncoded << '%' << std::hex << size_t((unsigned char)*it) << std::dec;
|
|
|
|
}
|
|
|
|
mOutput << XMLEscape(imageUrlEncoded.str());
|
|
|
|
mOutput << "</init_from>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</image>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes a color-or-texture entry into an effect definition
|
2020-04-30 17:28:06 +00:00
|
|
|
void ColladaExporter::WriteTextureColorEntry(const Surface &pSurface, const std::string &pTypeName, const std::string &imageId) {
|
2020-04-28 16:46:07 +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;
|
|
|
|
} else {
|
2020-04-30 17:28:06 +00:00
|
|
|
mOutput << startstr << "<texture texture=\"" << imageId << "\" texcoord=\"CHANNEL" << pSurface.channel << "\" />" << endstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
}
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</" << pTypeName << ">" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes the two parameters necessary for referencing a texture in an effect entry
|
2020-04-30 17:28:06 +00:00
|
|
|
void ColladaExporter::WriteTextureParamEntry(const Surface &pSurface, const std::string &pTypeName, const std::string &materialId) {
|
2020-04-28 16:46:07 +00:00
|
|
|
// if surface is a texture, write out the sampler and the surface parameters necessary to reference the texture
|
|
|
|
if (!pSurface.texture.empty()) {
|
2020-04-30 17:28:06 +00:00
|
|
|
mOutput << startstr << "<newparam sid=\"" << materialId << "-" << pTypeName << "-surface\">" << endstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<surface type=\"2D\">" << endstr;
|
|
|
|
PushTag();
|
2020-04-30 17:28:06 +00:00
|
|
|
mOutput << startstr << "<init_from>" << materialId << "-" << pTypeName << "-image</init_from>" << endstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</surface>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</newparam>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-30 17:28:06 +00:00
|
|
|
mOutput << startstr << "<newparam sid=\"" << materialId << "-" << pTypeName << "-sampler\">" << endstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<sampler2D>" << endstr;
|
|
|
|
PushTag();
|
2020-04-30 17:28:06 +00:00
|
|
|
mOutput << startstr << "<source>" << materialId << "-" << pTypeName << "-surface</source>" << endstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</sampler2D>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</newparam>" << endstr;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes a scalar property
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaExporter::WriteFloatEntry(const Property &pProperty, const std::string &pTypeName) {
|
|
|
|
if (pProperty.exist) {
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<" << pTypeName << ">" << endstr;
|
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<float sid=\"" << pTypeName << "\">" << pProperty.value << "</float>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</" << pTypeName << ">" << endstr;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes the material setup
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaExporter::WriteMaterials() {
|
2020-04-30 17:28:06 +00:00
|
|
|
std::vector<Material> materials;
|
2020-04-28 16:46:07 +00:00
|
|
|
materials.resize(mScene->mNumMaterials);
|
|
|
|
|
|
|
|
/// collect all materials from the scene
|
|
|
|
size_t numTextures = 0;
|
|
|
|
for (size_t a = 0; a < mScene->mNumMaterials; ++a) {
|
2020-04-30 17:28:06 +00:00
|
|
|
Material &material = materials[a];
|
|
|
|
material.id = GetObjectUniqueId(AiObjectType::Material, a);
|
|
|
|
material.name = GetObjectName(AiObjectType::Material, a);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-30 17:28:06 +00:00
|
|
|
const aiMaterial &mat = *(mScene->mMaterials[a]);
|
2020-04-28 16:46:07 +00:00
|
|
|
aiShadingMode shading = aiShadingMode_Flat;
|
2020-04-30 17:28:06 +00:00
|
|
|
material.shading_model = "phong";
|
|
|
|
if (mat.Get(AI_MATKEY_SHADING_MODEL, shading) == aiReturn_SUCCESS) {
|
2020-04-28 16:46:07 +00:00
|
|
|
if (shading == aiShadingMode_Phong) {
|
2020-04-30 17:28:06 +00:00
|
|
|
material.shading_model = "phong";
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (shading == aiShadingMode_Blinn) {
|
2020-04-30 17:28:06 +00:00
|
|
|
material.shading_model = "blinn";
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (shading == aiShadingMode_NoShading) {
|
2020-04-30 17:28:06 +00:00
|
|
|
material.shading_model = "constant";
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (shading == aiShadingMode_Gouraud) {
|
2020-04-30 17:28:06 +00:00
|
|
|
material.shading_model = "lambert";
|
2020-04-28 16:46:07 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-30 17:28:06 +00:00
|
|
|
if (ReadMaterialSurface(material.ambient, mat, aiTextureType_AMBIENT, AI_MATKEY_COLOR_AMBIENT))
|
|
|
|
++numTextures;
|
|
|
|
if (ReadMaterialSurface(material.diffuse, mat, aiTextureType_DIFFUSE, AI_MATKEY_COLOR_DIFFUSE))
|
|
|
|
++numTextures;
|
|
|
|
if (ReadMaterialSurface(material.specular, mat, aiTextureType_SPECULAR, AI_MATKEY_COLOR_SPECULAR))
|
|
|
|
++numTextures;
|
|
|
|
if (ReadMaterialSurface(material.emissive, mat, aiTextureType_EMISSIVE, AI_MATKEY_COLOR_EMISSIVE))
|
|
|
|
++numTextures;
|
|
|
|
if (ReadMaterialSurface(material.reflective, mat, aiTextureType_REFLECTION, AI_MATKEY_COLOR_REFLECTIVE))
|
|
|
|
++numTextures;
|
|
|
|
if (ReadMaterialSurface(material.transparent, mat, aiTextureType_OPACITY, AI_MATKEY_COLOR_TRANSPARENT))
|
|
|
|
++numTextures;
|
|
|
|
if (ReadMaterialSurface(material.normal, mat, aiTextureType_NORMALS, nullptr, 0, 0))
|
|
|
|
++numTextures;
|
|
|
|
|
|
|
|
material.shininess.exist = mat.Get(AI_MATKEY_SHININESS, material.shininess.value) == aiReturn_SUCCESS;
|
|
|
|
material.transparency.exist = mat.Get(AI_MATKEY_OPACITY, material.transparency.value) == aiReturn_SUCCESS;
|
|
|
|
material.index_refraction.exist = mat.Get(AI_MATKEY_REFRACTI, material.index_refraction.value) == aiReturn_SUCCESS;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
// output textures if present
|
|
|
|
if (numTextures > 0) {
|
|
|
|
mOutput << startstr << "<library_images>" << endstr;
|
|
|
|
PushTag();
|
2020-04-30 17:28:06 +00:00
|
|
|
for (const Material &mat : materials) {
|
|
|
|
WriteImageEntry(mat.ambient, mat.id + "-ambient-image");
|
|
|
|
WriteImageEntry(mat.diffuse, mat.id + "-diffuse-image");
|
|
|
|
WriteImageEntry(mat.specular, mat.id + "-specular-image");
|
|
|
|
WriteImageEntry(mat.emissive, mat.id + "-emission-image");
|
|
|
|
WriteImageEntry(mat.reflective, mat.id + "-reflective-image");
|
|
|
|
WriteImageEntry(mat.transparent, mat.id + "-transparent-image");
|
|
|
|
WriteImageEntry(mat.normal, mat.id + "-normal-image");
|
2020-04-28 16:46:07 +00:00
|
|
|
}
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</library_images>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
// output effects - those are the actual carriers of information
|
|
|
|
if (!materials.empty()) {
|
|
|
|
mOutput << startstr << "<library_effects>" << endstr;
|
|
|
|
PushTag();
|
2020-04-30 17:28:06 +00:00
|
|
|
for (const Material &mat : materials) {
|
2020-04-28 16:46:07 +00:00
|
|
|
// this is so ridiculous it must be right
|
2020-04-30 17:28:06 +00:00
|
|
|
mOutput << startstr << "<effect id=\"" << mat.id << "-fx\" name=\"" << mat.name << "\">" << endstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<profile_COMMON>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
|
|
|
|
// write sampler- and surface params for the texture entries
|
2020-04-30 17:28:06 +00:00
|
|
|
WriteTextureParamEntry(mat.emissive, "emission", mat.id);
|
|
|
|
WriteTextureParamEntry(mat.ambient, "ambient", mat.id);
|
|
|
|
WriteTextureParamEntry(mat.diffuse, "diffuse", mat.id);
|
|
|
|
WriteTextureParamEntry(mat.specular, "specular", mat.id);
|
|
|
|
WriteTextureParamEntry(mat.reflective, "reflective", mat.id);
|
|
|
|
WriteTextureParamEntry(mat.transparent, "transparent", mat.id);
|
|
|
|
WriteTextureParamEntry(mat.normal, "normal", mat.id);
|
2020-04-28 16:46:07 +00:00
|
|
|
|
|
|
|
mOutput << startstr << "<technique sid=\"standard\">" << endstr;
|
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<" << mat.shading_model << ">" << endstr;
|
|
|
|
PushTag();
|
|
|
|
|
2020-04-30 17:28:06 +00:00
|
|
|
WriteTextureColorEntry(mat.emissive, "emission", mat.id + "-emission-sampler");
|
|
|
|
WriteTextureColorEntry(mat.ambient, "ambient", mat.id + "-ambient-sampler");
|
|
|
|
WriteTextureColorEntry(mat.diffuse, "diffuse", mat.id + "-diffuse-sampler");
|
|
|
|
WriteTextureColorEntry(mat.specular, "specular", mat.id + "-specular-sampler");
|
2020-04-28 16:46:07 +00:00
|
|
|
WriteFloatEntry(mat.shininess, "shininess");
|
2020-04-30 17:28:06 +00:00
|
|
|
WriteTextureColorEntry(mat.reflective, "reflective", mat.id + "-reflective-sampler");
|
|
|
|
WriteTextureColorEntry(mat.transparent, "transparent", mat.id + "-transparent-sampler");
|
2020-04-28 16:46:07 +00:00
|
|
|
WriteFloatEntry(mat.transparency, "transparency");
|
|
|
|
WriteFloatEntry(mat.index_refraction, "index_of_refraction");
|
|
|
|
|
|
|
|
if (!mat.normal.texture.empty()) {
|
2020-04-30 17:28:06 +00:00
|
|
|
WriteTextureColorEntry(mat.normal, "bump", mat.id + "-normal-sampler");
|
2020-04-28 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</" << mat.shading_model << ">" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</technique>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</profile_COMMON>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</effect>" << endstr;
|
|
|
|
}
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</library_effects>" << endstr;
|
|
|
|
|
|
|
|
// write materials - they're just effect references
|
|
|
|
mOutput << startstr << "<library_materials>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
for (std::vector<Material>::const_iterator it = materials.begin(); it != materials.end(); ++it) {
|
|
|
|
const Material &mat = *it;
|
2020-04-30 17:28:06 +00:00
|
|
|
mOutput << startstr << "<material id=\"" << mat.id << "\" name=\"" << mat.name << "\">" << endstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
PushTag();
|
2020-04-30 17:28:06 +00:00
|
|
|
mOutput << startstr << "<instance_effect url=\"#" << mat.id << "-fx\"/>" << endstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</material>" << endstr;
|
|
|
|
}
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</library_materials>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 08:21:49 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes the controller library
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaExporter::WriteControllerLibrary() {
|
2017-03-21 08:21:49 +00:00
|
|
|
mOutput << startstr << "<library_controllers>" << endstr;
|
|
|
|
PushTag();
|
2020-04-28 16:46:07 +00:00
|
|
|
|
|
|
|
for (size_t a = 0; a < mScene->mNumMeshes; ++a) {
|
|
|
|
WriteController(a);
|
2019-04-24 07:03:59 +00:00
|
|
|
}
|
2017-03-21 08:21:49 +00:00
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</library_controllers>" << endstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes a skin controller of the given mesh
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaExporter::WriteController(size_t pIndex) {
|
|
|
|
const aiMesh *mesh = mScene->mMeshes[pIndex];
|
2020-04-30 17:28:06 +00:00
|
|
|
// Is there a skin controller?
|
|
|
|
if (mesh->mNumBones == 0 || mesh->mNumFaces == 0 || mesh->mNumVertices == 0)
|
2017-03-21 08:21:49 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-30 17:28:06 +00:00
|
|
|
const std::string idstr = GetObjectUniqueId(AiObjectType::Mesh, pIndex);
|
|
|
|
const std::string namestr = GetObjectName(AiObjectType::Mesh, pIndex);
|
2017-03-21 08:21:49 +00:00
|
|
|
|
2020-04-29 16:17:46 +00:00
|
|
|
mOutput << startstr << "<controller id=\"" << idstr << "-skin\" ";
|
2020-04-28 16:46:07 +00:00
|
|
|
mOutput << "name=\"skinCluster" << pIndex << "\">" << endstr;
|
2017-03-21 08:21:49 +00:00
|
|
|
PushTag();
|
|
|
|
|
2020-04-29 16:17:46 +00:00
|
|
|
mOutput << startstr << "<skin source=\"#" << idstr << "\">" << endstr;
|
2017-03-21 08:21:49 +00:00
|
|
|
PushTag();
|
|
|
|
|
|
|
|
// bind pose matrix
|
|
|
|
mOutput << startstr << "<bind_shape_matrix>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
|
|
|
|
// I think it is identity in general cases.
|
2017-03-22 13:11:17 +00:00
|
|
|
aiMatrix4x4 mat;
|
2017-03-22 20:33:53 +00:00
|
|
|
mOutput << startstr << mat.a1 << " " << mat.a2 << " " << mat.a3 << " " << mat.a4 << endstr;
|
|
|
|
mOutput << startstr << mat.b1 << " " << mat.b2 << " " << mat.b3 << " " << mat.b4 << endstr;
|
|
|
|
mOutput << startstr << mat.c1 << " " << mat.c2 << " " << mat.c3 << " " << mat.c4 << endstr;
|
|
|
|
mOutput << startstr << mat.d1 << " " << mat.d2 << " " << mat.d3 << " " << mat.d4 << endstr;
|
2017-03-21 08:21:49 +00:00
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</bind_shape_matrix>" << endstr;
|
|
|
|
|
2020-04-29 16:17:46 +00:00
|
|
|
mOutput << startstr << "<source id=\"" << idstr << "-skin-joints\" name=\"" << namestr << "-skin-joints\">" << endstr;
|
2017-03-22 20:33:53 +00:00
|
|
|
PushTag();
|
|
|
|
|
2020-04-29 16:17:46 +00:00
|
|
|
mOutput << startstr << "<Name_array id=\"" << idstr << "-skin-joints-array\" count=\"" << mesh->mNumBones << "\">";
|
2017-03-21 08:21:49 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t i = 0; i < mesh->mNumBones; ++i)
|
2020-05-01 10:13:38 +00:00
|
|
|
mOutput << GetBoneUniqueId(mesh->mBones[i]) << ' ';
|
2017-03-21 08:21:49 +00:00
|
|
|
|
|
|
|
mOutput << "</Name_array>" << endstr;
|
|
|
|
|
|
|
|
mOutput << startstr << "<technique_common>" << endstr;
|
|
|
|
PushTag();
|
2020-04-28 16:46:07 +00:00
|
|
|
|
2020-04-29 16:17:46 +00:00
|
|
|
mOutput << startstr << "<accessor source=\"#" << idstr << "-skin-joints-array\" count=\"" << mesh->mNumBones << "\" stride=\"" << 1 << "\">" << endstr;
|
2017-03-21 08:21:49 +00:00
|
|
|
PushTag();
|
|
|
|
|
2017-03-22 20:33:53 +00:00
|
|
|
mOutput << startstr << "<param name=\"JOINT\" type=\"Name\"></param>" << endstr;
|
2017-03-21 08:21:49 +00:00
|
|
|
|
|
|
|
PopTag();
|
2017-03-22 20:33:53 +00:00
|
|
|
mOutput << startstr << "</accessor>" << endstr;
|
2017-03-21 08:21:49 +00:00
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</technique_common>" << endstr;
|
|
|
|
|
2017-03-22 20:33:53 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</source>" << endstr;
|
|
|
|
|
2017-03-22 09:06:55 +00:00
|
|
|
std::vector<ai_real> bind_poses;
|
|
|
|
bind_poses.reserve(mesh->mNumBones * 16);
|
2020-04-28 16:46:07 +00:00
|
|
|
for (unsigned int i = 0; i < mesh->mNumBones; ++i)
|
|
|
|
for (unsigned int j = 0; j < 4; ++j)
|
2017-03-22 20:33:53 +00:00
|
|
|
bind_poses.insert(bind_poses.end(), mesh->mBones[i]->mOffsetMatrix[j], mesh->mBones[i]->mOffsetMatrix[j] + 4);
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
WriteFloatArray(idstr + "-skin-bind_poses", FloatType_Mat4x4, (const ai_real *)bind_poses.data(), bind_poses.size() / 16);
|
2017-03-22 20:33:53 +00:00
|
|
|
|
|
|
|
bind_poses.clear();
|
2020-04-28 16:46:07 +00:00
|
|
|
|
2017-03-22 20:33:53 +00:00
|
|
|
std::vector<ai_real> skin_weights;
|
|
|
|
skin_weights.reserve(mesh->mNumVertices * mesh->mNumBones);
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t i = 0; i < mesh->mNumBones; ++i)
|
|
|
|
for (size_t j = 0; j < mesh->mBones[i]->mNumWeights; ++j)
|
2017-03-22 20:33:53 +00:00
|
|
|
skin_weights.push_back(mesh->mBones[i]->mWeights[j].mWeight);
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
WriteFloatArray(idstr + "-skin-weights", FloatType_Weight, (const ai_real *)skin_weights.data(), skin_weights.size());
|
2017-03-22 20:33:53 +00:00
|
|
|
|
|
|
|
skin_weights.clear();
|
|
|
|
|
|
|
|
mOutput << startstr << "<joints>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
|
2020-04-29 16:17:46 +00:00
|
|
|
mOutput << startstr << "<input semantic=\"JOINT\" source=\"#" << idstr << "-skin-joints\"></input>" << endstr;
|
|
|
|
mOutput << startstr << "<input semantic=\"INV_BIND_MATRIX\" source=\"#" << idstr << "-skin-bind_poses\"></input>" << endstr;
|
2017-03-22 20:33:53 +00:00
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</joints>" << endstr;
|
|
|
|
|
|
|
|
mOutput << startstr << "<vertex_weights count=\"" << mesh->mNumVertices << "\">" << endstr;
|
|
|
|
PushTag();
|
2017-03-22 09:06:55 +00:00
|
|
|
|
2020-04-29 16:17:46 +00:00
|
|
|
mOutput << startstr << "<input semantic=\"JOINT\" source=\"#" << idstr << "-skin-joints\" offset=\"0\"></input>" << endstr;
|
|
|
|
mOutput << startstr << "<input semantic=\"WEIGHT\" source=\"#" << idstr << "-skin-weights\" offset=\"1\"></input>" << endstr;
|
2017-03-22 20:33:53 +00:00
|
|
|
|
|
|
|
mOutput << startstr << "<vcount>";
|
|
|
|
|
|
|
|
std::vector<ai_uint> num_influences(mesh->mNumVertices, (ai_uint)0);
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t i = 0; i < mesh->mNumBones; ++i)
|
|
|
|
for (size_t j = 0; j < mesh->mBones[i]->mNumWeights; ++j)
|
2017-03-22 20:33:53 +00:00
|
|
|
++num_influences[mesh->mBones[i]->mWeights[j].mVertexId];
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t i = 0; i < mesh->mNumVertices; ++i)
|
2017-03-22 20:33:53 +00:00
|
|
|
mOutput << num_influences[i] << " ";
|
|
|
|
|
|
|
|
mOutput << "</vcount>" << endstr;
|
|
|
|
|
|
|
|
mOutput << startstr << "<v>";
|
|
|
|
|
|
|
|
ai_uint joint_weight_indices_length = 0;
|
|
|
|
std::vector<ai_uint> accum_influences;
|
|
|
|
accum_influences.reserve(num_influences.size());
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t i = 0; i < num_influences.size(); ++i) {
|
2017-03-22 20:33:53 +00:00
|
|
|
accum_influences.push_back(joint_weight_indices_length);
|
|
|
|
joint_weight_indices_length += num_influences[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
ai_uint weight_index = 0;
|
|
|
|
std::vector<ai_int> joint_weight_indices(2 * joint_weight_indices_length, (ai_int)-1);
|
2020-04-28 16:46:07 +00:00
|
|
|
for (unsigned int i = 0; i < mesh->mNumBones; ++i)
|
|
|
|
for (unsigned j = 0; j < mesh->mBones[i]->mNumWeights; ++j) {
|
2017-03-22 20:33:53 +00:00
|
|
|
unsigned int vId = mesh->mBones[i]->mWeights[j].mVertexId;
|
2020-04-28 16:46:07 +00:00
|
|
|
for (ai_uint k = 0; k < num_influences[vId]; ++k) {
|
|
|
|
if (joint_weight_indices[2 * (accum_influences[vId] + k)] == -1) {
|
2017-03-22 20:33:53 +00:00
|
|
|
joint_weight_indices[2 * (accum_influences[vId] + k)] = i;
|
|
|
|
joint_weight_indices[2 * (accum_influences[vId] + k) + 1] = weight_index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++weight_index;
|
|
|
|
}
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t i = 0; i < joint_weight_indices.size(); ++i)
|
2017-03-22 20:33:53 +00:00
|
|
|
mOutput << joint_weight_indices[i] << " ";
|
|
|
|
|
|
|
|
num_influences.clear();
|
|
|
|
accum_influences.clear();
|
|
|
|
joint_weight_indices.clear();
|
|
|
|
|
|
|
|
mOutput << "</v>" << endstr;
|
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</vertex_weights>" << endstr;
|
2017-03-22 09:06:55 +00:00
|
|
|
|
2017-03-21 08:21:49 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</skin>" << endstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
|
2017-03-21 08:21:49 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</controller>" << endstr;
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes the geometry library
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaExporter::WriteGeometryLibrary() {
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<library_geometries>" << endstr;
|
|
|
|
PushTag();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t a = 0; a < mScene->mNumMeshes; ++a)
|
|
|
|
WriteGeometry(a);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</library_geometries>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes the given mesh
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaExporter::WriteGeometry(size_t pIndex) {
|
|
|
|
const aiMesh *mesh = mScene->mMeshes[pIndex];
|
2020-04-30 17:28:06 +00:00
|
|
|
const std::string geometryId = GetObjectUniqueId(AiObjectType::Mesh, pIndex);
|
|
|
|
const std::string geometryName = GetObjectName(AiObjectType::Mesh, pIndex);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
if (mesh->mNumFaces == 0 || mesh->mNumVertices == 0)
|
2017-01-27 11:29:18 +00:00
|
|
|
return;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// opening tag
|
2019-10-01 23:32:53 +00:00
|
|
|
mOutput << startstr << "<geometry id=\"" << geometryId << "\" name=\"" << geometryName << "\" >" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
PushTag();
|
|
|
|
|
|
|
|
mOutput << startstr << "<mesh>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
|
|
|
|
// Positions
|
2020-04-29 16:17:46 +00:00
|
|
|
WriteFloatArray(geometryId + "-positions", FloatType_Vector, (ai_real *)mesh->mVertices, mesh->mNumVertices);
|
2015-05-19 03:57:13 +00:00
|
|
|
// Normals, if any
|
2020-04-28 16:46:07 +00:00
|
|
|
if (mesh->HasNormals())
|
2020-04-29 16:17:46 +00:00
|
|
|
WriteFloatArray(geometryId + "-normals", FloatType_Vector, (ai_real *)mesh->mNormals, mesh->mNumVertices);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// texture coords
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a) {
|
|
|
|
if (mesh->HasTextureCoords(static_cast<unsigned int>(a))) {
|
2021-03-09 20:08:28 +00:00
|
|
|
WriteFloatArray(geometryId + "-tex" + ai_to_string(a), mesh->mNumUVComponents[a] == 3 ? FloatType_TexCoord3 : FloatType_TexCoord2,
|
2020-04-28 16:46:07 +00:00
|
|
|
(ai_real *)mesh->mTextureCoords[a], mesh->mNumVertices);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// vertex colors
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a) {
|
|
|
|
if (mesh->HasVertexColors(static_cast<unsigned int>(a)))
|
2021-03-09 20:08:28 +00:00
|
|
|
WriteFloatArray(geometryId + "-color" + ai_to_string(a), FloatType_Color, (ai_real *)mesh->mColors[a], mesh->mNumVertices);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// assemble vertex structure
|
2017-01-27 11:29:18 +00:00
|
|
|
// Only write input for POSITION since we will write other as shared inputs in polygon definition
|
2020-04-28 16:46:07 +00:00
|
|
|
mOutput << startstr << "<vertices id=\"" << geometryId << "-vertices"
|
|
|
|
<< "\">" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
PushTag();
|
2019-10-01 23:32:53 +00:00
|
|
|
mOutput << startstr << "<input semantic=\"POSITION\" source=\"#" << geometryId << "-positions\" />" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</vertices>" << endstr;
|
|
|
|
|
|
|
|
// count the number of lines, triangles and polygon meshes
|
|
|
|
int countLines = 0;
|
|
|
|
int countPoly = 0;
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t a = 0; a < mesh->mNumFaces; ++a) {
|
|
|
|
if (mesh->mFaces[a].mNumIndices == 2)
|
|
|
|
countLines++;
|
|
|
|
else if (mesh->mFaces[a].mNumIndices >= 3)
|
|
|
|
countPoly++;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// lines
|
2020-04-28 16:46:07 +00:00
|
|
|
if (countLines) {
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<lines count=\"" << countLines << "\" material=\"defaultMaterial\">" << endstr;
|
|
|
|
PushTag();
|
2019-10-01 23:32:53 +00:00
|
|
|
mOutput << startstr << "<input offset=\"0\" semantic=\"VERTEX\" source=\"#" << geometryId << "-vertices\" />" << endstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
if (mesh->HasNormals())
|
2019-10-01 23:32:53 +00:00
|
|
|
mOutput << startstr << "<input semantic=\"NORMAL\" source=\"#" << geometryId << "-normals\" />" << endstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a) {
|
|
|
|
if (mesh->HasTextureCoords(static_cast<unsigned int>(a)))
|
|
|
|
mOutput << startstr << "<input semantic=\"TEXCOORD\" source=\"#" << geometryId << "-tex" << a << "\" "
|
|
|
|
<< "set=\"" << a << "\""
|
|
|
|
<< " />" << endstr;
|
2017-01-27 11:29:18 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a) {
|
|
|
|
if (mesh->HasVertexColors(static_cast<unsigned int>(a)))
|
|
|
|
mOutput << startstr << "<input semantic=\"COLOR\" source=\"#" << geometryId << "-color" << a << "\" "
|
|
|
|
<< "set=\"" << a << "\""
|
|
|
|
<< " />" << endstr;
|
2017-01-27 11:29:18 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<p>";
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t a = 0; a < mesh->mNumFaces; ++a) {
|
|
|
|
const aiFace &face = mesh->mFaces[a];
|
2015-05-19 03:57:13 +00:00
|
|
|
if (face.mNumIndices != 2) continue;
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t b = 0; b < face.mNumIndices; ++b)
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << face.mIndices[b] << " ";
|
|
|
|
}
|
|
|
|
mOutput << "</p>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</lines>" << endstr;
|
|
|
|
}
|
|
|
|
|
2016-04-03 00:38:00 +00:00
|
|
|
// triangle - don't use it, because compatibility problems
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// polygons
|
2020-04-28 16:46:07 +00:00
|
|
|
if (countPoly) {
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<polylist count=\"" << countPoly << "\" material=\"defaultMaterial\">" << endstr;
|
|
|
|
PushTag();
|
2019-10-01 23:32:53 +00:00
|
|
|
mOutput << startstr << "<input offset=\"0\" semantic=\"VERTEX\" source=\"#" << geometryId << "-vertices\" />" << endstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
if (mesh->HasNormals())
|
2019-10-01 23:32:53 +00:00
|
|
|
mOutput << startstr << "<input offset=\"0\" semantic=\"NORMAL\" source=\"#" << geometryId << "-normals\" />" << endstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a) {
|
|
|
|
if (mesh->HasTextureCoords(static_cast<unsigned int>(a)))
|
|
|
|
mOutput << startstr << "<input offset=\"0\" semantic=\"TEXCOORD\" source=\"#" << geometryId << "-tex" << a << "\" "
|
|
|
|
<< "set=\"" << a << "\""
|
|
|
|
<< " />" << endstr;
|
2017-01-27 11:29:18 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a) {
|
|
|
|
if (mesh->HasVertexColors(static_cast<unsigned int>(a)))
|
|
|
|
mOutput << startstr << "<input offset=\"0\" semantic=\"COLOR\" source=\"#" << geometryId << "-color" << a << "\" "
|
|
|
|
<< "set=\"" << a << "\""
|
|
|
|
<< " />" << endstr;
|
2015-12-28 10:27:12 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
mOutput << startstr << "<vcount>";
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t a = 0; a < mesh->mNumFaces; ++a) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (mesh->mFaces[a].mNumIndices < 3) continue;
|
|
|
|
mOutput << mesh->mFaces[a].mNumIndices << " ";
|
|
|
|
}
|
|
|
|
mOutput << "</vcount>" << endstr;
|
|
|
|
|
|
|
|
mOutput << startstr << "<p>";
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t a = 0; a < mesh->mNumFaces; ++a) {
|
|
|
|
const aiFace &face = mesh->mFaces[a];
|
2015-05-19 03:57:13 +00:00
|
|
|
if (face.mNumIndices < 3) continue;
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t b = 0; b < face.mNumIndices; ++b)
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << face.mIndices[b] << " ";
|
|
|
|
}
|
|
|
|
mOutput << "</p>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</polylist>" << endstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// closing tags
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</mesh>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</geometry>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes a float array of the given type
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaExporter::WriteFloatArray(const std::string &pIdString, FloatDataType pType, const ai_real *pData, size_t pElementCount) {
|
2015-05-19 03:57:13 +00:00
|
|
|
size_t floatsPerElement = 0;
|
2020-04-28 16:46:07 +00:00
|
|
|
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;
|
|
|
|
case FloatType_Mat4x4: floatsPerElement = 16; break;
|
|
|
|
case FloatType_Weight: floatsPerElement = 1; break;
|
|
|
|
case FloatType_Time: floatsPerElement = 1; break;
|
|
|
|
default:
|
|
|
|
return;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 23:32:53 +00:00
|
|
|
std::string arrayId = XMLIDEncode(pIdString) + "-array";
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-10-01 23:32:53 +00:00
|
|
|
mOutput << startstr << "<source id=\"" << XMLIDEncode(pIdString) << "\" name=\"" << XMLEscape(pIdString) << "\">" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
PushTag();
|
|
|
|
|
|
|
|
// source array
|
2019-10-01 23:32:53 +00:00
|
|
|
mOutput << startstr << "<float_array id=\"" << arrayId << "\" count=\"" << pElementCount * floatsPerElement << "\"> ";
|
2015-05-19 03:57:13 +00:00
|
|
|
PushTag();
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
if (pType == FloatType_TexCoord2) {
|
|
|
|
for (size_t a = 0; a < pElementCount; ++a) {
|
|
|
|
mOutput << pData[a * 3 + 0] << " ";
|
|
|
|
mOutput << pData[a * 3 + 1] << " ";
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} 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] << " ";
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
|
|
|
for (size_t a = 0; a < pElementCount * floatsPerElement; ++a)
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << pData[a] << " ";
|
|
|
|
}
|
|
|
|
mOutput << "</float_array>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
|
|
|
|
// the usual Collada fun. Let's bloat it even more!
|
|
|
|
mOutput << startstr << "<technique_common>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
mOutput << startstr << "<accessor count=\"" << pElementCount << "\" offset=\"0\" source=\"#" << arrayId << "\" stride=\"" << floatsPerElement << "\">" << endstr;
|
|
|
|
PushTag();
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
case FloatType_Mat4x4:
|
|
|
|
mOutput << startstr << "<param name=\"TRANSFORM\" type=\"float4x4\" />" << endstr;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FloatType_Weight:
|
|
|
|
mOutput << startstr << "<param name=\"WEIGHT\" type=\"float\" />" << endstr;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// customized, add animation related
|
|
|
|
case FloatType_Time:
|
|
|
|
mOutput << startstr << "<param name=\"TIME\" type=\"float\" />" << endstr;
|
|
|
|
break;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</accessor>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</technique_common>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</source>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Writes the scene library
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaExporter::WriteSceneLibrary() {
|
2020-05-05 09:16:13 +00:00
|
|
|
// Determine if we are using the aiScene root or our own
|
|
|
|
std::string sceneName("Scene");
|
|
|
|
if (mAdd_root_node) {
|
|
|
|
mSceneId = MakeUniqueId(mUniqueIds, sceneName, std::string());
|
|
|
|
mUniqueIds.insert(mSceneId);
|
|
|
|
} else {
|
|
|
|
mSceneId = GetNodeUniqueId(mScene->mRootNode);
|
|
|
|
sceneName = GetNodeName(mScene->mRootNode);
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
mOutput << startstr << "<library_visual_scenes>" << endstr;
|
|
|
|
PushTag();
|
2020-05-05 09:16:13 +00:00
|
|
|
mOutput << startstr << "<visual_scene id=\"" + mSceneId + "\" name=\"" + sceneName + "\">" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
PushTag();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-05-05 09:16:13 +00:00
|
|
|
if (mAdd_root_node) {
|
|
|
|
// Export the root node
|
|
|
|
WriteNode(mScene->mRootNode);
|
|
|
|
} else {
|
|
|
|
// Have already exported the root node
|
|
|
|
for (size_t a = 0; a < mScene->mRootNode->mNumChildren; ++a)
|
|
|
|
WriteNode(mScene->mRootNode->mChildren[a]);
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</visual_scene>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</library_visual_scenes>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2017-10-21 10:28:14 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaExporter::WriteAnimationLibrary(size_t pIndex) {
|
|
|
|
const aiAnimation *anim = mScene->mAnimations[pIndex];
|
|
|
|
|
|
|
|
if (anim->mNumChannels == 0 && anim->mNumMeshChannels == 0 && anim->mNumMorphMeshChannels == 0)
|
|
|
|
return;
|
|
|
|
|
2020-04-30 17:28:06 +00:00
|
|
|
const std::string animationNameEscaped = GetObjectName(AiObjectType::Animation, pIndex);
|
|
|
|
const std::string idstrEscaped = GetObjectUniqueId(AiObjectType::Animation, pIndex);
|
2020-04-28 16:46:07 +00:00
|
|
|
|
2020-04-30 17:28:06 +00:00
|
|
|
mOutput << startstr << "<animation id=\"" + idstrEscaped + "\" name=\"" + animationNameEscaped + "\">" << endstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
PushTag();
|
2018-03-20 17:43:54 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
std::string cur_node_idstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t a = 0; a < anim->mNumChannels; ++a) {
|
|
|
|
const aiNodeAnim *nodeAnim = anim->mChannels[a];
|
|
|
|
|
|
|
|
// sanity check
|
2020-03-08 20:24:01 +00:00
|
|
|
if (nodeAnim->mNumPositionKeys != nodeAnim->mNumScalingKeys || nodeAnim->mNumPositionKeys != nodeAnim->mNumRotationKeys) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
|
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
cur_node_idstr.clear();
|
|
|
|
cur_node_idstr += nodeAnim->mNodeName.data;
|
|
|
|
cur_node_idstr += std::string("_matrix-input");
|
2017-10-21 10:28:14 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
std::vector<ai_real> frames;
|
|
|
|
for (size_t i = 0; i < nodeAnim->mNumPositionKeys; ++i) {
|
|
|
|
frames.push_back(static_cast<ai_real>(nodeAnim->mPositionKeys[i].mTime));
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteFloatArray(cur_node_idstr, FloatType_Time, (const ai_real *)frames.data(), frames.size());
|
|
|
|
frames.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
cur_node_idstr.clear();
|
2018-03-20 17:43:54 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
cur_node_idstr += nodeAnim->mNodeName.data;
|
|
|
|
cur_node_idstr += std::string("_matrix-output");
|
2020-04-28 16:46:07 +00:00
|
|
|
|
|
|
|
std::vector<ai_real> keyframes;
|
|
|
|
keyframes.reserve(nodeAnim->mNumPositionKeys * 16);
|
|
|
|
for (size_t i = 0; i < nodeAnim->mNumPositionKeys; ++i) {
|
|
|
|
aiVector3D Scaling = nodeAnim->mScalingKeys[i].mValue;
|
|
|
|
aiMatrix4x4 ScalingM; // identity
|
|
|
|
ScalingM[0][0] = Scaling.x;
|
|
|
|
ScalingM[1][1] = Scaling.y;
|
|
|
|
ScalingM[2][2] = Scaling.z;
|
|
|
|
|
|
|
|
aiQuaternion RotationQ = nodeAnim->mRotationKeys[i].mValue;
|
|
|
|
aiMatrix4x4 s = aiMatrix4x4(RotationQ.GetMatrix());
|
|
|
|
aiMatrix4x4 RotationM(s.a1, s.a2, s.a3, 0, s.b1, s.b2, s.b3, 0, s.c1, s.c2, s.c3, 0, 0, 0, 0, 1);
|
|
|
|
|
|
|
|
aiVector3D Translation = nodeAnim->mPositionKeys[i].mValue;
|
|
|
|
aiMatrix4x4 TranslationM; // identity
|
|
|
|
TranslationM[0][3] = Translation.x;
|
|
|
|
TranslationM[1][3] = Translation.y;
|
|
|
|
TranslationM[2][3] = Translation.z;
|
|
|
|
|
|
|
|
// Combine the above transformations
|
|
|
|
aiMatrix4x4 mat = TranslationM * RotationM * ScalingM;
|
|
|
|
|
|
|
|
for (unsigned int j = 0; j < 4; ++j) {
|
|
|
|
keyframes.insert(keyframes.end(), mat[j], mat[j] + 4);
|
2017-11-08 17:34:16 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WriteFloatArray(cur_node_idstr, FloatType_Mat4x4, (const ai_real *)keyframes.data(), keyframes.size() / 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::vector<std::string> names;
|
|
|
|
for (size_t i = 0; i < nodeAnim->mNumPositionKeys; ++i) {
|
|
|
|
if (nodeAnim->mPreState == aiAnimBehaviour_DEFAULT || nodeAnim->mPreState == aiAnimBehaviour_LINEAR || nodeAnim->mPreState == aiAnimBehaviour_REPEAT) {
|
|
|
|
names.push_back("LINEAR");
|
|
|
|
} else if (nodeAnim->mPostState == aiAnimBehaviour_CONSTANT) {
|
|
|
|
names.push_back("STEP");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string cur_node_idstr2 = nodeAnim->mNodeName.data + std::string("_matrix-interpolation");
|
2020-03-08 20:24:01 +00:00
|
|
|
std::string arrayId = XMLIDEncode(cur_node_idstr2) + "-array";
|
2020-04-28 16:46:07 +00:00
|
|
|
|
|
|
|
mOutput << startstr << "<source id=\"" << XMLIDEncode(cur_node_idstr2) << "\">" << endstr;
|
|
|
|
PushTag();
|
|
|
|
|
|
|
|
// source array
|
|
|
|
mOutput << startstr << "<Name_array id=\"" << arrayId << "\" count=\"" << names.size() << "\"> ";
|
|
|
|
for (size_t aa = 0; aa < names.size(); ++aa) {
|
|
|
|
mOutput << names[aa] << " ";
|
2017-11-08 17:34:16 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
mOutput << "</Name_array>" << endstr;
|
|
|
|
|
|
|
|
mOutput << startstr << "<technique_common>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
|
|
|
|
mOutput << startstr << "<accessor source=\"#" << arrayId << "\" count=\"" << names.size() << "\" stride=\"" << 1 << "\">" << endstr;
|
|
|
|
PushTag();
|
|
|
|
|
|
|
|
mOutput << startstr << "<param name=\"INTERPOLATION\" type=\"name\"></param>" << endstr;
|
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</accessor>" << endstr;
|
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</technique_common>" << endstr;
|
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</source>" << endstr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t a = 0; a < anim->mNumChannels; ++a) {
|
|
|
|
const aiNodeAnim *nodeAnim = anim->mChannels[a];
|
|
|
|
|
|
|
|
{
|
|
|
|
// samplers
|
|
|
|
const std::string node_idstr = nodeAnim->mNodeName.data + std::string("_matrix-sampler");
|
|
|
|
mOutput << startstr << "<sampler id=\"" << XMLIDEncode(node_idstr) << "\">" << endstr;
|
|
|
|
PushTag();
|
|
|
|
|
|
|
|
mOutput << startstr << "<input semantic=\"INPUT\" source=\"#" << XMLIDEncode(nodeAnim->mNodeName.data + std::string("_matrix-input")) << "\"/>" << endstr;
|
|
|
|
mOutput << startstr << "<input semantic=\"OUTPUT\" source=\"#" << XMLIDEncode(nodeAnim->mNodeName.data + std::string("_matrix-output")) << "\"/>" << endstr;
|
|
|
|
mOutput << startstr << "<input semantic=\"INTERPOLATION\" source=\"#" << XMLIDEncode(nodeAnim->mNodeName.data + std::string("_matrix-interpolation")) << "\"/>" << endstr;
|
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</sampler>" << endstr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t a = 0; a < anim->mNumChannels; ++a) {
|
|
|
|
const aiNodeAnim *nodeAnim = anim->mChannels[a];
|
|
|
|
|
|
|
|
{
|
|
|
|
// channels
|
|
|
|
mOutput << startstr << "<channel source=\"#" << XMLIDEncode(nodeAnim->mNodeName.data + std::string("_matrix-sampler")) << "\" target=\"" << XMLIDEncode(nodeAnim->mNodeName.data) << "/matrix\"/>" << endstr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</animation>" << endstr;
|
2017-10-21 10:28:14 +00:00
|
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaExporter::WriteAnimationsLibrary() {
|
|
|
|
if (mScene->mNumAnimations > 0) {
|
|
|
|
mOutput << startstr << "<library_animations>" << endstr;
|
|
|
|
PushTag();
|
|
|
|
|
|
|
|
// start recursive write at the root node
|
|
|
|
for (size_t a = 0; a < mScene->mNumAnimations; ++a)
|
|
|
|
WriteAnimationLibrary(a);
|
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</library_animations>" << endstr;
|
|
|
|
}
|
2017-10-21 10:28:14 +00:00
|
|
|
}
|
2016-06-06 01:01:27 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Helper to find a bone by name in the scene
|
2020-05-01 10:13:38 +00:00
|
|
|
aiBone *findBone(const aiScene *scene, const aiString &name) {
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t m = 0; m < scene->mNumMeshes; m++) {
|
|
|
|
aiMesh *mesh = scene->mMeshes[m];
|
|
|
|
for (size_t b = 0; b < mesh->mNumBones; b++) {
|
|
|
|
aiBone *bone = mesh->mBones[b];
|
2020-05-01 10:13:38 +00:00
|
|
|
if (name == bone->mName) {
|
2016-06-06 01:01:27 +00:00
|
|
|
return bone;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 10:39:43 +00:00
|
|
|
return nullptr;
|
2016-06-06 01:01:27 +00:00
|
|
|
}
|
|
|
|
|
2017-10-21 10:28:14 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-05-01 10:13:38 +00:00
|
|
|
// Helper to find the node associated with a bone in the scene
|
2020-04-28 16:46:07 +00:00
|
|
|
const aiNode *findBoneNode(const aiNode *aNode, const aiBone *bone) {
|
|
|
|
if (aNode && bone && aNode->mName == bone->mName) {
|
|
|
|
return aNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aNode && bone) {
|
|
|
|
for (unsigned int i = 0; i < aNode->mNumChildren; ++i) {
|
|
|
|
aiNode *aChild = aNode->mChildren[i];
|
2020-05-01 10:13:38 +00:00
|
|
|
const aiNode *foundFromChild = nullptr;
|
2020-04-28 16:46:07 +00:00
|
|
|
if (aChild) {
|
|
|
|
foundFromChild = findBoneNode(aChild, bone);
|
2020-05-01 10:13:38 +00:00
|
|
|
if (foundFromChild) {
|
|
|
|
return foundFromChild;
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:13:38 +00:00
|
|
|
return nullptr;
|
2017-10-21 10:28:14 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
const aiNode *findSkeletonRootNode(const aiScene *scene, const aiMesh *mesh) {
|
|
|
|
std::set<const aiNode *> topParentBoneNodes;
|
|
|
|
if (mesh && mesh->mNumBones > 0) {
|
|
|
|
for (unsigned int i = 0; i < mesh->mNumBones; ++i) {
|
|
|
|
aiBone *bone = mesh->mBones[i];
|
|
|
|
|
|
|
|
const aiNode *node = findBoneNode(scene->mRootNode, bone);
|
|
|
|
if (node) {
|
2020-05-01 10:13:38 +00:00
|
|
|
while (node->mParent && findBone(scene, node->mParent->mName) != nullptr) {
|
2020-04-28 16:46:07 +00:00
|
|
|
node = node->mParent;
|
|
|
|
}
|
|
|
|
topParentBoneNodes.insert(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!topParentBoneNodes.empty()) {
|
|
|
|
const aiNode *parentBoneNode = *topParentBoneNodes.begin();
|
|
|
|
if (topParentBoneNodes.size() == 1) {
|
|
|
|
return parentBoneNode;
|
|
|
|
} else {
|
|
|
|
for (auto it : topParentBoneNodes) {
|
|
|
|
if (it->mParent) return it->mParent;
|
|
|
|
}
|
|
|
|
return parentBoneNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:39:43 +00:00
|
|
|
return nullptr;
|
2017-10-21 10:28:14 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Recursively writes the given node
|
2020-04-30 17:28:06 +00:00
|
|
|
void ColladaExporter::WriteNode(const aiNode *pNode) {
|
2016-06-06 01:01:27 +00:00
|
|
|
// If the node is associated with a bone, it is a joint node (JOINT)
|
|
|
|
// otherwise it is a normal node (NODE)
|
2020-04-30 17:28:06 +00:00
|
|
|
// Assimp-specific: nodes with no name cannot be associated with bones
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *node_type;
|
2017-03-23 08:30:01 +00:00
|
|
|
bool is_joint, is_skeleton_root = false;
|
2020-05-01 10:13:38 +00:00
|
|
|
if (pNode->mName.length == 0 || nullptr == findBone(mScene, pNode->mName)) {
|
2016-06-06 01:01:27 +00:00
|
|
|
node_type = "NODE";
|
2017-03-23 08:30:01 +00:00
|
|
|
is_joint = false;
|
2016-06-06 01:01:27 +00:00
|
|
|
} else {
|
|
|
|
node_type = "JOINT";
|
2017-03-23 08:30:01 +00:00
|
|
|
is_joint = true;
|
2020-05-01 10:13:38 +00:00
|
|
|
if (!pNode->mParent || nullptr == findBone(mScene, pNode->mParent->mName)) {
|
2017-03-23 08:30:01 +00:00
|
|
|
is_skeleton_root = true;
|
2018-12-28 00:45:47 +00:00
|
|
|
}
|
2016-06-06 01:01:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 17:28:06 +00:00
|
|
|
const std::string node_id = GetNodeUniqueId(pNode);
|
|
|
|
const std::string node_name = GetNodeName(pNode);
|
2020-04-28 16:46:07 +00:00
|
|
|
mOutput << startstr << "<node ";
|
|
|
|
if (is_skeleton_root) {
|
2020-05-04 16:47:09 +00:00
|
|
|
mFoundSkeletonRootNodeID = node_id; // For now, only support one skeleton in a scene.
|
2020-04-28 16:46:07 +00:00
|
|
|
}
|
2020-05-04 16:47:09 +00:00
|
|
|
mOutput << "id=\"" << node_id << "\" " << (is_joint ? "sid=\"" + node_id + "\" " : "");
|
2020-04-29 16:17:46 +00:00
|
|
|
mOutput << "name=\"" << node_name
|
2016-06-06 01:01:27 +00:00
|
|
|
<< "\" type=\"" << node_type
|
|
|
|
<< "\">" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
PushTag();
|
|
|
|
|
|
|
|
// write transformation - we can directly put the matrix there
|
2016-04-03 00:38:00 +00:00
|
|
|
// TODO: (thom) decompose into scale - rot - quad to allow addressing it by animations afterwards
|
2018-12-04 14:44:49 +00:00
|
|
|
aiMatrix4x4 mat = pNode->mTransformation;
|
|
|
|
|
|
|
|
// If this node is a Camera node, the camera coordinate system needs to be multiplied in.
|
|
|
|
// When importing from Collada, the mLookAt is set to 0, 0, -1, and the node transform is unchanged.
|
|
|
|
// When importing from a different format, mLookAt is set to 0, 0, 1. Therefore, the local camera
|
|
|
|
// coordinate system must be changed to matche the Collada specification.
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t i = 0; i < mScene->mNumCameras; i++) {
|
|
|
|
if (mScene->mCameras[i]->mName == pNode->mName) {
|
2018-12-04 14:44:49 +00:00
|
|
|
aiMatrix4x4 sourceView;
|
|
|
|
mScene->mCameras[i]->GetCameraMatrix(sourceView);
|
|
|
|
|
|
|
|
aiMatrix4x4 colladaView;
|
|
|
|
colladaView.a1 = colladaView.c3 = -1; // move into -z space.
|
|
|
|
mat *= (sourceView * colladaView);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
|
|
|
|
// customized, sid should be 'matrix' to match with loader code.
|
2017-10-21 10:28:14 +00:00
|
|
|
//mOutput << startstr << "<matrix sid=\"transform\">";
|
2020-04-28 16:46:07 +00:00
|
|
|
mOutput << startstr << "<matrix sid=\"matrix\">";
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
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;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
if (pNode->mNumMeshes == 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
//check if it is a camera node
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t i = 0; i < mScene->mNumCameras; i++) {
|
|
|
|
if (mScene->mCameras[i]->mName == pNode->mName) {
|
2020-05-01 10:13:38 +00:00
|
|
|
mOutput << startstr << "<instance_camera url=\"#" << GetObjectUniqueId(AiObjectType::Camera, i) << "\"/>" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//check if it is a light node
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t i = 0; i < mScene->mNumLights; i++) {
|
|
|
|
if (mScene->mLights[i]->mName == pNode->mName) {
|
2020-05-01 10:13:38 +00:00
|
|
|
mOutput << startstr << "<instance_light url=\"#" << GetObjectUniqueId(AiObjectType::Light, i) << "\"/>" << endstr;
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
} else
|
|
|
|
// instance every geometry
|
|
|
|
for (size_t a = 0; a < pNode->mNumMeshes; ++a) {
|
|
|
|
const aiMesh *mesh = mScene->mMeshes[pNode->mMeshes[a]];
|
|
|
|
// do not instantiate mesh if empty. I wonder how this could happen
|
|
|
|
if (mesh->mNumFaces == 0 || mesh->mNumVertices == 0)
|
|
|
|
continue;
|
2017-03-24 04:04:40 +00:00
|
|
|
|
2020-04-30 17:28:06 +00:00
|
|
|
const std::string meshId = GetObjectUniqueId(AiObjectType::Mesh, pNode->mMeshes[a]);
|
2019-10-02 17:18:48 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
if (mesh->mNumBones == 0) {
|
2020-04-29 16:17:46 +00:00
|
|
|
mOutput << startstr << "<instance_geometry url=\"#" << meshId << "\">" << endstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
PushTag();
|
|
|
|
} else {
|
|
|
|
mOutput << startstr
|
2020-04-29 16:17:46 +00:00
|
|
|
<< "<instance_controller url=\"#" << meshId << "-skin\">"
|
2020-04-28 16:46:07 +00:00
|
|
|
<< endstr;
|
|
|
|
PushTag();
|
|
|
|
|
|
|
|
// note! this mFoundSkeletonRootNodeID some how affects animation, it makes the mesh attaches to armature skeleton root node.
|
|
|
|
// use the first bone to find skeleton root
|
2020-04-30 17:28:06 +00:00
|
|
|
const aiNode *skeletonRootBoneNode = findSkeletonRootNode(mScene, mesh);
|
2020-04-28 16:46:07 +00:00
|
|
|
if (skeletonRootBoneNode) {
|
2020-04-30 17:28:06 +00:00
|
|
|
mFoundSkeletonRootNodeID = GetNodeUniqueId(skeletonRootBoneNode);
|
2020-04-28 16:46:07 +00:00
|
|
|
}
|
|
|
|
mOutput << startstr << "<skeleton>#" << mFoundSkeletonRootNodeID << "</skeleton>" << endstr;
|
|
|
|
}
|
|
|
|
mOutput << startstr << "<bind_material>" << endstr;
|
2017-03-24 04:04:40 +00:00
|
|
|
PushTag();
|
2020-04-28 16:46:07 +00:00
|
|
|
mOutput << startstr << "<technique_common>" << endstr;
|
2017-03-24 04:04:40 +00:00
|
|
|
PushTag();
|
2020-04-30 17:28:06 +00:00
|
|
|
mOutput << startstr << "<instance_material symbol=\"defaultMaterial\" target=\"#" << GetObjectUniqueId(AiObjectType::Material, mesh->mMaterialIndex) << "\">" << endstr;
|
2020-04-28 16:46:07 +00:00
|
|
|
PushTag();
|
|
|
|
for (size_t aa = 0; aa < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++aa) {
|
|
|
|
if (mesh->HasTextureCoords(static_cast<unsigned int>(aa)))
|
|
|
|
// semantic as in <texture texcoord=...>
|
|
|
|
// input_semantic as in <input semantic=...>
|
|
|
|
// input_set as in <input set=...>
|
|
|
|
mOutput << startstr << "<bind_vertex_input semantic=\"CHANNEL" << aa << "\" input_semantic=\"TEXCOORD\" input_set=\"" << aa << "\"/>" << endstr;
|
|
|
|
}
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</instance_material>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</technique_common>" << endstr;
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</bind_material>" << endstr;
|
|
|
|
|
|
|
|
PopTag();
|
|
|
|
if (mesh->mNumBones == 0)
|
|
|
|
mOutput << startstr << "</instance_geometry>" << endstr;
|
|
|
|
else
|
|
|
|
mOutput << startstr << "</instance_controller>" << endstr;
|
2017-03-24 04:04:40 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// recurse into subnodes
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t a = 0; a < pNode->mNumChildren; ++a)
|
2020-04-30 17:28:06 +00:00
|
|
|
WriteNode(pNode->mChildren[a]);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
PopTag();
|
|
|
|
mOutput << startstr << "</node>" << endstr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 10:39:43 +00:00
|
|
|
void ColladaExporter::CreateNodeIds(const aiNode *node) {
|
2020-04-30 17:28:06 +00:00
|
|
|
GetNodeUniqueId(node);
|
|
|
|
for (size_t a = 0; a < node->mNumChildren; ++a)
|
|
|
|
CreateNodeIds(node->mChildren[a]);
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:39:43 +00:00
|
|
|
std::string ColladaExporter::GetNodeUniqueId(const aiNode *node) {
|
2020-04-30 17:28:06 +00:00
|
|
|
// Use the pointer as the key. This is safe because the scene is immutable.
|
|
|
|
auto idIt = mNodeIdMap.find(node);
|
|
|
|
if (idIt != mNodeIdMap.cend())
|
|
|
|
return idIt->second;
|
|
|
|
|
|
|
|
// Prefer the requested Collada Id if extant
|
|
|
|
std::string idStr;
|
|
|
|
aiString origId;
|
|
|
|
if (node->mMetaData && node->mMetaData->Get(AI_METADATA_COLLADA_ID, origId)) {
|
|
|
|
idStr = origId.C_Str();
|
|
|
|
} else {
|
|
|
|
idStr = node->mName.C_Str();
|
|
|
|
}
|
|
|
|
// Make sure the requested id is valid
|
|
|
|
if (idStr.empty())
|
|
|
|
idStr = "node";
|
|
|
|
else
|
|
|
|
idStr = XMLIDEncode(idStr);
|
2020-04-29 16:17:46 +00:00
|
|
|
|
2020-04-30 17:28:06 +00:00
|
|
|
// Ensure it's unique
|
2020-05-01 10:13:38 +00:00
|
|
|
idStr = MakeUniqueId(mUniqueIds, idStr, std::string());
|
2020-04-30 17:28:06 +00:00
|
|
|
mUniqueIds.insert(idStr);
|
|
|
|
mNodeIdMap.insert(std::make_pair(node, idStr));
|
|
|
|
return idStr;
|
2020-04-29 16:17:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 10:39:43 +00:00
|
|
|
std::string ColladaExporter::GetNodeName(const aiNode *node) {
|
2020-04-30 17:28:06 +00:00
|
|
|
|
|
|
|
return XMLEscape(node->mName.C_Str());
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:39:43 +00:00
|
|
|
std::string ColladaExporter::GetBoneUniqueId(const aiBone *bone) {
|
2020-05-01 10:13:38 +00:00
|
|
|
// Find the Node that is this Bone
|
|
|
|
const aiNode *boneNode = findBoneNode(mScene->mRootNode, bone);
|
|
|
|
if (boneNode == nullptr)
|
|
|
|
return std::string();
|
2020-04-30 17:28:06 +00:00
|
|
|
|
2020-05-01 10:13:38 +00:00
|
|
|
return GetNodeUniqueId(boneNode);
|
2020-04-30 17:28:06 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 10:39:43 +00:00
|
|
|
std::string ColladaExporter::GetObjectUniqueId(AiObjectType type, size_t pIndex) {
|
2020-04-30 17:28:06 +00:00
|
|
|
auto idIt = GetObjectIdMap(type).find(pIndex);
|
|
|
|
if (idIt != GetObjectIdMap(type).cend())
|
|
|
|
return idIt->second;
|
|
|
|
|
|
|
|
// Not seen this object before, create and add
|
|
|
|
NameIdPair result = AddObjectIndexToMaps(type, pIndex);
|
|
|
|
return result.second;
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:39:43 +00:00
|
|
|
std::string ColladaExporter::GetObjectName(AiObjectType type, size_t pIndex) {
|
2020-05-01 10:13:38 +00:00
|
|
|
auto objectName = GetObjectNameMap(type).find(pIndex);
|
|
|
|
if (objectName != GetObjectNameMap(type).cend())
|
|
|
|
return objectName->second;
|
2020-04-29 16:17:46 +00:00
|
|
|
|
2020-04-30 17:28:06 +00:00
|
|
|
// Not seen this object before, create and add
|
|
|
|
NameIdPair result = AddObjectIndexToMaps(type, pIndex);
|
|
|
|
return result.first;
|
2020-04-29 16:17:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 17:28:06 +00:00
|
|
|
// Determine unique id and add the name and id to the maps
|
|
|
|
// @param type object type
|
|
|
|
// @param index object index
|
|
|
|
// @param name in/out. Caller to set the original name if known.
|
|
|
|
// @param idStr in/out. Caller to set the preferred id if known.
|
2020-05-01 10:39:43 +00:00
|
|
|
ColladaExporter::NameIdPair ColladaExporter::AddObjectIndexToMaps(AiObjectType type, size_t index) {
|
2020-04-30 17:28:06 +00:00
|
|
|
|
|
|
|
std::string name;
|
2020-05-01 10:13:38 +00:00
|
|
|
std::string idStr;
|
|
|
|
std::string idPostfix;
|
2020-04-30 17:28:06 +00:00
|
|
|
|
2020-05-01 10:13:38 +00:00
|
|
|
// Get the name and id postfix
|
2020-04-30 17:28:06 +00:00
|
|
|
switch (type) {
|
|
|
|
case AiObjectType::Mesh: name = mScene->mMeshes[index]->mName.C_Str(); break;
|
|
|
|
case AiObjectType::Material: name = mScene->mMaterials[index]->GetName().C_Str(); break;
|
|
|
|
case AiObjectType::Animation: name = mScene->mAnimations[index]->mName.C_Str(); break;
|
2020-05-01 10:13:38 +00:00
|
|
|
case AiObjectType::Light:
|
|
|
|
name = mScene->mLights[index]->mName.C_Str();
|
|
|
|
idPostfix = "-light";
|
|
|
|
break;
|
|
|
|
case AiObjectType::Camera:
|
|
|
|
name = mScene->mCameras[index]->mName.C_Str();
|
|
|
|
idPostfix = "-camera";
|
|
|
|
break;
|
2020-04-30 17:28:06 +00:00
|
|
|
case AiObjectType::Count: throw std::logic_error("ColladaExporter::AiObjectType::Count is not an object type");
|
2020-04-29 16:17:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 17:28:06 +00:00
|
|
|
if (name.empty()) {
|
|
|
|
// Default ids if empty name
|
|
|
|
switch (type) {
|
2020-05-01 10:13:38 +00:00
|
|
|
case AiObjectType::Mesh: idStr = std::string("mesh_"); break;
|
|
|
|
case AiObjectType::Material: idStr = std::string("material_"); break; // This one should never happen
|
|
|
|
case AiObjectType::Animation: idStr = std::string("animation_"); break;
|
|
|
|
case AiObjectType::Light: idStr = std::string("light_"); break;
|
|
|
|
case AiObjectType::Camera: idStr = std::string("camera_"); break;
|
2020-04-30 17:28:06 +00:00
|
|
|
case AiObjectType::Count: throw std::logic_error("ColladaExporter::AiObjectType::Count is not an object type");
|
2020-04-29 16:17:46 +00:00
|
|
|
}
|
2021-03-09 20:08:28 +00:00
|
|
|
idStr.append(ai_to_string(index));
|
2020-04-30 17:28:06 +00:00
|
|
|
} else {
|
|
|
|
idStr = XMLIDEncode(name);
|
2020-04-29 16:17:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 17:28:06 +00:00
|
|
|
if (!name.empty())
|
|
|
|
name = XMLEscape(name);
|
2020-04-29 16:17:46 +00:00
|
|
|
|
2020-05-01 10:13:38 +00:00
|
|
|
idStr = MakeUniqueId(mUniqueIds, idStr, idPostfix);
|
2020-04-30 17:28:06 +00:00
|
|
|
|
|
|
|
// Add to maps
|
|
|
|
mUniqueIds.insert(idStr);
|
|
|
|
GetObjectIdMap(type).insert(std::make_pair(index, idStr));
|
2020-05-01 10:13:38 +00:00
|
|
|
GetObjectNameMap(type).insert(std::make_pair(index, name));
|
2020-04-30 17:28:06 +00:00
|
|
|
|
|
|
|
return std::make_pair(name, idStr);
|
2020-04-29 16:17:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 10:39:43 +00:00
|
|
|
} // end of namespace Assimp
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
2019-09-15 17:25:37 +00:00
|
|
|
#endif
|