assimp/code/AssetLib/Collada/ColladaParser.cpp

2408 lines
99 KiB
C++
Raw Normal View History

/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
2021-02-28 11:17:54 +00:00
Copyright (c) 2006-2021, assimp team
2018-01-28 18:42:05 +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 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
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
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
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
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/
/** @file ColladaParser.cpp
* @brief Implementation of the Collada parser helper
*/
#ifndef ASSIMP_BUILD_NO_COLLADA_IMPORTER
#include "ColladaParser.h"
#include <assimp/ParsingUtils.h>
#include <assimp/StringUtils.h>
#include <assimp/ZipArchiveIOSystem.h>
#include <assimp/commonMetaData.h>
#include <assimp/fast_atof.h>
#include <assimp/light.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/IOSystem.hpp>
#include <memory>
using namespace Assimp;
using namespace Assimp::Collada;
using namespace Assimp::Formatter;
2020-10-19 17:06:11 +00:00
static void ReportWarning(const char *msg, ...) {
ai_assert(nullptr != msg);
va_list args;
va_start(args, msg);
char szBuffer[3000];
const int iLen = vsprintf(szBuffer, msg, args);
ai_assert(iLen > 0);
va_end(args);
ASSIMP_LOG_WARN_F("Validation warning: ", std::string(szBuffer, iLen));
}
2020-10-19 21:53:27 +00:00
static bool FindCommonKey(const std::string &collada_key, const MetaKeyPairVector &key_renaming, size_t &found_index) {
for (size_t i = 0; i < key_renaming.size(); ++i) {
if (key_renaming[i].first == collada_key) {
found_index = i;
return true;
}
}
found_index = std::numeric_limits<size_t>::max();
return false;
}
static void readUrlAttribute(XmlNode &node, std::string &url) {
url.clear();
if (!XmlParser::getStdStrAttribute(node, "url", url)) {
return;
}
if (url[0] != '#') {
throw DeadlyImportError("Unknown reference format");
}
url = url.c_str() + 1;
}
// ------------------------------------------------------------------------------------------------
// Constructor to be privately used by Importer
ColladaParser::ColladaParser(IOSystem *pIOHandler, const std::string &pFile) :
mFileName(pFile),
2020-06-27 13:57:06 +00:00
mXmlParser(),
mDataLibrary(),
mAccessorLibrary(),
mMeshLibrary(),
mNodeLibrary(),
mImageLibrary(),
mEffectLibrary(),
mMaterialLibrary(),
mLightLibrary(),
mCameraLibrary(),
mControllerLibrary(),
mRootNode(nullptr),
mAnims(),
mUnitSize(1.0f),
mUpDirection(UP_Y),
2020-05-22 08:09:46 +00:00
mFormat(FV_1_5_n) {
if (nullptr == pIOHandler) {
throw DeadlyImportError("IOSystem is nullptr.");
}
std::unique_ptr<IOStream> daefile;
std::unique_ptr<ZipArchiveIOSystem> zip_archive;
// Determine type
std::string extension = BaseImporter::GetExtension(pFile);
if (extension != "dae") {
zip_archive.reset(new ZipArchiveIOSystem(pIOHandler, pFile));
}
if (zip_archive && zip_archive->isOpen()) {
std::string dae_filename = ReadZaeManifest(*zip_archive);
if (dae_filename.empty()) {
2020-09-25 19:00:09 +00:00
throw DeadlyImportError("Invalid ZAE");
}
daefile.reset(zip_archive->Open(dae_filename.c_str()));
if (daefile == nullptr) {
2020-09-25 19:00:09 +00:00
throw DeadlyImportError("Invalid ZAE manifest: '", dae_filename, "' is missing");
}
} else {
// attempt to open the file directly
daefile.reset(pIOHandler->Open(pFile));
if (daefile.get() == nullptr) {
throw DeadlyImportError("Failed to open file '", pFile, "'.");
}
}
2015-05-19 03:57:13 +00:00
// generate a XML reader for it
if (!mXmlParser.parse(daefile.get())) {
2020-09-25 19:00:09 +00:00
throw DeadlyImportError("Unable to read file, malformed XML");
2020-06-27 13:57:06 +00:00
}
2015-05-19 03:57:13 +00:00
// start reading
XmlNode node = mXmlParser.getRootNode();
2020-09-02 19:49:40 +00:00
XmlNode colladaNode = node.child("COLLADA");
if (colladaNode.empty()) {
return;
}
// Read content and embedded textures
ReadContents(colladaNode);
if (zip_archive && zip_archive->isOpen()) {
ReadEmbeddedTextures(*zip_archive);
}
}
// ------------------------------------------------------------------------------------------------
// Destructor, private as well
ColladaParser::~ColladaParser() {
for (auto & it : mNodeLibrary) {
delete it.second;
2020-10-14 18:34:46 +00:00
}
for (auto & it : mMeshLibrary) {
delete it.second;
2020-10-14 18:34:46 +00:00
}
}
// ------------------------------------------------------------------------------------------------
// Read a ZAE manifest and return the filename to attempt to open
std::string ColladaParser::ReadZaeManifest(ZipArchiveIOSystem &zip_archive) {
// Open the manifest
std::unique_ptr<IOStream> manifestfile(zip_archive.Open("manifest.xml"));
if (manifestfile == nullptr) {
// No manifest, hope there is only one .DAE inside
std::vector<std::string> file_list;
zip_archive.getFileListExtension(file_list, "dae");
2020-08-19 15:10:30 +00:00
if (file_list.empty()) {
return std::string();
2020-08-19 15:10:30 +00:00
}
return file_list.front();
}
2020-07-10 20:25:38 +00:00
XmlParser manifestParser;
if (!manifestParser.parse(manifestfile.get())) {
2020-07-10 20:25:38 +00:00
return std::string();
}
2020-09-14 19:35:36 +00:00
XmlNode root = manifestParser.getRootNode();
2020-09-23 19:23:12 +00:00
const std::string &name = root.name();
2020-07-10 20:25:38 +00:00
if (name != "dae_root") {
root = *manifestParser.findNode("dae_root");
2020-07-10 20:25:38 +00:00
if (nullptr == root) {
return std::string();
}
2020-08-30 19:10:04 +00:00
std::string v;
XmlParser::getValueAsString(root, v);
2020-08-30 19:10:04 +00:00
aiString ai_str(v);
2020-07-10 20:25:38 +00:00
UriDecodePath(ai_str);
return std::string(ai_str.C_Str());
}
2020-08-19 15:10:30 +00:00
return std::string();
}
// ------------------------------------------------------------------------------------------------
// Convert a path read from a collada file to the usual representation
void ColladaParser::UriDecodePath(aiString &ss) {
// TODO: collada spec, p 22. Handle URI correctly.
// For the moment we're just stripping the file:// away to make it work.
// Windows doesn't seem to be able to find stuff like
// 'file://..\LWO\LWO2\MappingModes\earthSpherical.jpg'
if (0 == strncmp(ss.data, "file://", 7)) {
ss.length -= 7;
memmove(ss.data, ss.data + 7, ss.length);
ss.data[ss.length] = '\0';
}
// Maxon Cinema Collada Export writes "file:///C:\andsoon" with three slashes...
// I need to filter it without destroying linux paths starting with "/somewhere"
#if defined(_MSC_VER)
if (ss.data[0] == '/' && isalpha((unsigned char)ss.data[1]) && ss.data[2] == ':') {
#else
if (ss.data[0] == '/' && isalpha((unsigned char)ss.data[1]) && ss.data[2] == ':') {
#endif
--ss.length;
::memmove(ss.data, ss.data + 1, ss.length);
ss.data[ss.length] = 0;
}
// find and convert all %xy special chars
char *out = ss.data;
for (const char *it = ss.data; it != ss.data + ss.length; /**/) {
if (*it == '%' && (it + 3) < ss.data + ss.length) {
// separate the number to avoid dragging in chars from behind into the parsing
char mychar[3] = { it[1], it[2], 0 };
size_t nbr = strtoul16(mychar);
it += 3;
*out++ = (char)(nbr & 0xFF);
} else {
*out++ = *it++;
}
}
// adjust length and terminator of the shortened string
*out = 0;
2019-10-14 11:15:02 +00:00
ai_assert(out > ss.data);
ss.length = static_cast<ai_uint32>(out - ss.data);
}
// ------------------------------------------------------------------------------------------------
// Reads the contents of the file
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadContents(XmlNode &node) {
2020-08-28 14:17:56 +00:00
const std::string name = node.name();
if (name == "COLLADA") {
std::string version;
2020-08-28 14:17:56 +00:00
if (XmlParser::getStdStrAttribute(node, "version", version)) {
2020-06-27 13:57:06 +00:00
aiString v;
v.Set(version.c_str());
2020-06-27 13:57:06 +00:00
mAssetMetaData.emplace(AI_METADATA_SOURCE_FORMAT_VERSION, v);
if (!::strncmp(version.c_str(), "1.5", 3)) {
2020-06-27 13:57:06 +00:00
mFormat = FV_1_5_n;
ASSIMP_LOG_DEBUG("Collada schema version is 1.5.n");
} else if (!::strncmp(version.c_str(), "1.4", 3)) {
2020-06-27 13:57:06 +00:00
mFormat = FV_1_4_n;
ASSIMP_LOG_DEBUG("Collada schema version is 1.4.n");
} else if (!::strncmp(version.c_str(), "1.3", 3)) {
2020-06-27 13:57:06 +00:00
mFormat = FV_1_3_n;
ASSIMP_LOG_DEBUG("Collada schema version is 1.3.n");
}
}
2020-08-28 14:17:56 +00:00
ReadStructure(node);
2020-06-27 13:57:06 +00:00
}
}
// ------------------------------------------------------------------------------------------------
// Reads the structure of the file
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadStructure(XmlNode &node) {
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
const std::string &currentName = currentNode.name();
2020-10-19 21:53:27 +00:00
if (currentName == "asset") {
2020-08-27 22:09:51 +00:00
ReadAssetInfo(currentNode);
2020-10-19 21:53:27 +00:00
} else if (currentName == "library_animations") {
2020-08-27 22:09:51 +00:00
ReadAnimationLibrary(currentNode);
2020-10-19 21:53:27 +00:00
} else if (currentName == "library_animation_clips") {
2020-08-27 22:09:51 +00:00
ReadAnimationClipLibrary(currentNode);
2020-10-19 21:53:27 +00:00
} else if (currentName == "library_controllers") {
2020-08-27 22:09:51 +00:00
ReadControllerLibrary(currentNode);
2020-10-19 21:53:27 +00:00
} else if (currentName == "library_images") {
2020-08-27 22:09:51 +00:00
ReadImageLibrary(currentNode);
2020-10-19 21:53:27 +00:00
} else if (currentName == "library_materials") {
2020-08-27 22:09:51 +00:00
ReadMaterialLibrary(currentNode);
2020-10-19 21:53:27 +00:00
} else if (currentName == "library_effects") {
2020-08-27 22:09:51 +00:00
ReadEffectLibrary(currentNode);
2020-10-19 21:53:27 +00:00
} else if (currentName == "library_geometries") {
2020-08-27 22:09:51 +00:00
ReadGeometryLibrary(currentNode);
2020-10-19 21:53:27 +00:00
} else if (currentName == "library_visual_scenes") {
2020-08-27 22:09:51 +00:00
ReadSceneLibrary(currentNode);
2020-10-19 21:53:27 +00:00
} else if (currentName == "library_lights") {
2020-08-27 22:09:51 +00:00
ReadLightLibrary(currentNode);
2020-10-19 21:53:27 +00:00
} else if (currentName == "library_cameras") {
2020-08-27 22:09:51 +00:00
ReadCameraLibrary(currentNode);
2020-10-19 21:53:27 +00:00
} else if (currentName == "library_nodes") {
2020-08-27 22:09:51 +00:00
ReadSceneNode(currentNode, nullptr); /* some hacking to reuse this piece of code */
2020-10-19 21:53:27 +00:00
} else if (currentName == "scene") {
2020-08-27 22:09:51 +00:00
ReadScene(currentNode);
2020-10-19 21:53:27 +00:00
}
2020-06-27 13:57:06 +00:00
}
PostProcessRootAnimations();
PostProcessControllers();
}
// ------------------------------------------------------------------------------------------------
// Reads asset information such as coordinate system information and legal blah
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadAssetInfo(XmlNode &node) {
2020-08-19 15:10:30 +00:00
if (node.empty()) {
return;
}
2020-06-27 13:57:06 +00:00
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
const std::string &currentName = currentNode.name();
if (currentName == "unit") {
2020-06-27 13:57:06 +00:00
mUnitSize = 1.f;
XmlParser::getRealAttribute(currentNode, "meter", mUnitSize);
2020-10-19 21:53:27 +00:00
} else if (currentName == "up_axis") {
2020-08-30 19:10:04 +00:00
std::string v;
2020-10-19 21:53:27 +00:00
if (!XmlParser::getValueAsString(currentNode, v)) {
continue;
}
2020-09-10 22:46:29 +00:00
if (v == "X_UP") {
2020-06-27 13:57:06 +00:00
mUpDirection = UP_X;
2020-09-10 22:46:29 +00:00
} else if (v == "Z_UP") {
2020-06-27 13:57:06 +00:00
mUpDirection = UP_Z;
} else {
mUpDirection = UP_Y;
}
2020-10-19 21:53:27 +00:00
} else if (currentName == "contributor") {
for (XmlNode currentChildNode : currentNode.children()) {
ReadMetaDataItem(currentChildNode, mAssetMetaData);
2020-09-27 18:20:44 +00:00
}
2020-09-22 13:51:21 +00:00
} else {
ReadMetaDataItem(currentNode, mAssetMetaData);
2020-06-27 13:57:06 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads a single string metadata item
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadMetaDataItem(XmlNode &node, StringMetaData &metadata) {
const Collada::MetaKeyPairVector &key_renaming = GetColladaAssimpMetaKeysCamelCase();
2020-06-27 13:57:06 +00:00
const std::string name = node.name();
2020-08-30 19:10:04 +00:00
if (name.empty()) {
return;
}
std::string v;
2020-10-19 17:06:11 +00:00
if (!XmlParser::getValueAsString(node, v)) {
return;
}
2020-08-30 19:10:04 +00:00
v = ai_trim(v);
2020-10-19 17:06:11 +00:00
aiString aistr;
aistr.Set(v);
2020-08-30 19:10:04 +00:00
2020-10-19 17:06:11 +00:00
std::string camel_key_str(name);
ToCamelCase(camel_key_str);
size_t found_index;
if (FindCommonKey(camel_key_str, key_renaming, found_index)) {
metadata.emplace(key_renaming[found_index].second, aistr);
} else {
metadata.emplace(camel_key_str, aistr);
2020-06-27 13:57:06 +00:00
}
}
// ------------------------------------------------------------------------------------------------
// Reads the animation clips
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadAnimationClipLibrary(XmlNode &node) {
if (node.empty()) {
return;
2020-06-27 13:57:06 +00:00
}
2020-06-27 13:57:06 +00:00
std::string animName;
2020-10-19 21:53:27 +00:00
if (!XmlParser::getStdStrAttribute(node, "name", animName)) {
if (!XmlParser::getStdStrAttribute( node, "id", animName )) {
animName = std::string("animation_") + ai_to_string(mAnimationClipLibrary.size());
2020-06-27 13:57:06 +00:00
}
}
std::pair<std::string, std::vector<std::string>> clip;
clip.first = animName;
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
const std::string &currentName = currentNode.name();
2020-06-27 13:57:06 +00:00
if (currentName == "instance_animation") {
2020-10-19 21:53:27 +00:00
std::string url;
readUrlAttribute(currentNode, url);
2020-10-19 21:53:27 +00:00
clip.second.push_back(url);
2020-06-27 13:57:06 +00:00
}
if (clip.second.size() > 0) {
mAnimationClipLibrary.push_back(clip);
}
}
}
void ColladaParser::PostProcessControllers() {
std::string meshId;
for (auto & it : mControllerLibrary) {
meshId = it.second.mMeshId;
2020-09-21 14:39:24 +00:00
if (meshId.empty()) {
2020-09-21 18:05:16 +00:00
continue;
2020-09-21 14:39:24 +00:00
}
ControllerLibrary::iterator findItr = mControllerLibrary.find(meshId);
while (findItr != mControllerLibrary.end()) {
meshId = findItr->second.mMeshId;
findItr = mControllerLibrary.find(meshId);
}
it.second.mMeshId = meshId;
}
}
// ------------------------------------------------------------------------------------------------
// Re-build animations from animation clip library, if present, otherwise combine single-channel animations
void ColladaParser::PostProcessRootAnimations() {
2020-06-27 13:57:06 +00:00
if (mAnimationClipLibrary.empty()) {
mAnims.CombineSingleChannelAnimations();
return;
}
2020-06-27 13:57:06 +00:00
Animation temp;
for (auto & it : mAnimationClipLibrary) {
std::string clipName = it.first;
2020-06-27 13:57:06 +00:00
Animation *clip = new Animation();
clip->mName = clipName;
2020-06-27 13:57:06 +00:00
temp.mSubAnims.push_back(clip);
for (std::string animationID : it.second) {
2020-06-27 13:57:06 +00:00
AnimationLibrary::iterator animation = mAnimationLibrary.find(animationID);
2020-06-27 13:57:06 +00:00
if (animation != mAnimationLibrary.end()) {
Animation *pSourceAnimation = animation->second;
pSourceAnimation->CollectChannelsRecursively(clip->mChannels);
}
}
2020-06-27 13:57:06 +00:00
}
2020-06-27 13:57:06 +00:00
mAnims = temp;
2020-06-27 13:57:06 +00:00
// Ensure no double deletes.
temp.mSubAnims.clear();
}
// ------------------------------------------------------------------------------------------------
// Reads the animation library
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadAnimationLibrary(XmlNode &node) {
2020-08-19 15:10:30 +00:00
if (node.empty()) {
return;
}
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
const std::string &currentName = currentNode.name();
2020-06-27 13:57:06 +00:00
if (currentName == "animation") {
2020-08-27 22:09:51 +00:00
ReadAnimation(currentNode, &mAnims);
2020-06-27 13:57:06 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads an animation into the given parent structure
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadAnimation(XmlNode &node, Collada::Animation *pParent) {
if (node.empty()) {
2015-05-19 03:57:13 +00:00
return;
2020-06-27 13:57:06 +00:00
}
2015-05-19 03:57:13 +00:00
// an <animation> element may be a container for grouping sub-elements or an animation channel
// this is the channel collection by ID, in case it has channels
using ChannelMap = std::map<std::string, AnimationChannel>;
2015-05-19 03:57:13 +00:00
ChannelMap channels;
// this is the anim container in case we're a container
Animation *anim = nullptr;
2015-05-19 03:57:13 +00:00
// optional name given as an attribute
std::string animName;
2020-10-19 21:53:27 +00:00
if (!XmlParser::getStdStrAttribute(node, "name", animName)) {
2020-06-27 13:57:06 +00:00
animName = "animation";
}
std::string animID;
2020-06-27 13:57:06 +00:00
pugi::xml_attribute idAttr = node.attribute("id");
if (idAttr) {
animID = idAttr.as_string();
}
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
const std::string &currentName = currentNode.name();
2020-06-27 13:57:06 +00:00
if (currentName == "animation") {
if (!anim) {
anim = new Animation;
anim->mName = animName;
pParent->mSubAnims.push_back(anim);
}
2015-05-19 03:57:13 +00:00
2020-06-27 13:57:06 +00:00
// recurse into the sub-element
2020-08-28 14:17:56 +00:00
ReadAnimation(currentNode, anim);
2020-06-27 13:57:06 +00:00
} else if (currentName == "source") {
2020-08-28 14:17:56 +00:00
ReadSource(currentNode);
2020-06-27 13:57:06 +00:00
} else if (currentName == "sampler") {
2020-10-19 21:53:27 +00:00
std::string id;
if (XmlParser::getStdStrAttribute(currentNode, "id", id)) {
2020-06-27 13:57:06 +00:00
// have it read into a channel
2020-10-19 21:53:27 +00:00
ChannelMap::iterator newChannel = channels.insert(std::make_pair(id, AnimationChannel())).first;
2020-08-28 14:17:56 +00:00
ReadAnimationSampler(currentNode, newChannel->second);
}
} else if (currentName == "channel") {
std::string source_name, target;
XmlParser::getStdStrAttribute(currentNode, "source", source_name);
XmlParser::getStdStrAttribute(currentNode, "target", target);
if (source_name[0] == '#') {
source_name = source_name.substr(1, source_name.size() - 1);
}
ChannelMap::iterator cit = channels.find(source_name);
if (cit != channels.end()) {
cit->second.mTarget = target;
2020-06-27 13:57:06 +00:00
}
}
}
2015-05-19 03:57:13 +00:00
// it turned out to have channels - add them
if (!channels.empty()) {
if (nullptr == anim) {
anim = new Animation;
anim->mName = animName;
pParent->mSubAnims.push_back(anim);
}
for (const auto & channel : channels) {
anim->mChannels.push_back(channel.second);
}
2020-09-27 19:09:06 +00:00
if (idAttr) {
mAnimationLibrary[animID] = anim;
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads an animation sampler into the given anim channel
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadAnimationSampler(XmlNode &node, Collada::AnimationChannel &pChannel) {
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
const std::string &currentName = currentNode.name();
2020-06-27 13:57:06 +00:00
if (currentName == "input") {
if (XmlParser::hasAttribute(currentNode, "semantic")) {
std::string semantic, sourceAttr;
XmlParser::getStdStrAttribute(currentNode, "semantic", semantic);
if (XmlParser::hasAttribute(currentNode, "source")) {
XmlParser::getStdStrAttribute(currentNode, "source", sourceAttr);
const char *source = sourceAttr.c_str();
2020-09-25 19:00:09 +00:00
if (source[0] != '#') {
throw DeadlyImportError("Unsupported URL format");
}
2020-07-10 20:25:38 +00:00
source++;
2020-10-19 21:53:27 +00:00
if (semantic == "INPUT") {
2020-07-10 20:25:38 +00:00
pChannel.mSourceTimes = source;
2020-10-19 21:53:27 +00:00
} else if (semantic == "OUTPUT") {
2020-07-10 20:25:38 +00:00
pChannel.mSourceValues = source;
2020-10-19 21:53:27 +00:00
} else if (semantic == "IN_TANGENT") {
2020-07-10 20:25:38 +00:00
pChannel.mInTanValues = source;
2020-10-19 21:53:27 +00:00
} else if (semantic == "OUT_TANGENT") {
2020-07-10 20:25:38 +00:00
pChannel.mOutTanValues = source;
2020-10-19 21:53:27 +00:00
} else if (semantic == "INTERPOLATION") {
2020-07-10 20:25:38 +00:00
pChannel.mInterpolationValues = source;
2020-10-19 21:53:27 +00:00
}
2020-07-10 20:25:38 +00:00
}
2020-06-27 13:57:06 +00:00
}
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads the skeleton controller library
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadControllerLibrary(XmlNode &node) {
if (node.empty()) {
2015-05-19 03:57:13 +00:00
return;
2020-06-27 13:57:06 +00:00
}
2015-05-19 03:57:13 +00:00
2020-09-10 22:46:29 +00:00
for (XmlNode &currentNode : node.children()) {
const std::string &currentName = currentNode.name();
if (currentName != "controller") {
2020-09-11 13:46:46 +00:00
continue;
2020-09-10 22:46:29 +00:00
}
std::string id;
if (XmlParser::getStdStrAttribute(currentNode, "id", id)) {
mControllerLibrary[id] = Controller();
ReadController(currentNode, mControllerLibrary[id]);
}
2020-09-10 22:46:29 +00:00
}
}
// ------------------------------------------------------------------------------------------------
// Reads a controller into the given mesh structure
void ColladaParser::ReadController(XmlNode &node, Collada::Controller &controller) {
2017-01-12 11:41:32 +00:00
// initial values
controller.mType = Skin;
controller.mMethod = Normalized;
XmlNodeIterator xmlIt(node);
xmlIt.collectChildrenPreOrder(node);
XmlNode currentNode;
while (xmlIt.getNext(currentNode)) {
//for (XmlNode &currentNode : node.children()) {
2020-08-19 20:44:13 +00:00
const std::string &currentName = currentNode.name();
2020-07-10 20:25:38 +00:00
if (currentName == "morph") {
controller.mType = Morph;
controller.mMeshId = currentNode.attribute("source").as_string();
2020-07-10 20:25:38 +00:00
int methodIndex = currentNode.attribute("method").as_int();
if (methodIndex > 0) {
2020-08-30 19:10:04 +00:00
std::string method;
XmlParser::getValueAsString(currentNode, method);
2020-09-10 22:46:29 +00:00
if (method == "RELATIVE") {
controller.mMethod = Relative;
2020-07-10 20:25:38 +00:00
}
}
} else if (currentName == "skin") {
std::string id;
if (XmlParser::getStdStrAttribute(currentNode, "source", id)) {
controller.mMeshId = id.substr(1, id.size()-1);
}
2020-07-10 20:25:38 +00:00
} else if (currentName == "bind_shape_matrix") {
2020-09-04 05:33:10 +00:00
std::string v;
XmlParser::getValueAsString(currentNode, v);
const char *content = v.c_str();
2020-07-10 20:25:38 +00:00
for (unsigned int a = 0; a < 16; a++) {
SkipSpacesAndLineEnd(&content);
2020-07-10 20:25:38 +00:00
// read a number
content = fast_atoreal_move<ai_real>(content, controller.mBindShapeMatrix[a]);
2020-07-10 20:25:38 +00:00
// skip whitespace after it
SkipSpacesAndLineEnd(&content);
}
} else if (currentName == "source") {
ReadSource(currentNode);
2020-08-27 15:05:09 +00:00
} else if (currentName == "joints") {
ReadControllerJoints(currentNode, controller);
2020-08-26 20:31:46 +00:00
} else if (currentName == "vertex_weights") {
ReadControllerWeights(currentNode, controller);
2020-08-26 20:31:46 +00:00
} else if (currentName == "targets") {
2020-08-28 14:17:56 +00:00
for (XmlNode currentChildNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
2020-08-27 22:09:51 +00:00
const std::string &currentChildName = currentChildNode.name();
2020-07-10 20:25:38 +00:00
if (currentChildName == "input") {
2020-08-27 22:09:51 +00:00
const char *semantics = currentChildNode.attribute("semantic").as_string();
const char *source = currentChildNode.attribute("source").as_string();
2020-07-10 20:25:38 +00:00
if (strcmp(semantics, "MORPH_TARGET") == 0) {
controller.mMorphTarget = source + 1;
2020-07-10 20:25:38 +00:00
} else if (strcmp(semantics, "MORPH_WEIGHT") == 0) {
controller.mMorphWeight = source + 1;
2020-07-10 20:25:38 +00:00
}
}
}
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads the joint definitions for the given controller
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadControllerJoints(XmlNode &node, Collada::Controller &pController) {
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
const std::string &currentName = currentNode.name();
2020-07-10 20:25:38 +00:00
if (currentName == "input") {
2020-08-19 20:44:13 +00:00
const char *attrSemantic = currentNode.attribute("semantic").as_string();
const char *attrSource = currentNode.attribute("source").as_string();
2020-07-10 20:25:38 +00:00
if (attrSource[0] != '#') {
2020-09-25 19:00:09 +00:00
throw DeadlyImportError("Unsupported URL format in \"", attrSource, "\" in source attribute of <joints> data <input> element");
2020-07-10 20:25:38 +00:00
}
2020-08-19 20:44:13 +00:00
++attrSource;
2020-07-10 20:25:38 +00:00
// parse source URL to corresponding source
if (strcmp(attrSemantic, "JOINT") == 0) {
pController.mJointNameSource = attrSource;
} else if (strcmp(attrSemantic, "INV_BIND_MATRIX") == 0) {
pController.mJointOffsetMatrixSource = attrSource;
} else {
2020-09-25 19:00:09 +00:00
throw DeadlyImportError("Unknown semantic \"" , attrSemantic , "\" in <joints> data <input> element");
2015-05-19 03:57:13 +00:00
}
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads the joint weights for the given controller
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadControllerWeights(XmlNode &node, Collada::Controller &pController) {
2020-07-10 20:25:38 +00:00
// Read vertex count from attributes and resize the array accordingly
int vertexCount=0;
2020-08-26 20:31:46 +00:00
XmlParser::getIntAttribute(node, "count", vertexCount);
pController.mWeightCounts.resize(vertexCount);
2015-05-19 03:57:13 +00:00
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
const std::string &currentName = currentNode.name();
2020-07-10 20:25:38 +00:00
if (currentName == "input") {
InputChannel channel;
2020-08-19 20:44:13 +00:00
const char *attrSemantic = currentNode.attribute("semantic").as_string();
const char *attrSource = currentNode.attribute("source").as_string();
channel.mOffset = currentNode.attribute("offset").as_int();
2020-07-10 20:25:38 +00:00
// local URLS always start with a '#'. We don't support global URLs
2020-08-19 20:44:13 +00:00
if (attrSource[0] != '#') {
2020-09-25 19:00:09 +00:00
throw DeadlyImportError( "Unsupported URL format in \"", attrSource, "\" in source attribute of <vertex_weights> data <input> element");
2020-08-19 20:44:13 +00:00
}
2020-07-10 20:25:38 +00:00
channel.mAccessor = attrSource + 1;
// parse source URL to corresponding source
if (strcmp(attrSemantic, "JOINT") == 0) {
pController.mWeightInputJoints = channel;
} else if (strcmp(attrSemantic, "WEIGHT") == 0) {
pController.mWeightInputWeights = channel;
} else {
2020-09-25 19:00:09 +00:00
throw DeadlyImportError("Unknown semantic \"", attrSemantic, "\" in <vertex_weights> data <input> element");
2020-07-10 20:25:38 +00:00
}
} else if (currentName == "vcount" && vertexCount > 0) {
const char *text = currentNode.text().as_string();
2020-07-10 20:25:38 +00:00
size_t numWeights = 0;
for (std::vector<size_t>::iterator it = pController.mWeightCounts.begin(); it != pController.mWeightCounts.end(); ++it) {
if (*text == 0) {
2020-09-25 19:00:09 +00:00
throw DeadlyImportError("Out of data while reading <vcount>");
2020-07-10 20:25:38 +00:00
}
*it = strtoul10(text, &text);
numWeights += *it;
SkipSpacesAndLineEnd(&text);
}
// reserve weight count
pController.mWeights.resize(numWeights);
} else if (currentName == "v" && vertexCount > 0) {
// read JointIndex - WeightIndex pairs
2020-08-30 19:10:04 +00:00
std::string stdText;
XmlParser::getValueAsString(currentNode, stdText);
const char *text = stdText.c_str();
2020-07-10 20:25:38 +00:00
for (std::vector<std::pair<size_t, size_t>>::iterator it = pController.mWeights.begin(); it != pController.mWeights.end(); ++it) {
2020-08-30 19:10:04 +00:00
if (text == 0) {
2020-09-25 19:00:09 +00:00
throw DeadlyImportError("Out of data while reading <vertex_weights>");
2020-07-10 20:25:38 +00:00
}
it->first = strtoul10(text, &text);
SkipSpacesAndLineEnd(&text);
2020-09-25 19:00:09 +00:00
if (*text == 0) {
throw DeadlyImportError("Out of data while reading <vertex_weights>");
}
2020-07-10 20:25:38 +00:00
it->second = strtoul10(text, &text);
SkipSpacesAndLineEnd(&text);
}
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads the image library contents
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadImageLibrary(XmlNode &node) {
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
2020-10-19 17:06:11 +00:00
const std::string &currentName = currentNode.name();
if (currentName == "image") {
std::string id;
if (XmlParser::getStdStrAttribute( currentNode, "id", id )) {
mImageLibrary[id] = Image();
// read on from there
ReadImage(currentNode, mImageLibrary[id]);
}
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads an image entry into the given image
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadImage(XmlNode &node, Collada::Image &pImage) {
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
2020-07-10 20:25:38 +00:00
const std::string currentName = currentNode.name();
if (currentName == "image") {
// Ignore
continue;
} else if (currentName == "init_from") {
if (mFormat == FV_1_4_n) {
// FIX: C4D exporter writes empty <init_from/> tags
if (!currentNode.empty()) {
2015-05-19 03:57:13 +00:00
// element content is filename - hopefully
2020-08-30 19:10:04 +00:00
const char *sz = currentNode.text().as_string();
if (nullptr != sz) {
aiString filepath(sz);
UriDecodePath(filepath);
pImage.mFileName = filepath.C_Str();
}
2020-07-10 20:25:38 +00:00
}
if (!pImage.mFileName.length()) {
pImage.mFileName = "unknown_texture";
}
}
} else if (mFormat == FV_1_5_n) {
2020-08-30 19:10:04 +00:00
std::string value;
2020-07-10 20:25:38 +00:00
XmlNode refChild = currentNode.child("ref");
XmlNode hexChild = currentNode.child("hex");
if (refChild) {
// element content is filename - hopefully
2020-08-30 19:10:04 +00:00
if (XmlParser::getValueAsString(refChild, value)) {
aiString filepath(value);
2020-07-10 20:25:38 +00:00
UriDecodePath(filepath);
pImage.mFileName = filepath.C_Str();
}
} else if (hexChild && !pImage.mFileName.length()) {
// embedded image. get format
2020-08-19 20:44:13 +00:00
pImage.mEmbeddedFormat = hexChild.attribute("format").as_string();
if (pImage.mEmbeddedFormat.empty()) {
2020-07-10 20:25:38 +00:00
ASSIMP_LOG_WARN("Collada: Unknown image file format");
}
2015-05-19 03:57:13 +00:00
2020-08-30 19:10:04 +00:00
XmlParser::getValueAsString(hexChild, value);
const char *data = value.c_str();
2020-07-10 20:25:38 +00:00
// hexadecimal-encoded binary octets. First of all, find the
// required buffer size to reserve enough storage.
const char *cur = data;
while (!IsSpaceOrNewLine(*cur)) {
++cur;
}
const unsigned int size = (unsigned int)(cur - data) * 2;
pImage.mImageData.resize(size);
for (unsigned int i = 0; i < size; ++i) {
pImage.mImageData[i] = HexOctetToDecimal(data + (i << 1));
2015-05-19 03:57:13 +00:00
}
}
2020-08-19 15:10:30 +00:00
}
2015-05-19 03:57:13 +00:00
}
}
// ------------------------------------------------------------------------------------------------
// Reads the material library
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadMaterialLibrary(XmlNode &node) {
2015-05-19 03:57:13 +00:00
std::map<std::string, int> names;
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
2020-08-19 20:44:13 +00:00
std::string id = currentNode.attribute("id").as_string();
std::string name = currentNode.attribute("name").as_string();
2020-07-10 20:25:38 +00:00
mMaterialLibrary[id] = Material();
if (!name.empty()) {
std::map<std::string, int>::iterator it = names.find(name);
if (it != names.end()) {
std::ostringstream strStream;
strStream << ++it->second;
name.append(" " + strStream.str());
} else {
names[name] = 0;
}
mMaterialLibrary[id].mName = name;
}
ReadMaterial(currentNode, mMaterialLibrary[id]);
}
}
// ------------------------------------------------------------------------------------------------
// Reads the light library
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadLightLibrary(XmlNode &node) {
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
2020-08-19 15:10:30 +00:00
const std::string &currentName = currentNode.name();
if (currentName == "light") {
2020-10-19 21:53:27 +00:00
std::string id;
if (XmlParser::getStdStrAttribute(currentNode, "id", id)) {
ReadLight(currentNode, mLightLibrary[id] = Light());
}
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads the camera library
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadCameraLibrary(XmlNode &node) {
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
2020-08-19 15:10:30 +00:00
const std::string &currentName = currentNode.name();
if (currentName == "camera") {
2020-10-19 21:53:27 +00:00
std::string id;
if (!XmlParser::getStdStrAttribute(currentNode, "id", id)) {
continue;
}
2020-08-19 15:10:30 +00:00
// create an entry and store it in the library under its ID
Camera &cam = mCameraLibrary[id];
2020-10-19 21:53:27 +00:00
std::string name;
if (!XmlParser::getStdStrAttribute(currentNode, "name", name)) {
continue;
}
2020-08-19 15:10:30 +00:00
if (!name.empty()) {
cam.mName = name;
2015-05-19 03:57:13 +00:00
}
2020-10-19 21:53:27 +00:00
ReadCamera(currentNode, cam);
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads a material entry into the given material
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadMaterial(XmlNode &node, Collada::Material &pMaterial) {
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
2020-08-19 15:10:30 +00:00
const std::string &currentName = currentNode.name();
2020-09-22 22:20:06 +00:00
if (currentName == "instance_effect") {
2020-10-19 21:53:27 +00:00
std::string url;
readUrlAttribute(currentNode, url);
pMaterial.mEffect = url.c_str();
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads a light entry into the given light
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadLight(XmlNode &node, Collada::Light &pLight) {
XmlNodeIterator xmlIt(node);
2020-08-30 19:10:04 +00:00
xmlIt.collectChildrenPreOrder(node);
XmlNode currentNode;
while (xmlIt.getNext(currentNode)) {
2020-08-19 15:10:30 +00:00
const std::string &currentName = currentNode.name();
if (currentName == "spot") {
pLight.mType = aiLightSource_SPOT;
} else if (currentName == "ambient") {
pLight.mType = aiLightSource_AMBIENT;
} else if (currentName == "directional") {
pLight.mType = aiLightSource_DIRECTIONAL;
} else if (currentName == "point") {
pLight.mType = aiLightSource_POINT;
} else if (currentName == "color") {
// text content contains 3 floats
2020-08-30 19:10:04 +00:00
std::string v;
XmlParser::getValueAsString(currentNode, v);
const char *content = v.c_str();
2020-08-19 15:10:30 +00:00
content = fast_atoreal_move<ai_real>(content, (ai_real &)pLight.mColor.r);
SkipSpacesAndLineEnd(&content);
content = fast_atoreal_move<ai_real>(content, (ai_real &)pLight.mColor.g);
SkipSpacesAndLineEnd(&content);
content = fast_atoreal_move<ai_real>(content, (ai_real &)pLight.mColor.b);
SkipSpacesAndLineEnd(&content);
} else if (currentName == "constant_attenuation") {
XmlParser::getRealAttribute(currentNode, "constant_attenuation", pLight.mAttConstant);
2020-08-19 15:10:30 +00:00
} else if (currentName == "linear_attenuation") {
XmlParser::getRealAttribute(currentNode, "linear_attenuation", pLight.mAttLinear);
2020-08-19 15:10:30 +00:00
} else if (currentName == "quadratic_attenuation") {
XmlParser::getRealAttribute(currentNode, "quadratic_attenuation", pLight.mAttQuadratic);
2020-08-19 15:10:30 +00:00
} else if (currentName == "falloff_angle") {
XmlParser::getRealAttribute(currentNode, "falloff_angle", pLight.mFalloffAngle);
2020-08-19 15:10:30 +00:00
} else if (currentName == "falloff_exponent") {
XmlParser::getRealAttribute(currentNode, "falloff_exponent", pLight.mFalloffExponent);
2020-08-19 15:10:30 +00:00
}
// FCOLLADA extensions
// -------------------------------------------------------
else if (currentName == "outer_cone") {
XmlParser::getRealAttribute(currentNode, "outer_cone", pLight.mOuterAngle);
2020-08-26 20:31:46 +00:00
} else if (currentName == "penumbra_angle") { // ... and this one is even deprecated
XmlParser::getRealAttribute(currentNode, "penumbra_angle", pLight.mPenumbraAngle);
2020-08-19 15:10:30 +00:00
} else if (currentName == "intensity") {
XmlParser::getRealAttribute(currentNode, "intensity", pLight.mIntensity);
2020-08-19 15:10:30 +00:00
} else if (currentName == "falloff") {
XmlParser::getRealAttribute(currentNode, "falloff", pLight.mOuterAngle);
2020-08-19 15:10:30 +00:00
} else if (currentName == "hotspot_beam") {
XmlParser::getRealAttribute(currentNode, "hotspot_beam", pLight.mFalloffAngle);
2020-08-19 15:10:30 +00:00
}
// OpenCOLLADA extensions
// -------------------------------------------------------
else if (currentName == "decay_falloff") {
XmlParser::getRealAttribute(currentNode, "decay_falloff", pLight.mOuterAngle);
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads a camera entry into the given light
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadCamera(XmlNode &node, Collada::Camera &camera) {
XmlNodeIterator xmlIt(node);
xmlIt.collectChildrenPreOrder(node);
XmlNode currentNode;
while (xmlIt.getNext(currentNode)) {
2020-08-19 15:10:30 +00:00
const std::string &currentName = currentNode.name();
if (currentName == "orthographic") {
camera.mOrtho = true;
} else if (currentName == "xfov" || currentName == "xmag") {
XmlParser::getValueAsFloat(currentNode, camera.mHorFov);
2020-08-19 15:10:30 +00:00
} else if (currentName == "yfov" || currentName == "ymag") {
XmlParser::getValueAsFloat(currentNode, camera.mVerFov);
2020-08-19 15:10:30 +00:00
} else if (currentName == "aspect_ratio") {
XmlParser::getValueAsFloat(currentNode, camera.mAspect);
2020-08-19 15:10:30 +00:00
} else if (currentName == "znear") {
XmlParser::getValueAsFloat(currentNode, camera.mZNear);
2020-08-19 15:10:30 +00:00
} else if (currentName == "zfar") {
XmlParser::getValueAsFloat(currentNode, camera.mZFar);
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads the effect library
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadEffectLibrary(XmlNode &node) {
if (node.empty()) {
2015-05-19 03:57:13 +00:00
return;
}
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
2020-08-19 15:10:30 +00:00
const std::string &currentName = currentNode.name();
if (currentName == "effect") {
2020-08-28 14:17:56 +00:00
// read ID. Do I have to repeat my ranting about "optional" attributes?
2020-08-27 22:09:51 +00:00
std::string id;
XmlParser::getStdStrAttribute(currentNode, "id", id);
2015-05-19 03:57:13 +00:00
2020-08-19 15:10:30 +00:00
// create an entry and store it in the library under its ID
mEffectLibrary[id] = Effect();
2015-05-19 03:57:13 +00:00
2020-08-19 15:10:30 +00:00
// read on from there
ReadEffect(currentNode, mEffectLibrary[id]);
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads an effect entry into the given effect
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadEffect(XmlNode &node, Collada::Effect &pEffect) {
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
2020-08-19 15:10:30 +00:00
const std::string &currentName = currentNode.name();
if (currentName == "profile_COMMON") {
ReadEffectProfileCommon(currentNode, pEffect);
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads an COMMON effect profile
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadEffectProfileCommon(XmlNode &node, Collada::Effect &pEffect) {
XmlNodeIterator xmlIt(node);
xmlIt.collectChildrenPreOrder(node);
XmlNode currentNode;
while (xmlIt.getNext(currentNode)) {
2020-08-19 20:44:13 +00:00
const std::string &currentName = currentNode.name();
if (currentName == "newparam") {
// save ID
std::string sid = currentNode.attribute("sid").as_string();
pEffect.mParams[sid] = EffectParam();
ReadEffectParam(currentNode, pEffect.mParams[sid]);
2020-08-24 05:44:54 +00:00
} else if (currentName == "technique" || currentName == "extra") {
2020-08-19 20:44:13 +00:00
// just syntactic sugar
} else if (mFormat == FV_1_4_n && currentName == "image") {
// read ID. Another entry which is "optional" by design but obligatory in reality
std::string id = currentNode.attribute("id").as_string();
2015-05-19 03:57:13 +00:00
2020-08-19 20:44:13 +00:00
// create an entry and store it in the library under its ID
mImageLibrary[id] = Image();
2015-05-19 03:57:13 +00:00
2020-08-19 20:44:13 +00:00
// read on from there
ReadImage(currentNode, mImageLibrary[id]);
} else if (currentName == "phong")
pEffect.mShadeType = Shade_Phong;
else if (currentName == "constant")
pEffect.mShadeType = Shade_Constant;
else if (currentName == "lambert")
pEffect.mShadeType = Shade_Lambert;
else if (currentName == "blinn")
pEffect.mShadeType = Shade_Blinn;
/* Color + texture properties */
else if (currentName == "emission")
ReadEffectColor(currentNode, pEffect.mEmissive, pEffect.mTexEmissive);
else if (currentName == "ambient")
ReadEffectColor(currentNode, pEffect.mAmbient, pEffect.mTexAmbient);
else if (currentName == "diffuse")
ReadEffectColor(currentNode, pEffect.mDiffuse, pEffect.mTexDiffuse);
else if (currentName == "specular")
ReadEffectColor(currentNode, pEffect.mSpecular, pEffect.mTexSpecular);
else if (currentName == "reflective") {
ReadEffectColor(currentNode, pEffect.mReflective, pEffect.mTexReflective);
} else if (currentName == "transparent") {
pEffect.mHasTransparency = true;
const char *opaque = currentNode.attribute("opaque").as_string();
//const char *opaque = mReader->getAttributeValueSafe("opaque");
if (::strcmp(opaque, "RGB_ZERO") == 0 || ::strcmp(opaque, "RGB_ONE") == 0) {
pEffect.mRGBTransparency = true;
2015-05-19 03:57:13 +00:00
}
2020-08-19 20:44:13 +00:00
// In RGB_ZERO mode, the transparency is interpreted in reverse, go figure...
if (::strcmp(opaque, "RGB_ZERO") == 0 || ::strcmp(opaque, "A_ZERO") == 0) {
pEffect.mInvertTransparency = true;
}
2015-05-19 03:57:13 +00:00
2020-08-19 20:44:13 +00:00
ReadEffectColor(currentNode, pEffect.mTransparent, pEffect.mTexTransparent);
} else if (currentName == "shininess")
ReadEffectFloat(currentNode, pEffect.mShininess);
else if (currentName == "reflectivity")
ReadEffectFloat(currentNode, pEffect.mReflectivity);
2020-08-19 20:44:13 +00:00
/* Single scalar properties */
else if (currentName == "transparency")
ReadEffectFloat(currentNode, pEffect.mTransparency);
else if (currentName == "index_of_refraction")
ReadEffectFloat(currentNode, pEffect.mRefractIndex);
2015-05-19 03:57:13 +00:00
2020-08-19 20:44:13 +00:00
// GOOGLEEARTH/OKINO extensions
// -------------------------------------------------------
else if (currentName == "double_sided")
2020-08-26 20:31:46 +00:00
XmlParser::getBoolAttribute(currentNode, currentName.c_str(), pEffect.mDoubleSided);
2020-08-19 20:44:13 +00:00
// FCOLLADA extensions
// -------------------------------------------------------
else if (currentName == "bump") {
aiColor4D dummy;
ReadEffectColor(currentNode, dummy, pEffect.mTexBump);
}
// MAX3D extensions
// -------------------------------------------------------
else if (currentName == "wireframe") {
2020-08-26 20:31:46 +00:00
XmlParser::getBoolAttribute(currentNode, currentName.c_str(), pEffect.mWireframe);
2020-08-19 20:44:13 +00:00
} else if (currentName == "faceted") {
2020-08-26 20:31:46 +00:00
XmlParser::getBoolAttribute(currentNode, currentName.c_str(), pEffect.mFaceted);
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Read texture wrapping + UV transform settings from a profile==Maya chunk
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadSamplerProperties(XmlNode &node, Sampler &out) {
if (node.empty()) {
2015-05-19 03:57:13 +00:00
return;
}
XmlNodeIterator xmlIt(node);
xmlIt.collectChildrenPreOrder(node);
XmlNode currentNode;
while (xmlIt.getNext(currentNode)) {
2020-08-19 20:44:13 +00:00
const std::string &currentName = currentNode.name();
// MAYA extensions
// -------------------------------------------------------
if (currentName == "wrapU") {
2020-08-26 20:31:46 +00:00
XmlParser::getBoolAttribute(currentNode, currentName.c_str(), out.mWrapU);
2020-08-19 20:44:13 +00:00
} else if (currentName == "wrapV") {
2020-08-26 20:31:46 +00:00
XmlParser::getBoolAttribute(currentNode, currentName.c_str(), out.mWrapV);
2020-08-19 20:44:13 +00:00
} else if (currentName == "mirrorU") {
2020-08-26 20:31:46 +00:00
XmlParser::getBoolAttribute(currentNode, currentName.c_str(), out.mMirrorU);
2020-08-19 20:44:13 +00:00
} else if (currentName == "mirrorV") {
2020-08-26 20:31:46 +00:00
XmlParser::getBoolAttribute(currentNode, currentName.c_str(), out.mMirrorV);
2020-08-24 05:44:54 +00:00
} else if (currentName == "repeatU") {
XmlParser::getRealAttribute(currentNode, currentName.c_str(), out.mTransform.mScaling.x);
2020-08-19 20:44:13 +00:00
} else if (currentName == "repeatV") {
XmlParser::getRealAttribute(currentNode, currentName.c_str(), out.mTransform.mScaling.y);
2020-08-24 05:44:54 +00:00
} else if (currentName == "offsetU") {
XmlParser::getRealAttribute(currentNode, currentName.c_str(), out.mTransform.mTranslation.x);
2020-08-24 05:44:54 +00:00
} else if (currentName == "offsetV") {
XmlParser::getRealAttribute(currentNode, currentName.c_str(), out.mTransform.mTranslation.y);
2020-08-24 05:44:54 +00:00
} else if (currentName == "rotateUV") {
XmlParser::getRealAttribute(currentNode, currentName.c_str(), out.mTransform.mRotation);
2020-08-19 20:44:13 +00:00
} else if (currentName == "blend_mode") {
2020-08-30 19:10:04 +00:00
std::string v;
XmlParser::getValueAsString(currentNode, v);
const char *sz = v.c_str();
2020-08-19 20:44:13 +00:00
// http://www.feelingsoftware.com/content/view/55/72/lang,en/
// NONE, OVER, IN, OUT, ADD, SUBTRACT, MULTIPLY, DIFFERENCE, LIGHTEN, DARKEN, SATURATE, DESATURATE and ILLUMINATE
if (0 == ASSIMP_strincmp(sz, "ADD", 3))
out.mOp = aiTextureOp_Add;
else if (0 == ASSIMP_strincmp(sz, "SUBTRACT", 8))
out.mOp = aiTextureOp_Subtract;
else if (0 == ASSIMP_strincmp(sz, "MULTIPLY", 8))
out.mOp = aiTextureOp_Multiply;
else {
ASSIMP_LOG_WARN("Collada: Unsupported MAYA texture blend mode");
2015-05-19 03:57:13 +00:00
}
2020-08-19 20:44:13 +00:00
}
// OKINO extensions
// -------------------------------------------------------
else if (currentName == "weighting") {
XmlParser::getRealAttribute(currentNode, currentName.c_str(), out.mWeighting);
2020-08-24 05:44:54 +00:00
} else if (currentName == "mix_with_previous_layer") {
XmlParser::getRealAttribute(currentNode, currentName.c_str(), out.mMixWithPrevious);
2020-08-19 20:44:13 +00:00
}
// MAX3D extensions
// -------------------------------------------------------
2020-08-24 05:44:54 +00:00
else if (currentName == "amount") {
XmlParser::getRealAttribute(currentNode, currentName.c_str(), out.mWeighting);
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads an effect entry containing a color or a texture defining that color
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadEffectColor(XmlNode &node, aiColor4D &pColor, Sampler &pSampler) {
if (node.empty()) {
2015-05-19 03:57:13 +00:00
return;
2020-06-27 13:57:06 +00:00
}
XmlNodeIterator xmlIt(node);
xmlIt.collectChildrenPreOrder(node);
XmlNode currentNode;
2015-05-19 03:57:13 +00:00
while (xmlIt.getNext(currentNode)) {
const std::string &currentName = currentNode.name();
if (currentName == "color") {
// text content contains 4 floats
2020-08-30 19:10:04 +00:00
std::string v;
XmlParser::getValueAsString(currentNode, v);
const char *content = v.c_str();
content = fast_atoreal_move<ai_real>(content, (ai_real &)pColor.r);
SkipSpacesAndLineEnd(&content);
content = fast_atoreal_move<ai_real>(content, (ai_real &)pColor.g);
SkipSpacesAndLineEnd(&content);
content = fast_atoreal_move<ai_real>(content, (ai_real &)pColor.b);
SkipSpacesAndLineEnd(&content);
content = fast_atoreal_move<ai_real>(content, (ai_real &)pColor.a);
SkipSpacesAndLineEnd(&content);
} else if (currentName == "texture") {
// get name of source texture/sampler
2020-08-24 05:44:54 +00:00
XmlParser::getStdStrAttribute(currentNode, "texture", pSampler.mName);
// get name of UV source channel. Specification demands it to be there, but some exporters
// don't write it. It will be the default UV channel in case it's missing.
2020-08-24 05:44:54 +00:00
XmlParser::getStdStrAttribute(currentNode, "texcoord", pSampler.mUVChannel);
// as we've read texture, the color needs to be 1,1,1,1
pColor = aiColor4D(1.f, 1.f, 1.f, 1.f);
2020-08-24 05:44:54 +00:00
} else if (currentName == "technique") {
std::string profile;
XmlParser::getStdStrAttribute(currentNode, "profile", profile);
// Some extensions are quite useful ... ReadSamplerProperties processes
// several extensions in MAYA, OKINO and MAX3D profiles.
2020-08-24 05:44:54 +00:00
if (!::strcmp(profile.c_str(), "MAYA") || !::strcmp(profile.c_str(), "MAX3D") || !::strcmp(profile.c_str(), "OKINO")) {
// get more information on this sampler
2020-08-24 05:44:54 +00:00
ReadSamplerProperties(currentNode, pSampler);
2015-05-19 03:57:13 +00:00
}
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads an effect entry containing a float
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadEffectFloat(XmlNode &node, ai_real &pFloat) {
2020-08-24 05:44:54 +00:00
pFloat = 0.f;
2020-09-27 18:20:44 +00:00
XmlNode floatNode = node.child("float");
if (floatNode.empty()) {
return;
2015-05-19 03:57:13 +00:00
}
2020-09-27 18:20:44 +00:00
XmlParser::getValueAsFloat(floatNode, pFloat);
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Reads an effect parameter specification of any kind
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadEffectParam(XmlNode &node, Collada::EffectParam &pParam) {
2020-08-24 05:44:54 +00:00
if (node.empty()) {
return;
}
2020-10-14 18:34:46 +00:00
XmlNodeIterator xmlIt(node);
xmlIt.collectChildrenPreOrder(node);
XmlNode currentNode;
while (xmlIt.getNext(currentNode)) {
2020-08-24 05:44:54 +00:00
const std::string &currentName = currentNode.name();
if (currentName == "surface") {
// image ID given inside <init_from> tags
2020-09-27 18:20:44 +00:00
XmlNode initNode = currentNode.child("init_from");
if (initNode) {
std::string v;
XmlParser::getValueAsString(initNode, v);
pParam.mType = Param_Surface;
pParam.mReference = v.c_str();
}
2020-08-24 05:44:54 +00:00
} else if (currentName == "sampler2D" && (FV_1_4_n == mFormat || FV_1_3_n == mFormat)) {
// surface ID is given inside <source> tags
const char *content = currentNode.value();
pParam.mType = Param_Sampler;
pParam.mReference = content;
} else if (currentName == "sampler2D") {
// surface ID is given inside <instance_image> tags
std::string url;
XmlParser::getStdStrAttribute(currentNode, "url", url);
2020-09-25 19:00:09 +00:00
if (url[0] != '#') {
throw DeadlyImportError("Unsupported URL format in instance_image");
2015-05-19 03:57:13 +00:00
}
2020-08-24 05:44:54 +00:00
pParam.mType = Param_Sampler;
pParam.mReference = url.c_str() + 1;
2020-10-14 18:34:46 +00:00
} else if (currentName == "source") {
const char *source = currentNode.child_value();
if (nullptr != source) {
pParam.mReference = source;
}
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads the geometry library contents
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadGeometryLibrary(XmlNode &node) {
if (node.empty()) {
2015-05-19 03:57:13 +00:00
return;
2020-06-27 13:57:06 +00:00
}
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
2020-08-24 05:44:54 +00:00
const std::string &currentName = currentNode.name();
if (currentName == "geometry") {
// read ID. Another entry which is "optional" by design but obligatory in reality
2015-05-19 03:57:13 +00:00
2020-08-24 05:44:54 +00:00
std::string id;
XmlParser::getStdStrAttribute(currentNode, "id", id);
// create a mesh and store it in the library under its (resolved) ID
// Skip and warn if ID is not unique
if (mMeshLibrary.find(id) == mMeshLibrary.cend()) {
std::unique_ptr<Mesh> mesh(new Mesh(id));
2020-08-24 05:44:54 +00:00
XmlParser::getStdStrAttribute(currentNode, "name", mesh->mName);
2015-05-19 03:57:13 +00:00
2020-08-24 05:44:54 +00:00
// read on from there
ReadGeometry(currentNode, *mesh);
// Read successfully, add to library
mMeshLibrary.insert({ id, mesh.release() });
}
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads a geometry from the geometry library.
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadGeometry(XmlNode &node, Collada::Mesh &pMesh) {
if (node.empty()) {
2015-05-19 03:57:13 +00:00
return;
2020-06-27 13:57:06 +00:00
}
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
2020-08-24 05:44:54 +00:00
const std::string &currentName = currentNode.name();
if (currentName == "mesh") {
ReadMesh(currentNode, pMesh);
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads a mesh from the geometry library
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadMesh(XmlNode &node, Mesh &pMesh) {
if (node.empty()) {
2015-05-19 03:57:13 +00:00
return;
2020-06-27 13:57:06 +00:00
}
2015-05-19 03:57:13 +00:00
XmlNodeIterator xmlIt(node);
xmlIt.collectChildrenPreOrder(node);
XmlNode currentNode;
while (xmlIt.getNext(currentNode)) {
2020-08-24 05:44:54 +00:00
const std::string &currentName = currentNode.name();
if (currentName == "source") {
ReadSource(currentNode);
} else if (currentName == "vertices") {
ReadVertexData(currentNode, pMesh);
2020-10-19 17:06:11 +00:00
} else if (currentName == "triangles" || currentName == "lines" || currentName == "linestrips" ||
currentName == "polygons" || currentName == "polylist" || currentName == "trifans" ||
currentName == "tristrips") {
2020-08-24 05:44:54 +00:00
ReadIndexData(currentNode, pMesh);
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Reads a source element
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadSource(XmlNode &node) {
2020-08-24 05:44:54 +00:00
if (node.empty()) {
return;
}
std::string sourceID;
XmlParser::getStdStrAttribute(node, "id", sourceID);
XmlNodeIterator xmlIt(node);
xmlIt.collectChildrenPreOrder(node);
XmlNode currentNode;
while (xmlIt.getNext(currentNode)) {
2020-08-24 05:44:54 +00:00
const std::string &currentName = currentNode.name();
if (currentName == "float_array" || currentName == "IDREF_array" || currentName == "Name_array") {
ReadDataArray(currentNode);
} else if (currentName == "technique_common") {
2020-08-30 19:10:04 +00:00
XmlNode technique = currentNode.child("accessor");
if (!technique.empty()) {
ReadAccessor(technique, sourceID);
}
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads a data array holding a number of floats, and stores it in the global library
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadDataArray(XmlNode &node) {
2020-08-24 05:44:54 +00:00
std::string name = node.name();
bool isStringArray = (name == "IDREF_array" || name == "Name_array");
2015-05-19 03:57:13 +00:00
// read attributes
2020-08-24 05:44:54 +00:00
std::string id;
XmlParser::getStdStrAttribute(node, "id", id);
unsigned int count = 0;
2020-08-26 20:31:46 +00:00
XmlParser::getUIntAttribute(node, "count", count);
2020-08-30 19:10:04 +00:00
std::string v;
XmlParser::getValueAsString(node, v);
v = ai_trim(v);
2020-08-30 19:10:04 +00:00
const char *content = v.c_str();
// read values and store inside an array in the data library
mDataLibrary[id] = Data();
Data &data = mDataLibrary[id];
data.mIsStringArray = isStringArray;
// some exporters write empty data arrays, but we need to conserve them anyways because others might reference them
if (content) {
if (isStringArray) {
data.mStrings.reserve(count);
2015-05-19 03:57:13 +00:00
std::string s;
for (unsigned int a = 0; a < count; a++) {
2020-09-25 19:00:09 +00:00
if (*content == 0) {
throw DeadlyImportError("Expected more values while reading IDREF_array contents.");
}
2015-05-19 03:57:13 +00:00
s.clear();
while (!IsSpaceOrNewLine(*content))
2015-05-19 03:57:13 +00:00
s += *content++;
data.mStrings.push_back(s);
2015-05-19 03:57:13 +00:00
SkipSpacesAndLineEnd(&content);
2015-05-19 03:57:13 +00:00
}
} else {
data.mValues.reserve(count);
2015-05-19 03:57:13 +00:00
for (unsigned int a = 0; a < count; a++) {
2020-09-25 19:00:09 +00:00
if (*content == 0) {
throw DeadlyImportError("Expected more values while reading float_array contents.");
}
2015-05-19 03:57:13 +00:00
ai_real value;
2015-05-19 03:57:13 +00:00
// read a number
2020-09-21 14:39:24 +00:00
//SkipSpacesAndLineEnd(&content);
content = fast_atoreal_move<ai_real>(content, value);
data.mValues.push_back(value);
2015-05-19 03:57:13 +00:00
// skip whitespace after it
SkipSpacesAndLineEnd(&content);
2015-05-19 03:57:13 +00:00
}
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads an accessor and stores it in the global library
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadAccessor(XmlNode &node, const std::string &pID) {
2015-05-19 03:57:13 +00:00
// read accessor attributes
2020-08-24 05:44:54 +00:00
std::string source;
XmlParser::getStdStrAttribute(node, "source", source);
2020-09-25 19:00:09 +00:00
if (source[0] != '#') {
throw DeadlyImportError("Unknown reference format in url \"", source, "\" in source attribute of <accessor> element.");
}
int count = 0;
2020-08-24 05:44:54 +00:00
XmlParser::getIntAttribute(node, "count", count);
2015-05-19 03:57:13 +00:00
unsigned int offset = 0;
2020-08-24 05:44:54 +00:00
if (XmlParser::hasAttribute(node, "offset")) {
XmlParser::getUIntAttribute(node, "offset", offset);
}
2015-05-19 03:57:13 +00:00
unsigned int stride = 1;
2020-08-24 05:44:54 +00:00
if (XmlParser::hasAttribute(node, "stride")) {
XmlParser::getUIntAttribute(node, "stride", stride);
}
2015-05-19 03:57:13 +00:00
// store in the library under the given ID
mAccessorLibrary[pID] = Accessor();
Accessor &acc = mAccessorLibrary[pID];
2015-05-19 03:57:13 +00:00
acc.mCount = count;
acc.mOffset = offset;
acc.mStride = stride;
2020-08-24 05:44:54 +00:00
acc.mSource = source.c_str() + 1; // ignore the leading '#'
2015-05-19 03:57:13 +00:00
acc.mSize = 0; // gets incremented with every param
XmlNodeIterator xmlIt(node);
xmlIt.collectChildrenPreOrder(node);
XmlNode currentNode;
while (xmlIt.getNext(currentNode)) {
2020-08-24 05:44:54 +00:00
const std::string &currentName = currentNode.name();
if (currentName == "param") {
// read data param
std::string name;
if (XmlParser::hasAttribute(currentNode, "name")) {
XmlParser::getStdStrAttribute(currentNode, "name", name);
//name = mReader->getAttributeValue(attrName);
// analyse for common type components and store it's sub-offset in the corresponding field
/* Cartesian coordinates */
if (name == "X")
acc.mSubOffset[0] = acc.mParams.size();
else if (name == "Y")
acc.mSubOffset[1] = acc.mParams.size();
else if (name == "Z")
acc.mSubOffset[2] = acc.mParams.size();
/* RGBA colors */
else if (name == "R")
acc.mSubOffset[0] = acc.mParams.size();
else if (name == "G")
acc.mSubOffset[1] = acc.mParams.size();
else if (name == "B")
acc.mSubOffset[2] = acc.mParams.size();
else if (name == "A")
acc.mSubOffset[3] = acc.mParams.size();
/* UVWQ (STPQ) texture coordinates */
else if (name == "S")
acc.mSubOffset[0] = acc.mParams.size();
else if (name == "T")
acc.mSubOffset[1] = acc.mParams.size();
else if (name == "P")
acc.mSubOffset[2] = acc.mParams.size();
/* Generic extra data, interpreted as UV data, too*/
else if (name == "U")
acc.mSubOffset[0] = acc.mParams.size();
else if (name == "V")
acc.mSubOffset[1] = acc.mParams.size();
}
if (XmlParser::hasAttribute(currentNode, "type")) {
2015-05-19 03:57:13 +00:00
// read data type
2020-08-24 05:44:54 +00:00
// TODO: (thom) I don't have a spec here at work. Check if there are other multi-value types
// which should be tested for here.
std::string type;
2020-08-26 20:31:46 +00:00
2020-08-24 05:44:54 +00:00
XmlParser::getStdStrAttribute(currentNode, "type", type);
if (type == "float4x4")
acc.mSize += 16;
else
acc.mSize += 1;
2015-05-19 03:57:13 +00:00
}
2020-08-24 05:44:54 +00:00
acc.mParams.push_back(name);
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads input declarations of per-vertex mesh data into the given mesh
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadVertexData(XmlNode &node, Mesh &pMesh) {
2015-05-19 03:57:13 +00:00
// extract the ID of the <vertices> element. Not that we care, but to catch strange referencing schemes we should warn about
2020-08-24 05:44:54 +00:00
XmlParser::getStdStrAttribute(node, "id", pMesh.mVertexID);
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
2020-08-24 05:44:54 +00:00
const std::string &currentName = currentNode.name();
2020-08-26 20:31:46 +00:00
if (currentName == "input") {
2020-08-24 05:44:54 +00:00
ReadInputChannel(currentNode, pMesh.mPerVertexData);
} else {
2020-09-25 19:00:09 +00:00
throw DeadlyImportError("Unexpected sub element <", currentName, "> in tag <vertices>");
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads input declarations of per-index mesh data into the given mesh
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadIndexData(XmlNode &node, Mesh &pMesh) {
2015-05-19 03:57:13 +00:00
std::vector<size_t> vcount;
std::vector<InputChannel> perIndexData;
unsigned int numPrimitives = 0;
2020-09-27 19:11:42 +00:00
XmlParser::getUIntAttribute(node, "count", numPrimitives);
2015-05-19 03:57:13 +00:00
// read primitive count from the attribute
2020-08-24 05:44:54 +00:00
//int attrCount = GetAttribute("count");
//size_t numPrimitives = (size_t)mReader->getAttributeValueAsInt(attrCount);
2015-05-19 03:57:13 +00:00
// some mesh types (e.g. tristrips) don't specify primitive count upfront,
// so we need to sum up the actual number of primitives while we read the <p>-tags
size_t actualPrimitives = 0;
SubMesh subgroup;
2020-08-26 20:31:46 +00:00
if (XmlParser::hasAttribute(node, "material")) {
2020-08-24 05:44:54 +00:00
XmlParser::getStdStrAttribute(node, "material", subgroup.mMaterial);
}
2015-05-19 03:57:13 +00:00
// distinguish between polys and triangles
2020-08-24 05:44:54 +00:00
std::string elementName = node.name();
2015-05-19 03:57:13 +00:00
PrimitiveType primType = Prim_Invalid;
2020-08-24 05:44:54 +00:00
if (elementName == "lines")
2015-05-19 03:57:13 +00:00
primType = Prim_Lines;
2020-08-24 05:44:54 +00:00
else if (elementName == "linestrips")
2015-05-19 03:57:13 +00:00
primType = Prim_LineStrip;
2020-08-24 05:44:54 +00:00
else if (elementName == "polygons")
2015-05-19 03:57:13 +00:00
primType = Prim_Polygon;
2020-08-24 05:44:54 +00:00
else if (elementName == "polylist")
2015-05-19 03:57:13 +00:00
primType = Prim_Polylist;
2020-08-24 05:44:54 +00:00
else if (elementName == "triangles")
2015-05-19 03:57:13 +00:00
primType = Prim_Triangles;
2020-08-24 05:44:54 +00:00
else if (elementName == "trifans")
2015-05-19 03:57:13 +00:00
primType = Prim_TriFans;
2020-08-24 05:44:54 +00:00
else if (elementName == "tristrips")
2015-05-19 03:57:13 +00:00
primType = Prim_TriStrips;
ai_assert(primType != Prim_Invalid);
2015-05-19 03:57:13 +00:00
2015-12-10 16:19:33 +00:00
// also a number of <input> elements, but in addition a <p> primitive collection and probably index counts for all primitives
XmlNodeIterator xmlIt(node);
xmlIt.collectChildrenPreOrder(node);
XmlNode currentNode;
while (xmlIt.getNext(currentNode)) {
2020-08-24 05:44:54 +00:00
const std::string &currentName = currentNode.name();
if (currentName == "input") {
ReadInputChannel(currentNode, perIndexData);
} else if (currentName == "vcount") {
if (!currentNode.empty()) {
if (numPrimitives) // It is possible to define a mesh without any primitives
{
// case <polylist> - specifies the number of indices for each polygon
2020-08-30 19:10:04 +00:00
std::string v;
XmlParser::getValueAsString(currentNode, v);
const char *content = v.c_str();
2020-08-24 05:44:54 +00:00
vcount.reserve(numPrimitives);
for (unsigned int a = 0; a < numPrimitives; a++) {
2020-09-25 19:00:09 +00:00
if (*content == 0) {
throw DeadlyImportError("Expected more values while reading <vcount> contents.");
2015-05-19 03:57:13 +00:00
}
2020-08-24 05:44:54 +00:00
// read a number
vcount.push_back((size_t)strtoul10(content, &content));
// skip whitespace after it
SkipSpacesAndLineEnd(&content);
2015-05-19 03:57:13 +00:00
}
}
}
2020-08-24 05:44:54 +00:00
} else if (currentName == "p") {
if (!currentNode.empty()) {
// now here the actual fun starts - these are the indices to construct the mesh data from
actualPrimitives += ReadPrimitives(currentNode, pMesh, perIndexData, numPrimitives, vcount, primType);
}
} else if (currentName == "extra") {
// skip
2020-08-26 20:31:46 +00:00
} else if (currentName == "ph") {
2020-08-24 05:44:54 +00:00
// skip
} else {
2020-09-25 19:00:09 +00:00
throw DeadlyImportError("Unexpected sub element <", currentName, "> in tag <", elementName, ">");
2015-05-19 03:57:13 +00:00
}
}
2015-05-19 03:52:10 +00:00
#ifdef ASSIMP_BUILD_DEBUG
if (primType != Prim_TriFans && primType != Prim_TriStrips && primType != Prim_LineStrip &&
primType != Prim_Lines) { // this is ONLY to workaround a bug in SketchUp 15.3.331 where it writes the wrong 'count' when it writes out the 'lines'.
2015-05-19 03:57:13 +00:00
ai_assert(actualPrimitives == numPrimitives);
}
#endif
2015-05-19 03:57:13 +00:00
// only when we're done reading all <p> tags (and thus know the final vertex count) can we commit the submesh
subgroup.mNumFaces = actualPrimitives;
pMesh.mSubMeshes.push_back(subgroup);
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Reads a single input channel element and stores it in the given array, if valid
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadInputChannel(XmlNode &node, std::vector<InputChannel> &poChannels) {
2015-05-19 03:57:13 +00:00
InputChannel channel;
// read semantic
2020-08-24 05:44:54 +00:00
std::string semantic;
XmlParser::getStdStrAttribute(node, "semantic", semantic);
channel.mType = GetTypeForSemantic(semantic);
2015-05-19 03:57:13 +00:00
// read source
2020-08-24 05:44:54 +00:00
std::string source;
XmlParser::getStdStrAttribute(node, "source", source);
2020-09-25 19:00:09 +00:00
if (source[0] != '#') {
throw DeadlyImportError("Unknown reference format in url \"", source, "\" in source attribute of <input> element.");
}
2020-08-24 05:44:54 +00:00
channel.mAccessor = source.c_str() + 1; // skipping the leading #, hopefully the remaining text is the accessor ID only
2015-05-19 03:57:13 +00:00
// read index offset, if per-index <input>
2020-08-26 20:31:46 +00:00
if (XmlParser::hasAttribute(node, "offset")) {
XmlParser::getUIntAttribute(node, "offset", (unsigned int &)channel.mOffset);
}
2015-05-19 03:57:13 +00:00
// read set if texture coordinates
if (channel.mType == IT_Texcoord || channel.mType == IT_Color) {
2020-08-26 20:31:46 +00:00
int attrSet = -1;
if (XmlParser::hasAttribute(node, "set")) {
XmlParser::getIntAttribute(node, "set", attrSet);
2015-05-19 03:57:13 +00:00
}
2020-08-26 20:31:46 +00:00
channel.mIndex = attrSet;
2015-05-19 03:57:13 +00:00
}
// store, if valid type
if (channel.mType != IT_Invalid)
poChannels.push_back(channel);
}
// ------------------------------------------------------------------------------------------------
// Reads a <p> primitive index list and assembles the mesh data into the given mesh
2020-06-27 13:57:06 +00:00
size_t ColladaParser::ReadPrimitives(XmlNode &node, Mesh &pMesh, std::vector<InputChannel> &pPerIndexChannels,
size_t pNumPrimitives, const std::vector<size_t> &pVCount, PrimitiveType pPrimType) {
2015-05-19 03:57:13 +00:00
// determine number of indices coming per vertex
// find the offset index for all per-vertex channels
size_t numOffsets = 1;
size_t perVertexOffset = SIZE_MAX; // invalid value
for (const InputChannel &channel : pPerIndexChannels) {
numOffsets = std::max(numOffsets, channel.mOffset + 1);
if (channel.mType == IT_Vertex)
2015-05-19 03:57:13 +00:00
perVertexOffset = channel.mOffset;
}
// determine the expected number of indices
size_t expectedPointCount = 0;
switch (pPrimType) {
2020-10-19 17:06:11 +00:00
case Prim_Polylist: {
for (size_t i : pVCount)
expectedPointCount += i;
break;
}
case Prim_Lines:
expectedPointCount = 2 * pNumPrimitives;
break;
case Prim_Triangles:
expectedPointCount = 3 * pNumPrimitives;
break;
default:
// other primitive types don't state the index count upfront... we need to guess
break;
2015-05-19 03:57:13 +00:00
}
// and read all indices into a temporary array
std::vector<size_t> indices;
if (expectedPointCount > 0) {
indices.reserve(expectedPointCount * numOffsets);
}
2015-05-19 03:57:13 +00:00
// It is possible to not contain any indices
if (pNumPrimitives > 0) {
2020-08-30 19:10:04 +00:00
std::string v;
XmlParser::getValueAsString(node, v);
const char *content = v.c_str();
SkipSpacesAndLineEnd(&content);
while (*content != 0) {
2015-05-19 03:57:13 +00:00
// read a value.
// Hack: (thom) Some exporters put negative indices sometimes. We just try to carry on anyways.
int value = std::max(0, strtol10(content, &content));
indices.push_back(size_t(value));
2015-05-19 03:57:13 +00:00
// skip whitespace after it
SkipSpacesAndLineEnd(&content);
2015-05-19 03:57:13 +00:00
}
}
// complain if the index count doesn't fit
if (expectedPointCount > 0 && indices.size() != expectedPointCount * numOffsets) {
if (pPrimType == Prim_Lines) {
// HACK: We just fix this number since SketchUp 15.3.331 writes the wrong 'count' for 'lines'
ReportWarning("Expected different index count in <p> element, %zu instead of %zu.", indices.size(), expectedPointCount * numOffsets);
pNumPrimitives = (indices.size() / numOffsets) / 2;
2020-09-25 19:00:09 +00:00
} else {
throw DeadlyImportError("Expected different index count in <p> element.");
}
} else if (expectedPointCount == 0 && (indices.size() % numOffsets) != 0) {
throw DeadlyImportError("Expected different index count in <p> element.");
}
2015-05-19 03:57:13 +00:00
// find the data for all sources
for (std::vector<InputChannel>::iterator it = pMesh.mPerVertexData.begin(); it != pMesh.mPerVertexData.end(); ++it) {
InputChannel &input = *it;
if (input.mResolved) {
2015-05-19 03:57:13 +00:00
continue;
}
2015-05-19 03:57:13 +00:00
// find accessor
input.mResolved = &ResolveLibraryReference(mAccessorLibrary, input.mAccessor);
2016-04-03 00:38:00 +00:00
// resolve accessor's data pointer as well, if necessary
const Accessor *acc = input.mResolved;
if (!acc->mData) {
acc->mData = &ResolveLibraryReference(mDataLibrary, acc->mSource);
}
2015-05-19 03:57:13 +00:00
}
// and the same for the per-index channels
for (std::vector<InputChannel>::iterator it = pPerIndexChannels.begin(); it != pPerIndexChannels.end(); ++it) {
InputChannel &input = *it;
if (input.mResolved) {
2015-05-19 03:57:13 +00:00
continue;
}
2015-05-19 03:57:13 +00:00
// ignore vertex pointer, it doesn't refer to an accessor
if (input.mType == IT_Vertex) {
2015-05-19 03:57:13 +00:00
// warn if the vertex channel does not refer to the <vertices> element in the same mesh
2020-09-25 19:00:09 +00:00
if (input.mAccessor != pMesh.mVertexID) {
throw DeadlyImportError("Unsupported vertex referencing scheme.");
}
2015-05-19 03:57:13 +00:00
continue;
}
// find accessor
input.mResolved = &ResolveLibraryReference(mAccessorLibrary, input.mAccessor);
2016-04-03 00:38:00 +00:00
// resolve accessor's data pointer as well, if necessary
const Accessor *acc = input.mResolved;
if (!acc->mData) {
acc->mData = &ResolveLibraryReference(mDataLibrary, acc->mSource);
}
2015-05-19 03:57:13 +00:00
}
// For continued primitives, the given count does not come all in one <p>, but only one primitive per <p>
size_t numPrimitives = pNumPrimitives;
if (pPrimType == Prim_TriFans || pPrimType == Prim_Polygon)
2015-05-19 03:57:13 +00:00
numPrimitives = 1;
// For continued primitives, the given count is actually the number of <p>'s inside the parent tag
if (pPrimType == Prim_TriStrips) {
2015-05-19 03:57:13 +00:00
size_t numberOfVertices = indices.size() / numOffsets;
numPrimitives = numberOfVertices - 2;
}
if (pPrimType == Prim_LineStrip) {
size_t numberOfVertices = indices.size() / numOffsets;
numPrimitives = numberOfVertices - 1;
}
2015-05-19 03:57:13 +00:00
pMesh.mFaceSize.reserve(numPrimitives);
pMesh.mFacePosIndices.reserve(indices.size() / numOffsets);
2015-05-19 03:57:13 +00:00
size_t polylistStartVertex = 0;
for (size_t currentPrimitive = 0; currentPrimitive < numPrimitives; currentPrimitive++) {
2015-05-19 03:57:13 +00:00
// determine number of points for this primitive
size_t numPoints = 0;
switch (pPrimType) {
case Prim_Lines:
numPoints = 2;
for (size_t currentVertex = 0; currentVertex < numPoints; currentVertex++)
CopyVertex(currentVertex, numOffsets, numPoints, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
break;
case Prim_LineStrip:
numPoints = 2;
for (size_t currentVertex = 0; currentVertex < numPoints; currentVertex++)
CopyVertex(currentVertex, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
break;
case Prim_Triangles:
numPoints = 3;
for (size_t currentVertex = 0; currentVertex < numPoints; currentVertex++)
CopyVertex(currentVertex, numOffsets, numPoints, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
break;
case Prim_TriStrips:
numPoints = 3;
ReadPrimTriStrips(numOffsets, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
break;
case Prim_Polylist:
numPoints = pVCount[currentPrimitive];
for (size_t currentVertex = 0; currentVertex < numPoints; currentVertex++)
CopyVertex(polylistStartVertex + currentVertex, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, 0, indices);
polylistStartVertex += numPoints;
break;
case Prim_TriFans:
case Prim_Polygon:
numPoints = indices.size() / numOffsets;
for (size_t currentVertex = 0; currentVertex < numPoints; currentVertex++)
CopyVertex(currentVertex, numOffsets, numPoints, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
break;
default:
// LineStrip is not supported due to expected index unmangling
2020-09-25 19:00:09 +00:00
throw DeadlyImportError("Unsupported primitive type.");
break;
2015-05-19 03:57:13 +00:00
}
// store the face size to later reconstruct the face from
pMesh.mFaceSize.push_back(numPoints);
2015-05-19 03:57:13 +00:00
}
// if I ever get my hands on that guy who invented this steaming pile of indirection...
return numPrimitives;
}
///@note This function won't work correctly if both PerIndex and PerVertex channels have same channels.
///For example if TEXCOORD present in both <vertices> and <polylist> tags this function will create wrong uv coordinates.
///It's not clear from COLLADA documentation is this allowed or not. For now only exporter fixed to avoid such behavior
2020-06-27 13:57:06 +00:00
void ColladaParser::CopyVertex(size_t currentVertex, size_t numOffsets, size_t numPoints, size_t perVertexOffset, Mesh &pMesh,
std::vector<InputChannel> &pPerIndexChannels, size_t currentPrimitive, const std::vector<size_t> &indices) {
2015-05-19 03:57:13 +00:00
// calculate the base offset of the vertex whose attributes we ant to copy
size_t baseOffset = currentPrimitive * numOffsets * numPoints + currentVertex * numOffsets;
// don't overrun the boundaries of the index list
ai_assert((baseOffset + numOffsets - 1) < indices.size());
2015-05-19 03:57:13 +00:00
// extract per-vertex channels using the global per-vertex offset
for (std::vector<InputChannel>::iterator it = pMesh.mPerVertexData.begin(); it != pMesh.mPerVertexData.end(); ++it) {
2015-05-19 03:57:13 +00:00
ExtractDataObjectFromChannel(*it, indices[baseOffset + perVertexOffset], pMesh);
}
2015-05-19 03:57:13 +00:00
// and extract per-index channels using there specified offset
for (std::vector<InputChannel>::iterator it = pPerIndexChannels.begin(); it != pPerIndexChannels.end(); ++it) {
2015-05-19 03:57:13 +00:00
ExtractDataObjectFromChannel(*it, indices[baseOffset + it->mOffset], pMesh);
}
2015-05-19 03:57:13 +00:00
// store the vertex-data index for later assignment of bone vertex weights
pMesh.mFacePosIndices.push_back(indices[baseOffset + perVertexOffset]);
}
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadPrimTriStrips(size_t numOffsets, size_t perVertexOffset, Mesh &pMesh, std::vector<InputChannel> &pPerIndexChannels,
size_t currentPrimitive, const std::vector<size_t> &indices) {
if (currentPrimitive % 2 != 0) {
2015-05-19 03:57:13 +00:00
//odd tristrip triangles need their indices mangled, to preserve winding direction
CopyVertex(1, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
CopyVertex(0, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
CopyVertex(2, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
} else { //for non tristrips or even tristrip triangles
2015-05-19 03:57:13 +00:00
CopyVertex(0, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
CopyVertex(1, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
CopyVertex(2, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
}
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Extracts a single object from an input channel and stores it in the appropriate mesh data array
void ColladaParser::ExtractDataObjectFromChannel(const InputChannel &pInput, size_t pLocalIndex, Mesh &pMesh) {
2015-05-19 03:57:13 +00:00
// ignore vertex referrer - we handle them that separate
if (pInput.mType == IT_Vertex) {
2015-05-19 03:57:13 +00:00
return;
}
2015-05-19 03:57:13 +00:00
const Accessor &acc = *pInput.mResolved;
2020-09-25 19:00:09 +00:00
if (pLocalIndex >= acc.mCount) {
throw DeadlyImportError("Invalid data index (", pLocalIndex, "/", acc.mCount, ") in primitive specification");
}
2015-05-19 03:57:13 +00:00
// get a pointer to the start of the data object referred to by the accessor and the local index
const ai_real *dataObject = &(acc.mData->mValues[0]) + acc.mOffset + pLocalIndex * acc.mStride;
2015-05-19 03:57:13 +00:00
// assemble according to the accessors component sub-offset list. We don't care, yet,
// what kind of object exactly we're extracting here
ai_real obj[4];
for (size_t c = 0; c < 4; ++c) {
2015-05-19 03:57:13 +00:00
obj[c] = dataObject[acc.mSubOffset[c]];
}
2015-05-19 03:57:13 +00:00
// now we reinterpret it according to the type we're reading here
switch (pInput.mType) {
case IT_Position: // ignore all position streams except 0 - there can be only one position
if (pInput.mIndex == 0) {
pMesh.mPositions.push_back(aiVector3D(obj[0], obj[1], obj[2]));
} else {
ASSIMP_LOG_ERROR("Collada: just one vertex position stream supported");
}
break;
case IT_Normal:
2015-05-19 03:57:13 +00:00
// pad to current vertex count if necessary
if (pMesh.mNormals.size() < pMesh.mPositions.size() - 1)
pMesh.mNormals.insert(pMesh.mNormals.end(), pMesh.mPositions.size() - pMesh.mNormals.size() - 1, aiVector3D(0, 1, 0));
2015-05-19 03:57:13 +00:00
// ignore all normal streams except 0 - there can be only one normal
if (pInput.mIndex == 0) {
pMesh.mNormals.push_back(aiVector3D(obj[0], obj[1], obj[2]));
} else {
ASSIMP_LOG_ERROR("Collada: just one vertex normal stream supported");
}
break;
case IT_Tangent:
2015-05-19 03:57:13 +00:00
// pad to current vertex count if necessary
if (pMesh.mTangents.size() < pMesh.mPositions.size() - 1)
pMesh.mTangents.insert(pMesh.mTangents.end(), pMesh.mPositions.size() - pMesh.mTangents.size() - 1, aiVector3D(1, 0, 0));
2015-05-19 03:57:13 +00:00
// ignore all tangent streams except 0 - there can be only one tangent
if (pInput.mIndex == 0) {
pMesh.mTangents.push_back(aiVector3D(obj[0], obj[1], obj[2]));
} else {
ASSIMP_LOG_ERROR("Collada: just one vertex tangent stream supported");
}
break;
case IT_Bitangent:
// pad to current vertex count if necessary
if (pMesh.mBitangents.size() < pMesh.mPositions.size() - 1) {
pMesh.mBitangents.insert(pMesh.mBitangents.end(), pMesh.mPositions.size() - pMesh.mBitangents.size() - 1, aiVector3D(0, 0, 1));
2015-05-19 03:57:13 +00:00
}
// ignore all bitangent streams except 0 - there can be only one bitangent
if (pInput.mIndex == 0) {
pMesh.mBitangents.push_back(aiVector3D(obj[0], obj[1], obj[2]));
} else {
ASSIMP_LOG_ERROR("Collada: just one vertex bitangent stream supported");
}
break;
case IT_Texcoord:
// up to 4 texture coord sets are fine, ignore the others
if (pInput.mIndex < AI_MAX_NUMBER_OF_TEXTURECOORDS) {
// pad to current vertex count if necessary
if (pMesh.mTexCoords[pInput.mIndex].size() < pMesh.mPositions.size() - 1)
pMesh.mTexCoords[pInput.mIndex].insert(pMesh.mTexCoords[pInput.mIndex].end(),
pMesh.mPositions.size() - pMesh.mTexCoords[pInput.mIndex].size() - 1, aiVector3D(0, 0, 0));
pMesh.mTexCoords[pInput.mIndex].push_back(aiVector3D(obj[0], obj[1], obj[2]));
if (0 != acc.mSubOffset[2] || 0 != acc.mSubOffset[3]) {
pMesh.mNumUVComponents[pInput.mIndex] = 3;
}
} else {
ASSIMP_LOG_ERROR("Collada: too many texture coordinate sets. Skipping.");
}
break;
case IT_Color:
// up to 4 color sets are fine, ignore the others
if (pInput.mIndex < AI_MAX_NUMBER_OF_COLOR_SETS) {
// pad to current vertex count if necessary
if (pMesh.mColors[pInput.mIndex].size() < pMesh.mPositions.size() - 1)
pMesh.mColors[pInput.mIndex].insert(pMesh.mColors[pInput.mIndex].end(),
pMesh.mPositions.size() - pMesh.mColors[pInput.mIndex].size() - 1, aiColor4D(0, 0, 0, 1));
aiColor4D result(0, 0, 0, 1);
for (size_t i = 0; i < pInput.mResolved->mSize; ++i) {
result[static_cast<unsigned int>(i)] = obj[pInput.mResolved->mSubOffset[i]];
}
pMesh.mColors[pInput.mIndex].push_back(result);
} else {
ASSIMP_LOG_ERROR("Collada: too many vertex color sets. Skipping.");
}
break;
default:
// IT_Invalid and IT_Vertex
ai_assert(false && "shouldn't ever get here");
2015-05-19 03:57:13 +00:00
}
}
// ------------------------------------------------------------------------------------------------
// Reads the library of node hierarchies and scene parts
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadSceneLibrary(XmlNode &node) {
if (node.empty()) {
2015-05-19 03:57:13 +00:00
return;
2020-06-27 13:57:06 +00:00
}
2015-05-19 03:57:13 +00:00
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
2020-08-26 20:31:46 +00:00
const std::string &currentName = currentNode.name();
if (currentName == "visual_scene") {
// read ID. Is optional according to the spec, but how on earth should a scene_instance refer to it then?
std::string id;
XmlParser::getStdStrAttribute(currentNode, "id", id);
2020-08-28 14:17:56 +00:00
// read name if given.
2020-08-26 20:31:46 +00:00
std::string attrName = "Scene";
if (XmlParser::hasAttribute(currentNode, "name")) {
XmlParser::getStdStrAttribute(currentNode, "name", attrName);
2015-05-19 03:57:13 +00:00
}
2020-08-26 20:31:46 +00:00
// create a node and store it in the library under its ID
Node *sceneNode = new Node;
sceneNode->mID = id;
sceneNode->mName = attrName;
mNodeLibrary[sceneNode->mID] = sceneNode;
2020-09-11 13:46:46 +00:00
ReadSceneNode(currentNode, sceneNode);
2015-05-19 03:57:13 +00:00
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads a scene node's contents including children and stores it in the given node
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadSceneNode(XmlNode &node, Node *pNode) {
2015-05-19 03:57:13 +00:00
// quit immediately on <bla/> elements
2020-06-27 13:57:06 +00:00
if (node.empty()) {
2015-05-19 03:57:13 +00:00
return;
2020-06-27 13:57:06 +00:00
}
2020-09-14 19:35:36 +00:00
for (XmlNode &currentNode : node.children()) {
2020-09-22 22:20:06 +00:00
const std::string &currentName = currentNode.name();
2020-08-26 20:31:46 +00:00
if (currentName == "node") {
Node *child = new Node;
if (XmlParser::hasAttribute(currentNode, "id")) {
XmlParser::getStdStrAttribute(currentNode, "id", child->mID);
}
if (XmlParser::hasAttribute(currentNode, "sid")) {
XmlParser::getStdStrAttribute(currentNode, "id", child->mSID);
}
if (XmlParser::hasAttribute(currentNode, "name")) {
XmlParser::getStdStrAttribute(currentNode, "name", child->mName);
}
if (pNode) {
pNode->mChildren.push_back(child);
child->mParent = pNode;
} else {
// no parent node given, probably called from <library_nodes> element.
// create new node in node library
mNodeLibrary[child->mID] = child;
}
2015-05-19 03:57:13 +00:00
2020-08-26 20:31:46 +00:00
// read on recursively from there
ReadSceneNode(currentNode, child);
continue;
} else if (!pNode) {
// For any further stuff we need a valid node to work on
continue;
}
if (currentName == "lookat") {
ReadNodeTransformation(currentNode, pNode, TF_LOOKAT);
} else if (currentName == "matrix") {
ReadNodeTransformation(currentNode, pNode, TF_MATRIX);
} else if (currentName == "rotate") {
ReadNodeTransformation(currentNode, pNode, TF_ROTATE);
} else if (currentName == "scale") {
ReadNodeTransformation(currentNode, pNode, TF_SCALE);
} else if (currentName == "skew") {
ReadNodeTransformation(currentNode, pNode, TF_SKEW);
} else if (currentName == "translate") {
ReadNodeTransformation(currentNode, pNode, TF_TRANSLATE);
} else if (currentName == "render" && pNode->mParent == nullptr && 0 == pNode->mPrimaryCamera.length()) {
// ... scene evaluation or, in other words, postprocessing pipeline,
// or, again in other words, a turing-complete description how to
// render a Collada scene. The only thing that is interesting for
// us is the primary camera.
if (XmlParser::hasAttribute(currentNode, "camera_node")) {
std::string s;
XmlParser::getStdStrAttribute(currentNode, "camera_node", s);
if (s[0] != '#') {
ASSIMP_LOG_ERROR("Collada: Unresolved reference format of camera");
} else {
2020-08-26 20:31:46 +00:00
pNode->mPrimaryCamera = s.c_str() + 1;
2015-05-19 03:57:13 +00:00
}
}
2020-08-26 20:31:46 +00:00
} else if (currentName == "instance_node") {
// find the node in the library
if (XmlParser::hasAttribute(currentNode, "url")) {
std::string s;
XmlParser::getStdStrAttribute(currentNode, "url", s);
if (s[0] != '#') {
ASSIMP_LOG_ERROR("Collada: Unresolved reference format of node");
} else {
pNode->mNodeInstances.push_back(NodeInstance());
pNode->mNodeInstances.back().mNode = s.c_str() + 1;
2015-05-19 03:57:13 +00:00
}
2020-08-26 20:31:46 +00:00
}
} else if (currentName == "instance_geometry" || currentName == "instance_controller") {
// Reference to a mesh or controller, with possible material associations
ReadNodeGeometry(currentNode, pNode);
} else if (currentName == "instance_light") {
// Reference to a light, name given in 'url' attribute
if (XmlParser::hasAttribute(currentNode, "url")) {
std::string url;
XmlParser::getStdStrAttribute(currentNode, "url", url);
2020-09-25 19:00:09 +00:00
if (url[0] != '#') {
throw DeadlyImportError("Unknown reference format in <instance_light> element");
2015-05-19 03:57:13 +00:00
}
2020-08-26 20:31:46 +00:00
pNode->mLights.push_back(LightInstance());
pNode->mLights.back().mLight = url.c_str() + 1;
}
} else if (currentName == "instance_camera") {
// Reference to a camera, name given in 'url' attribute
if (XmlParser::hasAttribute(currentNode, "url")) {
std::string url;
XmlParser::getStdStrAttribute(currentNode, "url", url);
if (url[0] != '#') {
2020-09-25 19:00:09 +00:00
throw DeadlyImportError("Unknown reference format in <instance_camera> element");
2015-05-19 03:57:13 +00:00
}
2020-08-26 20:31:46 +00:00
pNode->mCameras.push_back(CameraInstance());
pNode->mCameras.back().mCamera = url.c_str() + 1;
2015-05-19 03:57:13 +00:00
}
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads a node transformation entry of the given type and adds it to the given node's transformation list.
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadNodeTransformation(XmlNode &node, Node *pNode, TransformType pType) {
if (node.empty()) {
2015-05-19 03:57:13 +00:00
return;
2020-06-27 13:57:06 +00:00
}
2020-08-26 20:31:46 +00:00
std::string tagName = node.name();
2015-05-19 03:57:13 +00:00
Transform tf;
tf.mType = pType;
2015-05-19 03:52:10 +00:00
2015-05-19 03:57:13 +00:00
// read SID
2020-08-26 20:31:46 +00:00
if (XmlParser::hasAttribute(node, "sid")) {
XmlParser::getStdStrAttribute(node, "sid", tf.mID);
}
2015-05-19 03:57:13 +00:00
// how many parameters to read per transformation type
static const unsigned int sNumParameters[] = { 9, 4, 3, 3, 7, 16 };
2020-09-04 05:33:10 +00:00
std::string value;
XmlParser::getValueAsString(node, value);
const char *content = value.c_str();
2015-05-19 03:57:13 +00:00
// read as many parameters and store in the transformation
for (unsigned int a = 0; a < sNumParameters[pType]; a++) {
2015-05-19 03:57:13 +00:00
// read a number
content = fast_atoreal_move<ai_real>(content, tf.f[a]);
2015-05-19 03:57:13 +00:00
// skip whitespace after it
SkipSpacesAndLineEnd(&content);
2015-05-19 03:57:13 +00:00
}
2015-05-19 03:57:13 +00:00
// place the transformation at the queue of the node
pNode->mTransforms.push_back(tf);
}
// ------------------------------------------------------------------------------------------------
// Processes bind_vertex_input and bind elements
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadMaterialVertexInputBinding(XmlNode &node, Collada::SemanticMappingTable &tbl) {
2020-09-27 18:20:44 +00:00
std::string name = node.name();
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
2020-08-26 20:31:46 +00:00
const std::string &currentName = currentNode.name();
if (currentName == "bind_vertex_input") {
Collada::InputSemanticMapEntry vn;
// effect semantic
if (XmlParser::hasAttribute(currentNode, "semantic")) {
std::string s;
XmlParser::getStdStrAttribute(currentNode, "semantic", s);
XmlParser::getUIntAttribute(currentNode, "input_semantic", (unsigned int &)vn.mType);
2015-05-19 03:57:13 +00:00
}
2020-08-26 20:31:46 +00:00
std::string s;
XmlParser::getStdStrAttribute(currentNode, "semantic", s);
// input semantic
XmlParser::getUIntAttribute(currentNode, "input_semantic", (unsigned int &)vn.mType);
// index of input set
if (XmlParser::hasAttribute(currentNode, "input_set")) {
XmlParser::getUIntAttribute(currentNode, "input_set", vn.mSet);
}
tbl.mMap[s] = vn;
} else if (currentName == "bind") {
ASSIMP_LOG_WARN("Collada: Found unsupported <bind> element");
2015-05-19 03:57:13 +00:00
}
}
}
void ColladaParser::ReadEmbeddedTextures(ZipArchiveIOSystem &zip_archive) {
// Attempt to load any undefined Collada::Image in ImageLibrary
for (ImageLibrary::iterator it = mImageLibrary.begin(); it != mImageLibrary.end(); ++it) {
Collada::Image &image = (*it).second;
if (image.mImageData.empty()) {
std::unique_ptr<IOStream> image_file(zip_archive.Open(image.mFileName.c_str()));
if (image_file) {
image.mImageData.resize(image_file->FileSize());
image_file->Read(image.mImageData.data(), image_file->FileSize(), 1);
image.mEmbeddedFormat = BaseImporter::GetExtension(image.mFileName);
if (image.mEmbeddedFormat == "jpeg") {
image.mEmbeddedFormat = "jpg";
}
}
}
}
}
// ------------------------------------------------------------------------------------------------
// Reads a mesh reference in a node and adds it to the node's mesh list
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadNodeGeometry(XmlNode &node, Node *pNode) {
2015-05-19 03:57:13 +00:00
// referred mesh is given as an attribute of the <instance_geometry> element
2020-08-26 20:31:46 +00:00
std::string url;
XmlParser::getStdStrAttribute(node, "url", url);
2020-09-25 19:00:09 +00:00
if (url[0] != '#') {
throw DeadlyImportError("Unknown reference format");
}
2015-05-19 03:57:13 +00:00
Collada::MeshInstance instance;
2020-08-26 20:31:46 +00:00
instance.mMeshOrController = url.c_str() + 1; // skipping the leading #
2020-08-28 14:17:56 +00:00
for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
2020-08-26 20:31:46 +00:00
const std::string &currentName = currentNode.name();
2020-09-27 18:20:44 +00:00
if (currentName == "bind_material") {
XmlNode techNode = currentNode.child("technique_common");
if (techNode) {
XmlNode instanceMatNode = techNode.child("instance_material");
// read ID of the geometry subgroup and the target material
std::string group;
XmlParser::getStdStrAttribute(instanceMatNode, "symbol", group);
XmlParser::getStdStrAttribute(instanceMatNode, "target", url);
const char *urlMat = url.c_str();
Collada::SemanticMappingTable s;
if (urlMat[0] == '#')
urlMat++;
s.mMatName = urlMat;
// store the association
instance.mMaterials[group] = s;
ReadMaterialVertexInputBinding(instanceMatNode, s);
}
2015-05-19 03:57:13 +00:00
}
}
// store it
pNode->mMeshes.push_back(instance);
}
// ------------------------------------------------------------------------------------------------
// Reads the collada scene
2020-06-27 13:57:06 +00:00
void ColladaParser::ReadScene(XmlNode &node) {
if (node.empty()) {
2015-05-19 03:57:13 +00:00
return;
2020-06-27 13:57:06 +00:00
}
2015-05-19 03:57:13 +00:00
2020-10-19 21:53:27 +00:00
for (XmlNode &currentNode : node.children()) {
const std::string &currentName = currentNode.name();
2020-07-10 20:25:38 +00:00
if (currentName == "instance_visual_scene") {
// should be the first and only occurrence
2020-09-25 19:00:09 +00:00
if (mRootNode) {
throw DeadlyImportError("Invalid scene containing multiple root nodes in <instance_visual_scene> element");
2015-05-19 03:57:13 +00:00
}
2020-07-10 20:25:38 +00:00
// read the url of the scene to instance. Should be of format "#some_name"
2020-08-26 20:31:46 +00:00
std::string url;
XmlParser::getStdStrAttribute(currentNode, "url", url);
2020-07-10 20:25:38 +00:00
if (url[0] != '#') {
2020-09-25 19:00:09 +00:00
throw DeadlyImportError("Unknown reference format in <instance_visual_scene> element");
2020-07-10 20:25:38 +00:00
}
// find the referred scene, skip the leading #
2020-08-26 20:31:46 +00:00
NodeLibrary::const_iterator sit = mNodeLibrary.find(url.c_str() + 1);
2020-07-10 20:25:38 +00:00
if (sit == mNodeLibrary.end()) {
2020-09-25 19:00:09 +00:00
throw DeadlyImportError("Unable to resolve visual_scene reference \"", std::string(url), "\" in <instance_visual_scene> element.");
2020-07-10 20:25:38 +00:00
}
mRootNode = sit->second;
}
}
}
// ------------------------------------------------------------------------------------------------
2020-06-27 13:57:06 +00:00
// Calculates the resulting transformation from all the given transform steps
aiMatrix4x4 ColladaParser::CalculateResultTransform(const std::vector<Transform> &pTransforms) const {
2015-05-19 03:57:13 +00:00
aiMatrix4x4 res;
for (std::vector<Transform>::const_iterator it = pTransforms.begin(); it != pTransforms.end(); ++it) {
const Transform &tf = *it;
switch (tf.mType) {
case TF_LOOKAT: {
aiVector3D pos(tf.f[0], tf.f[1], tf.f[2]);
aiVector3D dstPos(tf.f[3], tf.f[4], tf.f[5]);
aiVector3D up = aiVector3D(tf.f[6], tf.f[7], tf.f[8]).Normalize();
aiVector3D dir = aiVector3D(dstPos - pos).Normalize();
aiVector3D right = (dir ^ up).Normalize();
res *= aiMatrix4x4(
right.x, up.x, -dir.x, pos.x,
right.y, up.y, -dir.y, pos.y,
right.z, up.z, -dir.z, pos.z,
0, 0, 0, 1);
break;
}
case TF_ROTATE: {
aiMatrix4x4 rot;
ai_real angle = tf.f[3] * ai_real(AI_MATH_PI) / ai_real(180.0);
aiVector3D axis(tf.f[0], tf.f[1], tf.f[2]);
aiMatrix4x4::Rotation(angle, axis, rot);
res *= rot;
break;
}
case TF_TRANSLATE: {
aiMatrix4x4 trans;
aiMatrix4x4::Translation(aiVector3D(tf.f[0], tf.f[1], tf.f[2]), trans);
res *= trans;
break;
}
case TF_SCALE: {
aiMatrix4x4 scale(tf.f[0], 0.0f, 0.0f, 0.0f, 0.0f, tf.f[1], 0.0f, 0.0f, 0.0f, 0.0f, tf.f[2], 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
res *= scale;
break;
}
case TF_SKEW:
// TODO: (thom)
ai_assert(false);
break;
case TF_MATRIX: {
aiMatrix4x4 mat(tf.f[0], tf.f[1], tf.f[2], tf.f[3], tf.f[4], tf.f[5], tf.f[6], tf.f[7],
tf.f[8], tf.f[9], tf.f[10], tf.f[11], tf.f[12], tf.f[13], tf.f[14], tf.f[15]);
res *= mat;
break;
}
default:
ai_assert(false);
break;
2015-05-19 03:57:13 +00:00
}
}
return res;
}
// ------------------------------------------------------------------------------------------------
// Determines the input data type for the given semantic string
Collada::InputType ColladaParser::GetTypeForSemantic(const std::string &semantic) {
if (semantic.empty()) {
ASSIMP_LOG_WARN("Vertex input type is empty.");
return IT_Invalid;
}
if (semantic == "POSITION")
2015-05-19 03:57:13 +00:00
return IT_Position;
else if (semantic == "TEXCOORD")
2015-05-19 03:57:13 +00:00
return IT_Texcoord;
else if (semantic == "NORMAL")
2015-05-19 03:57:13 +00:00
return IT_Normal;
else if (semantic == "COLOR")
2015-05-19 03:57:13 +00:00
return IT_Color;
else if (semantic == "VERTEX")
2015-05-19 03:57:13 +00:00
return IT_Vertex;
else if (semantic == "BINORMAL" || semantic == "TEXBINORMAL")
2015-05-19 03:57:13 +00:00
return IT_Bitangent;
else if (semantic == "TANGENT" || semantic == "TEXTANGENT")
2015-05-19 03:57:13 +00:00
return IT_Tangent;
ASSIMP_LOG_WARN_F("Unknown vertex input type \"", semantic, "\". Ignoring.");
2015-05-19 03:57:13 +00:00
return IT_Invalid;
}
#endif // !! ASSIMP_BUILD_NO_DAE_IMPORTER