| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501 |
- /*
- ---------------------------------------------------------------------------
- Open Asset Import Library (assimp)
- ---------------------------------------------------------------------------
- Copyright (c) 2006-2019, assimp team
- All rights reserved.
- Redistribution and use of this software in source and binary forms,
- with or without modification, are permitted provided that the following
- conditions are met:
- * Redistributions of source code must retain the above
- copyright notice, this list of conditions and the
- following disclaimer.
- * Redistributions in binary form must reproduce the above
- copyright notice, this list of conditions and the
- following disclaimer in the documentation and/or other
- materials provided with the distribution.
- * 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.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- ---------------------------------------------------------------------------
- */
- /** @file ColladaParser.cpp
- * @brief Implementation of the Collada parser helper
- */
- #ifndef ASSIMP_BUILD_NO_COLLADA_IMPORTER
- #include <sstream>
- #include <stdarg.h>
- #include "ColladaParser.h"
- #include <assimp/commonMetaData.h>
- #include <assimp/fast_atof.h>
- #include <assimp/ParsingUtils.h>
- #include <assimp/StringUtils.h>
- #include <assimp/DefaultLogger.hpp>
- #include <assimp/IOSystem.hpp>
- #include <assimp/light.h>
- #include <assimp/TinyFormatter.h>
- #include <assimp/ZipArchiveIOSystem.h>
- #include <memory>
- using namespace Assimp;
- using namespace Assimp::Collada;
- using namespace Assimp::Formatter;
- // ------------------------------------------------------------------------------------------------
- // Constructor to be privately used by Importer
- 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
- {
- // validate io-handler instance
- if (nullptr == pIOHandler) {
- throw DeadlyImportError("IOSystem is NULL.");
- }
- 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"));
- }
- }
- else {
- // attempt to open the file directly
- daefile.reset(pIOHandler->Open(pFile));
- if (daefile.get() == nullptr) {
- throw DeadlyImportError("Failed to open file '" + pFile + "'.");
- }
- }
- // generate a XML reader for it
- std::unique_ptr<CIrrXML_IOStreamReader> mIOWrapper(new CIrrXML_IOStreamReader(daefile.get()));
- mReader = irr::io::createIrrXMLReader(mIOWrapper.get());
- if (!mReader) {
- ThrowException("Unable to read file, malformed XML");
- }
- // start reading
- ReadContents();
- // read embedded textures
- if (zip_archive && zip_archive->isOpen()) {
- ReadEmbeddedTextures(*zip_archive);
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Destructor, private as well
- ColladaParser::~ColladaParser()
- {
- delete mReader;
- for (NodeLibrary::iterator it = mNodeLibrary.begin(); it != mNodeLibrary.end(); ++it)
- delete it->second;
- for (MeshLibrary::iterator it = mMeshLibrary.begin(); it != mMeshLibrary.end(); ++it)
- delete it->second;
- }
- // ------------------------------------------------------------------------------------------------
- // Read a ZAE manifest and return the filename to attempt to open
- std::string ColladaParser::ReadZaeManifest(ZipArchiveIOSystem &zip_archive) {
- // Open the manifest
- std::unique_ptr<IOStream> manifestfile(zip_archive.Open("manifest.xml"));
- if (manifestfile == nullptr)
- {
- // No manifest, hope there is only one .DAE inside
- std::vector<std::string> file_list;
- zip_archive.getFileListExtension(file_list, "dae");
- if (file_list.empty())
- return std::string();
- return file_list.front();
- }
- std::unique_ptr<CIrrXML_IOStreamReader> mIOWrapper(new CIrrXML_IOStreamReader(manifestfile.get()));
- std::unique_ptr<irr::io::IrrXMLReader> manifest_reader(irr::io::createIrrXMLReader(mIOWrapper.get()));
- while (manifest_reader->read())
- {
- // find the manifest "dae_root" element
- if (manifest_reader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (::strcmp(manifest_reader->getNodeName(), "dae_root") == 0)
- {
- 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();
- const char* filepath = manifest_reader->getNodeData();
- if (filepath == nullptr)
- return std::string();
- aiString ai_str(filepath);
- UriDecodePath(ai_str);
- return std::string(ai_str.C_Str());
- }
- }
- }
- return std::string();
- }
- // ------------------------------------------------------------------------------------------------
- // Convert a path read from a collada file to the usual representation
- void ColladaParser::UriDecodePath(aiString& ss)
- {
- // TODO: collada spec, p 22. Handle URI correctly.
- // For the moment we're just stripping the file:// away to make it work.
- // Windows doesn't seem to be able to find stuff like
- // 'file://..\LWO\LWO2\MappingModes\earthSpherical.jpg'
- if (0 == strncmp(ss.data, "file://", 7))
- {
- ss.length -= 7;
- memmove(ss.data, ss.data + 7, ss.length);
- ss.data[ss.length] = '\0';
- }
- // Maxon Cinema Collada Export writes "file:///C:\andsoon" with three slashes...
- // I need to filter it without destroying linux paths starting with "/somewhere"
- #if defined( _MSC_VER )
- if (ss.data[0] == '/' && isalpha((unsigned char)ss.data[1]) && ss.data[2] == ':') {
- #else
- if (ss.data[0] == '/' && isalpha(ss.data[1]) && ss.data[2] == ':') {
- #endif
- --ss.length;
- ::memmove(ss.data, ss.data + 1, ss.length);
- ss.data[ss.length] = 0;
- }
- // find and convert all %xy special chars
- char* out = ss.data;
- for (const char* it = ss.data; it != ss.data + ss.length; /**/)
- {
- if (*it == '%' && (it + 3) < ss.data + ss.length)
- {
- // separate the number to avoid dragging in chars from behind into the parsing
- char mychar[3] = { it[1], it[2], 0 };
- size_t nbr = strtoul16(mychar);
- it += 3;
- *out++ = (char)(nbr & 0xFF);
- }
- else
- {
- *out++ = *it++;
- }
- }
- // adjust length and terminator of the shortened string
- *out = 0;
- ai_assert(out > ss.data);
- ss.length = static_cast<ai_uint32>(out - ss.data);
- }
- // ------------------------------------------------------------------------------------------------
- // Read bool from text contents of current element
- bool ColladaParser::ReadBoolFromTextContent()
- {
- const char* cur = GetTextContent();
- return (!ASSIMP_strincmp(cur, "true", 4) || '0' != *cur);
- }
- // ------------------------------------------------------------------------------------------------
- // Read float from text contents of current element
- ai_real ColladaParser::ReadFloatFromTextContent()
- {
- const char* cur = GetTextContent();
- return fast_atof(cur);
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the contents of the file
- void ColladaParser::ReadContents()
- {
- while (mReader->read())
- {
- // handle the root element "COLLADA"
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("COLLADA"))
- {
- // check for 'version' attribute
- const int attrib = TestAttribute("version");
- if (attrib != -1) {
- const char* version = mReader->getAttributeValue(attrib);
- // Store declared format version string
- mAssetMetaData.emplace(AI_METADATA_SOURCE_FORMAT_VERSION, version);
- if (!::strncmp(version, "1.5", 3)) {
- mFormat = FV_1_5_n;
- ASSIMP_LOG_DEBUG("Collada schema version is 1.5.n");
- }
- else if (!::strncmp(version, "1.4", 3)) {
- mFormat = FV_1_4_n;
- ASSIMP_LOG_DEBUG("Collada schema version is 1.4.n");
- }
- else if (!::strncmp(version, "1.3", 3)) {
- mFormat = FV_1_3_n;
- ASSIMP_LOG_DEBUG("Collada schema version is 1.3.n");
- }
- }
- ReadStructure();
- }
- else
- {
- ASSIMP_LOG_DEBUG_F("Ignoring global element <", mReader->getNodeName(), ">.");
- SkipElement();
- }
- }
- else
- {
- // skip everything else silently
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the structure of the file
- void ColladaParser::ReadStructure()
- {
- while (mReader->read())
- {
- // beginning of elements
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("asset"))
- ReadAssetInfo();
- else if (IsElement("library_animations"))
- ReadAnimationLibrary();
- else if (IsElement("library_animation_clips"))
- ReadAnimationClipLibrary();
- else if (IsElement("library_controllers"))
- ReadControllerLibrary();
- else if (IsElement("library_images"))
- ReadImageLibrary();
- else if (IsElement("library_materials"))
- ReadMaterialLibrary();
- else if (IsElement("library_effects"))
- ReadEffectLibrary();
- else if (IsElement("library_geometries"))
- ReadGeometryLibrary();
- else if (IsElement("library_visual_scenes"))
- ReadSceneLibrary();
- else if (IsElement("library_lights"))
- ReadLightLibrary();
- else if (IsElement("library_cameras"))
- ReadCameraLibrary();
- else if (IsElement("library_nodes"))
- ReadSceneNode(NULL); /* some hacking to reuse this piece of code */
- else if (IsElement("scene"))
- ReadScene();
- else
- SkipElement();
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- break;
- }
- }
- PostProcessRootAnimations();
- PostProcessControllers();
- }
- // ------------------------------------------------------------------------------------------------
- // Reads asset information such as coordinate system information and legal blah
- void ColladaParser::ReadAssetInfo()
- {
- if (mReader->isEmptyElement())
- return;
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("unit"))
- {
- // read unit data from the element's attributes
- const int attrIndex = TestAttribute("meter");
- if (attrIndex == -1) {
- mUnitSize = 1.f;
- }
- else {
- mUnitSize = mReader->getAttributeValueAsFloat(attrIndex);
- }
- // consume the trailing stuff
- if (!mReader->isEmptyElement())
- SkipElement();
- }
- else if (IsElement("up_axis"))
- {
- // read content, strip whitespace, compare
- const char* content = GetTextContent();
- if (strncmp(content, "X_UP", 4) == 0)
- mUpDirection = UP_X;
- else if (strncmp(content, "Z_UP", 4) == 0)
- mUpDirection = UP_Z;
- else
- mUpDirection = UP_Y;
- // check element end
- TestClosing("up_axis");
- }
- else if (IsElement("contributor"))
- {
- ReadContributorInfo();
- }
- else
- {
- ReadMetaDataItem(mAssetMetaData, GetColladaAssimpMetaKeys());
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "asset") != 0)
- ThrowException("Expected end of <asset> element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the contributor info
- void ColladaParser::ReadContributorInfo()
- {
- if (mReader->isEmptyElement())
- return;
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- ReadMetaDataItem(mAssetMetaData, GetColladaAssimpMetaKeys());
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "contributor") != 0)
- ThrowException("Expected end of <contributor> element.");
- break;
- }
- }
- }
- bool FindCommonKey(const char *collada_key, const MetaKeyPairVector &key_renaming, size_t &found_index) {
- for (size_t i = 0; i < key_renaming.size(); ++i) {
- if (strcmp(key_renaming[i].first, collada_key) == 0) {
- found_index = i;
- return true;
- }
- }
- return false;
- }
- // ------------------------------------------------------------------------------------------------
- // Reads a single string metadata item
- void ColladaParser::ReadMetaDataItem(StringMetaData &metadata, const MetaKeyPairVector &key_renaming) {
- // Metadata such as created, keywords, subject etc
- const char *key_char = mReader->getNodeName();
- if (key_char != nullptr) {
- const std::string key_str(key_char);
- const char *value_char = TestTextContent();
- if (value_char != nullptr) {
- aiString aistr;
- aistr.Set(value_char);
- size_t found_index;
- if (FindCommonKey(key_str.c_str(), key_renaming, found_index)) {
- metadata.emplace(key_renaming[found_index].second, aistr);
- } else {
- std::string camel_key_str(key_str);
- ToCamelCase(camel_key_str);
- metadata.emplace(camel_key_str, aistr);
- }
- }
- TestClosing(key_str.c_str());
- }
- else
- SkipElement();
- }
- // ------------------------------------------------------------------------------------------------
- // Convert underscore_seperated to CamelCase: "authoring_tool" becomes "AuthoringTool"
- void ColladaParser::ToCamelCase(std::string &text)
- {
- if (text.empty())
- return;
- // Capitalise first character
- text[0] = ToUpper(text[0]);
- for (auto it = text.begin(); it != text.end(); /*iterated below*/)
- {
- if ((*it) == '_')
- {
- it = text.erase(it);
- if (it != text.end())
- (*it) = ToUpper(*it);
- }
- else
- ++it;
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the animation clips
- void ColladaParser::ReadAnimationClipLibrary()
- {
- if (mReader->isEmptyElement())
- return;
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("animation_clip"))
- {
- // 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());
- std::pair<std::string, std::vector<std::string> > clip;
- clip.first = animName;
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("instance_animation"))
- {
- int indexUrl = TestAttribute("url");
- if (indexUrl >= 0)
- {
- const char* url = mReader->getAttributeValue(indexUrl);
- if (url[0] != '#')
- ThrowException("Unknown reference format");
- url++;
- clip.second.push_back(url);
- }
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "animation_clip") != 0)
- ThrowException("Expected end of <animation_clip> element.");
- break;
- }
- }
- if (clip.second.size() > 0)
- {
- mAnimationClipLibrary.push_back(clip);
- }
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "library_animation_clips") != 0)
- ThrowException("Expected end of <library_animation_clips> element.");
- break;
- }
- }
- }
- void ColladaParser::PostProcessControllers()
- {
- std::string meshId;
- for (ControllerLibrary::iterator it = mControllerLibrary.begin(); it != mControllerLibrary.end(); ++it) {
- meshId = it->second.mMeshId;
- ControllerLibrary::iterator findItr = mControllerLibrary.find(meshId);
- while (findItr != mControllerLibrary.end()) {
- meshId = findItr->second.mMeshId;
- findItr = mControllerLibrary.find(meshId);
- }
- it->second.mMeshId = meshId;
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Re-build animations from animation clip library, if present, otherwise combine single-channel animations
- void ColladaParser::PostProcessRootAnimations()
- {
- if (mAnimationClipLibrary.size() > 0)
- {
- Animation temp;
- for (AnimationClipLibrary::iterator it = mAnimationClipLibrary.begin(); it != mAnimationClipLibrary.end(); ++it)
- {
- std::string clipName = it->first;
- Animation *clip = new Animation();
- clip->mName = clipName;
- temp.mSubAnims.push_back(clip);
- for (std::vector<std::string>::iterator a = it->second.begin(); a != it->second.end(); ++a)
- {
- std::string animationID = *a;
- AnimationLibrary::iterator animation = mAnimationLibrary.find(animationID);
- if (animation != mAnimationLibrary.end())
- {
- Animation *pSourceAnimation = animation->second;
- pSourceAnimation->CollectChannelsRecursively(clip->mChannels);
- }
- }
- }
- mAnims = temp;
- // Ensure no double deletes.
- temp.mSubAnims.clear();
- }
- else
- {
- mAnims.CombineSingleChannelAnimations();
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the animation library
- void ColladaParser::ReadAnimationLibrary()
- {
- if (mReader->isEmptyElement())
- return;
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("animation"))
- {
- // delegate the reading. Depending on the inner elements it will be a container or a anim channel
- ReadAnimation(&mAnims);
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "library_animations") != 0)
- ThrowException("Expected end of <library_animations> element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads an animation into the given parent structure
- void ColladaParser::ReadAnimation(Collada::Animation* pParent)
- {
- if (mReader->isEmptyElement())
- 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
- Animation* anim = NULL;
- // optional name given as an attribute
- std::string animName;
- std::string animID;
- int indexName = TestAttribute("name");
- int indexID = TestAttribute("id");
- if (indexID >= 0)
- animID = mReader->getAttributeValue(indexID);
- if (indexName >= 0)
- animName = mReader->getAttributeValue(indexName);
- else if (indexID >= 0)
- animName = animID;
- else
- animName = "animation";
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- // we have subanimations
- if (IsElement("animation"))
- {
- // create container from our element
- if (!anim)
- {
- anim = new Animation;
- anim->mName = animName;
- pParent->mSubAnims.push_back(anim);
- }
- // recurse into the subelement
- ReadAnimation(anim);
- }
- else if (IsElement("source"))
- {
- // possible animation data - we'll never know. Better store it
- ReadSource();
- }
- else if (IsElement("sampler"))
- {
- // read the ID to assign the corresponding collada channel afterwards.
- int indexID = GetAttribute("id");
- std::string id = mReader->getAttributeValue(indexID);
- ChannelMap::iterator newChannel = channels.insert(std::make_pair(id, AnimationChannel())).first;
- // have it read into a channel
- ReadAnimationSampler(newChannel->second);
- }
- else if (IsElement("channel"))
- {
- // 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.
- int indexTarget = GetAttribute("target");
- int indexSource = GetAttribute("source");
- const char* sourceId = mReader->getAttributeValue(indexSource);
- if (sourceId[0] == '#')
- sourceId++;
- ChannelMap::iterator cit = channels.find(sourceId);
- if (cit != channels.end())
- cit->second.mTarget = mReader->getAttributeValue(indexTarget);
- if (!mReader->isEmptyElement())
- SkipElement();
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "animation") != 0)
- ThrowException("Expected end of <animation> element.");
- break;
- }
- }
- // it turned out to have channels - add them
- if (!channels.empty())
- {
- // 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>.
- /*
- // special filtering for stupid exporters packing each channel into a separate animation
- if( channels.size() == 1)
- {
- pParent->mChannels.push_back( channels.begin()->second);
- } else
- */
- {
- // else create the animation, if not done yet, and store the channels
- if (!anim)
- {
- anim = new Animation;
- anim->mName = animName;
- pParent->mSubAnims.push_back(anim);
- }
- for (ChannelMap::const_iterator it = channels.begin(); it != channels.end(); ++it)
- anim->mChannels.push_back(it->second);
- if (indexID >= 0)
- {
- mAnimationLibrary[animID] = anim;
- }
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads an animation sampler into the given anim channel
- void ColladaParser::ReadAnimationSampler(Collada::AnimationChannel& pChannel)
- {
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("input"))
- {
- int indexSemantic = GetAttribute("semantic");
- const char* semantic = mReader->getAttributeValue(indexSemantic);
- int indexSource = GetAttribute("source");
- const char* source = mReader->getAttributeValue(indexSource);
- if (source[0] != '#')
- ThrowException("Unsupported URL format");
- source++;
- if (strcmp(semantic, "INPUT") == 0)
- pChannel.mSourceTimes = source;
- else if (strcmp(semantic, "OUTPUT") == 0)
- pChannel.mSourceValues = source;
- else if (strcmp(semantic, "IN_TANGENT") == 0)
- pChannel.mInTanValues = source;
- else if (strcmp(semantic, "OUT_TANGENT") == 0)
- pChannel.mOutTanValues = source;
- else if (strcmp(semantic, "INTERPOLATION") == 0)
- pChannel.mInterpolationValues = source;
- if (!mReader->isEmptyElement())
- SkipElement();
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "sampler") != 0)
- ThrowException("Expected end of <sampler> element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the skeleton controller library
- void ColladaParser::ReadControllerLibrary()
- {
- if (mReader->isEmptyElement())
- return;
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("controller"))
- {
- // read ID. Ask the spec if it's necessary or optional... you might be surprised.
- int attrID = GetAttribute("id");
- std::string id = mReader->getAttributeValue(attrID);
- // create an entry and store it in the library under its ID
- mControllerLibrary[id] = Controller();
- // read on from there
- ReadController(mControllerLibrary[id]);
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "library_controllers") != 0)
- ThrowException("Expected end of <library_controllers> element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads a controller into the given mesh structure
- void ColladaParser::ReadController(Collada::Controller& pController)
- {
- // initial values
- pController.mType = Skin;
- pController.mMethod = Normalized;
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- // two types of controllers: "skin" and "morph". Only the first one is relevant, we skip the other
- if (IsElement("morph"))
- {
- 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;
- }
- }
- else if (IsElement("skin"))
- {
- // 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
- int sourceIndex = GetAttribute("source");
- pController.mMeshId = mReader->getAttributeValue(sourceIndex) + 1;
- }
- else if (IsElement("bind_shape_matrix"))
- {
- // content is 16 floats to define a matrix... it seems to be important for some models
- const char* content = GetTextContent();
- // read the 16 floats
- for (unsigned int a = 0; a < 16; a++)
- {
- // read a number
- content = fast_atoreal_move<ai_real>(content, pController.mBindShapeMatrix[a]);
- // skip whitespace after it
- SkipSpacesAndLineEnd(&content);
- }
- TestClosing("bind_shape_matrix");
- }
- else if (IsElement("source"))
- {
- // data array - we have specialists to handle this
- ReadSource();
- }
- else if (IsElement("joints"))
- {
- ReadControllerJoints(pController);
- }
- else if (IsElement("vertex_weights"))
- {
- ReadControllerWeights(pController);
- }
- else if (IsElement("targets"))
- {
- while (mReader->read()) {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
- if (IsElement("input")) {
- 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;
- }
- else if (strcmp(semantics, "MORPH_WEIGHT") == 0)
- {
- pController.mMorphWeight = source + 1;
- }
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- if (strcmp(mReader->getNodeName(), "targets") == 0)
- break;
- else
- ThrowException("Expected end of <targets> element.");
- }
- }
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "controller") == 0)
- break;
- else if (strcmp(mReader->getNodeName(), "skin") != 0 && strcmp(mReader->getNodeName(), "morph") != 0)
- ThrowException("Expected end of <controller> element.");
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the joint definitions for the given controller
- void ColladaParser::ReadControllerJoints(Collada::Controller& pController)
- {
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- // Input channels for joint data. Two possible semantics: "JOINT" and "INV_BIND_MATRIX"
- if (IsElement("input"))
- {
- int indexSemantic = GetAttribute("semantic");
- const char* attrSemantic = mReader->getAttributeValue(indexSemantic);
- int indexSource = GetAttribute("source");
- const char* attrSource = mReader->getAttributeValue(indexSource);
- // local URLS always start with a '#'. We don't support global URLs
- if (attrSource[0] != '#')
- ThrowException(format() << "Unsupported URL format in \"" << attrSource << "\" in source attribute of <joints> data <input> element");
- attrSource++;
- // parse source URL to corresponding source
- if (strcmp(attrSemantic, "JOINT") == 0)
- pController.mJointNameSource = attrSource;
- else if (strcmp(attrSemantic, "INV_BIND_MATRIX") == 0)
- pController.mJointOffsetMatrixSource = attrSource;
- else
- ThrowException(format() << "Unknown semantic \"" << attrSemantic << "\" in <joints> data <input> element");
- // skip inner data, if present
- if (!mReader->isEmptyElement())
- SkipElement();
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "joints") != 0)
- ThrowException("Expected end of <joints> element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the joint weights for the given controller
- void ColladaParser::ReadControllerWeights(Collada::Controller& pController)
- {
- // read vertex count from attributes and resize the array accordingly
- int indexCount = GetAttribute("count");
- size_t vertexCount = (size_t)mReader->getAttributeValueAsInt(indexCount);
- pController.mWeightCounts.resize(vertexCount);
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- // Input channels for weight data. Two possible semantics: "JOINT" and "WEIGHT"
- if (IsElement("input") && vertexCount > 0)
- {
- InputChannel channel;
- int indexSemantic = GetAttribute("semantic");
- const char* attrSemantic = mReader->getAttributeValue(indexSemantic);
- int indexSource = GetAttribute("source");
- const char* attrSource = mReader->getAttributeValue(indexSource);
- int indexOffset = TestAttribute("offset");
- if (indexOffset >= 0)
- channel.mOffset = mReader->getAttributeValueAsInt(indexOffset);
- // local URLS always start with a '#'. We don't support global URLs
- if (attrSource[0] != '#')
- ThrowException(format() << "Unsupported URL format in \"" << attrSource << "\" in source attribute of <vertex_weights> data <input> element");
- channel.mAccessor = attrSource + 1;
- // parse source URL to corresponding source
- if (strcmp(attrSemantic, "JOINT") == 0)
- pController.mWeightInputJoints = channel;
- else if (strcmp(attrSemantic, "WEIGHT") == 0)
- pController.mWeightInputWeights = channel;
- else
- ThrowException(format() << "Unknown semantic \"" << attrSemantic << "\" in <vertex_weights> data <input> element");
- // skip inner data, if present
- if (!mReader->isEmptyElement())
- SkipElement();
- }
- else if (IsElement("vcount") && vertexCount > 0)
- {
- // read weight count per vertex
- const char* text = GetTextContent();
- size_t numWeights = 0;
- for (std::vector<size_t>::iterator it = pController.mWeightCounts.begin(); it != pController.mWeightCounts.end(); ++it)
- {
- if (*text == 0)
- ThrowException("Out of data while reading <vcount>");
- *it = strtoul10(text, &text);
- numWeights += *it;
- SkipSpacesAndLineEnd(&text);
- }
- TestClosing("vcount");
- // reserve weight count
- pController.mWeights.resize(numWeights);
- }
- else if (IsElement("v") && vertexCount > 0)
- {
- // read JointIndex - WeightIndex pairs
- const char* text = GetTextContent();
- for (std::vector< std::pair<size_t, size_t> >::iterator it = pController.mWeights.begin(); it != pController.mWeights.end(); ++it)
- {
- 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);
- }
- TestClosing("v");
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "vertex_weights") != 0)
- ThrowException("Expected end of <vertex_weights> element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the image library contents
- void ColladaParser::ReadImageLibrary()
- {
- if (mReader->isEmptyElement())
- return;
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
- if (IsElement("image"))
- {
- // read ID. Another entry which is "optional" by design but obligatory in reality
- int attrID = GetAttribute("id");
- std::string id = mReader->getAttributeValue(attrID);
- // create an entry and store it in the library under its ID
- mImageLibrary[id] = Image();
- // read on from there
- ReadImage(mImageLibrary[id]);
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- if (strcmp(mReader->getNodeName(), "library_images") != 0)
- ThrowException("Expected end of <library_images> element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads an image entry into the given image
- void ColladaParser::ReadImage(Collada::Image& pImage)
- {
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
- // Need to run different code paths here, depending on the Collada XSD version
- if (IsElement("image")) {
- SkipElement();
- }
- else if (IsElement("init_from"))
- {
- if (mFormat == FV_1_4_n)
- {
- // FIX: C4D exporter writes empty <init_from/> tags
- if (!mReader->isEmptyElement()) {
- // element content is filename - hopefully
- const char* sz = TestTextContent();
- if (sz)
- {
- aiString filepath(sz);
- UriDecodePath(filepath);
- pImage.mFileName = filepath.C_Str();
- }
- TestClosing("init_from");
- }
- if (!pImage.mFileName.length()) {
- pImage.mFileName = "unknown_texture";
- }
- }
- else if (mFormat == FV_1_5_n)
- {
- // 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) {
- ASSIMP_LOG_WARN("Collada: Ignoring texture array index");
- continue;
- }
- attrib = TestAttribute("mip_index");
- if (attrib != -1 && mReader->getAttributeValueAsInt(attrib) > 0) {
- ASSIMP_LOG_WARN("Collada: Ignoring MIP map layer");
- continue;
- }
- // TODO: correctly jump over cube and volume maps?
- }
- }
- else if (mFormat == FV_1_5_n)
- {
- if (IsElement("ref"))
- {
- // element content is filename - hopefully
- const char* sz = TestTextContent();
- if (sz)
- {
- aiString filepath(sz);
- UriDecodePath(filepath);
- pImage.mFileName = filepath.C_Str();
- }
- TestClosing("ref");
- }
- else if (IsElement("hex") && !pImage.mFileName.length())
- {
- // embedded image. get format
- const int attrib = TestAttribute("format");
- if (-1 == attrib)
- ASSIMP_LOG_WARN("Collada: Unknown image file format");
- else pImage.mEmbeddedFormat = mReader->getAttributeValue(attrib);
- const char* data = GetTextContent();
- // hexadecimal-encoded binary octets. First of all, find the
- // required buffer size to reserve enough storage.
- const char* cur = data;
- while (!IsSpaceOrNewLine(*cur)) cur++;
- const unsigned int size = (unsigned int)(cur - data) * 2;
- pImage.mImageData.resize(size);
- for (unsigned int i = 0; i < size; ++i)
- pImage.mImageData[i] = HexOctetToDecimal(data + (i << 1));
- TestClosing("hex");
- }
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- if (strcmp(mReader->getNodeName(), "image") == 0)
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the material library
- void ColladaParser::ReadMaterialLibrary()
- {
- if (mReader->isEmptyElement())
- return;
- std::map<std::string, int> names;
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("material"))
- {
- // read ID. By now you probably know my opinion about this "specification"
- int attrID = GetAttribute("id");
- std::string id = mReader->getAttributeValue(attrID);
- std::string name;
- int attrName = TestAttribute("name");
- if (attrName >= 0)
- name = mReader->getAttributeValue(attrName);
- // create an entry and store it in the library under its ID
- mMaterialLibrary[id] = Material();
- if (!name.empty())
- {
- std::map<std::string, int>::iterator it = names.find(name);
- if (it != names.end())
- {
- std::ostringstream strStream;
- strStream << ++it->second;
- name.append(" " + strStream.str());
- }
- else
- {
- names[name] = 0;
- }
- mMaterialLibrary[id].mName = name;
- }
- ReadMaterial(mMaterialLibrary[id]);
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "library_materials") != 0)
- ThrowException("Expected end of <library_materials> element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the light library
- void ColladaParser::ReadLightLibrary()
- {
- if (mReader->isEmptyElement())
- return;
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
- if (IsElement("light"))
- {
- // read ID. By now you probably know my opinion about this "specification"
- int attrID = GetAttribute("id");
- std::string id = mReader->getAttributeValue(attrID);
- // create an entry and store it in the library under its ID
- ReadLight(mLightLibrary[id] = Light());
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- if (strcmp(mReader->getNodeName(), "library_lights") != 0)
- ThrowException("Expected end of <library_lights> element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the camera library
- void ColladaParser::ReadCameraLibrary()
- {
- if (mReader->isEmptyElement())
- return;
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
- if (IsElement("camera"))
- {
- // read ID. By now you probably know my opinion about this "specification"
- int attrID = GetAttribute("id");
- std::string id = mReader->getAttributeValue(attrID);
- // create an entry and store it in the library under its ID
- Camera& cam = mCameraLibrary[id];
- attrID = TestAttribute("name");
- if (attrID != -1)
- cam.mName = mReader->getAttributeValue(attrID);
- ReadCamera(cam);
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- if (strcmp(mReader->getNodeName(), "library_cameras") != 0)
- ThrowException("Expected end of <library_cameras> element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads a material entry into the given material
- void ColladaParser::ReadMaterial(Collada::Material& pMaterial)
- {
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
- if (IsElement("material")) {
- SkipElement();
- }
- else if (IsElement("instance_effect"))
- {
- // referred effect by URL
- int attrUrl = GetAttribute("url");
- const char* url = mReader->getAttributeValue(attrUrl);
- if (url[0] != '#')
- ThrowException("Unknown reference format");
- pMaterial.mEffect = url + 1;
- SkipElement();
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- if (strcmp(mReader->getNodeName(), "material") != 0)
- ThrowException("Expected end of <material> element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads a light entry into the given light
- void ColladaParser::ReadLight(Collada::Light& pLight)
- {
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
- if (IsElement("light")) {
- SkipElement();
- }
- else if (IsElement("spot")) {
- pLight.mType = aiLightSource_SPOT;
- }
- else if (IsElement("ambient")) {
- pLight.mType = aiLightSource_AMBIENT;
- }
- else if (IsElement("directional")) {
- pLight.mType = aiLightSource_DIRECTIONAL;
- }
- else if (IsElement("point")) {
- pLight.mType = aiLightSource_POINT;
- }
- else if (IsElement("color")) {
- // text content contains 3 floats
- const char* content = GetTextContent();
- content = fast_atoreal_move<ai_real>(content, (ai_real&)pLight.mColor.r);
- SkipSpacesAndLineEnd(&content);
- content = fast_atoreal_move<ai_real>(content, (ai_real&)pLight.mColor.g);
- SkipSpacesAndLineEnd(&content);
- content = fast_atoreal_move<ai_real>(content, (ai_real&)pLight.mColor.b);
- SkipSpacesAndLineEnd(&content);
- TestClosing("color");
- }
- else if (IsElement("constant_attenuation")) {
- pLight.mAttConstant = ReadFloatFromTextContent();
- TestClosing("constant_attenuation");
- }
- else if (IsElement("linear_attenuation")) {
- pLight.mAttLinear = ReadFloatFromTextContent();
- TestClosing("linear_attenuation");
- }
- else if (IsElement("quadratic_attenuation")) {
- pLight.mAttQuadratic = ReadFloatFromTextContent();
- TestClosing("quadratic_attenuation");
- }
- else if (IsElement("falloff_angle")) {
- pLight.mFalloffAngle = ReadFloatFromTextContent();
- TestClosing("falloff_angle");
- }
- else if (IsElement("falloff_exponent")) {
- 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");
- }
- else if (IsElement("intensity")) {
- pLight.mIntensity = ReadFloatFromTextContent();
- TestClosing("intensity");
- }
- else if (IsElement("falloff")) {
- pLight.mOuterAngle = ReadFloatFromTextContent();
- TestClosing("falloff");
- }
- else if (IsElement("hotspot_beam")) {
- pLight.mFalloffAngle = ReadFloatFromTextContent();
- TestClosing("hotspot_beam");
- }
- // OpenCOLLADA extensions
- // -------------------------------------------------------
- else if (IsElement("decay_falloff")) {
- pLight.mOuterAngle = ReadFloatFromTextContent();
- TestClosing("decay_falloff");
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- if (strcmp(mReader->getNodeName(), "light") == 0)
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads a camera entry into the given light
- void ColladaParser::ReadCamera(Collada::Camera& pCamera)
- {
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
- if (IsElement("camera")) {
- SkipElement();
- }
- else if (IsElement("orthographic")) {
- pCamera.mOrtho = true;
- }
- else if (IsElement("xfov") || IsElement("xmag")) {
- pCamera.mHorFov = ReadFloatFromTextContent();
- TestClosing((pCamera.mOrtho ? "xmag" : "xfov"));
- }
- else if (IsElement("yfov") || IsElement("ymag")) {
- pCamera.mVerFov = ReadFloatFromTextContent();
- TestClosing((pCamera.mOrtho ? "ymag" : "yfov"));
- }
- else if (IsElement("aspect_ratio")) {
- pCamera.mAspect = ReadFloatFromTextContent();
- TestClosing("aspect_ratio");
- }
- else if (IsElement("znear")) {
- pCamera.mZNear = ReadFloatFromTextContent();
- TestClosing("znear");
- }
- else if (IsElement("zfar")) {
- pCamera.mZFar = ReadFloatFromTextContent();
- TestClosing("zfar");
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- if (strcmp(mReader->getNodeName(), "camera") == 0)
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the effect library
- void ColladaParser::ReadEffectLibrary()
- {
- if (mReader->isEmptyElement()) {
- return;
- }
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
- if (IsElement("effect"))
- {
- // read ID. Do I have to repeat my ranting about "optional" attributes?
- int attrID = GetAttribute("id");
- std::string id = mReader->getAttributeValue(attrID);
- // create an entry and store it in the library under its ID
- mEffectLibrary[id] = Effect();
- // read on from there
- ReadEffect(mEffectLibrary[id]);
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- if (strcmp(mReader->getNodeName(), "library_effects") != 0)
- ThrowException("Expected end of <library_effects> element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads an effect entry into the given effect
- void ColladaParser::ReadEffect(Collada::Effect& pEffect)
- {
- // for the moment we don't support any other type of effect.
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("profile_COMMON"))
- ReadEffectProfileCommon(pEffect);
- else
- SkipElement();
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "effect") != 0)
- ThrowException("Expected end of <effect> element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads an COMMON effect profile
- void ColladaParser::ReadEffectProfileCommon(Collada::Effect& pEffect)
- {
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("newparam")) {
- // save ID
- int attrSID = GetAttribute("sid");
- std::string sid = mReader->getAttributeValue(attrSID);
- pEffect.mParams[sid] = EffectParam();
- ReadEffectParam(pEffect.mParams[sid]);
- }
- else if (IsElement("technique") || IsElement("extra"))
- {
- // just syntactic sugar
- }
- else if (mFormat == FV_1_4_n && IsElement("image"))
- {
- // read ID. Another entry which is "optional" by design but obligatory in reality
- int attrID = GetAttribute("id");
- std::string id = mReader->getAttributeValue(attrID);
- // create an entry and store it in the library under its ID
- mImageLibrary[id] = Image();
- // read on from there
- ReadImage(mImageLibrary[id]);
- }
- /* Shading modes */
- else if (IsElement("phong"))
- pEffect.mShadeType = Shade_Phong;
- else if (IsElement("constant"))
- pEffect.mShadeType = Shade_Constant;
- else if (IsElement("lambert"))
- pEffect.mShadeType = Shade_Lambert;
- else if (IsElement("blinn"))
- pEffect.mShadeType = Shade_Blinn;
- /* Color + texture properties */
- 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);
- }
- else if (IsElement("transparent")) {
- pEffect.mHasTransparency = true;
- const char* opaque = mReader->getAttributeValueSafe("opaque");
- if (::strcmp(opaque, "RGB_ZERO") == 0 || ::strcmp(opaque, "RGB_ONE") == 0) {
- pEffect.mRGBTransparency = true;
- }
- // In RGB_ZERO mode, the transparency is interpreted in reverse, go figure...
- if (::strcmp(opaque, "RGB_ZERO") == 0 || ::strcmp(opaque, "A_ZERO") == 0) {
- pEffect.mInvertTransparency = true;
- }
- ReadEffectColor(pEffect.mTransparent, pEffect.mTexTransparent);
- }
- else if (IsElement("shininess"))
- ReadEffectFloat(pEffect.mShininess);
- else if (IsElement("reflectivity"))
- ReadEffectFloat(pEffect.mReflectivity);
- /* Single scalar properties */
- else if (IsElement("transparency"))
- ReadEffectFloat(pEffect.mTransparency);
- else if (IsElement("index_of_refraction"))
- ReadEffectFloat(pEffect.mRefractIndex);
- // GOOGLEEARTH/OKINO extensions
- // -------------------------------------------------------
- else if (IsElement("double_sided"))
- pEffect.mDoubleSided = ReadBoolFromTextContent();
- // FCOLLADA extensions
- // -------------------------------------------------------
- else if (IsElement("bump")) {
- aiColor4D dummy;
- ReadEffectColor(dummy, pEffect.mTexBump);
- }
- // MAX3D extensions
- // -------------------------------------------------------
- else if (IsElement("wireframe")) {
- pEffect.mWireframe = ReadBoolFromTextContent();
- TestClosing("wireframe");
- }
- else if (IsElement("faceted")) {
- pEffect.mFaceted = ReadBoolFromTextContent();
- TestClosing("faceted");
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- if (strcmp(mReader->getNodeName(), "profile_COMMON") == 0)
- {
- break;
- }
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Read texture wrapping + UV transform settings from a profile==Maya chunk
- void ColladaParser::ReadSamplerProperties(Sampler& out)
- {
- if (mReader->isEmptyElement()) {
- return;
- }
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
- // MAYA extensions
- // -------------------------------------------------------
- if (IsElement("wrapU")) {
- out.mWrapU = ReadBoolFromTextContent();
- TestClosing("wrapU");
- }
- else if (IsElement("wrapV")) {
- out.mWrapV = ReadBoolFromTextContent();
- TestClosing("wrapV");
- }
- else if (IsElement("mirrorU")) {
- out.mMirrorU = ReadBoolFromTextContent();
- TestClosing("mirrorU");
- }
- else if (IsElement("mirrorV")) {
- out.mMirrorV = ReadBoolFromTextContent();
- TestClosing("mirrorV");
- }
- else if (IsElement("repeatU")) {
- out.mTransform.mScaling.x = ReadFloatFromTextContent();
- TestClosing("repeatU");
- }
- else if (IsElement("repeatV")) {
- out.mTransform.mScaling.y = ReadFloatFromTextContent();
- TestClosing("repeatV");
- }
- else if (IsElement("offsetU")) {
- out.mTransform.mTranslation.x = ReadFloatFromTextContent();
- TestClosing("offsetU");
- }
- else if (IsElement("offsetV")) {
- out.mTransform.mTranslation.y = ReadFloatFromTextContent();
- TestClosing("offsetV");
- }
- else if (IsElement("rotateUV")) {
- out.mTransform.mRotation = ReadFloatFromTextContent();
- TestClosing("rotateUV");
- }
- else if (IsElement("blend_mode")) {
- const char* sz = GetTextContent();
- // http://www.feelingsoftware.com/content/view/55/72/lang,en/
- // NONE, OVER, IN, OUT, ADD, SUBTRACT, MULTIPLY, DIFFERENCE, LIGHTEN, DARKEN, SATURATE, DESATURATE and ILLUMINATE
- if (0 == ASSIMP_strincmp(sz, "ADD", 3))
- out.mOp = aiTextureOp_Add;
- else if (0 == ASSIMP_strincmp(sz, "SUBTRACT", 8))
- out.mOp = aiTextureOp_Subtract;
- else if (0 == ASSIMP_strincmp(sz, "MULTIPLY", 8))
- out.mOp = aiTextureOp_Multiply;
- else {
- ASSIMP_LOG_WARN("Collada: Unsupported MAYA texture blend mode");
- }
- TestClosing("blend_mode");
- }
- // OKINO extensions
- // -------------------------------------------------------
- else if (IsElement("weighting")) {
- out.mWeighting = ReadFloatFromTextContent();
- TestClosing("weighting");
- }
- else if (IsElement("mix_with_previous_layer")) {
- out.mMixWithPrevious = ReadFloatFromTextContent();
- TestClosing("mix_with_previous_layer");
- }
- // MAX3D extensions
- // -------------------------------------------------------
- else if (IsElement("amount")) {
- out.mWeighting = ReadFloatFromTextContent();
- TestClosing("amount");
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- if (strcmp(mReader->getNodeName(), "technique") == 0)
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads an effect entry containing a color or a texture defining that color
- void ColladaParser::ReadEffectColor(aiColor4D& pColor, Sampler& pSampler)
- {
- if (mReader->isEmptyElement())
- return;
- // Save current element name
- const std::string curElem = mReader->getNodeName();
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
- if (IsElement("color"))
- {
- // text content contains 4 floats
- const char* content = GetTextContent();
- content = fast_atoreal_move<ai_real>(content, (ai_real&)pColor.r);
- SkipSpacesAndLineEnd(&content);
- content = fast_atoreal_move<ai_real>(content, (ai_real&)pColor.g);
- SkipSpacesAndLineEnd(&content);
- content = fast_atoreal_move<ai_real>(content, (ai_real&)pColor.b);
- SkipSpacesAndLineEnd(&content);
- content = fast_atoreal_move<ai_real>(content, (ai_real&)pColor.a);
- SkipSpacesAndLineEnd(&content);
- TestClosing("color");
- }
- else if (IsElement("texture"))
- {
- // get name of source texture/sampler
- int attrTex = GetAttribute("texture");
- pSampler.mName = mReader->getAttributeValue(attrTex);
- // 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.
- attrTex = TestAttribute("texcoord");
- if (attrTex >= 0)
- pSampler.mUVChannel = mReader->getAttributeValue(attrTex);
- //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);
- }
- else if (IsElement("technique"))
- {
- const int _profile = GetAttribute("profile");
- const char* profile = mReader->getAttributeValue(_profile);
- // Some extensions are quite useful ... ReadSamplerProperties processes
- // several extensions in MAYA, OKINO and MAX3D profiles.
- if (!::strcmp(profile, "MAYA") || !::strcmp(profile, "MAX3D") || !::strcmp(profile, "OKINO"))
- {
- // get more information on this sampler
- ReadSamplerProperties(pSampler);
- }
- else SkipElement();
- }
- else if (!IsElement("extra"))
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- if (mReader->getNodeName() == curElem)
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads an effect entry containing a float
- void ColladaParser::ReadEffectFloat(ai_real& pFloat)
- {
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
- if (IsElement("float"))
- {
- // text content contains a single floats
- const char* content = GetTextContent();
- content = fast_atoreal_move<ai_real>(content, pFloat);
- SkipSpacesAndLineEnd(&content);
- TestClosing("float");
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads an effect parameter specification of any kind
- void ColladaParser::ReadEffectParam(Collada::EffectParam& pParam)
- {
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
- if (IsElement("surface"))
- {
- // image ID given inside <init_from> tags
- TestOpening("init_from");
- const char* content = GetTextContent();
- pParam.mType = Param_Surface;
- pParam.mReference = content;
- TestClosing("init_from");
- // don't care for remaining stuff
- SkipElement("surface");
- }
- else if (IsElement("sampler2D") && (FV_1_4_n == mFormat || FV_1_3_n == mFormat))
- {
- // surface ID is given inside <source> tags
- TestOpening("source");
- const char* content = GetTextContent();
- pParam.mType = Param_Sampler;
- pParam.mReference = content;
- TestClosing("source");
- // don't care for remaining stuff
- SkipElement("sampler2D");
- }
- else if (IsElement("sampler2D"))
- {
- // surface ID is given inside <instance_image> tags
- TestOpening("instance_image");
- int attrURL = GetAttribute("url");
- const char* url = mReader->getAttributeValue(attrURL);
- if (url[0] != '#')
- ThrowException("Unsupported URL format in instance_image");
- url++;
- pParam.mType = Param_Sampler;
- pParam.mReference = url;
- SkipElement("sampler2D");
- }
- else
- {
- // ignore unknown element
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the geometry library contents
- void ColladaParser::ReadGeometryLibrary()
- {
- if (mReader->isEmptyElement())
- return;
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("geometry"))
- {
- // read ID. Another entry which is "optional" by design but obligatory in reality
- int indexID = GetAttribute("id");
- std::string id = mReader->getAttributeValue(indexID);
- // TODO: (thom) support SIDs
- // ai_assert( TestAttribute( "sid") == -1);
- // create a mesh and store it in the library under its ID
- Mesh* mesh = new Mesh;
- mMeshLibrary[id] = mesh;
- // read the mesh name if it exists
- const int nameIndex = TestAttribute("name");
- if (nameIndex != -1)
- {
- mesh->mName = mReader->getAttributeValue(nameIndex);
- }
- // read on from there
- ReadGeometry(mesh);
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "library_geometries") != 0)
- ThrowException("Expected end of <library_geometries> element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads a geometry from the geometry library.
- void ColladaParser::ReadGeometry(Collada::Mesh* pMesh)
- {
- if (mReader->isEmptyElement())
- return;
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("mesh"))
- {
- // read on from there
- ReadMesh(pMesh);
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "geometry") != 0)
- ThrowException("Expected end of <geometry> element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads a mesh from the geometry library
- void ColladaParser::ReadMesh(Mesh* pMesh)
- {
- if (mReader->isEmptyElement())
- return;
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("source"))
- {
- // we have professionals dealing with this
- ReadSource();
- }
- else if (IsElement("vertices"))
- {
- // read per-vertex mesh data
- ReadVertexData(pMesh);
- }
- else if (IsElement("triangles") || IsElement("lines") || IsElement("linestrips")
- || IsElement("polygons") || IsElement("polylist") || IsElement("trifans") || IsElement("tristrips"))
- {
- // read per-index mesh data and faces setup
- ReadIndexData(pMesh);
- }
- else
- {
- // ignore the restf
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "technique_common") == 0)
- {
- // end of another meaningless element - read over it
- }
- else if (strcmp(mReader->getNodeName(), "mesh") == 0)
- {
- // end of <mesh> element - we're done here
- break;
- }
- else
- {
- // everything else should be punished
- ThrowException("Expected end of <mesh> element.");
- }
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads a source element
- void ColladaParser::ReadSource()
- {
- int indexID = GetAttribute("id");
- std::string sourceID = mReader->getAttributeValue(indexID);
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("float_array") || IsElement("IDREF_array") || IsElement("Name_array"))
- {
- ReadDataArray();
- }
- else if (IsElement("technique_common"))
- {
- // I don't care for your profiles
- }
- else if (IsElement("accessor"))
- {
- ReadAccessor(sourceID);
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "source") == 0)
- {
- // end of <source> - we're done
- break;
- }
- else if (strcmp(mReader->getNodeName(), "technique_common") == 0)
- {
- // end of another meaningless element - read over it
- }
- else
- {
- // everything else should be punished
- ThrowException("Expected end of <source> element.");
- }
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads a data array holding a number of floats, and stores it in the global library
- void ColladaParser::ReadDataArray()
- {
- std::string elmName = mReader->getNodeName();
- bool isStringArray = (elmName == "IDREF_array" || elmName == "Name_array");
- bool isEmptyElement = mReader->isEmptyElement();
- // read attributes
- int indexID = GetAttribute("id");
- std::string id = mReader->getAttributeValue(indexID);
- int indexCount = GetAttribute("count");
- unsigned int count = (unsigned int)mReader->getAttributeValueAsInt(indexCount);
- const char* content = TestTextContent();
- // read values and store inside an array in the data library
- mDataLibrary[id] = Data();
- Data& data = mDataLibrary[id];
- data.mIsStringArray = isStringArray;
- // some exporters write empty data arrays, but we need to conserve them anyways because others might reference them
- if (content)
- {
- if (isStringArray)
- {
- data.mStrings.reserve(count);
- std::string s;
- for (unsigned int a = 0; a < count; a++)
- {
- if (*content == 0)
- ThrowException("Expected more values while reading IDREF_array contents.");
- s.clear();
- while (!IsSpaceOrNewLine(*content))
- s += *content++;
- data.mStrings.push_back(s);
- SkipSpacesAndLineEnd(&content);
- }
- }
- else
- {
- data.mValues.reserve(count);
- for (unsigned int a = 0; a < count; a++)
- {
- if (*content == 0)
- ThrowException("Expected more values while reading float_array contents.");
- ai_real value;
- // read a number
- content = fast_atoreal_move<ai_real>(content, value);
- data.mValues.push_back(value);
- // skip whitespace after it
- SkipSpacesAndLineEnd(&content);
- }
- }
- }
- // test for closing tag
- if (!isEmptyElement)
- TestClosing(elmName.c_str());
- }
- // ------------------------------------------------------------------------------------------------
- // Reads an accessor and stores it in the global library
- void ColladaParser::ReadAccessor(const std::string& pID)
- {
- // read accessor attributes
- int attrSource = GetAttribute("source");
- const char* source = mReader->getAttributeValue(attrSource);
- 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");
- unsigned int offset = 0;
- if (attrOffset > -1)
- offset = (unsigned int)mReader->getAttributeValueAsInt(attrOffset);
- int attrStride = TestAttribute("stride");
- unsigned int stride = 1;
- if (attrStride > -1)
- stride = (unsigned int)mReader->getAttributeValueAsInt(attrStride);
- // store in the library under the given ID
- mAccessorLibrary[pID] = Accessor();
- Accessor& acc = mAccessorLibrary[pID];
- acc.mCount = count;
- acc.mOffset = offset;
- acc.mStride = stride;
- acc.mSource = source + 1; // ignore the leading '#'
- acc.mSize = 0; // gets incremented with every param
- // and read the components
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("param"))
- {
- // read data param
- int attrName = TestAttribute("name");
- std::string name;
- if (attrName > -1)
- {
- name = mReader->getAttributeValue(attrName);
- // analyse for common type components and store it's sub-offset in the corresponding field
- /* Cartesian coordinates */
- if (name == "X") acc.mSubOffset[0] = acc.mParams.size();
- else if (name == "Y") acc.mSubOffset[1] = acc.mParams.size();
- else if (name == "Z") acc.mSubOffset[2] = acc.mParams.size();
- /* RGBA colors */
- else if (name == "R") acc.mSubOffset[0] = acc.mParams.size();
- else if (name == "G") acc.mSubOffset[1] = acc.mParams.size();
- else if (name == "B") acc.mSubOffset[2] = acc.mParams.size();
- else if (name == "A") acc.mSubOffset[3] = acc.mParams.size();
- /* UVWQ (STPQ) texture coordinates */
- else if (name == "S") acc.mSubOffset[0] = acc.mParams.size();
- else if (name == "T") acc.mSubOffset[1] = acc.mParams.size();
- else if (name == "P") acc.mSubOffset[2] = acc.mParams.size();
- // else if( name == "Q") acc.mSubOffset[3] = acc.mParams.size();
- /* 4D uv coordinates are not supported in Assimp */
- /* 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();
- //else
- // DefaultLogger::get()->warn( format() << "Unknown accessor parameter \"" << name << "\". Ignoring data channel." );
- }
- // read data type
- int attrType = TestAttribute("type");
- if (attrType > -1)
- {
- // 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.
- std::string type = mReader->getAttributeValue(attrType);
- if (type == "float4x4")
- acc.mSize += 16;
- else
- acc.mSize += 1;
- }
- acc.mParams.push_back(name);
- // skip remaining stuff of this element, if any
- SkipElement();
- }
- else
- {
- ThrowException(format() << "Unexpected sub element <" << mReader->getNodeName() << "> in tag <accessor>");
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "accessor") != 0)
- ThrowException("Expected end of <accessor> element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads input declarations of per-vertex mesh data into the given mesh
- void ColladaParser::ReadVertexData(Mesh* pMesh)
- {
- // extract the ID of the <vertices> element. Not that we care, but to catch strange referencing schemes we should warn about
- int attrID = GetAttribute("id");
- pMesh->mVertexID = mReader->getAttributeValue(attrID);
- // a number of <input> elements
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("input"))
- {
- ReadInputChannel(pMesh->mPerVertexData);
- }
- else
- {
- ThrowException(format() << "Unexpected sub element <" << mReader->getNodeName() << "> in tag <vertices>");
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "vertices") != 0)
- ThrowException("Expected end of <vertices> element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads input declarations of per-index mesh data into the given mesh
- void ColladaParser::ReadIndexData(Mesh* pMesh)
- {
- std::vector<size_t> vcount;
- std::vector<InputChannel> perIndexData;
- // read primitive count from the attribute
- int attrCount = GetAttribute("count");
- size_t numPrimitives = (size_t)mReader->getAttributeValueAsInt(attrCount);
- // 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
- int attrMaterial = TestAttribute("material");
- SubMesh subgroup;
- if (attrMaterial > -1)
- subgroup.mMaterial = mReader->getAttributeValue(attrMaterial);
- // distinguish between polys and triangles
- std::string elementName = mReader->getNodeName();
- PrimitiveType primType = Prim_Invalid;
- if (IsElement("lines"))
- primType = Prim_Lines;
- else if (IsElement("linestrips"))
- primType = Prim_LineStrip;
- else if (IsElement("polygons"))
- primType = Prim_Polygon;
- else if (IsElement("polylist"))
- primType = Prim_Polylist;
- else if (IsElement("triangles"))
- primType = Prim_Triangles;
- else if (IsElement("trifans"))
- primType = Prim_TriFans;
- else if (IsElement("tristrips"))
- primType = Prim_TriStrips;
- ai_assert(primType != Prim_Invalid);
- // also a number of <input> elements, but in addition a <p> primitive collection and probably index counts for all primitives
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("input"))
- {
- ReadInputChannel(perIndexData);
- }
- else if (IsElement("vcount"))
- {
- if (!mReader->isEmptyElement())
- {
- if (numPrimitives) // It is possible to define a mesh without any primitives
- {
- // case <polylist> - specifies the number of indices for each polygon
- const char* content = GetTextContent();
- vcount.reserve(numPrimitives);
- for (unsigned int a = 0; a < numPrimitives; a++)
- {
- if (*content == 0)
- ThrowException("Expected more values while reading <vcount> contents.");
- // read a number
- vcount.push_back((size_t)strtoul10(content, &content));
- // skip whitespace after it
- SkipSpacesAndLineEnd(&content);
- }
- }
- TestClosing("vcount");
- }
- }
- else if (IsElement("p"))
- {
- if (!mReader->isEmptyElement())
- {
- // now here the actual fun starts - these are the indices to construct the mesh data from
- actualPrimitives += ReadPrimitives(pMesh, perIndexData, numPrimitives, vcount, primType);
- }
- }
- else if (IsElement("extra"))
- {
- SkipElement("extra");
- }
- else if (IsElement("ph")) {
- SkipElement("ph");
- }
- else {
- ThrowException(format() << "Unexpected sub element <" << mReader->getNodeName() << "> in tag <" << elementName << ">");
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (mReader->getNodeName() != elementName)
- ThrowException(format() << "Expected end of <" << elementName << "> element.");
- break;
- }
- }
- #ifdef ASSIMP_BUILD_DEBUG
- if (primType != Prim_TriFans && primType != Prim_TriStrips && primType != Prim_LineStrip &&
- primType != Prim_Lines) { // this is ONLY to workaround a bug in SketchUp 15.3.331 where it writes the wrong 'count' when it writes out the 'lines'.
- ai_assert(actualPrimitives == numPrimitives);
- }
- #endif
- // only when we're done reading all <p> tags (and thus know the final vertex count) can we commit the submesh
- subgroup.mNumFaces = actualPrimitives;
- pMesh->mSubMeshes.push_back(subgroup);
- }
- // ------------------------------------------------------------------------------------------------
- // Reads a single input channel element and stores it in the given array, if valid
- void ColladaParser::ReadInputChannel(std::vector<InputChannel>& poChannels)
- {
- InputChannel channel;
- // read semantic
- int attrSemantic = GetAttribute("semantic");
- std::string semantic = mReader->getAttributeValue(attrSemantic);
- channel.mType = GetTypeForSemantic(semantic);
- // read source
- int attrSource = GetAttribute("source");
- const char* source = mReader->getAttributeValue(attrSource);
- 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
- // read index offset, if per-index <input>
- int attrOffset = TestAttribute("offset");
- if (attrOffset > -1)
- channel.mOffset = mReader->getAttributeValueAsInt(attrOffset);
- // read set if texture coordinates
- if (channel.mType == IT_Texcoord || channel.mType == IT_Color) {
- int attrSet = TestAttribute("set");
- if (attrSet > -1) {
- attrSet = mReader->getAttributeValueAsInt(attrSet);
- if (attrSet < 0)
- ThrowException(format() << "Invalid index \"" << (attrSet) << "\" in set attribute of <input> element");
- channel.mIndex = attrSet;
- }
- }
- // store, if valid type
- if (channel.mType != IT_Invalid)
- poChannels.push_back(channel);
- // skip remaining stuff of this element, if any
- SkipElement();
- }
- // ------------------------------------------------------------------------------------------------
- // Reads a <p> primitive index list and assembles the mesh data into the given mesh
- size_t ColladaParser::ReadPrimitives(Mesh* pMesh, std::vector<InputChannel>& pPerIndexChannels,
- size_t pNumPrimitives, const std::vector<size_t>& pVCount, PrimitiveType pPrimType)
- {
- // determine number of indices coming per vertex
- // find the offset index for all per-vertex channels
- size_t numOffsets = 1;
- size_t perVertexOffset = SIZE_MAX; // invalid value
- for (const InputChannel& channel : pPerIndexChannels)
- {
- numOffsets = std::max(numOffsets, channel.mOffset + 1);
- if (channel.mType == IT_Vertex)
- perVertexOffset = channel.mOffset;
- }
- // determine the expected number of indices
- size_t expectedPointCount = 0;
- switch (pPrimType)
- {
- case Prim_Polylist:
- {
- for (size_t i : pVCount)
- expectedPointCount += i;
- break;
- }
- case Prim_Lines:
- expectedPointCount = 2 * pNumPrimitives;
- break;
- case Prim_Triangles:
- expectedPointCount = 3 * pNumPrimitives;
- break;
- default:
- // other primitive types don't state the index count upfront... we need to guess
- break;
- }
- // and read all indices into a temporary array
- std::vector<size_t> indices;
- if (expectedPointCount > 0)
- indices.reserve(expectedPointCount * numOffsets);
- if (pNumPrimitives > 0) // It is possible to not contain any indices
- {
- const char* content = GetTextContent();
- while (*content != 0)
- {
- // read a value.
- // Hack: (thom) Some exporters put negative indices sometimes. We just try to carry on anyways.
- int value = std::max(0, strtol10(content, &content));
- indices.push_back(size_t(value));
- // skip whitespace after it
- SkipSpacesAndLineEnd(&content);
- }
- }
- // complain if the index count doesn't fit
- if (expectedPointCount > 0 && indices.size() != expectedPointCount * numOffsets) {
- if (pPrimType == Prim_Lines) {
- // HACK: We just fix this number since SketchUp 15.3.331 writes the wrong 'count' for 'lines'
- ReportWarning("Expected different index count in <p> element, %zu instead of %zu.", indices.size(), expectedPointCount * numOffsets);
- pNumPrimitives = (indices.size() / numOffsets) / 2;
- }
- else
- ThrowException("Expected different index count in <p> element.");
- }
- else if (expectedPointCount == 0 && (indices.size() % numOffsets) != 0)
- ThrowException("Expected different index count in <p> element.");
- // find the data for all sources
- for (std::vector<InputChannel>::iterator it = pMesh->mPerVertexData.begin(); it != pMesh->mPerVertexData.end(); ++it)
- {
- InputChannel& input = *it;
- if (input.mResolved)
- continue;
- // find accessor
- input.mResolved = &ResolveLibraryReference(mAccessorLibrary, input.mAccessor);
- // resolve accessor's data pointer as well, if necessary
- const Accessor* acc = input.mResolved;
- if (!acc->mData)
- acc->mData = &ResolveLibraryReference(mDataLibrary, acc->mSource);
- }
- // and the same for the per-index channels
- for (std::vector<InputChannel>::iterator it = pPerIndexChannels.begin(); it != pPerIndexChannels.end(); ++it)
- {
- InputChannel& input = *it;
- if (input.mResolved)
- continue;
- // ignore vertex pointer, it doesn't refer to an accessor
- if (input.mType == IT_Vertex)
- {
- // warn if the vertex channel does not refer to the <vertices> element in the same mesh
- if (input.mAccessor != pMesh->mVertexID)
- ThrowException("Unsupported vertex referencing scheme.");
- continue;
- }
- // find accessor
- input.mResolved = &ResolveLibraryReference(mAccessorLibrary, input.mAccessor);
- // resolve accessor's data pointer as well, if necessary
- const Accessor* acc = input.mResolved;
- if (!acc->mData)
- acc->mData = &ResolveLibraryReference(mDataLibrary, acc->mSource);
- }
- // For continued primitives, the given count does not come all in one <p>, but only one primitive per <p>
- size_t numPrimitives = pNumPrimitives;
- if (pPrimType == Prim_TriFans || pPrimType == Prim_Polygon)
- numPrimitives = 1;
- // For continued primitives, the given count is actually the number of <p>'s inside the parent tag
- if (pPrimType == Prim_TriStrips) {
- size_t numberOfVertices = indices.size() / numOffsets;
- numPrimitives = numberOfVertices - 2;
- }
- if (pPrimType == Prim_LineStrip) {
- size_t numberOfVertices = indices.size() / numOffsets;
- numPrimitives = numberOfVertices - 1;
- }
- pMesh->mFaceSize.reserve(numPrimitives);
- pMesh->mFacePosIndices.reserve(indices.size() / numOffsets);
- size_t polylistStartVertex = 0;
- for (size_t currentPrimitive = 0; currentPrimitive < numPrimitives; currentPrimitive++)
- {
- // determine number of points for this primitive
- size_t numPoints = 0;
- switch (pPrimType)
- {
- case Prim_Lines:
- numPoints = 2;
- for (size_t currentVertex = 0; currentVertex < numPoints; currentVertex++)
- CopyVertex(currentVertex, numOffsets, numPoints, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
- break;
- case Prim_LineStrip:
- numPoints = 2;
- for (size_t currentVertex = 0; currentVertex < numPoints; currentVertex++)
- CopyVertex(currentVertex, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
- break;
- case Prim_Triangles:
- numPoints = 3;
- for (size_t currentVertex = 0; currentVertex < numPoints; currentVertex++)
- CopyVertex(currentVertex, numOffsets, numPoints, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
- break;
- case Prim_TriStrips:
- numPoints = 3;
- ReadPrimTriStrips(numOffsets, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
- break;
- case Prim_Polylist:
- numPoints = pVCount[currentPrimitive];
- for (size_t currentVertex = 0; currentVertex < numPoints; currentVertex++)
- CopyVertex(polylistStartVertex + currentVertex, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, 0, indices);
- polylistStartVertex += numPoints;
- break;
- case Prim_TriFans:
- case Prim_Polygon:
- numPoints = indices.size() / numOffsets;
- for (size_t currentVertex = 0; currentVertex < numPoints; currentVertex++)
- CopyVertex(currentVertex, numOffsets, numPoints, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
- break;
- default:
- // LineStrip is not supported due to expected index unmangling
- ThrowException("Unsupported primitive type.");
- break;
- }
- // store the face size to later reconstruct the face from
- pMesh->mFaceSize.push_back(numPoints);
- }
- // if I ever get my hands on that guy who invented this steaming pile of indirection...
- TestClosing("p");
- return numPrimitives;
- }
- ///@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
- 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) {
- // calculate the base offset of the vertex whose attributes we ant to copy
- size_t baseOffset = currentPrimitive * numOffsets * numPoints + currentVertex * numOffsets;
- // don't overrun the boundaries of the index list
- ai_assert((baseOffset + numOffsets - 1) < indices.size());
- // extract per-vertex channels using the global per-vertex offset
- for (std::vector<InputChannel>::iterator it = pMesh->mPerVertexData.begin(); it != pMesh->mPerVertexData.end(); ++it)
- 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
- pMesh->mFacePosIndices.push_back(indices[baseOffset + perVertexOffset]);
- }
- void ColladaParser::ReadPrimTriStrips(size_t numOffsets, size_t perVertexOffset, Mesh* pMesh, std::vector<InputChannel>& pPerIndexChannels, size_t currentPrimitive, const std::vector<size_t>& indices) {
- if (currentPrimitive % 2 != 0) {
- //odd tristrip triangles need their indices mangled, to preserve winding direction
- CopyVertex(1, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
- CopyVertex(0, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
- CopyVertex(2, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices);
- }
- else {//for non tristrips or even tristrip triangles
- 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);
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Extracts a single object from an input channel and stores it in the appropriate mesh data array
- void ColladaParser::ExtractDataObjectFromChannel(const InputChannel& pInput, size_t pLocalIndex, Mesh* pMesh)
- {
- // ignore vertex referrer - we handle them that separate
- if (pInput.mType == IT_Vertex)
- return;
- const Accessor& acc = *pInput.mResolved;
- if (pLocalIndex >= acc.mCount)
- ThrowException(format() << "Invalid data index (" << pLocalIndex << "/" << acc.mCount << ") in primitive specification");
- // get a pointer to the start of the data object referred to by the accessor and the local index
- const ai_real* dataObject = &(acc.mData->mValues[0]) + acc.mOffset + pLocalIndex * acc.mStride;
- // assemble according to the accessors component sub-offset list. We don't care, yet,
- // what kind of object exactly we're extracting here
- ai_real obj[4];
- for (size_t c = 0; c < 4; ++c)
- obj[c] = dataObject[acc.mSubOffset[c]];
- // now we reinterpret it according to the type we're reading here
- switch (pInput.mType)
- {
- case IT_Position: // ignore all position streams except 0 - there can be only one position
- if (pInput.mIndex == 0)
- pMesh->mPositions.push_back(aiVector3D(obj[0], obj[1], obj[2]));
- else
- ASSIMP_LOG_ERROR("Collada: just one vertex position stream supported");
- break;
- case IT_Normal:
- // pad to current vertex count if necessary
- if (pMesh->mNormals.size() < pMesh->mPositions.size() - 1)
- pMesh->mNormals.insert(pMesh->mNormals.end(), pMesh->mPositions.size() - pMesh->mNormals.size() - 1, aiVector3D(0, 1, 0));
- // ignore all normal streams except 0 - there can be only one normal
- if (pInput.mIndex == 0)
- pMesh->mNormals.push_back(aiVector3D(obj[0], obj[1], obj[2]));
- else
- ASSIMP_LOG_ERROR("Collada: just one vertex normal stream supported");
- break;
- case IT_Tangent:
- // pad to current vertex count if necessary
- if (pMesh->mTangents.size() < pMesh->mPositions.size() - 1)
- pMesh->mTangents.insert(pMesh->mTangents.end(), pMesh->mPositions.size() - pMesh->mTangents.size() - 1, aiVector3D(1, 0, 0));
- // ignore all tangent streams except 0 - there can be only one tangent
- if (pInput.mIndex == 0)
- pMesh->mTangents.push_back(aiVector3D(obj[0], obj[1], obj[2]));
- else
- ASSIMP_LOG_ERROR("Collada: just one vertex tangent stream supported");
- break;
- case IT_Bitangent:
- // pad to current vertex count if necessary
- if (pMesh->mBitangents.size() < pMesh->mPositions.size() - 1)
- pMesh->mBitangents.insert(pMesh->mBitangents.end(), pMesh->mPositions.size() - pMesh->mBitangents.size() - 1, aiVector3D(0, 0, 1));
- // ignore all bitangent streams except 0 - there can be only one bitangent
- if (pInput.mIndex == 0)
- pMesh->mBitangents.push_back(aiVector3D(obj[0], obj[1], obj[2]));
- else
- ASSIMP_LOG_ERROR("Collada: just one vertex bitangent stream supported");
- break;
- case IT_Texcoord:
- // up to 4 texture coord sets are fine, ignore the others
- if (pInput.mIndex < AI_MAX_NUMBER_OF_TEXTURECOORDS)
- {
- // pad to current vertex count if necessary
- if (pMesh->mTexCoords[pInput.mIndex].size() < pMesh->mPositions.size() - 1)
- pMesh->mTexCoords[pInput.mIndex].insert(pMesh->mTexCoords[pInput.mIndex].end(),
- pMesh->mPositions.size() - pMesh->mTexCoords[pInput.mIndex].size() - 1, aiVector3D(0, 0, 0));
- pMesh->mTexCoords[pInput.mIndex].push_back(aiVector3D(obj[0], obj[1], obj[2]));
- if (0 != acc.mSubOffset[2] || 0 != acc.mSubOffset[3]) /* hack ... consider cleaner solution */
- pMesh->mNumUVComponents[pInput.mIndex] = 3;
- }
- else
- {
- ASSIMP_LOG_ERROR("Collada: too many texture coordinate sets. Skipping.");
- }
- break;
- case IT_Color:
- // up to 4 color sets are fine, ignore the others
- if (pInput.mIndex < AI_MAX_NUMBER_OF_COLOR_SETS)
- {
- // pad to current vertex count if necessary
- if (pMesh->mColors[pInput.mIndex].size() < pMesh->mPositions.size() - 1)
- pMesh->mColors[pInput.mIndex].insert(pMesh->mColors[pInput.mIndex].end(),
- pMesh->mPositions.size() - pMesh->mColors[pInput.mIndex].size() - 1, aiColor4D(0, 0, 0, 1));
- aiColor4D result(0, 0, 0, 1);
- for (size_t i = 0; i < pInput.mResolved->mSize; ++i)
- {
- result[static_cast<unsigned int>(i)] = obj[pInput.mResolved->mSubOffset[i]];
- }
- pMesh->mColors[pInput.mIndex].push_back(result);
- }
- else
- {
- ASSIMP_LOG_ERROR("Collada: too many vertex color sets. Skipping.");
- }
- break;
- default:
- // IT_Invalid and IT_Vertex
- ai_assert(false && "shouldn't ever get here");
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the library of node hierarchies and scene parts
- void ColladaParser::ReadSceneLibrary()
- {
- if (mReader->isEmptyElement())
- return;
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- // a visual scene - generate root node under its ID and let ReadNode() do the recursive work
- if (IsElement("visual_scene"))
- {
- // read ID. Is optional according to the spec, but how on earth should a scene_instance refer to it then?
- int indexID = GetAttribute("id");
- const char* attrID = mReader->getAttributeValue(indexID);
- // read name if given.
- int indexName = TestAttribute("name");
- const char* attrName = "unnamed";
- if (indexName > -1)
- attrName = mReader->getAttributeValue(indexName);
- // create a node and store it in the library under its ID
- Node* node = new Node;
- node->mID = attrID;
- node->mName = attrName;
- mNodeLibrary[node->mID] = node;
- ReadSceneNode(node);
- }
- else
- {
- // ignore the rest
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "library_visual_scenes") == 0)
- //ThrowException( "Expected end of \"library_visual_scenes\" element.");
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads a scene node's contents including children and stores it in the given node
- void ColladaParser::ReadSceneNode(Node* pNode)
- {
- // quit immediately on <bla/> elements
- if (mReader->isEmptyElement())
- return;
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("node"))
- {
- Node* child = new Node;
- int attrID = TestAttribute("id");
- if (attrID > -1)
- child->mID = mReader->getAttributeValue(attrID);
- int attrSID = TestAttribute("sid");
- if (attrSID > -1)
- child->mSID = mReader->getAttributeValue(attrSID);
- int attrName = TestAttribute("name");
- if (attrName > -1)
- child->mName = mReader->getAttributeValue(attrName);
- // TODO: (thom) support SIDs
- // ai_assert( TestAttribute( "sid") == -1);
- if (pNode)
- {
- pNode->mChildren.push_back(child);
- child->mParent = pNode;
- }
- else
- {
- // no parent node given, probably called from <library_nodes> element.
- // create new node in node library
- mNodeLibrary[child->mID] = child;
- }
- // read on recursively from there
- ReadSceneNode(child);
- continue;
- }
- // For any further stuff we need a valid node to work on
- else if (!pNode)
- continue;
- 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);
- else if (IsElement("render") && pNode->mParent == NULL && 0 == pNode->mPrimaryCamera.length())
- {
- // ... scene evaluation or, in other words, postprocessing pipeline,
- // or, again in other words, a turing-complete description how to
- // render a Collada scene. The only thing that is interesting for
- // us is the primary camera.
- int attrId = TestAttribute("camera_node");
- if (-1 != attrId)
- {
- const char* s = mReader->getAttributeValue(attrId);
- if (s[0] != '#')
- ASSIMP_LOG_ERROR("Collada: Unresolved reference format of camera");
- else
- pNode->mPrimaryCamera = s + 1;
- }
- }
- else if (IsElement("instance_node"))
- {
- // find the node in the library
- int attrID = TestAttribute("url");
- if (attrID != -1)
- {
- const char* s = mReader->getAttributeValue(attrID);
- if (s[0] != '#')
- ASSIMP_LOG_ERROR("Collada: Unresolved reference format of node");
- else
- {
- pNode->mNodeInstances.push_back(NodeInstance());
- pNode->mNodeInstances.back().mNode = s + 1;
- }
- }
- }
- else if (IsElement("instance_geometry") || IsElement("instance_controller"))
- {
- // Reference to a mesh or controller, with possible material associations
- ReadNodeGeometry(pNode);
- }
- else if (IsElement("instance_light"))
- {
- // Reference to a light, name given in 'url' attribute
- int attrID = TestAttribute("url");
- if (-1 == attrID)
- ASSIMP_LOG_WARN("Collada: Expected url attribute in <instance_light> element");
- else
- {
- const char* url = mReader->getAttributeValue(attrID);
- if (url[0] != '#')
- ThrowException("Unknown reference format in <instance_light> element");
- pNode->mLights.push_back(LightInstance());
- pNode->mLights.back().mLight = url + 1;
- }
- }
- else if (IsElement("instance_camera"))
- {
- // Reference to a camera, name given in 'url' attribute
- int attrID = TestAttribute("url");
- if (-1 == attrID)
- ASSIMP_LOG_WARN("Collada: Expected url attribute in <instance_camera> element");
- else
- {
- const char* url = mReader->getAttributeValue(attrID);
- if (url[0] != '#')
- ThrowException("Unknown reference format in <instance_camera> element");
- pNode->mCameras.push_back(CameraInstance());
- pNode->mCameras.back().mCamera = url + 1;
- }
- }
- else
- {
- // skip everything else for the moment
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads a node transformation entry of the given type and adds it to the given node's transformation list.
- void ColladaParser::ReadNodeTransformation(Node* pNode, TransformType pType)
- {
- if (mReader->isEmptyElement())
- return;
- std::string tagName = mReader->getNodeName();
- Transform tf;
- tf.mType = pType;
- // read SID
- int indexSID = TestAttribute("sid");
- if (indexSID >= 0)
- tf.mID = mReader->getAttributeValue(indexSID);
- // how many parameters to read per transformation type
- static const unsigned int sNumParameters[] = { 9, 4, 3, 3, 7, 16 };
- const char* content = GetTextContent();
- // read as many parameters and store in the transformation
- for (unsigned int a = 0; a < sNumParameters[pType]; a++)
- {
- // read a number
- content = fast_atoreal_move<ai_real>(content, tf.f[a]);
- // skip whitespace after it
- SkipSpacesAndLineEnd(&content);
- }
- // place the transformation at the queue of the node
- pNode->mTransforms.push_back(tf);
- // and consume the closing tag
- TestClosing(tagName.c_str());
- }
- // ------------------------------------------------------------------------------------------------
- // Processes bind_vertex_input and bind elements
- void ColladaParser::ReadMaterialVertexInputBinding(Collada::SemanticMappingTable& tbl)
- {
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
- if (IsElement("bind_vertex_input"))
- {
- Collada::InputSemanticMapEntry vn;
- // effect semantic
- int n = GetAttribute("semantic");
- std::string s = mReader->getAttributeValue(n);
- // input semantic
- n = GetAttribute("input_semantic");
- vn.mType = GetTypeForSemantic(mReader->getAttributeValue(n));
- // index of input set
- n = TestAttribute("input_set");
- if (-1 != n)
- vn.mSet = mReader->getAttributeValueAsInt(n);
- tbl.mMap[s] = vn;
- }
- else if (IsElement("bind")) {
- ASSIMP_LOG_WARN("Collada: Found unsupported <bind> element");
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- if (strcmp(mReader->getNodeName(), "instance_material") == 0)
- break;
- }
- }
- }
- void ColladaParser::ReadEmbeddedTextures(ZipArchiveIOSystem& zip_archive)
- {
- // Attempt to load any undefined Collada::Image in ImageLibrary
- for (ImageLibrary::iterator it = mImageLibrary.begin(); it != mImageLibrary.end(); ++it) {
- Collada::Image &image = (*it).second;
- if (image.mImageData.empty()) {
- std::unique_ptr<IOStream> image_file(zip_archive.Open(image.mFileName.c_str()));
- if (image_file) {
- image.mImageData.resize(image_file->FileSize());
- image_file->Read(image.mImageData.data(), image_file->FileSize(), 1);
- image.mEmbeddedFormat = BaseImporter::GetExtension(image.mFileName);
- if (image.mEmbeddedFormat == "jpeg") {
- image.mEmbeddedFormat = "jpg";
- }
- }
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Reads a mesh reference in a node and adds it to the node's mesh list
- void ColladaParser::ReadNodeGeometry(Node* pNode)
- {
- // referred mesh is given as an attribute of the <instance_geometry> element
- int attrUrl = GetAttribute("url");
- const char* url = mReader->getAttributeValue(attrUrl);
- if (url[0] != '#')
- ThrowException("Unknown reference format");
- Collada::MeshInstance instance;
- instance.mMeshOrController = url + 1; // skipping the leading #
- if (!mReader->isEmptyElement())
- {
- // read material associations. Ignore additional elements in between
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT)
- {
- if (IsElement("instance_material"))
- {
- // read ID of the geometry subgroup and the target material
- int attrGroup = GetAttribute("symbol");
- std::string group = mReader->getAttributeValue(attrGroup);
- int attrMaterial = GetAttribute("target");
- const char* urlMat = mReader->getAttributeValue(attrMaterial);
- Collada::SemanticMappingTable s;
- if (urlMat[0] == '#')
- urlMat++;
- s.mMatName = urlMat;
- // resolve further material details + THIS UGLY AND NASTY semantic mapping stuff
- if (!mReader->isEmptyElement())
- ReadMaterialVertexInputBinding(s);
- // store the association
- instance.mMaterials[group] = s;
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
- {
- if (strcmp(mReader->getNodeName(), "instance_geometry") == 0
- || strcmp(mReader->getNodeName(), "instance_controller") == 0)
- break;
- }
- }
- }
- // store it
- pNode->mMeshes.push_back(instance);
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the collada scene
- void ColladaParser::ReadScene()
- {
- if (mReader->isEmptyElement())
- return;
- while (mReader->read())
- {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT) {
- if (IsElement("instance_visual_scene"))
- {
- // should be the first and only occurrence
- if (mRootNode)
- ThrowException("Invalid scene containing multiple root nodes in <instance_visual_scene> element");
- // read the url of the scene to instance. Should be of format "#some_name"
- int urlIndex = GetAttribute("url");
- const char* url = mReader->getAttributeValue(urlIndex);
- if (url[0] != '#')
- ThrowException("Unknown reference format in <instance_visual_scene> element");
- // find the referred scene, skip the leading #
- 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.");
- mRootNode = sit->second;
- }
- else {
- SkipElement();
- }
- }
- else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Aborts the file reading with an exception
- AI_WONT_RETURN void ColladaParser::ThrowException(const std::string& pError) const {
- throw DeadlyImportError(format() << "Collada: " << mFileName << " - " << pError);
- }
- void ColladaParser::ReportWarning(const char* msg, ...) {
- ai_assert(nullptr != msg);
- va_list args;
- va_start(args, msg);
- char szBuffer[3000];
- const int iLen = vsprintf(szBuffer, msg, args);
- ai_assert(iLen > 0);
- va_end(args);
- ASSIMP_LOG_WARN_F("Validation warning: ", std::string(szBuffer, iLen));
- }
- // ------------------------------------------------------------------------------------------------
- // Skips all data until the end node of the current element
- void ColladaParser::SkipElement() {
- // nothing to skip if it's an <element />
- if (mReader->isEmptyElement()) {
- return;
- }
- // reroute
- SkipElement(mReader->getNodeName());
- }
- // ------------------------------------------------------------------------------------------------
- // Skips all data until the end node of the given element
- void ColladaParser::SkipElement(const char* pElement) {
- // 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;
- while (mReader->read()) {
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) {
- if (mReader->getNodeName() == element) {
- break;
- }
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Tests for an opening element of the given name, throws an exception if not found
- void ColladaParser::TestOpening(const char* pName) {
- // read element start
- if (!mReader->read()) {
- ThrowException(format() << "Unexpected end of file while beginning of <" << pName << "> element.");
- }
- // whitespace in front is ok, just read again if found
- if (mReader->getNodeType() == irr::io::EXN_TEXT) {
- if (!mReader->read()) {
- ThrowException(format() << "Unexpected end of file while reading beginning of <" << pName << "> element.");
- }
- }
- if (mReader->getNodeType() != irr::io::EXN_ELEMENT || strcmp(mReader->getNodeName(), pName) != 0) {
- ThrowException(format() << "Expected start of <" << pName << "> element.");
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Tests for the closing tag of the given element, throws an exception if not found
- void ColladaParser::TestClosing(const char* pName) {
- // check if we have an empty (self-closing) element
- if (mReader->isEmptyElement()) {
- return;
- }
- // check if we're already on the closing tag and return right away
- if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END && strcmp(mReader->getNodeName(), pName) == 0) {
- return;
- }
- // if not, read some more
- if (!mReader->read()) {
- ThrowException(format() << "Unexpected end of file while reading end of <" << pName << "> element.");
- }
- // whitespace in front is ok, just read again if found
- if (mReader->getNodeType() == irr::io::EXN_TEXT) {
- if (!mReader->read()) {
- ThrowException(format() << "Unexpected end of file while reading end of <" << pName << "> element.");
- }
- }
- // but this has the be the closing tag, or we're lost
- if (mReader->getNodeType() != irr::io::EXN_ELEMENT_END || strcmp(mReader->getNodeName(), pName) != 0) {
- ThrowException(format() << "Expected end of <" << pName << "> element.");
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Returns the index of the named attribute or -1 if not found. Does not throw, therefore useful for optional attributes
- int ColladaParser::GetAttribute(const char* pAttr) const {
- int index = TestAttribute(pAttr);
- if (index != -1) {
- return index;
- }
- // attribute not found -> throw an exception
- ThrowException(format() << "Expected attribute \"" << pAttr << "\" for element <" << mReader->getNodeName() << ">.");
- return -1;
- }
- // ------------------------------------------------------------------------------------------------
- // Tests the present element for the presence of one attribute, returns its index or throws an exception if not found
- int ColladaParser::TestAttribute(const char* pAttr) const
- {
- for (int a = 0; a < mReader->getAttributeCount(); a++)
- if (strcmp(mReader->getAttributeName(a), pAttr) == 0)
- return a;
- return -1;
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the text contents of an element, throws an exception if not given. Skips leading whitespace.
- const char* ColladaParser::GetTextContent()
- {
- const char* sz = TestTextContent();
- if (!sz) {
- ThrowException("Invalid contents in element \"n\".");
- }
- return sz;
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the text contents of an element, returns NULL if not given. Skips leading whitespace.
- const char* ColladaParser::TestTextContent()
- {
- // present node should be the beginning of an element
- if (mReader->getNodeType() != irr::io::EXN_ELEMENT || mReader->isEmptyElement())
- return NULL;
- // read contents of the element
- if (!mReader->read())
- return NULL;
- if (mReader->getNodeType() != irr::io::EXN_TEXT && mReader->getNodeType() != irr::io::EXN_CDATA)
- return NULL;
- // skip leading whitespace
- const char* text = mReader->getNodeData();
- SkipSpacesAndLineEnd(&text);
- return text;
- }
- // ------------------------------------------------------------------------------------------------
- // Calculates the resulting transformation fromm all the given transform steps
- aiMatrix4x4 ColladaParser::CalculateResultTransform(const std::vector<Transform>& pTransforms) const
- {
- aiMatrix4x4 res;
- for (std::vector<Transform>::const_iterator it = pTransforms.begin(); it != pTransforms.end(); ++it)
- {
- const Transform& tf = *it;
- switch (tf.mType)
- {
- case TF_LOOKAT:
- {
- aiVector3D pos(tf.f[0], tf.f[1], tf.f[2]);
- aiVector3D dstPos(tf.f[3], tf.f[4], tf.f[5]);
- aiVector3D up = aiVector3D(tf.f[6], tf.f[7], tf.f[8]).Normalize();
- aiVector3D dir = aiVector3D(dstPos - pos).Normalize();
- aiVector3D right = (dir ^ up).Normalize();
- res *= aiMatrix4x4(
- right.x, up.x, -dir.x, pos.x,
- right.y, up.y, -dir.y, pos.y,
- right.z, up.z, -dir.z, pos.z,
- 0, 0, 0, 1);
- break;
- }
- case TF_ROTATE:
- {
- aiMatrix4x4 rot;
- ai_real angle = tf.f[3] * ai_real(AI_MATH_PI) / ai_real(180.0);
- aiVector3D axis(tf.f[0], tf.f[1], tf.f[2]);
- aiMatrix4x4::Rotation(angle, axis, rot);
- res *= rot;
- break;
- }
- case TF_TRANSLATE:
- {
- aiMatrix4x4 trans;
- aiMatrix4x4::Translation(aiVector3D(tf.f[0], tf.f[1], tf.f[2]), trans);
- res *= trans;
- break;
- }
- case TF_SCALE:
- {
- aiMatrix4x4 scale(tf.f[0], 0.0f, 0.0f, 0.0f, 0.0f, tf.f[1], 0.0f, 0.0f, 0.0f, 0.0f, tf.f[2], 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f);
- res *= scale;
- break;
- }
- case TF_SKEW:
- // TODO: (thom)
- ai_assert(false);
- break;
- case TF_MATRIX:
- {
- aiMatrix4x4 mat(tf.f[0], tf.f[1], tf.f[2], tf.f[3], tf.f[4], tf.f[5], tf.f[6], tf.f[7],
- tf.f[8], tf.f[9], tf.f[10], tf.f[11], tf.f[12], tf.f[13], tf.f[14], tf.f[15]);
- res *= mat;
- break;
- }
- default:
- ai_assert(false);
- break;
- }
- }
- return res;
- }
- // ------------------------------------------------------------------------------------------------
- // Determines the input data type for the given semantic string
- Collada::InputType ColladaParser::GetTypeForSemantic(const std::string& semantic)
- {
- if (semantic.empty()) {
- ASSIMP_LOG_WARN("Vertex input type is empty.");
- return IT_Invalid;
- }
- if (semantic == "POSITION")
- return IT_Position;
- else if (semantic == "TEXCOORD")
- return IT_Texcoord;
- else if (semantic == "NORMAL")
- return IT_Normal;
- else if (semantic == "COLOR")
- return IT_Color;
- else if (semantic == "VERTEX")
- return IT_Vertex;
- else if (semantic == "BINORMAL" || semantic == "TEXBINORMAL")
- return IT_Bitangent;
- else if (semantic == "TANGENT" || semantic == "TEXTANGENT")
- return IT_Tangent;
- ASSIMP_LOG_WARN_F("Unknown vertex input type \"", semantic, "\". Ignoring.");
- return IT_Invalid;
- }
- #endif // !! ASSIMP_BUILD_NO_DAE_IMPORTER
|