2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2020-01-20 13:53:12 +00:00
|
|
|
Copyright (c) 2006-2020, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2017-05-09 17:57:36 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the following
|
2015-05-19 03:48:29 +00:00
|
|
|
conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-05-19 03:48:29 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file ColladaParser.cpp
|
|
|
|
* @brief Implementation of the Collada parser helper
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_COLLADA_IMPORTER
|
|
|
|
|
|
|
|
#include "ColladaParser.h"
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/ParsingUtils.h>
|
|
|
|
#include <assimp/StringUtils.h>
|
|
|
|
#include <assimp/TinyFormatter.h>
|
2019-07-12 10:29:35 +00:00
|
|
|
#include <assimp/ZipArchiveIOSystem.h>
|
2020-04-28 16:46:07 +00:00
|
|
|
#include <assimp/commonMetaData.h>
|
|
|
|
#include <assimp/fast_atof.h>
|
|
|
|
#include <assimp/light.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <assimp/DefaultLogger.hpp>
|
|
|
|
#include <assimp/IOSystem.hpp>
|
|
|
|
#include <sstream>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2016-05-25 22:44:29 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
using namespace Assimp;
|
|
|
|
using namespace Assimp::Collada;
|
2016-04-05 22:03:05 +00:00
|
|
|
using namespace Assimp::Formatter;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
2020-04-28 16:46:07 +00:00
|
|
|
ColladaParser::ColladaParser(IOSystem *pIOHandler, const std::string &pFile) :
|
|
|
|
mFileName(pFile),
|
|
|
|
mReader(nullptr),
|
|
|
|
mDataLibrary(),
|
|
|
|
mAccessorLibrary(),
|
|
|
|
mMeshLibrary(),
|
|
|
|
mNodeLibrary(),
|
|
|
|
mImageLibrary(),
|
|
|
|
mEffectLibrary(),
|
|
|
|
mMaterialLibrary(),
|
|
|
|
mLightLibrary(),
|
|
|
|
mCameraLibrary(),
|
|
|
|
mControllerLibrary(),
|
|
|
|
mRootNode(nullptr),
|
|
|
|
mAnims(),
|
|
|
|
mUnitSize(1.0f),
|
|
|
|
mUpDirection(UP_Y),
|
|
|
|
mFormat(FV_1_5_n) // We assume the newest file format by default
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
2015-12-07 19:43:01 +00:00
|
|
|
// validate io-handler instance
|
2019-07-12 10:29:35 +00:00
|
|
|
if (nullptr == pIOHandler) {
|
|
|
|
throw DeadlyImportError("IOSystem is NULL.");
|
2015-12-06 11:18:33 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
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()) {
|
|
|
|
ThrowException(std::string("Invalid ZAE"));
|
|
|
|
}
|
|
|
|
|
|
|
|
daefile.reset(zip_archive->Open(dae_filename.c_str()));
|
|
|
|
if (daefile == nullptr) {
|
|
|
|
ThrowException(std::string("Invalid ZAE manifest: '") + std::string(dae_filename) + std::string("' is missing"));
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2019-07-12 10:29:35 +00:00
|
|
|
// attempt to open the file directly
|
|
|
|
daefile.reset(pIOHandler->Open(pFile));
|
|
|
|
if (daefile.get() == nullptr) {
|
|
|
|
throw DeadlyImportError("Failed to open file '" + pFile + "'.");
|
|
|
|
}
|
2015-12-06 11:18:33 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// generate a XML reader for it
|
2019-07-12 10:29:35 +00:00
|
|
|
std::unique_ptr<CIrrXML_IOStreamReader> mIOWrapper(new CIrrXML_IOStreamReader(daefile.get()));
|
|
|
|
mReader = irr::io::createIrrXMLReader(mIOWrapper.get());
|
2015-12-06 11:18:33 +00:00
|
|
|
if (!mReader) {
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException("Unable to read file, malformed XML");
|
2015-12-06 11:18:33 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// start reading
|
|
|
|
ReadContents();
|
2019-07-12 10:29:35 +00:00
|
|
|
|
|
|
|
// read embedded textures
|
|
|
|
if (zip_archive && zip_archive->isOpen()) {
|
2019-07-12 13:46:48 +00:00
|
|
|
ReadEmbeddedTextures(*zip_archive);
|
2019-07-12 10:29:35 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor, private as well
|
2020-04-28 16:46:07 +00:00
|
|
|
ColladaParser::~ColladaParser() {
|
2015-05-19 03:57:13 +00:00
|
|
|
delete mReader;
|
2019-07-12 10:29:35 +00:00
|
|
|
for (NodeLibrary::iterator it = mNodeLibrary.begin(); it != mNodeLibrary.end(); ++it)
|
2015-05-19 03:57:13 +00:00
|
|
|
delete it->second;
|
2019-07-12 10:29:35 +00:00
|
|
|
for (MeshLibrary::iterator it = mMeshLibrary.begin(); it != mMeshLibrary.end(); ++it)
|
2015-05-19 03:57:13 +00:00
|
|
|
delete it->second;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 10:29:35 +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"));
|
2020-04-28 16:46:07 +00:00
|
|
|
if (manifestfile == nullptr) {
|
2019-07-12 10:29:35 +00:00
|
|
|
// No manifest, hope there is only one .DAE inside
|
|
|
|
std::vector<std::string> file_list;
|
|
|
|
zip_archive.getFileListExtension(file_list, "dae");
|
|
|
|
|
|
|
|
if (file_list.empty())
|
|
|
|
return std::string();
|
|
|
|
|
|
|
|
return file_list.front();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<CIrrXML_IOStreamReader> mIOWrapper(new CIrrXML_IOStreamReader(manifestfile.get()));
|
2019-07-25 08:54:39 +00:00
|
|
|
std::unique_ptr<irr::io::IrrXMLReader> manifest_reader(irr::io::createIrrXMLReader(mIOWrapper.get()));
|
2019-07-12 10:29:35 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (manifest_reader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
// find the manifest "dae_root" element
|
2020-04-28 16:46:07 +00:00
|
|
|
if (manifest_reader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (::strcmp(manifest_reader->getNodeName(), "dae_root") == 0) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (!manifest_reader->read())
|
|
|
|
return std::string();
|
|
|
|
if (manifest_reader->getNodeType() != irr::io::EXN_TEXT && manifest_reader->getNodeType() != irr::io::EXN_CDATA)
|
|
|
|
return std::string();
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *filepath = manifest_reader->getNodeData();
|
2019-07-12 10:29:35 +00:00
|
|
|
if (filepath == nullptr)
|
|
|
|
return std::string();
|
|
|
|
|
2019-10-11 17:57:38 +00:00
|
|
|
aiString ai_str(filepath);
|
2019-10-14 10:27:34 +00:00
|
|
|
UriDecodePath(ai_str);
|
2019-10-11 17:57:38 +00:00
|
|
|
|
|
|
|
return std::string(ai_str.C_Str());
|
2019-07-12 10:29:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
2019-10-11 17:57:38 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Convert a path read from a collada file to the usual representation
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::UriDecodePath(aiString &ss) {
|
2019-10-11 17:57:38 +00:00
|
|
|
// 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'
|
2020-04-28 16:46:07 +00:00
|
|
|
if (0 == strncmp(ss.data, "file://", 7)) {
|
2019-10-11 17:57:38 +00:00
|
|
|
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"
|
2020-04-28 16:46:07 +00:00
|
|
|
#if defined(_MSC_VER)
|
2019-10-11 17:57:38 +00:00
|
|
|
if (ss.data[0] == '/' && isalpha((unsigned char)ss.data[1]) && ss.data[2] == ':') {
|
|
|
|
#else
|
|
|
|
if (ss.data[0] == '/' && isalpha(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
|
2020-04-28 16:46:07 +00:00
|
|
|
char *out = ss.data;
|
|
|
|
for (const char *it = ss.data; it != ss.data + ss.length; /**/) {
|
|
|
|
if (*it == '%' && (it + 3) < ss.data + ss.length) {
|
2019-10-11 17:57:38 +00:00
|
|
|
// 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);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2019-10-11 17:57:38 +00:00
|
|
|
*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);
|
2019-10-11 17:57:38 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Read bool from text contents of current element
|
2020-04-28 16:46:07 +00:00
|
|
|
bool ColladaParser::ReadBoolFromTextContent() {
|
|
|
|
const char *cur = GetTextContent();
|
|
|
|
if (nullptr == cur) {
|
2019-12-13 10:20:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-07-12 10:29:35 +00:00
|
|
|
return (!ASSIMP_strincmp(cur, "true", 4) || '0' != *cur);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Read float from text contents of current element
|
2020-04-28 16:46:07 +00:00
|
|
|
ai_real ColladaParser::ReadFloatFromTextContent() {
|
|
|
|
const char *cur = GetTextContent();
|
|
|
|
if (nullptr == cur) {
|
2019-12-13 10:20:50 +00:00
|
|
|
return 0.0;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
return fast_atof(cur);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the contents of the file
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadContents() {
|
|
|
|
while (mReader->read()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// handle the root element "COLLADA"
|
2020-04-28 16:46:07 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("COLLADA")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// check for 'version' attribute
|
|
|
|
const int attrib = TestAttribute("version");
|
|
|
|
if (attrib != -1) {
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *version = mReader->getAttributeValue(attrib);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-12-09 14:05:41 +00:00
|
|
|
// Store declared format version string
|
2019-12-13 10:20:50 +00:00
|
|
|
aiString v;
|
|
|
|
v.Set(version);
|
2020-04-28 16:46:07 +00:00
|
|
|
mAssetMetaData.emplace(AI_METADATA_SOURCE_FORMAT_VERSION, v);
|
2019-12-09 14:05:41 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
if (!::strncmp(version, "1.5", 3)) {
|
|
|
|
mFormat = FV_1_5_n;
|
2018-04-19 14:48:43 +00:00
|
|
|
ASSIMP_LOG_DEBUG("Collada schema version is 1.5.n");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (!::strncmp(version, "1.4", 3)) {
|
2019-07-12 10:29:35 +00:00
|
|
|
mFormat = FV_1_4_n;
|
2018-04-19 14:48:43 +00:00
|
|
|
ASSIMP_LOG_DEBUG("Collada schema version is 1.4.n");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (!::strncmp(version, "1.3", 3)) {
|
2019-07-12 10:29:35 +00:00
|
|
|
mFormat = FV_1_3_n;
|
2018-04-19 14:48:43 +00:00
|
|
|
ASSIMP_LOG_DEBUG("Collada schema version is 1.3.n");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadStructure();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2020-05-15 15:59:42 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG_F("Ignoring global element <", mReader->getNodeName(), ">.");
|
2015-05-19 03:57:13 +00:00
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// skip everything else silently
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the structure of the file
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadStructure() {
|
|
|
|
while (mReader->read()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// beginning of elements
|
2020-04-28 16:46:07 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (IsElement("asset"))
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadAssetInfo();
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("library_animations"))
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadAnimationLibrary();
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("library_animation_clips"))
|
|
|
|
ReadAnimationClipLibrary();
|
|
|
|
else if (IsElement("library_controllers"))
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadControllerLibrary();
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("library_images"))
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadImageLibrary();
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("library_materials"))
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadMaterialLibrary();
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("library_effects"))
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadEffectLibrary();
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("library_geometries"))
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadGeometryLibrary();
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("library_visual_scenes"))
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadSceneLibrary();
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("library_lights"))
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadLightLibrary();
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("library_cameras"))
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadCameraLibrary();
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("library_nodes"))
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadSceneNode(NULL); /* some hacking to reuse this piece of code */
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("scene"))
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadScene();
|
|
|
|
else
|
|
|
|
SkipElement();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-02-10 22:57:29 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
PostProcessRootAnimations();
|
2018-03-16 04:41:40 +00:00
|
|
|
PostProcessControllers();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2017-11-09 22:19:26 +00:00
|
|
|
// Reads asset information such as coordinate system information and legal blah
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadAssetInfo() {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("unit")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read unit data from the element's attributes
|
2019-07-12 10:29:35 +00:00
|
|
|
const int attrIndex = TestAttribute("meter");
|
2015-05-19 03:57:13 +00:00
|
|
|
if (attrIndex == -1) {
|
|
|
|
mUnitSize = 1.f;
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2019-07-12 10:29:35 +00:00
|
|
|
mUnitSize = mReader->getAttributeValueAsFloat(attrIndex);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// consume the trailing stuff
|
2019-07-12 10:29:35 +00:00
|
|
|
if (!mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
SkipElement();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("up_axis")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read content, strip whitespace, compare
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *content = GetTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strncmp(content, "X_UP", 4) == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
mUpDirection = UP_X;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (strncmp(content, "Z_UP", 4) == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
mUpDirection = UP_Z;
|
|
|
|
else
|
|
|
|
mUpDirection = UP_Y;
|
|
|
|
|
|
|
|
// check element end
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("up_axis");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("contributor")) {
|
2019-04-09 15:28:15 +00:00
|
|
|
ReadContributorInfo();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2020-01-02 12:35:00 +00:00
|
|
|
ReadMetaDataItem(mAssetMetaData);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "asset") != 0)
|
|
|
|
ThrowException("Expected end of <asset> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 15:28:15 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the contributor info
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadContributorInfo() {
|
2019-04-09 15:28:15 +00:00
|
|
|
if (mReader->isEmptyElement())
|
|
|
|
return;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2020-01-02 12:35:00 +00:00
|
|
|
ReadMetaDataItem(mAssetMetaData);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-04-09 15:28:15 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "contributor") != 0)
|
|
|
|
ThrowException("Expected end of <contributor> element.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 12:35:00 +00:00
|
|
|
static bool FindCommonKey(const std::string &collada_key, const MetaKeyPairVector &key_renaming, size_t &found_index) {
|
2019-12-09 14:05:41 +00:00
|
|
|
for (size_t i = 0; i < key_renaming.size(); ++i) {
|
2020-04-28 16:46:07 +00:00
|
|
|
if (key_renaming[i].first == collada_key) {
|
2019-12-09 14:05:41 +00:00
|
|
|
found_index = i;
|
|
|
|
return true;
|
2020-04-28 16:46:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-02 12:35:00 +00:00
|
|
|
found_index = std::numeric_limits<size_t>::max();
|
2019-12-09 14:05:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-09 15:28:15 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads a single string metadata item
|
2020-01-02 12:35:00 +00:00
|
|
|
void ColladaParser::ReadMetaDataItem(StringMetaData &metadata) {
|
|
|
|
const Collada::MetaKeyPairVector &key_renaming = GetColladaAssimpMetaKeysCamelCase();
|
2020-04-28 16:46:07 +00:00
|
|
|
// Metadata such as created, keywords, subject etc
|
|
|
|
const char *key_char = mReader->getNodeName();
|
|
|
|
if (key_char != nullptr) {
|
2019-04-09 15:28:15 +00:00
|
|
|
const std::string key_str(key_char);
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *value_char = TestTextContent();
|
|
|
|
if (value_char != nullptr) {
|
2019-04-09 15:28:15 +00:00
|
|
|
aiString aistr;
|
2020-04-28 16:46:07 +00:00
|
|
|
aistr.Set(value_char);
|
2019-12-09 14:05:41 +00:00
|
|
|
|
2020-01-02 12:35:00 +00:00
|
|
|
std::string camel_key_str(key_str);
|
2020-04-28 16:46:07 +00:00
|
|
|
ToCamelCase(camel_key_str);
|
2020-01-02 12:35:00 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
size_t found_index;
|
|
|
|
if (FindCommonKey(camel_key_str, key_renaming, found_index)) {
|
2019-12-09 14:05:41 +00:00
|
|
|
metadata.emplace(key_renaming[found_index].second, aistr);
|
|
|
|
} else {
|
2020-04-28 16:46:07 +00:00
|
|
|
metadata.emplace(camel_key_str, aistr);
|
|
|
|
}
|
2019-04-09 15:28:15 +00:00
|
|
|
}
|
2019-06-20 15:30:52 +00:00
|
|
|
TestClosing(key_str.c_str());
|
2020-04-28 16:46:07 +00:00
|
|
|
} else
|
2019-04-09 15:28:15 +00:00
|
|
|
SkipElement();
|
|
|
|
}
|
|
|
|
|
2016-02-07 22:36:49 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the animation clips
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadAnimationClipLibrary() {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->isEmptyElement())
|
|
|
|
return;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("animation_clip")) {
|
2019-07-12 10:29:35 +00:00
|
|
|
// optional name given as an attribute
|
|
|
|
std::string animName;
|
|
|
|
int indexName = TestAttribute("name");
|
|
|
|
int indexID = TestAttribute("id");
|
|
|
|
if (indexName >= 0)
|
|
|
|
animName = mReader->getAttributeValue(indexName);
|
|
|
|
else if (indexID >= 0)
|
|
|
|
animName = mReader->getAttributeValue(indexID);
|
|
|
|
else
|
|
|
|
animName = std::string("animation_") + to_string(mAnimationClipLibrary.size());
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
std::pair<std::string, std::vector<std::string>> clip;
|
2019-07-12 10:29:35 +00:00
|
|
|
|
|
|
|
clip.first = animName;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("instance_animation")) {
|
2019-07-12 10:29:35 +00:00
|
|
|
int indexUrl = TestAttribute("url");
|
2020-04-28 16:46:07 +00:00
|
|
|
if (indexUrl >= 0) {
|
|
|
|
const char *url = mReader->getAttributeValue(indexUrl);
|
2019-07-12 10:29:35 +00:00
|
|
|
if (url[0] != '#')
|
|
|
|
ThrowException("Unknown reference format");
|
|
|
|
|
|
|
|
url++;
|
|
|
|
|
|
|
|
clip.second.push_back(url);
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2019-07-12 10:29:35 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "animation_clip") != 0)
|
|
|
|
ThrowException("Expected end of <animation_clip> element.");
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
if (clip.second.size() > 0) {
|
2019-07-12 10:29:35 +00:00
|
|
|
mAnimationClipLibrary.push_back(clip);
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2019-07-12 10:29:35 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "library_animation_clips") != 0)
|
|
|
|
ThrowException("Expected end of <library_animation_clips> element.");
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-02-07 22:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::PostProcessControllers() {
|
2018-06-17 18:37:09 +00:00
|
|
|
std::string meshId;
|
|
|
|
for (ControllerLibrary::iterator it = mControllerLibrary.begin(); it != mControllerLibrary.end(); ++it) {
|
|
|
|
meshId = it->second.mMeshId;
|
|
|
|
ControllerLibrary::iterator findItr = mControllerLibrary.find(meshId);
|
2019-07-12 10:29:35 +00:00
|
|
|
while (findItr != mControllerLibrary.end()) {
|
2018-06-17 18:37:09 +00:00
|
|
|
meshId = findItr->second.mMeshId;
|
|
|
|
findItr = mControllerLibrary.find(meshId);
|
|
|
|
}
|
2019-07-12 10:29:35 +00:00
|
|
|
|
2018-06-17 18:37:09 +00:00
|
|
|
it->second.mMeshId = meshId;
|
|
|
|
}
|
2018-03-16 04:41:40 +00:00
|
|
|
}
|
|
|
|
|
2016-02-07 22:36:49 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-10 22:57:29 +00:00
|
|
|
// Re-build animations from animation clip library, if present, otherwise combine single-channel animations
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::PostProcessRootAnimations() {
|
|
|
|
if (mAnimationClipLibrary.size() > 0) {
|
2019-07-12 10:29:35 +00:00
|
|
|
Animation temp;
|
2016-02-07 22:36:49 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
for (AnimationClipLibrary::iterator it = mAnimationClipLibrary.begin(); it != mAnimationClipLibrary.end(); ++it) {
|
2019-07-12 10:29:35 +00:00
|
|
|
std::string clipName = it->first;
|
2016-02-07 22:36:49 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
Animation *clip = new Animation();
|
|
|
|
clip->mName = clipName;
|
2016-02-07 22:36:49 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
temp.mSubAnims.push_back(clip);
|
2016-02-07 22:36:49 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
for (std::vector<std::string>::iterator a = it->second.begin(); a != it->second.end(); ++a) {
|
2019-07-12 10:29:35 +00:00
|
|
|
std::string animationID = *a;
|
2016-02-07 22:36:49 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
AnimationLibrary::iterator animation = mAnimationLibrary.find(animationID);
|
2016-02-07 22:36:49 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
if (animation != mAnimationLibrary.end()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
Animation *pSourceAnimation = animation->second;
|
2016-02-07 22:36:49 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
pSourceAnimation->CollectChannelsRecursively(clip->mChannels);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-07 22:36:49 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
mAnims = temp;
|
2016-02-07 22:36:49 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
// Ensure no double deletes.
|
|
|
|
temp.mSubAnims.clear();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2019-07-12 10:29:35 +00:00
|
|
|
mAnims.CombineSingleChannelAnimations();
|
|
|
|
}
|
2016-02-07 22:36:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the animation library
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadAnimationLibrary() {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (mReader->isEmptyElement())
|
|
|
|
return;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("animation")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// delegate the reading. Depending on the inner elements it will be a container or a anim channel
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadAnimation(&mAnims);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "library_animations") != 0)
|
|
|
|
ThrowException("Expected end of <library_animations> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads an animation into the given parent structure
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadAnimation(Collada::Animation *pParent) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
typedef std::map<std::string, AnimationChannel> ChannelMap;
|
|
|
|
ChannelMap channels;
|
|
|
|
// this is the anim container in case we're a container
|
2020-04-28 16:46:07 +00:00
|
|
|
Animation *anim = NULL;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// optional name given as an attribute
|
|
|
|
std::string animName;
|
2019-07-12 10:29:35 +00:00
|
|
|
std::string animID;
|
|
|
|
int indexName = TestAttribute("name");
|
|
|
|
int indexID = TestAttribute("id");
|
2016-02-07 22:36:49 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
if (indexID >= 0)
|
|
|
|
animID = mReader->getAttributeValue(indexID);
|
2016-02-07 22:36:49 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
if (indexName >= 0)
|
|
|
|
animName = mReader->getAttributeValue(indexName);
|
|
|
|
else if (indexID >= 0)
|
2016-02-07 22:36:49 +00:00
|
|
|
animName = animID;
|
2015-05-19 03:57:13 +00:00
|
|
|
else
|
|
|
|
animName = "animation";
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// we have subanimations
|
2020-04-28 16:46:07 +00:00
|
|
|
if (IsElement("animation")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// create container from our element
|
2020-04-28 16:46:07 +00:00
|
|
|
if (!anim) {
|
2015-05-19 03:57:13 +00:00
|
|
|
anim = new Animation;
|
|
|
|
anim->mName = animName;
|
2019-07-12 10:29:35 +00:00
|
|
|
pParent->mSubAnims.push_back(anim);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// recurse into the subelement
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadAnimation(anim);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("source")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// possible animation data - we'll never know. Better store it
|
|
|
|
ReadSource();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("sampler")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read the ID to assign the corresponding collada channel afterwards.
|
2020-03-01 12:15:45 +00:00
|
|
|
int indexId = GetAttribute("id");
|
|
|
|
std::string id = mReader->getAttributeValue(indexId);
|
2019-07-12 10:29:35 +00:00
|
|
|
ChannelMap::iterator newChannel = channels.insert(std::make_pair(id, AnimationChannel())).first;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// have it read into a channel
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadAnimationSampler(newChannel->second);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("channel")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// the binding element whose whole purpose is to provide the target to animate
|
|
|
|
// Thanks, Collada! A directly posted information would have been too simple, I guess.
|
|
|
|
// Better add another indirection to that! Can't have enough of those.
|
2019-07-12 10:29:35 +00:00
|
|
|
int indexTarget = GetAttribute("target");
|
|
|
|
int indexSource = GetAttribute("source");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *sourceId = mReader->getAttributeValue(indexSource);
|
2019-07-12 10:29:35 +00:00
|
|
|
if (sourceId[0] == '#')
|
2015-05-19 03:57:13 +00:00
|
|
|
sourceId++;
|
2019-07-12 10:29:35 +00:00
|
|
|
ChannelMap::iterator cit = channels.find(sourceId);
|
|
|
|
if (cit != channels.end())
|
|
|
|
cit->second.mTarget = mReader->getAttributeValue(indexTarget);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
if (!mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
SkipElement();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "animation") != 0)
|
|
|
|
ThrowException("Expected end of <animation> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// it turned out to have channels - add them
|
2020-04-28 16:46:07 +00:00
|
|
|
if (!channels.empty()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
// FIXME: Is this essentially doing the same as "single-anim-node" codepath in
|
|
|
|
// ColladaLoader::StoreAnimations? For now, this has been deferred to after
|
|
|
|
// all animations and all clips have been read. Due to handling of
|
|
|
|
// <library_animation_clips> this cannot be done here, as the channel owner
|
|
|
|
// is lost, and some exporters make up animations by referring to multiple
|
|
|
|
// single-channel animations from an <instance_animation>.
|
2020-04-28 16:46:07 +00:00
|
|
|
/*
|
2015-05-19 03:57:13 +00:00
|
|
|
// special filtering for stupid exporters packing each channel into a separate animation
|
|
|
|
if( channels.size() == 1)
|
|
|
|
{
|
|
|
|
pParent->mChannels.push_back( channels.begin()->second);
|
|
|
|
} else
|
2016-02-07 22:36:49 +00:00
|
|
|
*/
|
2015-05-19 03:57:13 +00:00
|
|
|
{
|
|
|
|
// else create the animation, if not done yet, and store the channels
|
2020-04-28 16:46:07 +00:00
|
|
|
if (!anim) {
|
2015-05-19 03:57:13 +00:00
|
|
|
anim = new Animation;
|
|
|
|
anim->mName = animName;
|
2019-07-12 10:29:35 +00:00
|
|
|
pParent->mSubAnims.push_back(anim);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2019-07-12 10:29:35 +00:00
|
|
|
for (ChannelMap::const_iterator it = channels.begin(); it != channels.end(); ++it)
|
|
|
|
anim->mChannels.push_back(it->second);
|
2016-02-07 22:36:49 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
if (indexID >= 0) {
|
2019-07-12 10:29:35 +00:00
|
|
|
mAnimationLibrary[animID] = anim;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads an animation sampler into the given anim channel
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadAnimationSampler(Collada::AnimationChannel &pChannel) {
|
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("input")) {
|
2019-07-12 10:29:35 +00:00
|
|
|
int indexSemantic = GetAttribute("semantic");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *semantic = mReader->getAttributeValue(indexSemantic);
|
2019-07-12 10:29:35 +00:00
|
|
|
int indexSource = GetAttribute("source");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *source = mReader->getAttributeValue(indexSource);
|
2019-07-12 10:29:35 +00:00
|
|
|
if (source[0] != '#')
|
|
|
|
ThrowException("Unsupported URL format");
|
2015-05-19 03:57:13 +00:00
|
|
|
source++;
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(semantic, "INPUT") == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
pChannel.mSourceTimes = source;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (strcmp(semantic, "OUTPUT") == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
pChannel.mSourceValues = source;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (strcmp(semantic, "IN_TANGENT") == 0)
|
2017-01-12 11:41:32 +00:00
|
|
|
pChannel.mInTanValues = source;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (strcmp(semantic, "OUT_TANGENT") == 0)
|
2017-01-12 11:41:32 +00:00
|
|
|
pChannel.mOutTanValues = source;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (strcmp(semantic, "INTERPOLATION") == 0)
|
2017-01-12 11:41:32 +00:00
|
|
|
pChannel.mInterpolationValues = source;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
if (!mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
SkipElement();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "sampler") != 0)
|
|
|
|
ThrowException("Expected end of <sampler> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the skeleton controller library
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadControllerLibrary() {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (mReader->isEmptyElement())
|
|
|
|
return;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("controller")) {
|
2016-04-03 00:38:00 +00:00
|
|
|
// read ID. Ask the spec if it's necessary or optional... you might be surprised.
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrID = GetAttribute("id");
|
|
|
|
std::string id = mReader->getAttributeValue(attrID);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// create an entry and store it in the library under its ID
|
|
|
|
mControllerLibrary[id] = Controller();
|
|
|
|
|
|
|
|
// read on from there
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadController(mControllerLibrary[id]);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "library_controllers") != 0)
|
|
|
|
ThrowException("Expected end of <library_controllers> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads a controller into the given mesh structure
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadController(Collada::Controller &pController) {
|
2017-01-12 11:41:32 +00:00
|
|
|
// initial values
|
|
|
|
pController.mType = Skin;
|
|
|
|
pController.mMethod = Normalized;
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// two types of controllers: "skin" and "morph". Only the first one is relevant, we skip the other
|
2020-04-28 16:46:07 +00:00
|
|
|
if (IsElement("morph")) {
|
2017-01-12 11:41:32 +00:00
|
|
|
pController.mType = Morph;
|
|
|
|
int baseIndex = GetAttribute("source");
|
|
|
|
pController.mMeshId = mReader->getAttributeValue(baseIndex) + 1;
|
|
|
|
int methodIndex = GetAttribute("method");
|
|
|
|
if (methodIndex > 0) {
|
|
|
|
const char *method = mReader->getAttributeValue(methodIndex);
|
|
|
|
if (strcmp(method, "RELATIVE") == 0)
|
|
|
|
pController.mMethod = Relative;
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("skin")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read the mesh it refers to. According to the spec this could also be another
|
|
|
|
// controller, but I refuse to implement every single idea they've come up with
|
2019-07-12 10:29:35 +00:00
|
|
|
int sourceIndex = GetAttribute("source");
|
|
|
|
pController.mMeshId = mReader->getAttributeValue(sourceIndex) + 1;
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("bind_shape_matrix")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// content is 16 floats to define a matrix... it seems to be important for some models
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *content = GetTextContent();
|
2017-01-26 16:28:39 +00:00
|
|
|
|
|
|
|
// read the 16 floats
|
2020-04-28 16:46:07 +00:00
|
|
|
for (unsigned int a = 0; a < 16; a++) {
|
2017-01-26 16:28:39 +00:00
|
|
|
// read a number
|
2019-07-12 10:29:35 +00:00
|
|
|
content = fast_atoreal_move<ai_real>(content, pController.mBindShapeMatrix[a]);
|
2017-01-26 16:28:39 +00:00
|
|
|
// skip whitespace after it
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipSpacesAndLineEnd(&content);
|
2017-01-26 16:28:39 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("bind_shape_matrix");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("source")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// data array - we have specialists to handle this
|
|
|
|
ReadSource();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("joints")) {
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadControllerJoints(pController);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("vertex_weights")) {
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadControllerWeights(pController);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("targets")) {
|
2017-01-12 11:41:32 +00:00
|
|
|
while (mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("input")) {
|
2017-01-12 11:41:32 +00:00
|
|
|
int semanticsIndex = GetAttribute("semantic");
|
|
|
|
int sourceIndex = GetAttribute("source");
|
|
|
|
|
|
|
|
const char *semantics = mReader->getAttributeValue(semanticsIndex);
|
|
|
|
const char *source = mReader->getAttributeValue(sourceIndex);
|
|
|
|
if (strcmp(semantics, "MORPH_TARGET") == 0) {
|
|
|
|
pController.mMorphTarget = source + 1;
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (strcmp(semantics, "MORPH_WEIGHT") == 0) {
|
2017-01-12 11:41:32 +00:00
|
|
|
pController.mMorphWeight = source + 1;
|
|
|
|
}
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "targets") == 0)
|
2017-01-12 11:41:32 +00:00
|
|
|
break;
|
|
|
|
else
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException("Expected end of <targets> element.");
|
2017-01-12 11:41:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "controller") == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (strcmp(mReader->getNodeName(), "skin") != 0 && strcmp(mReader->getNodeName(), "morph") != 0)
|
|
|
|
ThrowException("Expected end of <controller> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the joint definitions for the given controller
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadControllerJoints(Collada::Controller &pController) {
|
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Input channels for joint data. Two possible semantics: "JOINT" and "INV_BIND_MATRIX"
|
2020-04-28 16:46:07 +00:00
|
|
|
if (IsElement("input")) {
|
2019-07-12 10:29:35 +00:00
|
|
|
int indexSemantic = GetAttribute("semantic");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *attrSemantic = mReader->getAttributeValue(indexSemantic);
|
2019-07-12 10:29:35 +00:00
|
|
|
int indexSource = GetAttribute("source");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *attrSource = mReader->getAttributeValue(indexSource);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// local URLS always start with a '#'. We don't support global URLs
|
2019-07-12 10:29:35 +00:00
|
|
|
if (attrSource[0] != '#')
|
|
|
|
ThrowException(format() << "Unsupported URL format in \"" << attrSource << "\" in source attribute of <joints> data <input> element");
|
2015-05-19 03:57:13 +00:00
|
|
|
attrSource++;
|
|
|
|
|
|
|
|
// parse source URL to corresponding source
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(attrSemantic, "JOINT") == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
pController.mJointNameSource = attrSource;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (strcmp(attrSemantic, "INV_BIND_MATRIX") == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
pController.mJointOffsetMatrixSource = attrSource;
|
|
|
|
else
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException(format() << "Unknown semantic \"" << attrSemantic << "\" in <joints> data <input> element");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// skip inner data, if present
|
2019-07-12 10:29:35 +00:00
|
|
|
if (!mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
SkipElement();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "joints") != 0)
|
|
|
|
ThrowException("Expected end of <joints> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the joint weights for the given controller
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadControllerWeights(Collada::Controller &pController) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read vertex count from attributes and resize the array accordingly
|
2019-07-12 10:29:35 +00:00
|
|
|
int indexCount = GetAttribute("count");
|
|
|
|
size_t vertexCount = (size_t)mReader->getAttributeValueAsInt(indexCount);
|
|
|
|
pController.mWeightCounts.resize(vertexCount);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Input channels for weight data. Two possible semantics: "JOINT" and "WEIGHT"
|
2020-04-28 16:46:07 +00:00
|
|
|
if (IsElement("input") && vertexCount > 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
InputChannel channel;
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
int indexSemantic = GetAttribute("semantic");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *attrSemantic = mReader->getAttributeValue(indexSemantic);
|
2019-07-12 10:29:35 +00:00
|
|
|
int indexSource = GetAttribute("source");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *attrSource = mReader->getAttributeValue(indexSource);
|
2019-07-12 10:29:35 +00:00
|
|
|
int indexOffset = TestAttribute("offset");
|
|
|
|
if (indexOffset >= 0)
|
|
|
|
channel.mOffset = mReader->getAttributeValueAsInt(indexOffset);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// local URLS always start with a '#'. We don't support global URLs
|
2019-07-12 10:29:35 +00:00
|
|
|
if (attrSource[0] != '#')
|
|
|
|
ThrowException(format() << "Unsupported URL format in \"" << attrSource << "\" in source attribute of <vertex_weights> data <input> element");
|
2015-05-19 03:57:13 +00:00
|
|
|
channel.mAccessor = attrSource + 1;
|
|
|
|
|
|
|
|
// parse source URL to corresponding source
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(attrSemantic, "JOINT") == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
pController.mWeightInputJoints = channel;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (strcmp(attrSemantic, "WEIGHT") == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
pController.mWeightInputWeights = channel;
|
|
|
|
else
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException(format() << "Unknown semantic \"" << attrSemantic << "\" in <vertex_weights> data <input> element");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// skip inner data, if present
|
2019-07-12 10:29:35 +00:00
|
|
|
if (!mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
SkipElement();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("vcount") && vertexCount > 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read weight count per vertex
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *text = GetTextContent();
|
2015-05-19 03:57:13 +00:00
|
|
|
size_t numWeights = 0;
|
2020-04-28 16:46:07 +00:00
|
|
|
for (std::vector<size_t>::iterator it = pController.mWeightCounts.begin(); it != pController.mWeightCounts.end(); ++it) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (*text == 0)
|
|
|
|
ThrowException("Out of data while reading <vcount>");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
*it = strtoul10(text, &text);
|
2015-05-19 03:57:13 +00:00
|
|
|
numWeights += *it;
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipSpacesAndLineEnd(&text);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("vcount");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// reserve weight count
|
2019-07-12 10:29:35 +00:00
|
|
|
pController.mWeights.resize(numWeights);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("v") && vertexCount > 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read JointIndex - WeightIndex pairs
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *text = GetTextContent();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
for (std::vector<std::pair<size_t, size_t>>::iterator it = pController.mWeights.begin(); it != pController.mWeights.end(); ++it) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (*text == 0)
|
|
|
|
ThrowException("Out of data while reading <vertex_weights>");
|
|
|
|
it->first = strtoul10(text, &text);
|
|
|
|
SkipSpacesAndLineEnd(&text);
|
|
|
|
if (*text == 0)
|
|
|
|
ThrowException("Out of data while reading <vertex_weights>");
|
|
|
|
it->second = strtoul10(text, &text);
|
|
|
|
SkipSpacesAndLineEnd(&text);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("v");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "vertex_weights") != 0)
|
|
|
|
ThrowException("Expected end of <vertex_weights> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the image library contents
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadImageLibrary() {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2020-04-28 16:46:07 +00:00
|
|
|
if (IsElement("image")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read ID. Another entry which is "optional" by design but obligatory in reality
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrID = GetAttribute("id");
|
|
|
|
std::string id = mReader->getAttributeValue(attrID);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// create an entry and store it in the library under its ID
|
|
|
|
mImageLibrary[id] = Image();
|
|
|
|
|
|
|
|
// read on from there
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadImage(mImageLibrary[id]);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "library_images") != 0)
|
|
|
|
ThrowException("Expected end of <library_images> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads an image entry into the given image
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadImage(Collada::Image &pImage) {
|
|
|
|
while (mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Need to run different code paths here, depending on the Collada XSD version
|
|
|
|
if (IsElement("image")) {
|
2015-05-19 03:48:29 +00:00
|
|
|
SkipElement();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("init_from")) {
|
|
|
|
if (mFormat == FV_1_4_n) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// FIX: C4D exporter writes empty <init_from/> tags
|
|
|
|
if (!mReader->isEmptyElement()) {
|
|
|
|
// element content is filename - hopefully
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *sz = TestTextContent();
|
|
|
|
if (sz) {
|
2019-10-11 17:57:38 +00:00
|
|
|
aiString filepath(sz);
|
2019-10-14 10:27:34 +00:00
|
|
|
UriDecodePath(filepath);
|
2019-10-11 17:57:38 +00:00
|
|
|
pImage.mFileName = filepath.C_Str();
|
|
|
|
}
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("init_from");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
if (!pImage.mFileName.length()) {
|
|
|
|
pImage.mFileName = "unknown_texture";
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mFormat == FV_1_5_n) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// make sure we skip over mip and array initializations, which
|
|
|
|
// we don't support, but which could confuse the loader if
|
|
|
|
// they're not skipped.
|
|
|
|
int attrib = TestAttribute("array_index");
|
|
|
|
if (attrib != -1 && mReader->getAttributeValueAsInt(attrib) > 0) {
|
2018-04-19 14:48:43 +00:00
|
|
|
ASSIMP_LOG_WARN("Collada: Ignoring texture array index");
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
attrib = TestAttribute("mip_index");
|
|
|
|
if (attrib != -1 && mReader->getAttributeValueAsInt(attrib) > 0) {
|
2018-04-19 14:48:43 +00:00
|
|
|
ASSIMP_LOG_WARN("Collada: Ignoring MIP map layer");
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: correctly jump over cube and volume maps?
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mFormat == FV_1_5_n) {
|
|
|
|
if (IsElement("ref")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// element content is filename - hopefully
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *sz = TestTextContent();
|
|
|
|
if (sz) {
|
2019-10-11 17:57:38 +00:00
|
|
|
aiString filepath(sz);
|
2019-10-14 10:27:34 +00:00
|
|
|
UriDecodePath(filepath);
|
2019-10-11 17:57:38 +00:00
|
|
|
pImage.mFileName = filepath.C_Str();
|
|
|
|
}
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("ref");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("hex") && !pImage.mFileName.length()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// embedded image. get format
|
|
|
|
const int attrib = TestAttribute("format");
|
|
|
|
if (-1 == attrib)
|
2018-04-19 14:48:43 +00:00
|
|
|
ASSIMP_LOG_WARN("Collada: Unknown image file format");
|
2020-04-28 16:46:07 +00:00
|
|
|
else
|
|
|
|
pImage.mEmbeddedFormat = mReader->getAttributeValue(attrib);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *data = GetTextContent();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// hexadecimal-encoded binary octets. First of all, find the
|
|
|
|
// required buffer size to reserve enough storage.
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *cur = data;
|
|
|
|
while (!IsSpaceOrNewLine(*cur))
|
|
|
|
cur++;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
const unsigned int size = (unsigned int)(cur - data) * 2;
|
2015-05-19 03:57:13 +00:00
|
|
|
pImage.mImageData.resize(size);
|
2019-07-12 10:29:35 +00:00
|
|
|
for (unsigned int i = 0; i < size; ++i)
|
|
|
|
pImage.mImageData[i] = HexOctetToDecimal(data + (i << 1));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("hex");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "image") == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the material library
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadMaterialLibrary() {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
std::map<std::string, int> names;
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("material")) {
|
2015-12-10 16:19:33 +00:00
|
|
|
// read ID. By now you probably know my opinion about this "specification"
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrID = GetAttribute("id");
|
|
|
|
std::string id = mReader->getAttributeValue(attrID);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
std::string name;
|
|
|
|
int attrName = TestAttribute("name");
|
|
|
|
if (attrName >= 0)
|
2019-07-12 10:29:35 +00:00
|
|
|
name = mReader->getAttributeValue(attrName);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// create an entry and store it in the library under its ID
|
|
|
|
mMaterialLibrary[id] = Material();
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
if (!name.empty()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
std::map<std::string, int>::iterator it = names.find(name);
|
2020-04-28 16:46:07 +00:00
|
|
|
if (it != names.end()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
std::ostringstream strStream;
|
|
|
|
strStream << ++it->second;
|
2019-07-12 10:29:35 +00:00
|
|
|
name.append(" " + strStream.str());
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
names[name] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mMaterialLibrary[id].mName = name;
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadMaterial(mMaterialLibrary[id]);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "library_materials") != 0)
|
|
|
|
ThrowException("Expected end of <library_materials> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the light library
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadLightLibrary() {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2020-04-28 16:46:07 +00:00
|
|
|
if (IsElement("light")) {
|
2015-12-10 16:19:33 +00:00
|
|
|
// read ID. By now you probably know my opinion about this "specification"
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrID = GetAttribute("id");
|
|
|
|
std::string id = mReader->getAttributeValue(attrID);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// create an entry and store it in the library under its ID
|
|
|
|
ReadLight(mLightLibrary[id] = Light());
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "library_lights") != 0)
|
|
|
|
ThrowException("Expected end of <library_lights> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the camera library
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadCameraLibrary() {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2020-04-28 16:46:07 +00:00
|
|
|
if (IsElement("camera")) {
|
2015-12-10 16:19:33 +00:00
|
|
|
// read ID. By now you probably know my opinion about this "specification"
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrID = GetAttribute("id");
|
|
|
|
std::string id = mReader->getAttributeValue(attrID);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// create an entry and store it in the library under its ID
|
2020-04-28 16:46:07 +00:00
|
|
|
Camera &cam = mCameraLibrary[id];
|
2019-07-12 10:29:35 +00:00
|
|
|
attrID = TestAttribute("name");
|
2015-05-19 03:57:13 +00:00
|
|
|
if (attrID != -1)
|
2019-07-12 10:29:35 +00:00
|
|
|
cam.mName = mReader->getAttributeValue(attrID);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
ReadCamera(cam);
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "library_cameras") != 0)
|
|
|
|
ThrowException("Expected end of <library_cameras> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads a material entry into the given material
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadMaterial(Collada::Material &pMaterial) {
|
|
|
|
while (mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (IsElement("material")) {
|
|
|
|
SkipElement();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("instance_effect")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// referred effect by URL
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrUrl = GetAttribute("url");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *url = mReader->getAttributeValue(attrUrl);
|
2019-07-12 10:29:35 +00:00
|
|
|
if (url[0] != '#')
|
|
|
|
ThrowException("Unknown reference format");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
pMaterial.mEffect = url + 1;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
SkipElement();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
2015-05-19 03:48:29 +00:00
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "material") != 0)
|
|
|
|
ThrowException("Expected end of <material> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads a light entry into the given light
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadLight(Collada::Light &pLight) {
|
|
|
|
while (mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2015-05-19 03:48:29 +00:00
|
|
|
if (IsElement("light")) {
|
|
|
|
SkipElement();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("spot")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pLight.mType = aiLightSource_SPOT;
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("ambient")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pLight.mType = aiLightSource_AMBIENT;
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("directional")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pLight.mType = aiLightSource_DIRECTIONAL;
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("point")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pLight.mType = aiLightSource_POINT;
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("color")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// text content contains 3 floats
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *content = GetTextContent();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
content = fast_atoreal_move<ai_real>(content, (ai_real &)pLight.mColor.r);
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipSpacesAndLineEnd(&content);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
content = fast_atoreal_move<ai_real>(content, (ai_real &)pLight.mColor.g);
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipSpacesAndLineEnd(&content);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
content = fast_atoreal_move<ai_real>(content, (ai_real &)pLight.mColor.b);
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipSpacesAndLineEnd(&content);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("color");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("constant_attenuation")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pLight.mAttConstant = ReadFloatFromTextContent();
|
|
|
|
TestClosing("constant_attenuation");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("linear_attenuation")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pLight.mAttLinear = ReadFloatFromTextContent();
|
|
|
|
TestClosing("linear_attenuation");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("quadratic_attenuation")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pLight.mAttQuadratic = ReadFloatFromTextContent();
|
|
|
|
TestClosing("quadratic_attenuation");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("falloff_angle")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pLight.mFalloffAngle = ReadFloatFromTextContent();
|
|
|
|
TestClosing("falloff_angle");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("falloff_exponent")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pLight.mFalloffExponent = ReadFloatFromTextContent();
|
|
|
|
TestClosing("falloff_exponent");
|
|
|
|
}
|
|
|
|
// FCOLLADA extensions
|
|
|
|
// -------------------------------------------------------
|
|
|
|
else if (IsElement("outer_cone")) {
|
|
|
|
pLight.mOuterAngle = ReadFloatFromTextContent();
|
|
|
|
TestClosing("outer_cone");
|
|
|
|
}
|
|
|
|
// ... and this one is even deprecated
|
|
|
|
else if (IsElement("penumbra_angle")) {
|
|
|
|
pLight.mPenumbraAngle = ReadFloatFromTextContent();
|
|
|
|
TestClosing("penumbra_angle");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("intensity")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pLight.mIntensity = ReadFloatFromTextContent();
|
|
|
|
TestClosing("intensity");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("falloff")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pLight.mOuterAngle = ReadFloatFromTextContent();
|
|
|
|
TestClosing("falloff");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("hotspot_beam")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pLight.mFalloffAngle = ReadFloatFromTextContent();
|
|
|
|
TestClosing("hotspot_beam");
|
|
|
|
}
|
2015-08-28 14:20:17 +00:00
|
|
|
// OpenCOLLADA extensions
|
|
|
|
// -------------------------------------------------------
|
|
|
|
else if (IsElement("decay_falloff")) {
|
|
|
|
pLight.mOuterAngle = ReadFloatFromTextContent();
|
|
|
|
TestClosing("decay_falloff");
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "light") == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads a camera entry into the given light
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadCamera(Collada::Camera &pCamera) {
|
|
|
|
while (mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (IsElement("camera")) {
|
2015-05-19 03:48:29 +00:00
|
|
|
SkipElement();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("orthographic")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pCamera.mOrtho = true;
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("xfov") || IsElement("xmag")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pCamera.mHorFov = ReadFloatFromTextContent();
|
|
|
|
TestClosing((pCamera.mOrtho ? "xmag" : "xfov"));
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("yfov") || IsElement("ymag")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pCamera.mVerFov = ReadFloatFromTextContent();
|
|
|
|
TestClosing((pCamera.mOrtho ? "ymag" : "yfov"));
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("aspect_ratio")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pCamera.mAspect = ReadFloatFromTextContent();
|
|
|
|
TestClosing("aspect_ratio");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("znear")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pCamera.mZNear = ReadFloatFromTextContent();
|
|
|
|
TestClosing("znear");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("zfar")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pCamera.mZFar = ReadFloatFromTextContent();
|
|
|
|
TestClosing("zfar");
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "camera") == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the effect library
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadEffectLibrary() {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (mReader->isEmptyElement()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2020-04-28 16:46:07 +00:00
|
|
|
if (IsElement("effect")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read ID. Do I have to repeat my ranting about "optional" attributes?
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrID = GetAttribute("id");
|
|
|
|
std::string id = mReader->getAttributeValue(attrID);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// create an entry and store it in the library under its ID
|
|
|
|
mEffectLibrary[id] = Effect();
|
|
|
|
// read on from there
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadEffect(mEffectLibrary[id]);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "library_effects") != 0)
|
|
|
|
ThrowException("Expected end of <library_effects> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads an effect entry into the given effect
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadEffect(Collada::Effect &pEffect) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// for the moment we don't support any other type of effect.
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (IsElement("profile_COMMON"))
|
|
|
|
ReadEffectProfileCommon(pEffect);
|
2015-05-19 03:57:13 +00:00
|
|
|
else
|
|
|
|
SkipElement();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "effect") != 0)
|
|
|
|
ThrowException("Expected end of <effect> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads an COMMON effect profile
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadEffectProfileCommon(Collada::Effect &pEffect) {
|
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (IsElement("newparam")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// save ID
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrSID = GetAttribute("sid");
|
|
|
|
std::string sid = mReader->getAttributeValue(attrSID);
|
2015-05-19 03:57:13 +00:00
|
|
|
pEffect.mParams[sid] = EffectParam();
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadEffectParam(pEffect.mParams[sid]);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("technique") || IsElement("extra")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// just syntactic sugar
|
|
|
|
}
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
else if (mFormat == FV_1_4_n && IsElement("image")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read ID. Another entry which is "optional" by design but obligatory in reality
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrID = GetAttribute("id");
|
|
|
|
std::string id = mReader->getAttributeValue(attrID);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// create an entry and store it in the library under its ID
|
|
|
|
mImageLibrary[id] = Image();
|
|
|
|
|
|
|
|
// read on from there
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadImage(mImageLibrary[id]);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Shading modes */
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("phong"))
|
2015-05-19 03:57:13 +00:00
|
|
|
pEffect.mShadeType = Shade_Phong;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("constant"))
|
2015-05-19 03:57:13 +00:00
|
|
|
pEffect.mShadeType = Shade_Constant;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("lambert"))
|
2015-05-19 03:57:13 +00:00
|
|
|
pEffect.mShadeType = Shade_Lambert;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("blinn"))
|
2015-05-19 03:57:13 +00:00
|
|
|
pEffect.mShadeType = Shade_Blinn;
|
|
|
|
|
|
|
|
/* Color + texture properties */
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("emission"))
|
|
|
|
ReadEffectColor(pEffect.mEmissive, pEffect.mTexEmissive);
|
|
|
|
else if (IsElement("ambient"))
|
|
|
|
ReadEffectColor(pEffect.mAmbient, pEffect.mTexAmbient);
|
|
|
|
else if (IsElement("diffuse"))
|
|
|
|
ReadEffectColor(pEffect.mDiffuse, pEffect.mTexDiffuse);
|
|
|
|
else if (IsElement("specular"))
|
|
|
|
ReadEffectColor(pEffect.mSpecular, pEffect.mTexSpecular);
|
|
|
|
else if (IsElement("reflective")) {
|
|
|
|
ReadEffectColor(pEffect.mReflective, pEffect.mTexReflective);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("transparent")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pEffect.mHasTransparency = true;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *opaque = mReader->getAttributeValueSafe("opaque");
|
2016-04-05 20:53:54 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
if (::strcmp(opaque, "RGB_ZERO") == 0 || ::strcmp(opaque, "RGB_ONE") == 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pEffect.mRGBTransparency = true;
|
|
|
|
}
|
|
|
|
|
2015-12-08 14:24:07 +00:00
|
|
|
// In RGB_ZERO mode, the transparency is interpreted in reverse, go figure...
|
2019-07-12 10:29:35 +00:00
|
|
|
if (::strcmp(opaque, "RGB_ZERO") == 0 || ::strcmp(opaque, "A_ZERO") == 0) {
|
|
|
|
pEffect.mInvertTransparency = true;
|
|
|
|
}
|
2015-12-08 14:24:07 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadEffectColor(pEffect.mTransparent, pEffect.mTexTransparent);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("shininess"))
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadEffectFloat(pEffect.mShininess);
|
|
|
|
else if (IsElement("reflectivity"))
|
|
|
|
ReadEffectFloat(pEffect.mReflectivity);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
/* Single scalar properties */
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("transparency"))
|
|
|
|
ReadEffectFloat(pEffect.mTransparency);
|
|
|
|
else if (IsElement("index_of_refraction"))
|
|
|
|
ReadEffectFloat(pEffect.mRefractIndex);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// GOOGLEEARTH/OKINO extensions
|
|
|
|
// -------------------------------------------------------
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("double_sided"))
|
2015-05-19 03:57:13 +00:00
|
|
|
pEffect.mDoubleSided = ReadBoolFromTextContent();
|
|
|
|
|
|
|
|
// FCOLLADA extensions
|
|
|
|
// -------------------------------------------------------
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("bump")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiColor4D dummy;
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadEffectColor(dummy, pEffect.mTexBump);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MAX3D extensions
|
|
|
|
// -------------------------------------------------------
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("wireframe")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pEffect.mWireframe = ReadBoolFromTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("wireframe");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("faceted")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pEffect.mFaceted = ReadBoolFromTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("faceted");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
|
|
|
if (strcmp(mReader->getNodeName(), "profile_COMMON") == 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Read texture wrapping + UV transform settings from a profile==Maya chunk
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadSamplerProperties(Sampler &out) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (mReader->isEmptyElement()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// MAYA extensions
|
|
|
|
// -------------------------------------------------------
|
2019-07-12 10:29:35 +00:00
|
|
|
if (IsElement("wrapU")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out.mWrapU = ReadBoolFromTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("wrapU");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("wrapV")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out.mWrapV = ReadBoolFromTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("wrapV");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("mirrorU")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out.mMirrorU = ReadBoolFromTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("mirrorU");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("mirrorV")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out.mMirrorV = ReadBoolFromTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("mirrorV");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("repeatU")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out.mTransform.mScaling.x = ReadFloatFromTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("repeatU");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("repeatV")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out.mTransform.mScaling.y = ReadFloatFromTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("repeatV");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("offsetU")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out.mTransform.mTranslation.x = ReadFloatFromTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("offsetU");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("offsetV")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out.mTransform.mTranslation.y = ReadFloatFromTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("offsetV");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("rotateUV")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out.mTransform.mRotation = ReadFloatFromTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("rotateUV");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("blend_mode")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *sz = GetTextContent();
|
2015-05-19 03:57: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
|
2019-07-12 10:29:35 +00:00
|
|
|
if (0 == ASSIMP_strincmp(sz, "ADD", 3))
|
2015-05-19 03:57:13 +00:00
|
|
|
out.mOp = aiTextureOp_Add;
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (0 == ASSIMP_strincmp(sz, "SUBTRACT", 8))
|
2015-05-19 03:57:13 +00:00
|
|
|
out.mOp = aiTextureOp_Subtract;
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (0 == ASSIMP_strincmp(sz, "MULTIPLY", 8))
|
2015-05-19 03:57:13 +00:00
|
|
|
out.mOp = aiTextureOp_Multiply;
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
else {
|
2018-04-19 14:48:43 +00:00
|
|
|
ASSIMP_LOG_WARN("Collada: Unsupported MAYA texture blend mode");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("blend_mode");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
// OKINO extensions
|
|
|
|
// -------------------------------------------------------
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("weighting")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out.mWeighting = ReadFloatFromTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("weighting");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("mix_with_previous_layer")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out.mMixWithPrevious = ReadFloatFromTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("mix_with_previous_layer");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
// MAX3D extensions
|
|
|
|
// -------------------------------------------------------
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("amount")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out.mWeighting = ReadFloatFromTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("amount");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "technique") == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads an effect entry containing a color or a texture defining that color
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadEffectColor(aiColor4D &pColor, Sampler &pSampler) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (mReader->isEmptyElement())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Save current element name
|
|
|
|
const std::string curElem = mReader->getNodeName();
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2020-04-28 16:46:07 +00:00
|
|
|
if (IsElement("color")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// text content contains 4 floats
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *content = GetTextContent();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
content = fast_atoreal_move<ai_real>(content, (ai_real &)pColor.r);
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipSpacesAndLineEnd(&content);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
content = fast_atoreal_move<ai_real>(content, (ai_real &)pColor.g);
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipSpacesAndLineEnd(&content);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
content = fast_atoreal_move<ai_real>(content, (ai_real &)pColor.b);
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipSpacesAndLineEnd(&content);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
content = fast_atoreal_move<ai_real>(content, (ai_real &)pColor.a);
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipSpacesAndLineEnd(&content);
|
|
|
|
TestClosing("color");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("texture")) {
|
2017-01-28 07:02:02 +00:00
|
|
|
// get name of source texture/sampler
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrTex = GetAttribute("texture");
|
|
|
|
pSampler.mName = mReader->getAttributeValue(attrTex);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// 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.
|
2019-07-12 10:29:35 +00:00
|
|
|
attrTex = TestAttribute("texcoord");
|
|
|
|
if (attrTex >= 0)
|
|
|
|
pSampler.mUVChannel = mReader->getAttributeValue(attrTex);
|
2015-05-19 03:57:13 +00:00
|
|
|
//SkipElement();
|
|
|
|
|
|
|
|
// 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-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("technique")) {
|
2019-07-12 10:29:35 +00:00
|
|
|
const int _profile = GetAttribute("profile");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *profile = mReader->getAttributeValue(_profile);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Some extensions are quite useful ... ReadSamplerProperties processes
|
|
|
|
// several extensions in MAYA, OKINO and MAX3D profiles.
|
2020-04-28 16:46:07 +00:00
|
|
|
if (!::strcmp(profile, "MAYA") || !::strcmp(profile, "MAX3D") || !::strcmp(profile, "OKINO")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// get more information on this sampler
|
|
|
|
ReadSamplerProperties(pSampler);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else
|
|
|
|
SkipElement();
|
|
|
|
} else if (!IsElement("extra")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (mReader->getNodeName() == curElem)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads an effect entry containing a float
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadEffectFloat(ai_real &pFloat) {
|
|
|
|
while (mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2020-04-28 16:46:07 +00:00
|
|
|
if (IsElement("float")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// text content contains a single floats
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *content = GetTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
content = fast_atoreal_move<ai_real>(content, pFloat);
|
|
|
|
SkipSpacesAndLineEnd(&content);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("float");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Reads an effect parameter specification of any kind
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadEffectParam(Collada::EffectParam &pParam) {
|
|
|
|
while (mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2020-04-28 16:46:07 +00:00
|
|
|
if (IsElement("surface")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// image ID given inside <init_from> tags
|
2019-07-12 10:29:35 +00:00
|
|
|
TestOpening("init_from");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *content = GetTextContent();
|
2015-05-19 03:57:13 +00:00
|
|
|
pParam.mType = Param_Surface;
|
|
|
|
pParam.mReference = content;
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("init_from");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// don't care for remaining stuff
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipElement("surface");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("sampler2D") && (FV_1_4_n == mFormat || FV_1_3_n == mFormat)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// surface ID is given inside <source> tags
|
2019-07-12 10:29:35 +00:00
|
|
|
TestOpening("source");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *content = GetTextContent();
|
2015-05-19 03:57:13 +00:00
|
|
|
pParam.mType = Param_Sampler;
|
|
|
|
pParam.mReference = content;
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("source");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// don't care for remaining stuff
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipElement("sampler2D");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("sampler2D")) {
|
2015-11-19 11:41:26 +00:00
|
|
|
// surface ID is given inside <instance_image> tags
|
2019-07-12 10:29:35 +00:00
|
|
|
TestOpening("instance_image");
|
2015-11-19 11:41:26 +00:00
|
|
|
int attrURL = GetAttribute("url");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *url = mReader->getAttributeValue(attrURL);
|
2019-07-12 10:29:35 +00:00
|
|
|
if (url[0] != '#')
|
|
|
|
ThrowException("Unsupported URL format in instance_image");
|
2015-11-19 11:41:26 +00:00
|
|
|
url++;
|
|
|
|
pParam.mType = Param_Sampler;
|
|
|
|
pParam.mReference = url;
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipElement("sampler2D");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore unknown element
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the geometry library contents
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadGeometryLibrary() {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("geometry")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read ID. Another entry which is "optional" by design but obligatory in reality
|
2019-07-12 10:29:35 +00:00
|
|
|
int indexID = GetAttribute("id");
|
|
|
|
std::string id = mReader->getAttributeValue(indexID);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// TODO: (thom) support SIDs
|
|
|
|
// ai_assert( TestAttribute( "sid") == -1);
|
|
|
|
|
2020-04-29 16:17:46 +00:00
|
|
|
// 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));
|
|
|
|
|
|
|
|
// read the mesh name if it exists
|
|
|
|
const int nameIndex = TestAttribute("name");
|
|
|
|
if (nameIndex != -1) {
|
|
|
|
mesh->mName = mReader->getAttributeValue(nameIndex);
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-04-29 16:17:46 +00:00
|
|
|
// read on from there
|
|
|
|
ReadGeometry(*mesh);
|
|
|
|
// Read successfully, add to library
|
|
|
|
mMeshLibrary.insert({ id, mesh.release() });
|
|
|
|
} else {
|
|
|
|
ASSIMP_LOG_ERROR_F("Collada: Skipped duplicate geometry id: \"", id, "\"");
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "library_geometries") != 0)
|
|
|
|
ThrowException("Expected end of <library_geometries> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads a geometry from the geometry library.
|
2020-04-29 16:17:46 +00:00
|
|
|
void ColladaParser::ReadGeometry(Collada::Mesh &pMesh) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("mesh")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read on from there
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadMesh(pMesh);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "geometry") != 0)
|
|
|
|
ThrowException("Expected end of <geometry> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads a mesh from the geometry library
|
2020-04-29 16:17:46 +00:00
|
|
|
void ColladaParser::ReadMesh(Mesh &pMesh) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("source")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// we have professionals dealing with this
|
|
|
|
ReadSource();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("vertices")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read per-vertex mesh data
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadVertexData(pMesh);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("triangles") || IsElement("lines") || IsElement("linestrips") || IsElement("polygons") || IsElement("polylist") || IsElement("trifans") || IsElement("tristrips")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read per-index mesh data and faces setup
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadIndexData(pMesh);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2017-07-11 08:42:10 +00:00
|
|
|
// ignore the restf
|
2015-05-19 03:57:13 +00:00
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
|
|
|
if (strcmp(mReader->getNodeName(), "technique_common") == 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// end of another meaningless element - read over it
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (strcmp(mReader->getNodeName(), "mesh") == 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// end of <mesh> element - we're done here
|
|
|
|
break;
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// everything else should be punished
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException("Expected end of <mesh> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Reads a source element
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadSource() {
|
2019-07-12 10:29:35 +00:00
|
|
|
int indexID = GetAttribute("id");
|
|
|
|
std::string sourceID = mReader->getAttributeValue(indexID);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("float_array") || IsElement("IDREF_array") || IsElement("Name_array")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadDataArray();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("technique_common")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// I don't care for your profiles
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("accessor")) {
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadAccessor(sourceID);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
|
|
|
if (strcmp(mReader->getNodeName(), "source") == 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// end of <source> - we're done
|
|
|
|
break;
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (strcmp(mReader->getNodeName(), "technique_common") == 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// end of another meaningless element - read over it
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// everything else should be punished
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException("Expected end of <source> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads a data array holding a number of floats, and stores it in the global library
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadDataArray() {
|
2015-05-19 03:57:13 +00:00
|
|
|
std::string elmName = mReader->getNodeName();
|
|
|
|
bool isStringArray = (elmName == "IDREF_array" || elmName == "Name_array");
|
2019-07-12 10:29:35 +00:00
|
|
|
bool isEmptyElement = mReader->isEmptyElement();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// read attributes
|
2019-07-12 10:29:35 +00:00
|
|
|
int indexID = GetAttribute("id");
|
|
|
|
std::string id = mReader->getAttributeValue(indexID);
|
|
|
|
int indexCount = GetAttribute("count");
|
|
|
|
unsigned int count = (unsigned int)mReader->getAttributeValueAsInt(indexCount);
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *content = TestTextContent();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
// read values and store inside an array in the data library
|
|
|
|
mDataLibrary[id] = Data();
|
2020-04-28 16:46:07 +00:00
|
|
|
Data &data = mDataLibrary[id];
|
2019-07-12 10:29:35 +00:00
|
|
|
data.mIsStringArray = isStringArray;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
// some exporters write empty data arrays, but we need to conserve them anyways because others might reference them
|
2020-04-28 16:46:07 +00:00
|
|
|
if (content) {
|
|
|
|
if (isStringArray) {
|
2019-07-12 10:29:35 +00:00
|
|
|
data.mStrings.reserve(count);
|
2015-05-19 03:57:13 +00:00
|
|
|
std::string s;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
for (unsigned int a = 0; a < count; a++) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (*content == 0)
|
|
|
|
ThrowException("Expected more values while reading IDREF_array contents.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
s.clear();
|
2019-07-12 10:29:35 +00:00
|
|
|
while (!IsSpaceOrNewLine(*content))
|
2015-05-19 03:57:13 +00:00
|
|
|
s += *content++;
|
2019-07-12 10:29:35 +00:00
|
|
|
data.mStrings.push_back(s);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipSpacesAndLineEnd(&content);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2019-07-12 10:29:35 +00:00
|
|
|
data.mValues.reserve(count);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
for (unsigned int a = 0; a < count; a++) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (*content == 0)
|
|
|
|
ThrowException("Expected more values while reading float_array contents.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2016-07-16 02:14:36 +00:00
|
|
|
ai_real value;
|
2015-05-19 03:57:13 +00:00
|
|
|
// read a number
|
2019-07-12 10:29:35 +00:00
|
|
|
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
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipSpacesAndLineEnd(&content);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
// test for closing tag
|
|
|
|
if (!isEmptyElement)
|
|
|
|
TestClosing(elmName.c_str());
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads an accessor and stores it in the global library
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadAccessor(const std::string &pID) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read accessor attributes
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrSource = GetAttribute("source");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *source = mReader->getAttributeValue(attrSource);
|
2019-07-12 10:29:35 +00:00
|
|
|
if (source[0] != '#')
|
|
|
|
ThrowException(format() << "Unknown reference format in url \"" << source << "\" in source attribute of <accessor> element.");
|
|
|
|
int attrCount = GetAttribute("count");
|
|
|
|
unsigned int count = (unsigned int)mReader->getAttributeValueAsInt(attrCount);
|
|
|
|
int attrOffset = TestAttribute("offset");
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int offset = 0;
|
2019-07-12 10:29:35 +00:00
|
|
|
if (attrOffset > -1)
|
|
|
|
offset = (unsigned int)mReader->getAttributeValueAsInt(attrOffset);
|
|
|
|
int attrStride = TestAttribute("stride");
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int stride = 1;
|
2019-07-12 10:29:35 +00:00
|
|
|
if (attrStride > -1)
|
|
|
|
stride = (unsigned int)mReader->getAttributeValueAsInt(attrStride);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// store in the library under the given ID
|
|
|
|
mAccessorLibrary[pID] = Accessor();
|
2020-04-28 16:46:07 +00:00
|
|
|
Accessor &acc = mAccessorLibrary[pID];
|
2015-05-19 03:57:13 +00:00
|
|
|
acc.mCount = count;
|
|
|
|
acc.mOffset = offset;
|
|
|
|
acc.mStride = stride;
|
2019-07-12 10:29:35 +00:00
|
|
|
acc.mSource = source + 1; // ignore the leading '#'
|
2015-05-19 03:57:13 +00:00
|
|
|
acc.mSize = 0; // gets incremented with every param
|
|
|
|
|
|
|
|
// and read the components
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("param")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read data param
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrName = TestAttribute("name");
|
2015-05-19 03:57:13 +00:00
|
|
|
std::string name;
|
2020-04-28 16:46:07 +00:00
|
|
|
if (attrName > -1) {
|
2019-07-12 10:29:35 +00:00
|
|
|
name = mReader->getAttributeValue(attrName);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// analyse for common type components and store it's sub-offset in the corresponding field
|
|
|
|
|
|
|
|
/* Cartesian coordinates */
|
2020-04-28 16:46:07 +00:00
|
|
|
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();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
/* RGBA colors */
|
2020-04-28 16:46:07 +00:00
|
|
|
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();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
/* UVWQ (STPQ) texture coordinates */
|
2020-04-28 16:46:07 +00:00
|
|
|
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();
|
2019-07-12 10:29:35 +00:00
|
|
|
// else if( name == "Q") acc.mSubOffset[3] = acc.mParams.size();
|
2020-04-28 16:46:07 +00:00
|
|
|
/* 4D uv coordinates are not supported in Assimp */
|
2019-07-12 10:29:35 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
/* 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();
|
2015-05-19 03:57:13 +00:00
|
|
|
//else
|
2016-04-05 22:03:05 +00:00
|
|
|
// DefaultLogger::get()->warn( format() << "Unknown accessor parameter \"" << name << "\". Ignoring data channel." );
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// read data type
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrType = TestAttribute("type");
|
2020-04-28 16:46:07 +00:00
|
|
|
if (attrType > -1) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// for the moment we only distinguish between a 4x4 matrix and anything else.
|
|
|
|
// 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.
|
2019-07-12 10:29:35 +00:00
|
|
|
std::string type = mReader->getAttributeValue(attrType);
|
|
|
|
if (type == "float4x4")
|
2015-05-19 03:57:13 +00:00
|
|
|
acc.mSize += 16;
|
|
|
|
else
|
|
|
|
acc.mSize += 1;
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
acc.mParams.push_back(name);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// skip remaining stuff of this element, if any
|
|
|
|
SkipElement();
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException(format() << "Unexpected sub element <" << mReader->getNodeName() << "> in tag <accessor>");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "accessor") != 0)
|
|
|
|
ThrowException("Expected end of <accessor> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads input declarations of per-vertex mesh data into the given mesh
|
2020-04-29 16:17:46 +00:00
|
|
|
void ColladaParser::ReadVertexData(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
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrID = GetAttribute("id");
|
2020-04-29 16:17:46 +00:00
|
|
|
pMesh.mVertexID = mReader->getAttributeValue(attrID);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// a number of <input> elements
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("input")) {
|
2020-04-29 16:17:46 +00:00
|
|
|
ReadInputChannel(pMesh.mPerVertexData);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException(format() << "Unexpected sub element <" << mReader->getNodeName() << "> in tag <vertices>");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "vertices") != 0)
|
|
|
|
ThrowException("Expected end of <vertices> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads input declarations of per-index mesh data into the given mesh
|
2020-04-29 16:17:46 +00:00
|
|
|
void ColladaParser::ReadIndexData(Mesh &pMesh) {
|
2015-05-19 03:57:13 +00:00
|
|
|
std::vector<size_t> vcount;
|
|
|
|
std::vector<InputChannel> perIndexData;
|
|
|
|
|
|
|
|
// read primitive count from the attribute
|
2019-07-12 10:29:35 +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;
|
|
|
|
|
|
|
|
// material subgroup
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrMaterial = TestAttribute("material");
|
2015-05-19 03:57:13 +00:00
|
|
|
SubMesh subgroup;
|
2019-07-12 10:29:35 +00:00
|
|
|
if (attrMaterial > -1)
|
|
|
|
subgroup.mMaterial = mReader->getAttributeValue(attrMaterial);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// distinguish between polys and triangles
|
|
|
|
std::string elementName = mReader->getNodeName();
|
|
|
|
PrimitiveType primType = Prim_Invalid;
|
2019-07-12 10:29:35 +00:00
|
|
|
if (IsElement("lines"))
|
2015-05-19 03:57:13 +00:00
|
|
|
primType = Prim_Lines;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("linestrips"))
|
2015-05-19 03:57:13 +00:00
|
|
|
primType = Prim_LineStrip;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("polygons"))
|
2015-05-19 03:57:13 +00:00
|
|
|
primType = Prim_Polygon;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("polylist"))
|
2015-05-19 03:57:13 +00:00
|
|
|
primType = Prim_Polylist;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("triangles"))
|
2015-05-19 03:57:13 +00:00
|
|
|
primType = Prim_Triangles;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("trifans"))
|
2015-05-19 03:57:13 +00:00
|
|
|
primType = Prim_TriFans;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (IsElement("tristrips"))
|
2015-05-19 03:57:13 +00:00
|
|
|
primType = Prim_TriStrips;
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
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
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("input")) {
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadInputChannel(perIndexData);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("vcount")) {
|
|
|
|
if (!mReader->isEmptyElement()) {
|
|
|
|
if (numPrimitives) // It is possible to define a mesh without any primitives
|
2015-05-19 03:57:13 +00:00
|
|
|
{
|
|
|
|
// case <polylist> - specifies the number of indices for each polygon
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *content = GetTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
vcount.reserve(numPrimitives);
|
2020-04-28 16:46:07 +00:00
|
|
|
for (unsigned int a = 0; a < numPrimitives; a++) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (*content == 0)
|
|
|
|
ThrowException("Expected more values while reading <vcount> contents.");
|
2015-05-19 03:57:13 +00:00
|
|
|
// read a number
|
2019-07-12 10:29:35 +00:00
|
|
|
vcount.push_back((size_t)strtoul10(content, &content));
|
2015-05-19 03:57:13 +00:00
|
|
|
// skip whitespace after it
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipSpacesAndLineEnd(&content);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("vcount");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("p")) {
|
|
|
|
if (!mReader->isEmptyElement()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// now here the actual fun starts - these are the indices to construct the mesh data from
|
|
|
|
actualPrimitives += ReadPrimitives(pMesh, perIndexData, numPrimitives, vcount, primType);
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("extra")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
SkipElement("extra");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("ph")) {
|
2017-07-11 08:42:10 +00:00
|
|
|
SkipElement("ph");
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException(format() << "Unexpected sub element <" << mReader->getNodeName() << "> in tag <" << elementName << ">");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeName() != elementName)
|
|
|
|
ThrowException(format() << "Expected end of <" << elementName << "> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
#ifdef ASSIMP_BUILD_DEBUG
|
2019-07-12 10:29:35 +00:00
|
|
|
if (primType != Prim_TriFans && primType != Prim_TriStrips && primType != Prim_LineStrip &&
|
2020-04-28 16:46:07 +00:00
|
|
|
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);
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
#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;
|
2020-04-29 16:17:46 +00:00
|
|
|
pMesh.mSubMeshes.push_back(subgroup);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Reads a single input channel element and stores it in the given array, if valid
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadInputChannel(std::vector<InputChannel> &poChannels) {
|
2015-05-19 03:57:13 +00:00
|
|
|
InputChannel channel;
|
|
|
|
|
|
|
|
// read semantic
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrSemantic = GetAttribute("semantic");
|
|
|
|
std::string semantic = mReader->getAttributeValue(attrSemantic);
|
|
|
|
channel.mType = GetTypeForSemantic(semantic);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// read source
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrSource = GetAttribute("source");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *source = mReader->getAttributeValue(attrSource);
|
2019-07-12 10:29:35 +00:00
|
|
|
if (source[0] != '#')
|
|
|
|
ThrowException(format() << "Unknown reference format in url \"" << source << "\" in source attribute of <input> element.");
|
|
|
|
channel.mAccessor = source + 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>
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrOffset = TestAttribute("offset");
|
|
|
|
if (attrOffset > -1)
|
|
|
|
channel.mOffset = mReader->getAttributeValueAsInt(attrOffset);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// read set if texture coordinates
|
2019-07-12 10:29:35 +00:00
|
|
|
if (channel.mType == IT_Texcoord || channel.mType == IT_Color) {
|
2015-05-19 03:57:13 +00:00
|
|
|
int attrSet = TestAttribute("set");
|
2019-07-12 10:29:35 +00:00
|
|
|
if (attrSet > -1) {
|
|
|
|
attrSet = mReader->getAttributeValueAsInt(attrSet);
|
|
|
|
if (attrSet < 0)
|
|
|
|
ThrowException(format() << "Invalid index \"" << (attrSet) << "\" in set attribute of <input> element");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
channel.mIndex = attrSet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// store, if valid type
|
2019-07-12 10:29:35 +00:00
|
|
|
if (channel.mType != IT_Invalid)
|
|
|
|
poChannels.push_back(channel);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// skip remaining stuff of this element, if any
|
|
|
|
SkipElement();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads a <p> primitive index list and assembles the mesh data into the given mesh
|
2020-04-29 16:17:46 +00:00
|
|
|
size_t ColladaParser::ReadPrimitives(Mesh &pMesh, std::vector<InputChannel> &pPerIndexChannels,
|
2020-04-28 16:46:07 +00:00
|
|
|
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
|
2020-04-28 16:46:07 +00:00
|
|
|
for (const InputChannel &channel : pPerIndexChannels) {
|
2019-07-12 10:29:35 +00:00
|
|
|
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;
|
2020-04-28 16:46:07 +00:00
|
|
|
switch (pPrimType) {
|
|
|
|
case Prim_Polylist: {
|
2019-07-12 10:29:35 +00:00
|
|
|
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;
|
2019-07-12 10:29:35 +00:00
|
|
|
if (expectedPointCount > 0)
|
|
|
|
indices.reserve(expectedPointCount * numOffsets);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2016-04-03 00:38:00 +00:00
|
|
|
if (pNumPrimitives > 0) // It is possible to not contain any indices
|
2015-05-19 03:57:13 +00:00
|
|
|
{
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *content = GetTextContent();
|
|
|
|
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.
|
2019-07-12 10:29:35 +00:00
|
|
|
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
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipSpacesAndLineEnd(&content);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
// complain if the index count doesn't fit
|
|
|
|
if (expectedPointCount > 0 && indices.size() != expectedPointCount * numOffsets) {
|
2015-07-27 02:16:01 +00:00
|
|
|
if (pPrimType == Prim_Lines) {
|
|
|
|
// HACK: We just fix this number since SketchUp 15.3.331 writes the wrong 'count' for 'lines'
|
2019-07-12 10:29:35 +00:00
|
|
|
ReportWarning("Expected different index count in <p> element, %zu instead of %zu.", indices.size(), expectedPointCount * numOffsets);
|
2015-07-27 02:16:01 +00:00
|
|
|
pNumPrimitives = (indices.size() / numOffsets) / 2;
|
2020-04-28 16:46:07 +00:00
|
|
|
} else
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException("Expected different index count in <p> element.");
|
2015-07-27 02:16:01 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (expectedPointCount == 0 && (indices.size() % numOffsets) != 0)
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException("Expected different index count in <p> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
// find the data for all sources
|
2020-04-29 16:17:46 +00:00
|
|
|
for (std::vector<InputChannel>::iterator it = pMesh.mPerVertexData.begin(); it != pMesh.mPerVertexData.end(); ++it) {
|
2020-04-28 16:46:07 +00:00
|
|
|
InputChannel &input = *it;
|
2019-07-12 10:29:35 +00:00
|
|
|
if (input.mResolved)
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// find accessor
|
2019-07-12 10:29:35 +00:00
|
|
|
input.mResolved = &ResolveLibraryReference(mAccessorLibrary, input.mAccessor);
|
2016-04-03 00:38:00 +00:00
|
|
|
// resolve accessor's data pointer as well, if necessary
|
2020-04-28 16:46:07 +00:00
|
|
|
const Accessor *acc = input.mResolved;
|
2019-07-12 10:29:35 +00:00
|
|
|
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
|
2020-04-28 16:46:07 +00:00
|
|
|
for (std::vector<InputChannel>::iterator it = pPerIndexChannels.begin(); it != pPerIndexChannels.end(); ++it) {
|
|
|
|
InputChannel &input = *it;
|
2019-07-12 10:29:35 +00:00
|
|
|
if (input.mResolved)
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// ignore vertex pointer, it doesn't refer to an accessor
|
2020-04-28 16:46:07 +00:00
|
|
|
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-04-29 16:17:46 +00:00
|
|
|
if (input.mAccessor != pMesh.mVertexID)
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException("Unsupported vertex referencing scheme.");
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find accessor
|
2019-07-12 10:29:35 +00:00
|
|
|
input.mResolved = &ResolveLibraryReference(mAccessorLibrary, input.mAccessor);
|
2016-04-03 00:38:00 +00:00
|
|
|
// resolve accessor's data pointer as well, if necessary
|
2020-04-28 16:46:07 +00:00
|
|
|
const Accessor *acc = input.mResolved;
|
2019-07-12 10:29:35 +00:00
|
|
|
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;
|
2019-07-12 10:29:35 +00:00
|
|
|
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
|
2019-07-12 10:29:35 +00:00
|
|
|
if (pPrimType == Prim_TriStrips) {
|
2015-05-19 03:57:13 +00:00
|
|
|
size_t numberOfVertices = indices.size() / numOffsets;
|
|
|
|
numPrimitives = numberOfVertices - 2;
|
|
|
|
}
|
2017-08-23 15:52:53 +00:00
|
|
|
if (pPrimType == Prim_LineStrip) {
|
|
|
|
size_t numberOfVertices = indices.size() / numOffsets;
|
|
|
|
numPrimitives = numberOfVertices - 1;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-29 16:17:46 +00:00
|
|
|
pMesh.mFaceSize.reserve(numPrimitives);
|
|
|
|
pMesh.mFacePosIndices.reserve(indices.size() / numOffsets);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
size_t polylistStartVertex = 0;
|
2020-04-28 16:46:07 +00:00
|
|
|
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;
|
2020-04-28 16:46:07 +00:00
|
|
|
switch (pPrimType) {
|
2019-07-12 10:29:35 +00:00
|
|
|
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
|
|
|
|
ThrowException("Unsupported primitive type.");
|
|
|
|
break;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// store the face size to later reconstruct the face from
|
2020-04-29 16:17:46 +00:00
|
|
|
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...
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing("p");
|
2015-05-19 03:57:13 +00:00
|
|
|
return numPrimitives;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 11:29:18 +00:00
|
|
|
///@note This function willn'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-04-29 16:17:46 +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
|
2017-11-02 10:13:52 +00:00
|
|
|
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
|
2020-04-29 16:17:46 +00:00
|
|
|
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);
|
|
|
|
// and extract per-index channels using there specified offset
|
|
|
|
for (std::vector<InputChannel>::iterator it = pPerIndexChannels.begin(); it != pPerIndexChannels.end(); ++it)
|
|
|
|
ExtractDataObjectFromChannel(*it, indices[baseOffset + it->mOffset], pMesh);
|
|
|
|
|
|
|
|
// store the vertex-data index for later assignment of bone vertex weights
|
2020-04-29 16:17:46 +00:00
|
|
|
pMesh.mFacePosIndices.push_back(indices[baseOffset + perVertexOffset]);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 16:17:46 +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) {
|
2019-07-12 10:29:35 +00:00
|
|
|
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);
|
2020-04-28 16:46:07 +00:00
|
|
|
} 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:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
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
|
2020-04-29 16:17:46 +00:00
|
|
|
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
|
2019-07-12 10:29:35 +00:00
|
|
|
if (pInput.mType == IT_Vertex)
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
const Accessor &acc = *pInput.mResolved;
|
2019-07-12 10:29:35 +00:00
|
|
|
if (pLocalIndex >= acc.mCount)
|
|
|
|
ThrowException(format() << "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
|
2020-04-28 16:46:07 +00:00
|
|
|
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
|
2016-07-16 02:14:36 +00:00
|
|
|
ai_real obj[4];
|
2019-07-12 10:29:35 +00:00
|
|
|
for (size_t c = 0; c < 4; ++c)
|
2015-05-19 03:57:13 +00:00
|
|
|
obj[c] = dataObject[acc.mSubOffset[c]];
|
|
|
|
|
|
|
|
// now we reinterpret it according to the type we're reading here
|
2020-04-28 16:46:07 +00:00
|
|
|
switch (pInput.mType) {
|
2019-07-12 10:29:35 +00:00
|
|
|
case IT_Position: // ignore all position streams except 0 - there can be only one position
|
|
|
|
if (pInput.mIndex == 0)
|
2020-04-29 16:17:46 +00:00
|
|
|
pMesh.mPositions.push_back(aiVector3D(obj[0], obj[1], obj[2]));
|
2019-07-12 10:29:35 +00:00
|
|
|
else
|
|
|
|
ASSIMP_LOG_ERROR("Collada: just one vertex position stream supported");
|
|
|
|
break;
|
|
|
|
case IT_Normal:
|
|
|
|
// pad to current vertex count if necessary
|
2020-04-29 16:17:46 +00:00
|
|
|
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));
|
2019-07-12 10:29:35 +00:00
|
|
|
|
|
|
|
// ignore all normal streams except 0 - there can be only one normal
|
|
|
|
if (pInput.mIndex == 0)
|
2020-04-29 16:17:46 +00:00
|
|
|
pMesh.mNormals.push_back(aiVector3D(obj[0], obj[1], obj[2]));
|
2019-07-12 10:29:35 +00:00
|
|
|
else
|
|
|
|
ASSIMP_LOG_ERROR("Collada: just one vertex normal stream supported");
|
|
|
|
break;
|
|
|
|
case IT_Tangent:
|
|
|
|
// pad to current vertex count if necessary
|
2020-04-29 16:17:46 +00:00
|
|
|
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));
|
2019-07-12 10:29:35 +00:00
|
|
|
|
|
|
|
// ignore all tangent streams except 0 - there can be only one tangent
|
|
|
|
if (pInput.mIndex == 0)
|
2020-04-29 16:17:46 +00:00
|
|
|
pMesh.mTangents.push_back(aiVector3D(obj[0], obj[1], obj[2]));
|
2019-07-12 10:29:35 +00:00
|
|
|
else
|
|
|
|
ASSIMP_LOG_ERROR("Collada: just one vertex tangent stream supported");
|
|
|
|
break;
|
|
|
|
case IT_Bitangent:
|
|
|
|
// pad to current vertex count if necessary
|
2020-04-29 16:17:46 +00:00
|
|
|
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));
|
2019-07-12 10:29:35 +00:00
|
|
|
|
|
|
|
// ignore all bitangent streams except 0 - there can be only one bitangent
|
|
|
|
if (pInput.mIndex == 0)
|
2020-04-29 16:17:46 +00:00
|
|
|
pMesh.mBitangents.push_back(aiVector3D(obj[0], obj[1], obj[2]));
|
2019-07-12 10:29:35 +00:00
|
|
|
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
|
2020-04-28 16:46:07 +00:00
|
|
|
if (pInput.mIndex < AI_MAX_NUMBER_OF_TEXTURECOORDS) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// pad to current vertex count if necessary
|
2020-04-29 16:17:46 +00:00
|
|
|
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));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-29 16:17:46 +00:00
|
|
|
pMesh.mTexCoords[pInput.mIndex].push_back(aiVector3D(obj[0], obj[1], obj[2]));
|
2019-07-12 10:29:35 +00:00
|
|
|
if (0 != acc.mSubOffset[2] || 0 != acc.mSubOffset[3]) /* hack ... consider cleaner solution */
|
2020-04-29 16:17:46 +00:00
|
|
|
pMesh.mNumUVComponents[pInput.mIndex] = 3;
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2019-07-12 10:29:35 +00:00
|
|
|
ASSIMP_LOG_ERROR("Collada: too many texture coordinate sets. Skipping.");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IT_Color:
|
|
|
|
// up to 4 color sets are fine, ignore the others
|
2020-04-28 16:46:07 +00:00
|
|
|
if (pInput.mIndex < AI_MAX_NUMBER_OF_COLOR_SETS) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// pad to current vertex count if necessary
|
2020-04-29 16:17:46 +00:00
|
|
|
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));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
aiColor4D result(0, 0, 0, 1);
|
2020-04-28 16:46:07 +00:00
|
|
|
for (size_t i = 0; i < pInput.mResolved->mSize; ++i) {
|
2019-07-12 10:29:35 +00:00
|
|
|
result[static_cast<unsigned int>(i)] = obj[pInput.mResolved->mSubOffset[i]];
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-04-29 16:17:46 +00:00
|
|
|
pMesh.mColors[pInput.mIndex].push_back(result);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2019-07-12 10:29:35 +00:00
|
|
|
ASSIMP_LOG_ERROR("Collada: too many vertex color sets. Skipping.");
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// IT_Invalid and IT_Vertex
|
|
|
|
ai_assert(false && "shouldn't ever get here");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the library of node hierarchies and scene parts
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadSceneLibrary() {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// a visual scene - generate root node under its ID and let ReadNode() do the recursive work
|
2020-04-28 16:46:07 +00:00
|
|
|
if (IsElement("visual_scene")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read ID. Is optional according to the spec, but how on earth should a scene_instance refer to it then?
|
2019-07-12 10:29:35 +00:00
|
|
|
int indexID = GetAttribute("id");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *attrID = mReader->getAttributeValue(indexID);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// read name if given.
|
2019-07-12 10:29:35 +00:00
|
|
|
int indexName = TestAttribute("name");
|
2020-05-04 16:47:09 +00:00
|
|
|
const char *attrName = "Scene";
|
2019-07-12 10:29:35 +00:00
|
|
|
if (indexName > -1)
|
|
|
|
attrName = mReader->getAttributeValue(indexName);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// create a node and store it in the library under its ID
|
2020-04-28 16:46:07 +00:00
|
|
|
Node *node = new Node;
|
2015-05-19 03:57:13 +00:00
|
|
|
node->mID = attrID;
|
|
|
|
node->mName = attrName;
|
|
|
|
mNodeLibrary[node->mID] = node;
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadSceneNode(node);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore the rest
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "library_visual_scenes") == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
//ThrowException( "Expected end of \"library_visual_scenes\" element.");
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
break;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads a scene node's contents including children and stores it in the given node
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadSceneNode(Node *pNode) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// quit immediately on <bla/> elements
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("node")) {
|
|
|
|
Node *child = new Node;
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrID = TestAttribute("id");
|
|
|
|
if (attrID > -1)
|
|
|
|
child->mID = mReader->getAttributeValue(attrID);
|
|
|
|
int attrSID = TestAttribute("sid");
|
|
|
|
if (attrSID > -1)
|
|
|
|
child->mSID = mReader->getAttributeValue(attrSID);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrName = TestAttribute("name");
|
|
|
|
if (attrName > -1)
|
|
|
|
child->mName = mReader->getAttributeValue(attrName);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// TODO: (thom) support SIDs
|
|
|
|
// ai_assert( TestAttribute( "sid") == -1);
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
if (pNode) {
|
2019-07-12 10:29:35 +00:00
|
|
|
pNode->mChildren.push_back(child);
|
2015-05-19 03:57:13 +00:00
|
|
|
child->mParent = pNode;
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// no parent node given, probably called from <library_nodes> element.
|
|
|
|
// create new node in node library
|
|
|
|
mNodeLibrary[child->mID] = child;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read on recursively from there
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadSceneNode(child);
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// For any further stuff we need a valid node to work on
|
|
|
|
else if (!pNode)
|
|
|
|
continue;
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
if (IsElement("lookat"))
|
|
|
|
ReadNodeTransformation(pNode, TF_LOOKAT);
|
|
|
|
else if (IsElement("matrix"))
|
|
|
|
ReadNodeTransformation(pNode, TF_MATRIX);
|
|
|
|
else if (IsElement("rotate"))
|
|
|
|
ReadNodeTransformation(pNode, TF_ROTATE);
|
|
|
|
else if (IsElement("scale"))
|
|
|
|
ReadNodeTransformation(pNode, TF_SCALE);
|
|
|
|
else if (IsElement("skew"))
|
|
|
|
ReadNodeTransformation(pNode, TF_SKEW);
|
|
|
|
else if (IsElement("translate"))
|
|
|
|
ReadNodeTransformation(pNode, TF_TRANSLATE);
|
2020-04-28 16:46:07 +00:00
|
|
|
else if (IsElement("render") && pNode->mParent == NULL && 0 == pNode->mPrimaryCamera.length()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ... 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.
|
|
|
|
int attrId = TestAttribute("camera_node");
|
2020-04-28 16:46:07 +00:00
|
|
|
if (-1 != attrId) {
|
|
|
|
const char *s = mReader->getAttributeValue(attrId);
|
2015-05-19 03:57:13 +00:00
|
|
|
if (s[0] != '#')
|
2018-04-19 14:48:43 +00:00
|
|
|
ASSIMP_LOG_ERROR("Collada: Unresolved reference format of camera");
|
2015-05-19 03:57:13 +00:00
|
|
|
else
|
2019-07-12 10:29:35 +00:00
|
|
|
pNode->mPrimaryCamera = s + 1;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("instance_node")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// find the node in the library
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrID = TestAttribute("url");
|
2020-04-28 16:46:07 +00:00
|
|
|
if (attrID != -1) {
|
|
|
|
const char *s = mReader->getAttributeValue(attrID);
|
2015-05-19 03:57:13 +00:00
|
|
|
if (s[0] != '#')
|
2018-04-19 14:48:43 +00:00
|
|
|
ASSIMP_LOG_ERROR("Collada: Unresolved reference format of node");
|
2020-04-28 16:46:07 +00:00
|
|
|
else {
|
2015-05-19 03:57:13 +00:00
|
|
|
pNode->mNodeInstances.push_back(NodeInstance());
|
2019-07-12 10:29:35 +00:00
|
|
|
pNode->mNodeInstances.back().mNode = s + 1;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("instance_geometry") || IsElement("instance_controller")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Reference to a mesh or controller, with possible material associations
|
2019-07-12 10:29:35 +00:00
|
|
|
ReadNodeGeometry(pNode);
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("instance_light")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Reference to a light, name given in 'url' attribute
|
|
|
|
int attrID = TestAttribute("url");
|
|
|
|
if (-1 == attrID)
|
2018-04-19 15:21:21 +00:00
|
|
|
ASSIMP_LOG_WARN("Collada: Expected url attribute in <instance_light> element");
|
2020-04-28 16:46:07 +00:00
|
|
|
else {
|
|
|
|
const char *url = mReader->getAttributeValue(attrID);
|
2019-07-12 10:29:35 +00:00
|
|
|
if (url[0] != '#')
|
|
|
|
ThrowException("Unknown reference format in <instance_light> element");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
pNode->mLights.push_back(LightInstance());
|
2019-07-12 10:29:35 +00:00
|
|
|
pNode->mLights.back().mLight = url + 1;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("instance_camera")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Reference to a camera, name given in 'url' attribute
|
|
|
|
int attrID = TestAttribute("url");
|
|
|
|
if (-1 == attrID)
|
2018-04-19 15:21:21 +00:00
|
|
|
ASSIMP_LOG_WARN("Collada: Expected url attribute in <instance_camera> element");
|
2020-04-28 16:46:07 +00:00
|
|
|
else {
|
|
|
|
const char *url = mReader->getAttributeValue(attrID);
|
2019-07-12 10:29:35 +00:00
|
|
|
if (url[0] != '#')
|
|
|
|
ThrowException("Unknown reference format in <instance_camera> element");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
pNode->mCameras.push_back(CameraInstance());
|
2019-07-12 10:29:35 +00:00
|
|
|
pNode->mCameras.back().mCamera = url + 1;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// skip everything else for the moment
|
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads a node transformation entry of the given type and adds it to the given node's transformation list.
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadNodeTransformation(Node *pNode, TransformType pType) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
std::string tagName = mReader->getNodeName();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
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
|
2019-07-12 10:29:35 +00:00
|
|
|
int indexSID = TestAttribute("sid");
|
|
|
|
if (indexSID >= 0)
|
|
|
|
tf.mID = mReader->getAttributeValue(indexSID);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
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-04-28 16:46:07 +00:00
|
|
|
const char *content = GetTextContent();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// read as many parameters and store in the transformation
|
2020-04-28 16:46:07 +00:00
|
|
|
for (unsigned int a = 0; a < sNumParameters[pType]; a++) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read a number
|
2019-07-12 10:29:35 +00:00
|
|
|
content = fast_atoreal_move<ai_real>(content, tf.f[a]);
|
2015-05-19 03:57:13 +00:00
|
|
|
// skip whitespace after it
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipSpacesAndLineEnd(&content);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// place the transformation at the queue of the node
|
2019-07-12 10:29:35 +00:00
|
|
|
pNode->mTransforms.push_back(tf);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// and consume the closing tag
|
2019-07-12 10:29:35 +00:00
|
|
|
TestClosing(tagName.c_str());
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Processes bind_vertex_input and bind elements
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadMaterialVertexInputBinding(Collada::SemanticMappingTable &tbl) {
|
|
|
|
while (mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2020-04-28 16:46:07 +00:00
|
|
|
if (IsElement("bind_vertex_input")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
Collada::InputSemanticMapEntry vn;
|
|
|
|
|
|
|
|
// effect semantic
|
|
|
|
int n = GetAttribute("semantic");
|
|
|
|
std::string s = mReader->getAttributeValue(n);
|
|
|
|
|
|
|
|
// input semantic
|
|
|
|
n = GetAttribute("input_semantic");
|
2019-07-12 10:29:35 +00:00
|
|
|
vn.mType = GetTypeForSemantic(mReader->getAttributeValue(n));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// index of input set
|
|
|
|
n = TestAttribute("input_set");
|
|
|
|
if (-1 != n)
|
|
|
|
vn.mSet = mReader->getAttributeValueAsInt(n);
|
|
|
|
|
|
|
|
tbl.mMap[s] = vn;
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (IsElement("bind")) {
|
2018-04-19 15:21:21 +00:00
|
|
|
ASSIMP_LOG_WARN("Collada: Found unsupported <bind> element");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (strcmp(mReader->getNodeName(), "instance_material") == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadEmbeddedTextures(ZipArchiveIOSystem &zip_archive) {
|
2019-07-12 13:46:48 +00:00
|
|
|
// 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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads a mesh reference in a node and adds it to the node's mesh list
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadNodeGeometry(Node *pNode) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// referred mesh is given as an attribute of the <instance_geometry> element
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrUrl = GetAttribute("url");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *url = mReader->getAttributeValue(attrUrl);
|
2019-07-12 10:29:35 +00:00
|
|
|
if (url[0] != '#')
|
|
|
|
ThrowException("Unknown reference format");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
Collada::MeshInstance instance;
|
2019-07-12 10:29:35 +00:00
|
|
|
instance.mMeshOrController = url + 1; // skipping the leading #
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
if (!mReader->isEmptyElement()) {
|
2016-12-08 02:31:51 +00:00
|
|
|
// read material associations. Ignore additional elements in between
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
if (IsElement("instance_material")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read ID of the geometry subgroup and the target material
|
2019-07-12 10:29:35 +00:00
|
|
|
int attrGroup = GetAttribute("symbol");
|
|
|
|
std::string group = mReader->getAttributeValue(attrGroup);
|
|
|
|
int attrMaterial = GetAttribute("target");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *urlMat = mReader->getAttributeValue(attrMaterial);
|
2015-05-19 03:57:13 +00:00
|
|
|
Collada::SemanticMappingTable s;
|
2019-07-12 10:29:35 +00:00
|
|
|
if (urlMat[0] == '#')
|
2015-05-19 03:57:13 +00:00
|
|
|
urlMat++;
|
|
|
|
|
|
|
|
s.mMatName = urlMat;
|
|
|
|
|
|
|
|
// resolve further material details + THIS UGLY AND NASTY semantic mapping stuff
|
2019-07-12 10:29:35 +00:00
|
|
|
if (!mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadMaterialVertexInputBinding(s);
|
|
|
|
|
|
|
|
// store the association
|
|
|
|
instance.mMaterials[group] = s;
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
|
|
|
if (strcmp(mReader->getNodeName(), "instance_geometry") == 0 || strcmp(mReader->getNodeName(), "instance_controller") == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// store it
|
2019-07-12 10:29:35 +00:00
|
|
|
pNode->mMeshes.push_back(instance);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the collada scene
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReadScene() {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
while (mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
2020-04-28 16:46:07 +00:00
|
|
|
if (IsElement("instance_visual_scene")) {
|
2016-04-03 00:38:00 +00:00
|
|
|
// should be the first and only occurrence
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mRootNode)
|
|
|
|
ThrowException("Invalid scene containing multiple root nodes in <instance_visual_scene> element");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// read the url of the scene to instance. Should be of format "#some_name"
|
2019-07-12 10:29:35 +00:00
|
|
|
int urlIndex = GetAttribute("url");
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *url = mReader->getAttributeValue(urlIndex);
|
2019-07-12 10:29:35 +00:00
|
|
|
if (url[0] != '#')
|
|
|
|
ThrowException("Unknown reference format in <instance_visual_scene> element");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// find the referred scene, skip the leading #
|
2019-07-12 10:29:35 +00:00
|
|
|
NodeLibrary::const_iterator sit = mNodeLibrary.find(url + 1);
|
|
|
|
if (sit == mNodeLibrary.end())
|
|
|
|
ThrowException("Unable to resolve visual_scene reference \"" + std::string(url) + "\" in <instance_visual_scene> element.");
|
2015-05-19 03:57:13 +00:00
|
|
|
mRootNode = sit->second;
|
2020-04-28 16:46:07 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
SkipElement();
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
} else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Aborts the file reading with an exception
|
2020-04-28 16:46:07 +00:00
|
|
|
AI_WONT_RETURN void ColladaParser::ThrowException(const std::string &pError) const {
|
2019-07-12 10:29:35 +00:00
|
|
|
throw DeadlyImportError(format() << "Collada: " << mFileName << " - " << pError);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2019-10-31 19:18:39 +00:00
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::ReportWarning(const char *msg, ...) {
|
2019-10-31 19:18:39 +00:00
|
|
|
ai_assert(nullptr != msg);
|
2016-04-05 20:53:54 +00:00
|
|
|
|
2015-07-27 02:16:01 +00:00
|
|
|
va_list args;
|
2019-07-12 10:29:35 +00:00
|
|
|
va_start(args, msg);
|
2016-04-05 20:53:54 +00:00
|
|
|
|
2015-07-27 02:16:01 +00:00
|
|
|
char szBuffer[3000];
|
2019-07-12 10:29:35 +00:00
|
|
|
const int iLen = vsprintf(szBuffer, msg, args);
|
2015-07-27 02:16:01 +00:00
|
|
|
ai_assert(iLen > 0);
|
2016-04-05 20:53:54 +00:00
|
|
|
|
2015-07-27 02:16:01 +00:00
|
|
|
va_end(args);
|
2019-07-12 10:29:35 +00:00
|
|
|
ASSIMP_LOG_WARN_F("Validation warning: ", std::string(szBuffer, iLen));
|
2015-07-27 02:16:01 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Skips all data until the end node of the current element
|
2019-10-31 19:18:39 +00:00
|
|
|
void ColladaParser::SkipElement() {
|
2015-05-19 03:57:13 +00:00
|
|
|
// nothing to skip if it's an <element />
|
2019-10-31 19:18:39 +00:00
|
|
|
if (mReader->isEmptyElement()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
2019-10-31 19:18:39 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// reroute
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipElement(mReader->getNodeName());
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Skips all data until the end node of the given element
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::SkipElement(const char *pElement) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// copy the current node's name because it'a pointer to the reader's internal buffer,
|
|
|
|
// which is going to change with the upcoming parsing
|
|
|
|
std::string element = pElement;
|
2019-10-31 19:18:39 +00:00
|
|
|
while (mReader->read()) {
|
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
|
|
|
|
if (mReader->getNodeName() == element) {
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
2019-10-31 19:18:39 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Tests for an opening element of the given name, throws an exception if not found
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::TestOpening(const char *pName) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read element start
|
2019-10-31 19:18:39 +00:00
|
|
|
if (!mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException(format() << "Unexpected end of file while beginning of <" << pName << "> element.");
|
2019-10-31 19:18:39 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
// whitespace in front is ok, just read again if found
|
2019-10-31 19:18:39 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_TEXT) {
|
|
|
|
if (!mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException(format() << "Unexpected end of file while reading beginning of <" << pName << "> element.");
|
2019-10-31 19:18:39 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-10-31 19:18:39 +00:00
|
|
|
if (mReader->getNodeType() != irr::io::EXN_ELEMENT || strcmp(mReader->getNodeName(), pName) != 0) {
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException(format() << "Expected start of <" << pName << "> element.");
|
2019-10-31 19:18:39 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Tests for the closing tag of the given element, throws an exception if not found
|
2020-04-28 16:46:07 +00:00
|
|
|
void ColladaParser::TestClosing(const char *pName) {
|
2019-10-25 13:10:54 +00:00
|
|
|
// check if we have an empty (self-closing) element
|
2019-10-31 19:18:39 +00:00
|
|
|
if (mReader->isEmptyElement()) {
|
2019-10-25 13:10:54 +00:00
|
|
|
return;
|
2019-10-31 19:18:39 +00:00
|
|
|
}
|
2019-10-25 13:10:54 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// check if we're already on the closing tag and return right away
|
2019-10-31 19:18:39 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END && strcmp(mReader->getNodeName(), pName) == 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
2019-10-31 19:18:39 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// if not, read some more
|
2019-10-31 19:18:39 +00:00
|
|
|
if (!mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException(format() << "Unexpected end of file while reading end of <" << pName << "> element.");
|
2019-10-31 19:18:39 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
// whitespace in front is ok, just read again if found
|
2019-10-31 19:18:39 +00:00
|
|
|
if (mReader->getNodeType() == irr::io::EXN_TEXT) {
|
|
|
|
if (!mReader->read()) {
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException(format() << "Unexpected end of file while reading end of <" << pName << "> element.");
|
2019-10-31 19:18:39 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// but this has the be the closing tag, or we're lost
|
2019-10-31 19:18:39 +00:00
|
|
|
if (mReader->getNodeType() != irr::io::EXN_ELEMENT_END || strcmp(mReader->getNodeName(), pName) != 0) {
|
2019-07-12 10:29:35 +00:00
|
|
|
ThrowException(format() << "Expected end of <" << pName << "> element.");
|
2019-10-31 19:18:39 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns the index of the named attribute or -1 if not found. Does not throw, therefore useful for optional attributes
|
2020-04-28 16:46:07 +00:00
|
|
|
int ColladaParser::GetAttribute(const char *pAttr) const {
|
2019-07-12 10:29:35 +00:00
|
|
|
int index = TestAttribute(pAttr);
|
2020-03-01 12:15:45 +00:00
|
|
|
if (index == -1) {
|
|
|
|
ThrowException(format() << "Expected attribute \"" << pAttr << "\" for element <" << mReader->getNodeName() << ">.");
|
2019-10-31 19:18:39 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// attribute not found -> throw an exception
|
2020-03-01 12:15:45 +00:00
|
|
|
return index;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Tests the present element for the presence of one attribute, returns its index or throws an exception if not found
|
2020-04-28 16:46:07 +00:00
|
|
|
int ColladaParser::TestAttribute(const char *pAttr) const {
|
2019-07-12 10:29:35 +00:00
|
|
|
for (int a = 0; a < mReader->getAttributeCount(); a++)
|
|
|
|
if (strcmp(mReader->getAttributeName(a), pAttr) == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
return a;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
return -1;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the text contents of an element, throws an exception if not given. Skips leading whitespace.
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *ColladaParser::GetTextContent() {
|
|
|
|
const char *sz = TestTextContent();
|
2019-07-12 10:29:35 +00:00
|
|
|
if (!sz) {
|
|
|
|
ThrowException("Invalid contents in element \"n\".");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
return sz;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the text contents of an element, returns NULL if not given. Skips leading whitespace.
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *ColladaParser::TestTextContent() {
|
2015-05-19 03:57:13 +00:00
|
|
|
// present node should be the beginning of an element
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeType() != irr::io::EXN_ELEMENT || mReader->isEmptyElement())
|
2015-05-19 03:57:13 +00:00
|
|
|
return NULL;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// read contents of the element
|
2019-07-12 10:29:35 +00:00
|
|
|
if (!mReader->read())
|
2015-05-19 03:57:13 +00:00
|
|
|
return NULL;
|
2019-07-12 10:29:35 +00:00
|
|
|
if (mReader->getNodeType() != irr::io::EXN_TEXT && mReader->getNodeType() != irr::io::EXN_CDATA)
|
2015-05-19 03:57:13 +00:00
|
|
|
return NULL;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// skip leading whitespace
|
2020-04-28 16:46:07 +00:00
|
|
|
const char *text = mReader->getNodeData();
|
2019-07-12 10:29:35 +00:00
|
|
|
SkipSpacesAndLineEnd(&text);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
return text;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Calculates the resulting transformation fromm all the given transform steps
|
2020-04-28 16:46:07 +00:00
|
|
|
aiMatrix4x4 ColladaParser::CalculateResultTransform(const std::vector<Transform> &pTransforms) const {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiMatrix4x4 res;
|
|
|
|
|
2020-04-28 16:46:07 +00:00
|
|
|
for (std::vector<Transform>::const_iterator it = pTransforms.begin(); it != pTransforms.end(); ++it) {
|
|
|
|
const Transform &tf = *it;
|
|
|
|
switch (tf.mType) {
|
|
|
|
case TF_LOOKAT: {
|
2019-07-12 10:29:35 +00:00
|
|
|
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(
|
2020-04-28 16:46:07 +00:00
|
|
|
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);
|
2019-07-12 10:29:35 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
case TF_ROTATE: {
|
2019-07-12 10:29:35 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
case TF_TRANSLATE: {
|
2019-07-12 10:29:35 +00:00
|
|
|
aiMatrix4x4 trans;
|
|
|
|
aiMatrix4x4::Translation(aiVector3D(tf.f[0], tf.f[1], tf.f[2]), trans);
|
|
|
|
res *= trans;
|
|
|
|
break;
|
|
|
|
}
|
2020-04-28 16:46:07 +00:00
|
|
|
case TF_SCALE: {
|
2019-07-12 10:29:35 +00:00
|
|
|
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,
|
2020-04-28 16:46:07 +00:00
|
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
2019-07-12 10:29:35 +00:00
|
|
|
res *= scale;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TF_SKEW:
|
|
|
|
// TODO: (thom)
|
|
|
|
ai_assert(false);
|
|
|
|
break;
|
2020-04-28 16:46:07 +00:00
|
|
|
case TF_MATRIX: {
|
2019-07-12 10:29:35 +00:00
|
|
|
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],
|
2020-04-28 16:46:07 +00:00
|
|
|
tf.f[8], tf.f[9], tf.f[10], tf.f[11], tf.f[12], tf.f[13], tf.f[14], tf.f[15]);
|
2019-07-12 10:29:35 +00:00
|
|
|
res *= mat;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
ai_assert(false);
|
|
|
|
break;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Determines the input data type for the given semantic string
|
2020-04-28 16:46:07 +00:00
|
|
|
Collada::InputType ColladaParser::GetTypeForSemantic(const std::string &semantic) {
|
2019-07-12 10:29:35 +00:00
|
|
|
if (semantic.empty()) {
|
|
|
|
ASSIMP_LOG_WARN("Vertex input type is empty.");
|
2016-06-04 15:53:51 +00:00
|
|
|
return IT_Invalid;
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
if (semantic == "POSITION")
|
2015-05-19 03:57:13 +00:00
|
|
|
return IT_Position;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (semantic == "TEXCOORD")
|
2015-05-19 03:57:13 +00:00
|
|
|
return IT_Texcoord;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (semantic == "NORMAL")
|
2015-05-19 03:57:13 +00:00
|
|
|
return IT_Normal;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (semantic == "COLOR")
|
2015-05-19 03:57:13 +00:00
|
|
|
return IT_Color;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (semantic == "VERTEX")
|
2015-05-19 03:57:13 +00:00
|
|
|
return IT_Vertex;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (semantic == "BINORMAL" || semantic == "TEXBINORMAL")
|
2015-05-19 03:57:13 +00:00
|
|
|
return IT_Bitangent;
|
2019-07-12 10:29:35 +00:00
|
|
|
else if (semantic == "TANGENT" || semantic == "TEXTANGENT")
|
2015-05-19 03:57:13 +00:00
|
|
|
return IT_Tangent;
|
|
|
|
|
2019-07-12 10:29:35 +00:00
|
|
|
ASSIMP_LOG_WARN_F("Unknown vertex input type \"", semantic, "\". Ignoring.");
|
2015-05-19 03:57:13 +00:00
|
|
|
return IT_Invalid;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !! ASSIMP_BUILD_NO_DAE_IMPORTER
|