lldaeloader.cpp 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908
  1. /**
  2. * @file lldaeloader.cpp
  3. * @brief LLDAELoader class implementation
  4. *
  5. * $LicenseInfo:firstyear=2013&license=viewergpl$
  6. *
  7. * Copyright (c) 2013, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include <regex>
  34. #include "boost/algorithm/string/replace.hpp"
  35. #include "boost/lexical_cast.hpp"
  36. // gcc 13 does not like the colladom headers...
  37. #if defined(GCC_VERSION) && GCC_VERSION >= 130000
  38. # pragma GCC diagnostic ignored "-Woverloaded-virtual"
  39. #endif
  40. #include "dae.h"
  41. #include "dae/daeErrorHandler.h"
  42. #include "dom/domAsset.h"
  43. #include "dom/domBind_material.h"
  44. #include "dom/domCOLLADA.h"
  45. #include "dom/domConstants.h"
  46. #include "dom/domController.h"
  47. #include "dom/domEffect.h"
  48. #include "dom/domGeometry.h"
  49. #include "dom/domInstance_geometry.h"
  50. #include "dom/domInstance_material.h"
  51. #include "dom/domInstance_node.h"
  52. #include "dom/domInstance_effect.h"
  53. #include "dom/domMaterial.h"
  54. #include "dom/domMatrix.h"
  55. #include "dom/domNode.h"
  56. #include "dom/domProfile_COMMON.h"
  57. #include "dom/domRotate.h"
  58. #include "dom/domScale.h"
  59. #include "dom/domTranslate.h"
  60. #include "dom/domVisual_scene.h"
  61. #include "lldaeloader.h"
  62. #include "llmatrix4a.h"
  63. #include "lljoint.h"
  64. #include "llsdserialize.h"
  65. #include "llstring.h"
  66. #include "lluri.h"
  67. std::string colladaVersion[VERSIONTYPE_COUNT+1] =
  68. {
  69. "1.4.0",
  70. "1.4.1",
  71. "Unsupported"
  72. };
  73. static const std::string lod_suffix[LLModel::NUM_LODS] =
  74. {
  75. "_LOD0",
  76. "_LOD1",
  77. "_LOD2",
  78. "",
  79. "_PHYS",
  80. };
  81. constexpr U32 LIMIT_MATERIALS_OUTPUT = 12;
  82. //-----------------------------------------------------------------------------
  83. // DAE error logger
  84. //-----------------------------------------------------------------------------
  85. class LLDaeErrorHandler : public daeErrorHandler
  86. {
  87. protected:
  88. LOG_CLASS(LLDaeErrorHandler);
  89. public:
  90. virtual void handleError(daeString msg)
  91. {
  92. llwarns << "Error in DAE file: " << msg << llendl;
  93. }
  94. virtual void handleWarning(daeString msg)
  95. {
  96. llwarns << msg << llendl;
  97. }
  98. };
  99. LLDaeErrorHandler gDaeErrorHandler;
  100. class LLSetDaeErrorHandler
  101. {
  102. public:
  103. LLSetDaeErrorHandler()
  104. {
  105. daeErrorHandler::setErrorHandler(&gDaeErrorHandler);
  106. }
  107. ~LLSetDaeErrorHandler()
  108. {
  109. daeErrorHandler::setErrorHandler(NULL);
  110. }
  111. };
  112. //-----------------------------------------------------------------------------
  113. bool get_dom_sources(const domInputLocalOffset_Array& inputs, S32& pos_offset,
  114. S32& tc_offset, S32& norm_offset, S32& idx_stride,
  115. domSource*& pos_source, domSource*& tc_source,
  116. domSource*& norm_source)
  117. {
  118. idx_stride = 0;
  119. for (U32 j = 0; j < inputs.getCount(); ++j)
  120. {
  121. idx_stride = llmax((S32)inputs[j]->getOffset(), idx_stride);
  122. if (strcmp(COMMON_PROFILE_INPUT_VERTEX, inputs[j]->getSemantic()) == 0)
  123. {
  124. // Found vertex array
  125. const domURIFragmentType& uri = inputs[j]->getSource();
  126. daeElementRef elem = uri.getElement();
  127. domVertices* vertices = (domVertices*)elem.cast();
  128. if (!vertices)
  129. {
  130. return false;
  131. }
  132. domInputLocal_Array& v_inp = vertices->getInput_array();
  133. for (U32 k = 0; k < v_inp.getCount(); ++k)
  134. {
  135. if (strcmp(COMMON_PROFILE_INPUT_POSITION,
  136. v_inp[k]->getSemantic()) == 0)
  137. {
  138. pos_offset = inputs[j]->getOffset();
  139. const domURIFragmentType& uri = v_inp[k]->getSource();
  140. daeElementRef elem = uri.getElement();
  141. pos_source = (domSource*)elem.cast();
  142. }
  143. if (strcmp(COMMON_PROFILE_INPUT_NORMAL,
  144. v_inp[k]->getSemantic()) == 0)
  145. {
  146. norm_offset = inputs[j]->getOffset();
  147. const domURIFragmentType& uri = v_inp[k]->getSource();
  148. daeElementRef elem = uri.getElement();
  149. norm_source = (domSource*)elem.cast();
  150. }
  151. }
  152. }
  153. if (strcmp(COMMON_PROFILE_INPUT_NORMAL, inputs[j]->getSemantic()) == 0)
  154. {
  155. // Found normal array for this triangle list
  156. norm_offset = inputs[j]->getOffset();
  157. const domURIFragmentType& uri = inputs[j]->getSource();
  158. daeElementRef elem = uri.getElement();
  159. norm_source = (domSource*)elem.cast();
  160. }
  161. else if (strcmp(COMMON_PROFILE_INPUT_TEXCOORD,
  162. inputs[j]->getSemantic()) == 0)
  163. {
  164. // Found texCoords
  165. tc_offset = inputs[j]->getOffset();
  166. const domURIFragmentType& uri = inputs[j]->getSource();
  167. daeElementRef elem = uri.getElement();
  168. tc_source = (domSource*)elem.cast();
  169. }
  170. }
  171. ++idx_stride;
  172. return true;
  173. }
  174. LLModel::EModelStatus load_face_from_dom_tris(std::vector<LLVolumeFace>& face_list,
  175. std::vector<std::string>& materials,
  176. domTrianglesRef& tri,
  177. LLSD& log_msg)
  178. {
  179. LLVolumeFace face;
  180. std::vector<LLVolumeFace::VertexData> verts;
  181. std::vector<U16> indices;
  182. const domInputLocalOffset_Array& inputs = tri->getInput_array();
  183. S32 pos_offset = -1;
  184. S32 tc_offset = -1;
  185. S32 norm_offset = -1;
  186. domSource* pos_source = NULL;
  187. domSource* tc_source = NULL;
  188. domSource* norm_source = NULL;
  189. S32 idx_stride = 0;
  190. if (!get_dom_sources(inputs, pos_offset, tc_offset, norm_offset,
  191. idx_stride, pos_source, tc_source, norm_source))
  192. {
  193. llwarns << "Could not find dom sources for basic geometry data. Invalid model."
  194. << llendl;
  195. LLSD args;
  196. args["Message"] = "ParsingErrorBadElement";
  197. log_msg.append(args);
  198. return LLModel::BAD_ELEMENT;
  199. }
  200. if (!pos_source || !pos_source->getFloat_array())
  201. {
  202. llwarns << "Unable to process mesh without position data. Invalid model."
  203. << llendl;
  204. LLSD args;
  205. args["Message"] = "ParsingErrorPositionInvalidModel";
  206. log_msg.append(args);
  207. return LLModel::BAD_ELEMENT;
  208. }
  209. domPRef p = tri->getP();
  210. domListOfUInts& idx = p->getValue();
  211. domListOfFloats dummy;
  212. domListOfFloats& v = pos_source && pos_source->getFloat_array() ?
  213. pos_source->getFloat_array()->getValue() : dummy;
  214. domListOfFloats& tc = tc_source && tc_source->getFloat_array() ?
  215. tc_source->getFloat_array()->getValue() : dummy;
  216. domListOfFloats& n = norm_source && norm_source->getFloat_array() ?
  217. norm_source->getFloat_array()->getValue() : dummy;
  218. U32 index_count = idx.getCount();
  219. U32 vertex_count = v.getCount();
  220. U32 tc_count = tc.getCount();
  221. U32 norm_count = n.getCount();
  222. if (pos_source)
  223. {
  224. if (vertex_count == 0)
  225. {
  226. llwarns << "Unable to process mesh with empty position array. Invalid model."
  227. << llendl;
  228. return LLModel::BAD_ELEMENT;
  229. }
  230. face.mExtents[0].set(v[0], v[1], v[2]);
  231. face.mExtents[1].set(v[0], v[1], v[2]);
  232. }
  233. LLVolumeFace::VertexMapData::PointMap point_map;
  234. for (U32 i = 0; i < index_count; i += idx_stride)
  235. {
  236. LLVolumeFace::VertexData cv;
  237. if (pos_source)
  238. {
  239. if (i + pos_offset >= index_count)
  240. {
  241. return LLModel::BAD_ELEMENT;
  242. }
  243. U32 index = 3 * idx[i + pos_offset];
  244. if (index + 2 >= vertex_count)
  245. {
  246. llwarns << "Out of range index data. Invalid model." << llendl;
  247. return LLModel::BAD_ELEMENT;
  248. }
  249. cv.setPosition(LLVector4a(v[index], v[index + 1], v[index + 2]));
  250. if (!cv.getPosition().isFinite3())
  251. {
  252. llwarns << "Found NaN while loading position coords from DAE model. Invalid model."
  253. << llendl;
  254. return LLModel::BAD_ELEMENT;
  255. }
  256. }
  257. if (tc_source)
  258. {
  259. if (i + tc_offset >= index_count)
  260. {
  261. return LLModel::BAD_ELEMENT;
  262. }
  263. U32 index = 2 * idx[i + tc_offset];
  264. if (index + 1 >= tc_count)
  265. {
  266. llwarns << "Out of range tex coords indices. Invalid model." << llendl;
  267. return LLModel::BAD_ELEMENT;
  268. }
  269. cv.mTexCoord.set(tc[index], tc[index + 1]);
  270. if (!cv.mTexCoord.isFinite())
  271. {
  272. llwarns << "Found NaN while loading tex coords from DAE model. Invalid model."
  273. << llendl;
  274. return LLModel::BAD_ELEMENT;
  275. }
  276. }
  277. if (norm_source)
  278. {
  279. if (i + norm_offset >= index_count)
  280. {
  281. return LLModel::BAD_ELEMENT;
  282. }
  283. U32 index = 3 * idx[i + norm_offset];
  284. if (index + 2 >= norm_count)
  285. {
  286. llwarns << "Out of range normals indices. Invalid model." << llendl;
  287. return LLModel::BAD_ELEMENT;
  288. }
  289. cv.setNormal(LLVector4a(n[index], n[index + 1], n[index + 2]));
  290. if (!cv.getNormal().isFinite3())
  291. {
  292. llwarns << "Found NaN while loading normals from DAE model. Invalid model."
  293. << llendl;
  294. return LLModel::BAD_ELEMENT;
  295. }
  296. }
  297. bool found = false;
  298. LLVolumeFace::VertexMapData::PointMap::iterator point_iter;
  299. point_iter = point_map.find(LLVector3(cv.getPosition().getF32ptr()));
  300. if (point_iter != point_map.end())
  301. {
  302. for (U32 j = 0, cnt = point_iter->second.size(); j < cnt; ++j)
  303. {
  304. // We have a matching loc
  305. if ((point_iter->second)[j] == cv)
  306. {
  307. U16 shared_index = (point_iter->second)[j].mIndex;
  308. // Do not share verts within the same tri, degenerate
  309. U32 indx_size = indices.size();
  310. U32 verts_new_tri = indx_size % 3;
  311. if ((verts_new_tri < 1 ||
  312. indices[indx_size - 1] != shared_index) &&
  313. (verts_new_tri < 2 ||
  314. indices[indx_size - 2] != shared_index))
  315. {
  316. found = true;
  317. indices.push_back(shared_index);
  318. }
  319. break;
  320. }
  321. }
  322. }
  323. if (!found)
  324. {
  325. update_min_max(face.mExtents[0], face.mExtents[1], cv.getPosition());
  326. verts.emplace_back(cv);
  327. if (verts.size() >= 65535)
  328. {
  329. llwarns << "Attempted to write model exceeding 16-bit index buffer limitation."
  330. << llendl;
  331. return LLModel::VERTEX_NUMBER_OVERFLOW;
  332. }
  333. U16 index = (U16)(verts.size() - 1);
  334. indices.push_back(index);
  335. LLVolumeFace::VertexMapData d;
  336. d.setPosition(cv.getPosition());
  337. d.mTexCoord = cv.mTexCoord;
  338. d.setNormal(cv.getNormal());
  339. d.mIndex = index;
  340. if (point_iter != point_map.end())
  341. {
  342. point_iter->second.emplace_back(d);
  343. }
  344. else
  345. {
  346. point_map[LLVector3(d.getPosition().getF32ptr())].emplace_back(d);
  347. }
  348. }
  349. if (indices.size() % 3 == 0 && verts.size() >= 65532)
  350. {
  351. std::string material;
  352. if (tri->getMaterial())
  353. {
  354. material = std::string(tri->getMaterial());
  355. }
  356. materials.emplace_back(material);
  357. face_list.push_back(face);
  358. face_list.rbegin()->fillFromLegacyData(verts, indices);
  359. LLVolumeFace& new_face = *face_list.rbegin();
  360. if (!norm_source)
  361. {
  362. // NOTE: normals are part of the same buffer as mPositions, do
  363. // not free them separately.
  364. new_face.mNormals = NULL;
  365. }
  366. if (!tc_source)
  367. {
  368. // NOTE: texture coordinates are part of the same buffer as
  369. // mPositions, do not free them separately.
  370. new_face.mTexCoords = NULL;
  371. }
  372. face = LLVolumeFace();
  373. face.mExtents[0].set(v[0], v[1], v[2]);
  374. face.mExtents[1].set(v[0], v[1], v[2]);
  375. verts.clear();
  376. indices.clear();
  377. point_map.clear();
  378. }
  379. }
  380. if (!verts.empty())
  381. {
  382. std::string material;
  383. if (tri->getMaterial())
  384. {
  385. material = std::string(tri->getMaterial());
  386. }
  387. materials.emplace_back(material);
  388. face_list.push_back(face);
  389. face_list.rbegin()->fillFromLegacyData(verts, indices);
  390. LLVolumeFace& new_face = *face_list.rbegin();
  391. if (!norm_source)
  392. {
  393. // NOTE: normals are part of the same buffer as mPositions, do not
  394. // free them separately.
  395. new_face.mNormals = NULL;
  396. }
  397. if (!tc_source)
  398. {
  399. // NOTE: texture coordinates are part of the same buffer as
  400. // mPositions, do not free them separately.
  401. new_face.mTexCoords = NULL;
  402. }
  403. }
  404. return LLModel::NO_ERRORS;
  405. }
  406. LLModel::EModelStatus load_face_from_dom_polylist(std::vector<LLVolumeFace>& face_list,
  407. std::vector<std::string>& materials,
  408. domPolylistRef& poly,
  409. LLSD& log_msg)
  410. {
  411. domPRef p = poly->getP();
  412. domListOfUInts& idx = p->getValue();
  413. if (idx.getCount() == 0)
  414. {
  415. return LLModel::NO_ERRORS;
  416. }
  417. const domInputLocalOffset_Array& inputs = poly->getInput_array();
  418. domListOfUInts& vcount = poly->getVcount()->getValue();
  419. S32 pos_offset = -1;
  420. S32 tc_offset = -1;
  421. S32 norm_offset = -1;
  422. domSource* pos_source = NULL;
  423. domSource* tc_source = NULL;
  424. domSource* norm_source = NULL;
  425. S32 idx_stride = 0;
  426. if (!get_dom_sources(inputs, pos_offset, tc_offset, norm_offset,
  427. idx_stride, pos_source, tc_source,
  428. norm_source))
  429. {
  430. llwarns << "Could not get DOM sources for basic geometry data. Invalid model."
  431. << llendl;
  432. LLSD args;
  433. args["Message"] = "ParsingErrorBadElement";
  434. log_msg.append(args);
  435. return LLModel::BAD_ELEMENT;
  436. }
  437. LLVolumeFace face;
  438. std::vector<U16> indices;
  439. std::vector<LLVolumeFace::VertexData> verts;
  440. domListOfFloats v;
  441. domListOfFloats tc;
  442. domListOfFloats n;
  443. U32 index_count = idx.getCount();
  444. U32 vertex_count = 0;
  445. U32 tc_count = 0;
  446. U32 norm_count = 0;
  447. if (pos_source)
  448. {
  449. v = pos_source->getFloat_array()->getValue();
  450. face.mExtents[0].set(v[0], v[1], v[2]);
  451. face.mExtents[1].set(v[0], v[1], v[2]);
  452. vertex_count = v.getCount();
  453. }
  454. if (tc_source)
  455. {
  456. tc = tc_source->getFloat_array()->getValue();
  457. tc_count = tc.getCount();
  458. }
  459. if (norm_source)
  460. {
  461. n = norm_source->getFloat_array()->getValue();
  462. norm_count = n.getCount();
  463. }
  464. LLVolumeFace::VertexMapData::PointMap point_map;
  465. U32 cur_idx = 0;
  466. bool log_tc_msg = true;
  467. for (U32 i = 0; i < vcount.getCount(); ++i)
  468. {
  469. // For each polygon
  470. U32 first_index = 0;
  471. U32 last_index = 0;
  472. for (U32 j = 0; j < vcount[i]; ++j)
  473. {
  474. // For each vertex
  475. LLVolumeFace::VertexData cv;
  476. if (pos_source)
  477. {
  478. if (cur_idx + pos_offset >= index_count)
  479. {
  480. llwarns << "Out of range position indices. Invalid model."
  481. << llendl;
  482. return LLModel::BAD_ELEMENT;
  483. }
  484. U32 index = 3 * idx[cur_idx + pos_offset];
  485. if (index + 2 >= vertex_count)
  486. {
  487. llwarns << "Out of range position indices. Invalid model."
  488. << llendl;
  489. return LLModel::BAD_ELEMENT;
  490. }
  491. cv.getPosition().set(v[index], v[index + 1], v[index + 2]);
  492. if (!cv.getPosition().isFinite3())
  493. {
  494. llwarns << "Found NaN while loading positions from DAE model. Invalid model."
  495. << llendl;
  496. LLSD args;
  497. args["Message"] = "PositionNaN";
  498. log_msg.append(args);
  499. return LLModel::BAD_ELEMENT;
  500. }
  501. }
  502. if (tc_source)
  503. {
  504. if (cur_idx + tc_offset >= index_count)
  505. {
  506. llwarns << "Out of range text coords indices. Invalid model."
  507. << llendl;
  508. return LLModel::BAD_ELEMENT;
  509. }
  510. U32 index = 2 * idx[cur_idx + tc_offset];
  511. if (index + 1 < tc_count)
  512. {
  513. cv.mTexCoord.set(tc[index], tc[index + 1]);
  514. if (!cv.mTexCoord.isFinite())
  515. {
  516. llwarns << "Found NaN while loading texture coordinates from DAE model. Invalid model."
  517. << llendl;
  518. return LLModel::BAD_ELEMENT;
  519. }
  520. }
  521. else if (log_tc_msg)
  522. {
  523. log_tc_msg = false;
  524. llwarns << "Texture coordinates data is not complete."
  525. << llendl;
  526. LLSD args;
  527. args["Message"] = "IncompleteTC";
  528. log_msg.append(args);
  529. }
  530. }
  531. if (norm_source)
  532. {
  533. if (cur_idx + norm_offset >= index_count)
  534. {
  535. llwarns << "Out of range normals indices. Invalid model."
  536. << llendl;
  537. return LLModel::BAD_ELEMENT;
  538. }
  539. U32 index = 3 * idx[cur_idx + norm_offset];
  540. if (index + 2 >= norm_count)
  541. {
  542. llwarns << "Out of range normals indices. Invalid model."
  543. << llendl;
  544. return LLModel::BAD_ELEMENT;
  545. }
  546. cv.getNormal().set(n[index], n[index + 1], n[index + 2]);
  547. if (!cv.getNormal().isFinite3())
  548. {
  549. llwarns << "Found NaN while loading normals from DAE model. Invalid model."
  550. << llendl;
  551. LLSD args;
  552. args["Message"] = "NormalsNaN";
  553. log_msg.append(args);
  554. return LLModel::BAD_ELEMENT;
  555. }
  556. }
  557. cur_idx += idx_stride;
  558. bool found = false;
  559. LLVolumeFace::VertexMapData::PointMap::iterator point_iter;
  560. LLVector3 pos3(cv.getPosition().getF32ptr());
  561. point_iter = point_map.find(pos3);
  562. if (point_iter != point_map.end())
  563. {
  564. for (U32 k = 0; k < point_iter->second.size(); ++k)
  565. {
  566. if ((point_iter->second)[k] == cv)
  567. {
  568. found = true;
  569. U32 index = (point_iter->second)[k].mIndex;
  570. if (j == 0)
  571. {
  572. first_index = index;
  573. }
  574. else if (j == 1)
  575. {
  576. last_index = index;
  577. }
  578. else
  579. {
  580. indices.push_back(first_index);
  581. indices.push_back(last_index);
  582. indices.push_back(index);
  583. last_index = index;
  584. }
  585. break;
  586. }
  587. }
  588. }
  589. if (!found)
  590. {
  591. update_min_max(face.mExtents[0], face.mExtents[1],
  592. cv.getPosition());
  593. verts.emplace_back(cv);
  594. if (verts.size() >= 65535)
  595. {
  596. llwarns << "Attempted to write model exceeding 16-bit index buffer limitation."
  597. << llendl;
  598. return LLModel::VERTEX_NUMBER_OVERFLOW ;
  599. }
  600. U16 index = (U16)(verts.size() - 1);
  601. if (j == 0)
  602. {
  603. first_index = index;
  604. }
  605. else if (j == 1)
  606. {
  607. last_index = index;
  608. }
  609. else
  610. {
  611. indices.push_back(first_index);
  612. indices.push_back(last_index);
  613. indices.push_back(index);
  614. last_index = index;
  615. }
  616. LLVolumeFace::VertexMapData d;
  617. d.setPosition(cv.getPosition());
  618. d.mTexCoord = cv.mTexCoord;
  619. d.setNormal(cv.getNormal());
  620. d.mIndex = index;
  621. if (point_iter != point_map.end())
  622. {
  623. point_iter->second.emplace_back(d);
  624. }
  625. else
  626. {
  627. point_map[pos3].emplace_back(d);
  628. }
  629. }
  630. if (indices.size() % 3 == 0 && indices.size() >= 65532)
  631. {
  632. std::string material;
  633. if (poly->getMaterial())
  634. {
  635. material = std::string(poly->getMaterial());
  636. }
  637. materials.emplace_back(material);
  638. face_list.push_back(face);
  639. face_list.rbegin()->fillFromLegacyData(verts, indices);
  640. LLVolumeFace& new_face = *face_list.rbegin();
  641. if (!norm_source)
  642. {
  643. // NOTE: normals are part of the same buffer as mPositions,
  644. // do not free them separately.
  645. new_face.mNormals = NULL;
  646. }
  647. if (!tc_source)
  648. {
  649. // NOTE: texture coordinates are part of the same buffer as
  650. // mPositions, do not free them separately.
  651. new_face.mTexCoords = NULL;
  652. }
  653. face = LLVolumeFace();
  654. face.mExtents[0].set(v[0], v[1], v[2]);
  655. face.mExtents[1].set(v[0], v[1], v[2]);
  656. verts.clear();
  657. indices.clear();
  658. point_map.clear();
  659. }
  660. }
  661. }
  662. if (!verts.empty())
  663. {
  664. std::string material;
  665. if (poly->getMaterial())
  666. {
  667. material = std::string(poly->getMaterial());
  668. }
  669. materials.emplace_back(material);
  670. face_list.push_back(face);
  671. face_list.rbegin()->fillFromLegacyData(verts, indices);
  672. LLVolumeFace& new_face = *face_list.rbegin();
  673. if (!norm_source)
  674. {
  675. // NOTE: normals are part of the same buffer as mPositions, do not
  676. // free them separately.
  677. new_face.mNormals = NULL;
  678. }
  679. if (!tc_source)
  680. {
  681. // NOTE: texture coordinates are part of the same buffer as
  682. // mPositions, do not free them separately.
  683. new_face.mTexCoords = NULL;
  684. }
  685. }
  686. return LLModel::NO_ERRORS;
  687. }
  688. LLModel::EModelStatus load_face_from_dom_polygons(std::vector<LLVolumeFace>& face_list,
  689. std::vector<std::string>& materials,
  690. domPolygonsRef& poly)
  691. {
  692. LLVolumeFace face;
  693. std::vector<U16> indices;
  694. std::vector<LLVolumeFace::VertexData> verts;
  695. const domInputLocalOffset_Array& inputs = poly->getInput_array();
  696. S32 v_offset = -1;
  697. S32 n_offset = -1;
  698. S32 t_offset = -1;
  699. domListOfFloats* v = NULL;
  700. domListOfFloats* n = NULL;
  701. domListOfFloats* t = NULL;
  702. U32 stride = 0;
  703. for (U32 i = 0; i < inputs.getCount(); ++i)
  704. {
  705. stride = llmax((U32)inputs[i]->getOffset() + 1, stride);
  706. if (strcmp(COMMON_PROFILE_INPUT_VERTEX, inputs[i]->getSemantic()) == 0)
  707. {
  708. // Found vertex array
  709. v_offset = inputs[i]->getOffset();
  710. const domURIFragmentType& uri = inputs[i]->getSource();
  711. daeElementRef elem = uri.getElement();
  712. domVertices* vertices = (domVertices*)elem.cast();
  713. if (!vertices)
  714. {
  715. llwarns << "Could not find vertex source. Invalid model."
  716. << llendl;
  717. return LLModel::BAD_ELEMENT;
  718. }
  719. domInputLocal_Array& v_inp = vertices->getInput_array();
  720. for (U32 k = 0; k < v_inp.getCount(); ++k)
  721. {
  722. if (strcmp(COMMON_PROFILE_INPUT_POSITION,
  723. v_inp[k]->getSemantic()) == 0)
  724. {
  725. const domURIFragmentType& uri = v_inp[k]->getSource();
  726. daeElementRef elem = uri.getElement();
  727. domSource* src = (domSource*)elem.cast();
  728. if (!src)
  729. {
  730. llwarns << "Could not find DOM source. Invalid model."
  731. << llendl;
  732. return LLModel::BAD_ELEMENT;
  733. }
  734. v = &(src->getFloat_array()->getValue());
  735. }
  736. }
  737. }
  738. else if (strcmp(COMMON_PROFILE_INPUT_NORMAL,
  739. inputs[i]->getSemantic()) == 0)
  740. {
  741. n_offset = inputs[i]->getOffset();
  742. // Found normal array for this triangle list
  743. const domURIFragmentType& uri = inputs[i]->getSource();
  744. daeElementRef elem = uri.getElement();
  745. domSource* src = (domSource*)elem.cast();
  746. if (!src)
  747. {
  748. llwarns << "Could not find DOM source. Invalid model."
  749. << llendl;
  750. return LLModel::BAD_ELEMENT;
  751. }
  752. n = &(src->getFloat_array()->getValue());
  753. }
  754. else if (strcmp(COMMON_PROFILE_INPUT_TEXCOORD,
  755. inputs[i]->getSemantic()) == 0 &&
  756. inputs[i]->getSet() == 0)
  757. {
  758. // Found texCoords
  759. t_offset = inputs[i]->getOffset();
  760. const domURIFragmentType& uri = inputs[i]->getSource();
  761. daeElementRef elem = uri.getElement();
  762. domSource* src = (domSource*)elem.cast();
  763. if (!src)
  764. {
  765. llwarns << "Could not find DOM source. Invalid model."
  766. << llendl;
  767. return LLModel::BAD_ELEMENT;
  768. }
  769. t = &(src->getFloat_array()->getValue());
  770. }
  771. }
  772. domP_Array& ps = poly->getP_array();
  773. // Make a triangle list in <verts>
  774. for (U32 i = 0; i < ps.getCount(); ++i)
  775. {
  776. // For each polygon
  777. domListOfUInts& idx = ps[i]->getValue();
  778. for (U32 j = 0; j < idx.getCount() / stride; ++j)
  779. {
  780. // For each vertex
  781. if (j > 2)
  782. {
  783. U32 size = verts.size();
  784. verts.emplace_back(verts[size - 3]);
  785. verts.emplace_back(verts[size - 1]);
  786. }
  787. LLVolumeFace::VertexData vert;
  788. if (v)
  789. {
  790. U32 v_idx = idx[j * stride + v_offset] * 3;
  791. v_idx = llclamp(v_idx, 0U, (U32)v->getCount());
  792. vert.getPosition().set(v->get(v_idx), v->get(v_idx + 1),
  793. v->get(v_idx + 2));
  794. if (!vert.getPosition().isFinite3())
  795. {
  796. llwarns << "Found NaN while loading position data from DAE model. Invalid model."
  797. << llendl;
  798. return LLModel::BAD_ELEMENT;
  799. }
  800. }
  801. // Bound-check n and t lookups because some FBX to DAE converters
  802. // use negative indices and empty arrays to indicate data does not
  803. // exist for a particular channel
  804. if (n && n->getCount() > 0)
  805. {
  806. U32 n_idx = idx[j * stride + n_offset] * 3;
  807. n_idx = llclamp(n_idx, 0U, (U32)n->getCount());
  808. vert.getNormal().set(n->get(n_idx), n->get(n_idx + 1),
  809. n->get(n_idx + 2));
  810. if (!vert.getNormal().isFinite3())
  811. {
  812. llwarns << "Found NaN while loading normals from DAE model. Invalid model."
  813. << llendl;
  814. return LLModel::BAD_ELEMENT;
  815. }
  816. }
  817. else
  818. {
  819. vert.getNormal().clear();
  820. }
  821. if (t && t->getCount() > 0)
  822. {
  823. U32 t_idx = idx[j * stride + t_offset] * 2;
  824. t_idx = llclamp(t_idx, 0U, (U32)t->getCount());
  825. vert.mTexCoord.set(t->get(t_idx), t->get(t_idx + 1));
  826. if (!vert.mTexCoord.isFinite())
  827. {
  828. llwarns << "Found NaN while loading tex coords from DAE model. Invalid model."
  829. << llendl;
  830. return LLModel::BAD_ELEMENT;
  831. }
  832. }
  833. else
  834. {
  835. vert.mTexCoord.clear();
  836. }
  837. verts.emplace_back(vert);
  838. }
  839. }
  840. if (verts.empty())
  841. {
  842. return LLModel::NO_ERRORS;
  843. }
  844. face.mExtents[0] = verts[0].getPosition();
  845. face.mExtents[1] = verts[0].getPosition();
  846. // Create a map of unique vertices to indices
  847. std::map<LLVolumeFace::VertexData, U32> vert_idx;
  848. U32 cur_idx = 0;
  849. for (U32 i = 0; i < verts.size(); ++i)
  850. {
  851. std::map<LLVolumeFace::VertexData, U32>::iterator iter =
  852. vert_idx.find(verts[i]);
  853. if (iter == vert_idx.end())
  854. {
  855. vert_idx[verts[i]] = cur_idx++;
  856. }
  857. }
  858. // Build vertex array from map
  859. std::vector<LLVolumeFace::VertexData> new_verts;
  860. size_t vert_count = vert_idx.size();
  861. if (vert_count >= (size_t)U16_MAX)
  862. {
  863. llwarns << "Too many vertices: " << vert_count << "- Max is: "
  864. << U16_MAX << llendl;
  865. llassert(false);
  866. }
  867. new_verts.resize(vert_count);
  868. for (std::map<LLVolumeFace::VertexData, U32>::iterator iter = vert_idx.begin();
  869. iter != vert_idx.end(); ++iter)
  870. {
  871. new_verts[iter->second] = iter->first;
  872. update_min_max(face.mExtents[0], face.mExtents[1],
  873. iter->first.getPosition());
  874. }
  875. // Build index array from map
  876. indices.resize(verts.size());
  877. for (U32 i = 0; i < verts.size(); ++i)
  878. {
  879. indices[i] = vert_idx[verts[i]];
  880. // Assume GL_TRIANGLES: compare 0-1, 1-2, 3-4, 4-5 but not 2-3 or 5-6
  881. if (i % 3 != 0 && indices[i - 1] != indices[i])
  882. {
  883. llwarns << "Detected degenerate triangle at index: " << i
  884. << llendl;
  885. }
  886. }
  887. #if 0 // DEBUG just build an expanded triangle list
  888. for (U32 i = 0; i < verts.size(); ++i)
  889. {
  890. indices.push_back((U16)i);
  891. update_min_max(face.mExtents[0], face.mExtents[1],
  892. verts[i].getPosition());
  893. }
  894. #endif
  895. if (!new_verts.empty())
  896. {
  897. std::string material;
  898. if (poly->getMaterial())
  899. {
  900. material = std::string(poly->getMaterial());
  901. }
  902. materials.emplace_back(material);
  903. face_list.push_back(face);
  904. face_list.rbegin()->fillFromLegacyData(new_verts, indices);
  905. LLVolumeFace& new_face = *face_list.rbegin();
  906. if (!n)
  907. {
  908. // NOTE: normals are part of the same buffer as mPositions, do not
  909. // free them separately.
  910. new_face.mNormals = NULL;
  911. }
  912. if (!t)
  913. {
  914. // NOTE: texture coordinates are part of the same buffer as
  915. // mPositions, do not free them separately.
  916. new_face.mTexCoords = NULL;
  917. }
  918. }
  919. return LLModel::NO_ERRORS;
  920. }
  921. //-----------------------------------------------------------------------------
  922. // LLDAELoader
  923. //-----------------------------------------------------------------------------
  924. LLDAELoader::LLDAELoader(const std::string& filename, S32 lod,
  925. load_callback_t load_cb,
  926. joint_lookup_func_t joint_lookup_func,
  927. texture_load_func_t texture_load_func,
  928. state_callback_t state_cb, void* userdata,
  929. JointTransformMap& joint_transform_map,
  930. JointNameSet& joints_from_nodes,
  931. std::map<std::string, std::string>& joint_alias_map,
  932. U32 max_joints_per_mesh, U32 model_limit,
  933. bool preprocess)
  934. : LLModelLoader(filename, lod, load_cb, joint_lookup_func, texture_load_func,
  935. state_cb, userdata, joint_transform_map, joints_from_nodes,
  936. joint_alias_map, max_joints_per_mesh),
  937. mGeneratedModelLimit(model_limit),
  938. mPreprocessDAE(preprocess)
  939. {
  940. }
  941. struct ModelSort
  942. {
  943. bool operator()(const LLPointer<LLModel>& lhs,
  944. const LLPointer<LLModel>& rhs)
  945. {
  946. if (lhs->mSubmodelID < rhs->mSubmodelID)
  947. {
  948. return true;
  949. }
  950. return LLStringUtil::compareInsensitive(lhs->mLabel, rhs->mLabel) < 0;
  951. }
  952. };
  953. bool LLDAELoader::openFile(const std::string& filename)
  954. {
  955. setLoadState(READING_FILE);
  956. // Setup a DAE error handler
  957. LLSetDaeErrorHandler dae_error_handler;
  958. const std::string allowed =
  959. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%-._~:\"|\\/";
  960. std::string uri_filename = LLURI::escape(filename, allowed);
  961. // No suitable slm exists, load from the .dae file
  962. DAE dae;
  963. domCOLLADA* dom;
  964. if (mPreprocessDAE)
  965. {
  966. dom = dae.openFromMemory(uri_filename,
  967. preprocessDAE(filename).c_str());
  968. }
  969. else
  970. {
  971. llinfos << "Skipping pre-processing of DAE file: " << filename
  972. << llendl;
  973. dom = dae.open(uri_filename);
  974. }
  975. if (!dom)
  976. {
  977. llwarns <<" Error with dae; traditionally indicates a corrupt file."
  978. << llendl;
  979. LLSD args;
  980. args["Message"] = "ParsingErrorCorrupt";
  981. mWarningsArray.append(args);
  982. setLoadState(ERROR_PARSING);
  983. return false;
  984. }
  985. // Dom version
  986. daeString dom_version = dae.getDomVersion();
  987. std::string sldom(dom_version);
  988. llinfos << "Collada importer version: " << sldom << llendl;
  989. // Dae version
  990. domVersionType doc_version = dom->getVersion();
  991. // 0 = v1.4, 1 = v1.4.1, 2 = currently unsupported, however may work
  992. if (doc_version > 1)
  993. {
  994. doc_version = VERSIONTYPE_COUNT;
  995. }
  996. llinfos << "Dae version: " << colladaVersion[doc_version] << llendl;
  997. daeDatabase* db = dae.getDatabase();
  998. if (!db)
  999. {
  1000. llwarns << "NULL database ! Aborted." << llendl;
  1001. return false;
  1002. }
  1003. daeInt count = db->getElementCount(NULL, COLLADA_TYPE_MESH);
  1004. daeDocument* doc = dae.getDoc(uri_filename);
  1005. if (!doc)
  1006. {
  1007. LLSD args;
  1008. args["Message"] = "ParsingErrorNoDoc";
  1009. mWarningsArray.append(args);
  1010. llwarns << "Cannot find internal DAE doc" << llendl;
  1011. return false;
  1012. }
  1013. daeElement* root = doc->getDomRoot();
  1014. if (!root)
  1015. {
  1016. llwarns << "Document has no root" << llendl;
  1017. return false;
  1018. }
  1019. // Verify some basic properties of the dae
  1020. // 1. Basic validity check on controller
  1021. U32 controller_count = (U32)db->getElementCount(NULL, "controller");
  1022. bool result = false;
  1023. for (U32 i = 0; i < controller_count; ++i)
  1024. {
  1025. domController* controllerp = NULL;
  1026. db->getElement((daeElement**)&controllerp, i, NULL, "controller");
  1027. result = verifyController(controllerp);
  1028. if (!result)
  1029. {
  1030. LLSD args;
  1031. args["Message"] = "ParsingErrorBadElement";
  1032. mWarningsArray.append(args);
  1033. llinfos << "Could not verify controller" << llendl;
  1034. setLoadState(ERROR_PARSING);
  1035. return true;
  1036. }
  1037. }
  1038. // Get unit scale
  1039. mTransform.setIdentity();
  1040. domAsset::domUnit* unit =
  1041. daeSafeCast<domAsset::domUnit>(root->getDescendant(daeElement::matchType(domAsset::domUnit::ID())));
  1042. if (unit)
  1043. {
  1044. F32 meter = unit->getMeter();
  1045. mTransform.mMatrix[0][0] = meter;
  1046. mTransform.mMatrix[1][1] = meter;
  1047. mTransform.mMatrix[2][2] = meter;
  1048. }
  1049. // Get up axis rotation
  1050. LLMatrix4 rotation;
  1051. domUpAxisType up = UPAXISTYPE_Y_UP; // Default is Y_UP
  1052. domAsset::domUp_axis* up_axis =
  1053. daeSafeCast<domAsset::domUp_axis>(root->getDescendant(daeElement::matchType(domAsset::domUp_axis::ID())));
  1054. if (up_axis)
  1055. {
  1056. up = up_axis->getValue();
  1057. }
  1058. if (up == UPAXISTYPE_X_UP)
  1059. {
  1060. rotation.initRotation(0.f, 90.f * DEG_TO_RAD, 0.f);
  1061. }
  1062. else if (up == UPAXISTYPE_Y_UP)
  1063. {
  1064. rotation.initRotation(90.f * DEG_TO_RAD, 0.f, 0.f);
  1065. }
  1066. rotation *= mTransform;
  1067. mTransform = rotation;
  1068. mTransform.condition();
  1069. U32 submodel_limit = count > 0 ? mGeneratedModelLimit / count : 0;
  1070. for (daeInt idx = 0; idx < count; ++idx)
  1071. {
  1072. // Build map of domEntities to LLModel
  1073. domMesh* mesh = NULL;
  1074. db->getElement((daeElement**)&mesh, idx, NULL, COLLADA_TYPE_MESH);
  1075. if (mesh)
  1076. {
  1077. std::vector<LLModel*> models;
  1078. loadModelsFromDomMesh(mesh, models, submodel_limit);
  1079. std::vector<LLModel*>::iterator i;
  1080. i = models.begin();
  1081. while (i != models.end())
  1082. {
  1083. LLModel* mdl = *i;
  1084. if (mdl->getStatus() != LLModel::NO_ERRORS)
  1085. {
  1086. setLoadState(ERROR_MODEL + mdl->getStatus());
  1087. return false;
  1088. }
  1089. if (mdl && mdl->validate(true))
  1090. {
  1091. mModelList.push_back(mdl);
  1092. mModelsMap[mesh].push_back(mdl);
  1093. }
  1094. ++i;
  1095. }
  1096. }
  1097. }
  1098. std::sort(mModelList.begin(), mModelList.end(), ModelSort());
  1099. #if LL_NORMALIZE_ALL_MODELS
  1100. if (!mNoNormalize)
  1101. {
  1102. LLModel::normalizeModels(mModelList);
  1103. }
  1104. #endif
  1105. model_list::iterator model_iter = mModelList.begin();
  1106. std::vector<std::string>::iterator mat_iter, end_iter;
  1107. while (model_iter != mModelList.end())
  1108. {
  1109. LLModel* mdl = *model_iter;
  1110. if (!mdl) continue; // Paranoia
  1111. U32 material_count = mdl->mMaterialList.size();
  1112. llinfos << "Importing " << mdl->mLabel << " model with "
  1113. << material_count << " material references" << llendl;
  1114. mat_iter = mdl->mMaterialList.begin();
  1115. end_iter = material_count > LIMIT_MATERIALS_OUTPUT ?
  1116. mat_iter + LIMIT_MATERIALS_OUTPUT :
  1117. mdl->mMaterialList.end();
  1118. while (mat_iter != end_iter)
  1119. {
  1120. llinfos << " - " << mdl->mLabel << " references " << (*mat_iter)
  1121. << llendl;
  1122. ++mat_iter;
  1123. }
  1124. ++model_iter;
  1125. }
  1126. domGeometry* geom;
  1127. count = db->getElementCount(NULL, COLLADA_TYPE_SKIN);
  1128. for (daeInt idx = 0; idx < count; ++idx)
  1129. {
  1130. // Add skinned meshes as instances
  1131. domSkin* skin = NULL;
  1132. db->getElement((daeElement**)&skin, idx, NULL, COLLADA_TYPE_SKIN);
  1133. if (skin)
  1134. {
  1135. geom = daeSafeCast<domGeometry>(skin->getSource().getElement());
  1136. if (geom)
  1137. {
  1138. domMesh* mesh = geom->getMesh();
  1139. if (mesh)
  1140. {
  1141. std::vector<LLPointer<LLModel> >::iterator it;
  1142. it = mModelsMap[mesh].begin();
  1143. while (it != mModelsMap[mesh].end())
  1144. {
  1145. LLPointer<LLModel> mdl = *it;
  1146. LLDAELoader::processDomModel(mdl, &dae, root, mesh,
  1147. skin);
  1148. ++it;
  1149. }
  1150. }
  1151. }
  1152. }
  1153. }
  1154. llinfos << "Collada skins processed: " << count << llendl;
  1155. daeElement* scene = root->getDescendant("visual_scene");
  1156. if (!scene)
  1157. {
  1158. llwarns << "Document has no visual_scene" << llendl;
  1159. LLSD args;
  1160. args["Message"] = "ParsingErrorNoScene";
  1161. mWarningsArray.append(args);
  1162. setLoadState(ERROR_PARSING);
  1163. return true;
  1164. }
  1165. setLoadState(DONE);
  1166. bool bad_element = false;
  1167. processElement(scene, bad_element, &dae);
  1168. if (bad_element)
  1169. {
  1170. llwarns << "Scene could not be parsed" << llendl;
  1171. LLSD args;
  1172. args["Message"] = "ParsingErrorCantParseScene";
  1173. mWarningsArray.append(args);
  1174. setLoadState(ERROR_PARSING);
  1175. }
  1176. return true;
  1177. }
  1178. // Open a DAE file for some preprocessing (like removing space characters in
  1179. // IDs), see MAINT-5678.
  1180. std::string LLDAELoader::preprocessDAE(const std::string& filename)
  1181. {
  1182. llinfos << "Preprocessing dae file '" << filename
  1183. << "' to remove spaces from the names, ids, etc." << llendl;
  1184. llifstream in_file;
  1185. in_file.open(filename.c_str());
  1186. std::stringstream str_stream;
  1187. str_stream << in_file.rdbuf();
  1188. std::string buffer = str_stream.str();
  1189. try
  1190. {
  1191. std::regex re("\"[\\w\\.@#$-]*(\\s[\\w\\.@#$-]*)+\"");
  1192. std::sregex_iterator next(buffer.begin(), buffer.end(), re);
  1193. std::sregex_iterator end;
  1194. while (next != end)
  1195. {
  1196. std::smatch match = *next;
  1197. std::string s = match.str();
  1198. LL_DEBUGS("MeshUpload") << "Found: '" << s << "'" << LL_ENDL;
  1199. LLStringUtil::replaceChar(s, ' ', '_');
  1200. LL_DEBUGS("MeshUpload") << "Replacing with: '" << s << "'"
  1201. << LL_ENDL;
  1202. LLStringUtil::replaceString(buffer, match.str(), s);
  1203. ++next;
  1204. }
  1205. }
  1206. catch (std::regex_error& e)
  1207. {
  1208. llwarns << "Regex error: " << e.what() << llendl;
  1209. }
  1210. return buffer;
  1211. }
  1212. void LLDAELoader::processDomModel(LLModel* model, DAE* dae, daeElement* root,
  1213. domMesh* mesh, domSkin* skin)
  1214. {
  1215. if (!model || !dae || !root || !model || !skin)
  1216. {
  1217. llwarns << "NULL pointer passed !" << llendl;
  1218. llassert(false);
  1219. return;
  1220. }
  1221. LLVector3 mesh_scale_vector;
  1222. LLVector3 mesh_translation_vector;
  1223. model->getNormalizedScaleTranslation(mesh_scale_vector,
  1224. mesh_translation_vector);
  1225. LLMatrix4 normalized_transformation;
  1226. normalized_transformation.setTranslation(mesh_translation_vector);
  1227. LLMatrix4 mesh_scale;
  1228. mesh_scale.initScale(mesh_scale_vector);
  1229. mesh_scale *= normalized_transformation;
  1230. normalized_transformation = mesh_scale;
  1231. LLMatrix4a inv_mat;
  1232. inv_mat.loadu(normalized_transformation);
  1233. inv_mat.invert();
  1234. LLMatrix4 inv_norm_trans(inv_mat.getF32ptr());
  1235. domSkin::domBind_shape_matrix* bind_mat = skin->getBind_shape_matrix();
  1236. if (bind_mat)
  1237. {
  1238. // Get bind shape matrix
  1239. domFloat4x4& dom_value = bind_mat->getValue();
  1240. LLMeshSkinInfo& skin_info = model->mSkinInfo;
  1241. for (S32 i = 0; i < 4; ++i)
  1242. {
  1243. for (S32 j = 0; j < 4; ++j)
  1244. {
  1245. skin_info.mBindShapeMatrix.mMatrix[i][j] = dom_value[i + j * 4];
  1246. }
  1247. }
  1248. LLMatrix4 trans = normalized_transformation;
  1249. trans *= skin_info.mBindShapeMatrix;
  1250. skin_info.mBindShapeMatrix = trans;
  1251. }
  1252. // Some collada setup for accessing the skeleton
  1253. U32 skeleton_count = dae->getDatabase()->getElementCount(NULL, "skeleton");
  1254. std::vector<domInstance_controller::domSkeleton*> skeletons;
  1255. for (U32 i = 0; i < skeleton_count; ++i)
  1256. {
  1257. daeElement* elementp = 0;
  1258. dae->getDatabase()->getElement(&elementp, i, 0, "skeleton");
  1259. // Try to get at the skeletal instance controller
  1260. domInstance_controller::domSkeleton* skeletonp =
  1261. daeSafeCast<domInstance_controller::domSkeleton>(elementp);
  1262. if (skeletonp)
  1263. {
  1264. daeElement* skeletonrootnodep = skeletonp->getValue().getElement();
  1265. if (skeletonrootnodep)
  1266. {
  1267. skeletons.push_back(skeletonp);
  1268. }
  1269. }
  1270. }
  1271. bool missing_skel_or_scene = false;
  1272. skeleton_count = skeletons.size();
  1273. if (skeleton_count)
  1274. {
  1275. // Get at least one skeleton
  1276. for (U32 i = 0; i < skeleton_count; ++i)
  1277. {
  1278. domInstance_controller::domSkeleton* skeletonp = skeletons[i];
  1279. if (!skeletonp) continue; // Paranoia
  1280. daeElement* skeletonrootnodep = skeletonp->getValue().getElement();
  1281. if (!skeletonrootnodep) continue; // Paranoia
  1282. // Once we have the root node, start acccessing it's joint
  1283. // components
  1284. const size_t joint_count = mJointMap.size();
  1285. JointMap::const_iterator joint_it = mJointMap.begin();
  1286. // Loop over all the possible joints within the .dae using the
  1287. // allowed joint list in the ctor.
  1288. char str[65];
  1289. str[64] = '\0';
  1290. for (size_t i = 0; i < joint_count; ++i, ++joint_it)
  1291. {
  1292. // Build a joint for the resolver to work with
  1293. snprintf(str, 64, "./%s", joint_it->first.c_str());
  1294. // Setup the resolver
  1295. daeSIDResolver resolver(skeletonrootnodep, str);
  1296. // Look for the joint
  1297. domNode* jointp = daeSafeCast<domNode>(resolver.getElement());
  1298. if (jointp)
  1299. {
  1300. // Pull out the translate id and store it in the
  1301. // jointTranslations map
  1302. daeSIDResolver joint_rsv_a(jointp, "./translate");
  1303. domTranslate* trans_a =
  1304. daeSafeCast<domTranslate>(joint_rsv_a.getElement());
  1305. daeSIDResolver joint_rsv_b(jointp, "./location");
  1306. domTranslate* trans_b =
  1307. daeSafeCast<domTranslate>(joint_rsv_b.getElement());
  1308. LLMatrix4 working_transform;
  1309. // Translation via SID
  1310. if (trans_a)
  1311. {
  1312. extractTranslation(trans_a, working_transform);
  1313. }
  1314. else if (trans_b)
  1315. {
  1316. extractTranslation(trans_b, working_transform);
  1317. }
  1318. else
  1319. {
  1320. // Translation via child from element
  1321. daeElement* translate_elemp;
  1322. translate_elemp = getChildFromElement(jointp, "translate");
  1323. if (translate_elemp &&
  1324. translate_elemp->typeID() != domTranslate::ID())
  1325. {
  1326. llwarns << "The found element is not a translate node"
  1327. << llendl;
  1328. missing_skel_or_scene = true;
  1329. }
  1330. else if (translate_elemp)
  1331. {
  1332. extractTranslationViaElement(translate_elemp,
  1333. working_transform);
  1334. }
  1335. else
  1336. {
  1337. extractTranslationViaSID(jointp,
  1338. working_transform);
  1339. }
  1340. }
  1341. // Store the joint transform w/respect to it's name.
  1342. mJointList[joint_it->second.c_str()] = working_transform;
  1343. }
  1344. }
  1345. // If anything failed in regards to extracting the skeleton, joints
  1346. // or translation id, mention it
  1347. if (missing_skel_or_scene)
  1348. {
  1349. llwarns << "Partial jointmap found in asset: did you mean to just have a partial map ?"
  1350. << llendl;
  1351. }
  1352. }
  1353. }
  1354. else
  1355. {
  1356. // If no skeleton, do a breadth-first search to get at specific joints
  1357. daeElement* scenep = root->getDescendant("visual_scene");
  1358. if (!scenep)
  1359. {
  1360. llwarns << "No visual scene; unable to parse bone offsets."
  1361. << llendl;
  1362. missing_skel_or_scene = true;
  1363. }
  1364. else
  1365. {
  1366. // Get the children at this level
  1367. daeTArray<daeSmartRef<daeElement> > children = scenep->getChildren();
  1368. // Process any children that are joints; not all children are
  1369. // joints, some could be ambient lights, cameras, geometry etc..
  1370. for (S32 i = 0, child_count = children.getCount();
  1371. i < child_count; ++i)
  1372. {
  1373. domNode* nodep = daeSafeCast<domNode>(children[i]);
  1374. if (nodep)
  1375. {
  1376. processJointNode(nodep, mJointList);
  1377. }
  1378. }
  1379. }
  1380. }
  1381. domSkin::domJoints* joints = skin->getJoints();
  1382. if (!joints)
  1383. {
  1384. llwarns << "NULL skin joints pointer ! Aborting." << llendl;
  1385. return;
  1386. }
  1387. domInputLocal_Array& joint_input = joints->getInput_array();
  1388. for (size_t i = 0; i < joint_input.getCount(); ++i)
  1389. {
  1390. domInputLocal* input = joint_input.get(i);
  1391. if (!input) continue; // Paranoia
  1392. xsNMTOKEN semantic = input->getSemantic();
  1393. if (strcmp(semantic, COMMON_PROFILE_INPUT_JOINT) == 0)
  1394. {
  1395. // Found joint source, fill model->mJointMap and
  1396. // model->mSkinInfo.mJointNames
  1397. daeElement* elem = input->getSource().getElement();
  1398. domSource* source = daeSafeCast<domSource>(elem);
  1399. if (source)
  1400. {
  1401. domName_array* names_source = source->getName_array();
  1402. if (names_source)
  1403. {
  1404. domListOfNames& names = names_source->getValue();
  1405. for (size_t j = 0; j < names.getCount(); ++j)
  1406. {
  1407. std::string name(names.get(j));
  1408. if (mJointMap.find(name) != mJointMap.end())
  1409. {
  1410. name = mJointMap[name];
  1411. }
  1412. model->mSkinInfo.mJointNames.emplace_back(name);
  1413. model->mSkinInfo.mJointKeys.push_back(LLJoint::getKey(name));
  1414. }
  1415. }
  1416. else
  1417. {
  1418. domIDREF_array* names_source = source->getIDREF_array();
  1419. if (names_source)
  1420. {
  1421. xsIDREFS& names = names_source->getValue();
  1422. for (size_t j = 0; j < names.getCount(); ++j)
  1423. {
  1424. std::string name(names.get(j).getID());
  1425. if (mJointMap.find(name) != mJointMap.end())
  1426. {
  1427. name = mJointMap[name];
  1428. }
  1429. model->mSkinInfo.mJointNames.emplace_back(name);
  1430. model->mSkinInfo.mJointKeys.push_back(LLJoint::getKey(name));
  1431. }
  1432. }
  1433. }
  1434. }
  1435. }
  1436. else if (strcmp(semantic, COMMON_PROFILE_INPUT_INV_BIND_MATRIX) == 0)
  1437. {
  1438. // Found inv_bind_matrix array, fill model->mInvBindMatrix
  1439. domSource* source =
  1440. daeSafeCast<domSource>(input->getSource().getElement());
  1441. if (source)
  1442. {
  1443. domFloat_array* t = source->getFloat_array();
  1444. if (t)
  1445. {
  1446. domListOfFloats& transform = t->getValue();
  1447. S32 count = transform.getCount() / 16;
  1448. for (S32 k = 0; k < count; ++k)
  1449. {
  1450. LLMatrix4 mat;
  1451. for (S32 i = 0; i < 4; ++i)
  1452. {
  1453. for (S32 j = 0; j < 4; ++j)
  1454. {
  1455. mat.mMatrix[i][j] = transform[i + 4 * j + 16 * k];
  1456. }
  1457. }
  1458. model->mSkinInfo.mInvBindMatrix.emplace_back(mat);
  1459. }
  1460. }
  1461. }
  1462. }
  1463. }
  1464. auto& inv_bind_vec = model->mSkinInfo.mInvBindMatrix;
  1465. size_t mat_size = llmin(inv_bind_vec.size(),
  1466. LL_CHARACTER_MAX_ANIMATED_JOINTS);
  1467. model->mSkinInfo.mInvBindShapeMatrix.resize(mat_size);
  1468. if (mat_size)
  1469. {
  1470. LLMatrix4a bind_shape, inv_bind, mat;
  1471. bind_shape.loadu(model->mSkinInfo.mBindShapeMatrix);
  1472. for (size_t i = 0; i < mat_size; ++i)
  1473. {
  1474. inv_bind.loadu(inv_bind_vec[i]);
  1475. mat.matMul(bind_shape, inv_bind);
  1476. model->mSkinInfo.mInvBindShapeMatrix[i].set(mat.getF32ptr());
  1477. }
  1478. }
  1479. // Now that we have parsed the joint array, let's determine if we have a
  1480. // full rig (which means we have all the joint sthat are required for an
  1481. // avatar versus a skinned asset attached to a node in a file that contains
  1482. // an entire skeleton, but does not use the skeleton).
  1483. buildJointToNodeMappingFromScene(root);
  1484. critiqueRigForUploadApplicability(model->mSkinInfo.mJointNames);
  1485. if (!missing_skel_or_scene)
  1486. {
  1487. // *FIXME: mesh_id is used to determine which mesh gets to set the
  1488. // joint offset, in the event of a conflict. Since we do not know the
  1489. // mesh id yet, we cannot/ guarantee that joint offsets will be applied
  1490. // with the same priority as in the uploaded model. If the file
  1491. // contains multiple meshes with conflicting joint offsets, preview may
  1492. // be incorrect.
  1493. LLUUID fake_mesh_id;
  1494. fake_mesh_id.generate();
  1495. // Set the joint translations on the avatar
  1496. for (JointMap::const_iterator it = mJointMap.begin(),
  1497. end = mJointMap.end();
  1498. it != end; ++it)
  1499. {
  1500. const std::string& joint_name = it->first;
  1501. if (mJointList.find(joint_name) == mJointList.end())
  1502. {
  1503. continue;
  1504. }
  1505. LLJoint* jointp = mJointLookupFunc(joint_name, mUserData);
  1506. if (!jointp)
  1507. {
  1508. // Most likely an error in the asset.
  1509. llwarns << "Tried to apply joint position from .dae for joint "
  1510. << joint_name << ", but it did not exist in the avatar rig."
  1511. << llendl;
  1512. continue;
  1513. }
  1514. LLMatrix4 joint_tf = mJointList[joint_name];
  1515. const LLVector3& joint_pos = joint_tf.getTranslation();
  1516. if (jointp->aboveJointPosThreshold(joint_pos))
  1517. {
  1518. jointp->addAttachmentPosOverride(joint_pos, fake_mesh_id, "");
  1519. if (model->mSkinInfo.mLockScaleIfJointPosition)
  1520. {
  1521. jointp->addAttachmentScaleOverride(jointp->getDefaultScale(),
  1522. fake_mesh_id, "");
  1523. }
  1524. }
  1525. }
  1526. }
  1527. // We need to construct the alternate bind matrix (which contains the new
  1528. // joint positions) in the same order as they were stored in the joint
  1529. // buffer. The joints associated with the skeleton are not stored in the
  1530. // same order as they are in the exported joint buffer. This remaps the
  1531. // skeletal joints to be in the same order as the joints stored in the
  1532. // model.
  1533. std::vector<std::string>::const_iterator joint_it =
  1534. model->mSkinInfo.mJointNames.begin();
  1535. const size_t joint_count = model->mSkinInfo.mJointNames.size();
  1536. const size_t inv_mat_size = model->mSkinInfo.mInvBindMatrix.size();
  1537. if (inv_mat_size < joint_count)
  1538. {
  1539. llwarns << "Joint count (" << joint_count
  1540. << ") is greater than in bing matrix size (" << inv_mat_size
  1541. << "): some joint will not have an alternate bind matrix "
  1542. << llendl;
  1543. }
  1544. for (size_t i = 0, count = llmin(joint_count, inv_mat_size); i < count;
  1545. ++i, ++joint_it)
  1546. {
  1547. const std::string& joint_name = *joint_it;
  1548. if (mJointMap.find(joint_name) == mJointMap.end())
  1549. {
  1550. LL_DEBUGS("MeshUpload") << "Possibly misnamed/missing joint: "
  1551. << joint_name << LL_ENDL;
  1552. continue;
  1553. }
  1554. // Look for the joint xform that we extracted from the skeleton, using
  1555. // the joint_it as the key and store it in the alternate bind matrix
  1556. LLMatrix4 new_inverse = model->mSkinInfo.mInvBindMatrix[i];
  1557. new_inverse.setTranslation(mJointList[joint_name].getTranslation());
  1558. model->mSkinInfo.mAlternateBindMatrix.emplace_back(new_inverse);
  1559. }
  1560. U32 bind_count = model->mSkinInfo.mAlternateBindMatrix.size();
  1561. if (bind_count > 0 && bind_count != joint_count)
  1562. {
  1563. llwarns << "Model " << model->mLabel
  1564. << " has invalid joint bind matrix list." << llendl;
  1565. }
  1566. // Grab raw position array
  1567. domVertices* verts = mesh->getVertices();
  1568. if (verts)
  1569. {
  1570. domInputLocal_Array& inputs = verts->getInput_array();
  1571. for (size_t i = 0; i < inputs.getCount() && model->mPosition.empty();
  1572. ++i)
  1573. {
  1574. if (strcmp(inputs[i]->getSemantic(),
  1575. COMMON_PROFILE_INPUT_POSITION) != 0)
  1576. {
  1577. continue;
  1578. }
  1579. domSource* pos_source =
  1580. daeSafeCast<domSource>(inputs[i]->getSource().getElement());
  1581. if (!pos_source)
  1582. {
  1583. continue;
  1584. }
  1585. domFloat_array* pos_array = pos_source->getFloat_array();
  1586. if (!pos_array)
  1587. {
  1588. continue;
  1589. }
  1590. domListOfFloats& pos = pos_array->getValue();
  1591. for (size_t j = 0; j < pos.getCount(); j += 3)
  1592. {
  1593. if (pos.getCount() <= j + 2)
  1594. {
  1595. llwarns << "Invalid position array size - Skipping"
  1596. << llendl;
  1597. llassert(false);
  1598. continue;
  1599. }
  1600. LLVector3 v(pos[j], pos[j + 1], pos[j + 2]);
  1601. // Transform from COLLADA space to volume space
  1602. model->mPosition.emplace_back(v * inv_norm_trans);
  1603. }
  1604. }
  1605. }
  1606. // Grab skin weights array
  1607. domSkin::domVertex_weights* weights = skin->getVertex_weights();
  1608. if (weights)
  1609. {
  1610. domInputLocalOffset_Array& inputs = weights->getInput_array();
  1611. domFloat_array* vertex_weights = NULL;
  1612. for (size_t i = 0; i < inputs.getCount(); ++i)
  1613. {
  1614. if (strcmp(inputs[i]->getSemantic(),
  1615. COMMON_PROFILE_INPUT_WEIGHT) != 0)
  1616. {
  1617. continue;
  1618. }
  1619. domSource* weight_source =
  1620. daeSafeCast<domSource>(inputs[i]->getSource().getElement());
  1621. if (weight_source)
  1622. {
  1623. vertex_weights = weight_source->getFloat_array();
  1624. }
  1625. }
  1626. if (vertex_weights)
  1627. {
  1628. domListOfFloats& w = vertex_weights->getValue();
  1629. domListOfUInts& vcount = weights->getVcount()->getValue();
  1630. domListOfInts& v = weights->getV()->getValue();
  1631. U32 c_idx = 0;
  1632. for (size_t vc_idx = 0; vc_idx < vcount.getCount(); ++vc_idx)
  1633. {
  1634. daeUInt count = vcount[vc_idx];
  1635. // Create list of weights that influence this vertex
  1636. LLModel::weight_list weight_list;
  1637. for (daeUInt i = 0; i < count; ++i)
  1638. {
  1639. // For each weight
  1640. daeInt joint_idx = v[c_idx++];
  1641. daeInt weight_idx = v[c_idx++];
  1642. if (joint_idx == -1)
  1643. {
  1644. // Ignore bindings to bind_shape_matrix
  1645. continue;
  1646. }
  1647. F32 weight_value = w[weight_idx];
  1648. weight_list.emplace_back(joint_idx, weight_value);
  1649. }
  1650. // Sort by joint weight
  1651. std::sort(weight_list.begin(), weight_list.end(),
  1652. LLModel::CompareWeightGreater());
  1653. std::vector<LLModel::JointWeight> wght;
  1654. F32 total = 0.f;
  1655. for (size_t i = 0, wcount = llmin(4, (S32)weight_list.size());
  1656. i < wcount; ++i)
  1657. {
  1658. // Take up to 4 most significant weights
  1659. if (weight_list[i].mWeight > 0.f)
  1660. {
  1661. wght.emplace_back(weight_list[i]);
  1662. total += weight_list[i].mWeight;
  1663. }
  1664. }
  1665. if (total == 0.f)
  1666. {
  1667. llwarns << "Null total weight ! Cannot normalize weights."
  1668. << llendl;
  1669. continue;
  1670. }
  1671. F32 scale = 1.f / total;
  1672. if (scale != 1.f)
  1673. {
  1674. // Normalize weights
  1675. for (U32 i = 0; i < wght.size(); ++i)
  1676. {
  1677. wght[i].mWeight *= scale;
  1678. }
  1679. }
  1680. model->mSkinWeights[model->mPosition[vc_idx]] = wght;
  1681. }
  1682. }
  1683. }
  1684. // Add instance to scene for this model
  1685. LLMatrix4 transformation;
  1686. transformation.initScale(mesh_scale_vector);
  1687. transformation.setTranslation(mesh_translation_vector);
  1688. transformation *= mTransform;
  1689. std::map<std::string, LLImportMaterial> materials;
  1690. for (U32 i = 0; i < model->mMaterialList.size(); ++i)
  1691. {
  1692. materials[model->mMaterialList[i]] = LLImportMaterial();
  1693. }
  1694. mScene[transformation].emplace_back(model, model->mLabel, transformation,
  1695. materials);
  1696. stretch_extents(model, transformation, mExtents[0], mExtents[1],
  1697. mFirstTransform);
  1698. }
  1699. void LLDAELoader::buildJointToNodeMappingFromScene(daeElement* rootp)
  1700. {
  1701. daeElement* scenep = rootp ? rootp->getDescendant("visual_scene") : NULL;
  1702. if (scenep)
  1703. {
  1704. daeTArray<daeSmartRef<daeElement> > children = scenep->getChildren();
  1705. for (S32 i = 0, count = children.getCount(); i < count; ++i)
  1706. {
  1707. domNode* nodep = daeSafeCast<domNode>(children[i]);
  1708. processJointToNodeMapping(nodep);
  1709. }
  1710. }
  1711. }
  1712. void LLDAELoader::processJointToNodeMapping(domNode* nodep)
  1713. {
  1714. if (!nodep)
  1715. {
  1716. llwarns << "NULL node pointer passed" << llendl;
  1717. return;
  1718. }
  1719. if (isNodeAJoint(nodep))
  1720. {
  1721. // Store the parent
  1722. std::string nodeName = nodep->getName();
  1723. if (!nodeName.empty())
  1724. {
  1725. mJointsFromNode.push_front(nodep->getName());
  1726. }
  1727. }
  1728. // Process the children, if any.
  1729. processChildJoints(nodep);
  1730. }
  1731. void LLDAELoader::processChildJoints(domNode* parent_node)
  1732. {
  1733. if (parent_node)
  1734. {
  1735. daeTArray<daeSmartRef<daeElement> > grand_child =
  1736. parent_node->getChildren();
  1737. for (S32 i = 0, count = grand_child.getCount(); i < count; ++i)
  1738. {
  1739. domNode* nodep = daeSafeCast<domNode>(grand_child[i]);
  1740. if (nodep)
  1741. {
  1742. processJointToNodeMapping(nodep);
  1743. }
  1744. }
  1745. }
  1746. }
  1747. bool LLDAELoader::isNodeAJoint(domNode* nodep)
  1748. {
  1749. return nodep && LLModelLoader::isNodeAJoint(nodep->getName());
  1750. }
  1751. bool LLDAELoader::verifyCount(S32 expected, S32 result)
  1752. {
  1753. if (expected != result)
  1754. {
  1755. llwarns << "Error. Expected: "<< expected << " - Got: " << result
  1756. << "vertice" << llendl;
  1757. return false;
  1758. }
  1759. return true;
  1760. }
  1761. bool LLDAELoader::verifyController(domController* controllerp)
  1762. {
  1763. bool result = true;
  1764. domSkin* skinp = controllerp->getSkin();
  1765. if (skinp)
  1766. {
  1767. xsAnyURI& uri = skinp->getSource();
  1768. domElement* elementp = uri.getElement();
  1769. if (!elementp)
  1770. {
  1771. llinfos << "Cannot resolve skin source" << llendl;
  1772. return false;
  1773. }
  1774. daeString type_str = elementp->getTypeName();
  1775. if (stricmp(type_str, "geometry") == 0)
  1776. {
  1777. // Skin is referenced directly by geometry; get the vertice count
  1778. // from skin
  1779. domSkin::domVertex_weights* vertweightp =
  1780. skinp->getVertex_weights();
  1781. if (!vertweightp)
  1782. {
  1783. llwarns << "No weigths !" << llendl;
  1784. return false;
  1785. }
  1786. S32 vert_weights_count = vertweightp->getCount();
  1787. domGeometry* geometryp =
  1788. (domGeometry*)((domElement*)uri.getElement());
  1789. if (!geometryp)
  1790. {
  1791. llwarns << "No geometry !" << llendl;
  1792. return false;
  1793. }
  1794. domMesh* meshp = geometryp->getMesh();
  1795. if (meshp)
  1796. {
  1797. // Get vertice count from geometry
  1798. domVertices* verticesp = meshp->getVertices();
  1799. if (!verticesp)
  1800. {
  1801. llwarns << "No vertex !" << llendl;
  1802. return false;
  1803. }
  1804. xsAnyURI src = verticesp->getInput_array()[0]->getSource();
  1805. domSource* sourcep =
  1806. (domSource*)((domElement*)src.getElement());
  1807. if (!sourcep)
  1808. {
  1809. llwarns << "No source !" << llendl;
  1810. return false;
  1811. }
  1812. U32 vert_count =
  1813. sourcep->getTechnique_common()->getAccessor()->getCount();
  1814. result = verifyCount(vert_count, vert_weights_count);
  1815. if (!result)
  1816. {
  1817. return result;
  1818. }
  1819. }
  1820. S32 vcnt_count = vertweightp->getVcount()->getValue().getCount();
  1821. result = verifyCount(vcnt_count, vert_weights_count);
  1822. if (!result)
  1823. {
  1824. return result;
  1825. }
  1826. domInputLocalOffset_Array& inputs = vertweightp->getInput_array();
  1827. S32 sum = 0;
  1828. for (S32 i = 0; i < vcnt_count; ++i)
  1829. {
  1830. sum += vertweightp->getVcount()->getValue()[i];
  1831. }
  1832. result = verifyCount(sum * inputs.getCount(),
  1833. (S32)vertweightp->getV()->getValue().getCount());
  1834. }
  1835. }
  1836. return result;
  1837. }
  1838. void LLDAELoader::extractTranslation(domTranslate* translatep,
  1839. LLMatrix4& transform)
  1840. {
  1841. domFloat3 joint_trans = translatep->getValue();
  1842. LLVector3 single_joint_trans(joint_trans[0], joint_trans[1],
  1843. joint_trans[2]);
  1844. transform.setTranslation(single_joint_trans);
  1845. }
  1846. void LLDAELoader::extractTranslationViaElement(daeElement* translate_elemp,
  1847. LLMatrix4& transform)
  1848. {
  1849. if (translate_elemp)
  1850. {
  1851. domTranslate* trans_childp =
  1852. dynamic_cast<domTranslate*>(translate_elemp);
  1853. if (trans_childp)
  1854. {
  1855. domFloat3 translate_child = trans_childp->getValue();
  1856. LLVector3 single_joint_trans(translate_child[0],
  1857. translate_child[1],
  1858. translate_child[2]);
  1859. transform.setTranslation(single_joint_trans);
  1860. }
  1861. }
  1862. }
  1863. void LLDAELoader::extractTranslationViaSID(daeElement* elementp,
  1864. LLMatrix4& transform)
  1865. {
  1866. if (elementp)
  1867. {
  1868. daeSIDResolver resolver(elementp, "./transform");
  1869. domMatrix* matrixp = daeSafeCast<domMatrix>(resolver.getElement());
  1870. // We are only extracting out the translational component atm
  1871. LLMatrix4 working_transform;
  1872. if (matrixp)
  1873. {
  1874. domFloat4x4 domArray = matrixp->getValue();
  1875. for (S32 i = 0; i < 4; ++i)
  1876. {
  1877. for (S32 j = 0; j < 4; ++j)
  1878. {
  1879. working_transform.mMatrix[i][j] = domArray[i + j * 4];
  1880. }
  1881. }
  1882. LLVector3 trans = working_transform.getTranslation();
  1883. transform.setTranslation(trans);
  1884. }
  1885. }
  1886. else
  1887. {
  1888. llwarns << "Element is nonexistent; empty/unsupported node." << llendl;
  1889. }
  1890. }
  1891. void LLDAELoader::processJointNode(domNode* nodep,
  1892. JointTransformMap& jointTransforms)
  1893. {
  1894. if (!nodep->getName())
  1895. {
  1896. llwarns << "Nameless node, cannot process" << llendl;
  1897. return;
  1898. }
  1899. // 1. handle the incoming node - extract out translation via SID or element
  1900. if (isNodeAJoint(nodep))
  1901. {
  1902. LLMatrix4 working_transform;
  1903. // Pull out the translate id and store it in the jointTranslations map
  1904. daeSIDResolver joint_rsv_a(nodep, "./translate");
  1905. domTranslate* trans_a =
  1906. daeSafeCast<domTranslate>(joint_rsv_a.getElement());
  1907. daeSIDResolver joint_rsv_b(nodep, "./location");
  1908. domTranslate* trans_b =
  1909. daeSafeCast<domTranslate>(joint_rsv_b.getElement());
  1910. // Translation via SID was successful
  1911. if (trans_a)
  1912. {
  1913. extractTranslation(trans_a, working_transform);
  1914. }
  1915. else if (trans_b)
  1916. {
  1917. extractTranslation(trans_b, working_transform);
  1918. }
  1919. else
  1920. {
  1921. // Translation via child from element
  1922. daeElement* translate_elemp = getChildFromElement(nodep,
  1923. "translate");
  1924. if (!translate_elemp ||
  1925. translate_elemp->typeID() != domTranslate::ID())
  1926. {
  1927. daeSIDResolver jointResolver(nodep, "./matrix");
  1928. domMatrix* matrixp =
  1929. daeSafeCast<domMatrix>(jointResolver.getElement());
  1930. if (matrixp)
  1931. {
  1932. domFloat4x4 domArray = matrixp->getValue();
  1933. for (S32 i = 0; i < 4; ++i)
  1934. {
  1935. for (S32 j = 0; j < 4; ++j)
  1936. {
  1937. working_transform.mMatrix[i][j] =
  1938. domArray[i + j * 4];
  1939. }
  1940. }
  1941. }
  1942. else
  1943. {
  1944. llwarns << "The element found is not translate or matrix node; most likely a corrupt export !"
  1945. << llendl;
  1946. }
  1947. }
  1948. else
  1949. {
  1950. extractTranslationViaElement(translate_elemp,
  1951. working_transform);
  1952. }
  1953. }
  1954. // Store the working transform relative to the nodes name.
  1955. jointTransforms[nodep->getName()] = working_transform;
  1956. }
  1957. // 2. handle the nodes children
  1958. // Gather and handle the incoming nodes children
  1959. daeTArray<daeSmartRef<daeElement> > grand_child = nodep->getChildren();
  1960. S32 grand_child_count = grand_child.getCount();
  1961. for (S32 i = 0; i < grand_child_count; ++i)
  1962. {
  1963. domNode* child_nodep = daeSafeCast<domNode>(grand_child[i]);
  1964. if (child_nodep)
  1965. {
  1966. processJointNode(child_nodep, jointTransforms);
  1967. }
  1968. }
  1969. }
  1970. daeElement* LLDAELoader::getChildFromElement(daeElement* elementp,
  1971. const std::string& name)
  1972. {
  1973. daeElement* element_chilp = elementp->getChild(name.c_str());
  1974. if (element_chilp)
  1975. {
  1976. return element_chilp;
  1977. }
  1978. LL_DEBUGS("MeshUpload") << "Could not find child '" << name
  1979. << "' for element '"
  1980. << elementp->getAttribute("id") << "'" << llendl;
  1981. return NULL;
  1982. }
  1983. void LLDAELoader::processElement(daeElement* element, bool& bad_element,
  1984. DAE* dae)
  1985. {
  1986. LLMatrix4 saved_transform;
  1987. bool pushed_mat = false;
  1988. domNode* node = daeSafeCast<domNode>(element);
  1989. if (node)
  1990. {
  1991. pushed_mat = true;
  1992. saved_transform = mTransform;
  1993. }
  1994. domTranslate* translate = daeSafeCast<domTranslate>(element);
  1995. if (translate)
  1996. {
  1997. domFloat3 dom_value = translate->getValue();
  1998. LLMatrix4 translation;
  1999. translation.setTranslation(LLVector3(dom_value[0], dom_value[1],
  2000. dom_value[2]));
  2001. translation *= mTransform;
  2002. mTransform = translation;
  2003. mTransform.condition();
  2004. }
  2005. domRotate* rotate = daeSafeCast<domRotate>(element);
  2006. if (rotate)
  2007. {
  2008. domFloat4 dom_value = rotate->getValue();
  2009. LLMatrix4 rotation;
  2010. rotation.initRotTrans(dom_value[3] * DEG_TO_RAD,
  2011. LLVector3(dom_value[0], dom_value[1],
  2012. dom_value[2]),
  2013. LLVector3(0.f, 0.f, 0.f));
  2014. rotation *= mTransform;
  2015. mTransform = rotation;
  2016. mTransform.condition();
  2017. }
  2018. domScale* scale = daeSafeCast<domScale>(element);
  2019. if (scale)
  2020. {
  2021. domFloat3 dom_value = scale->getValue();
  2022. LLVector3 scale_vector = LLVector3(dom_value[0], dom_value[1],
  2023. dom_value[2]);
  2024. // Set all values positive, since we do not currently support mirrored
  2025. // meshes
  2026. scale_vector.abs();
  2027. LLMatrix4 scaling;
  2028. scaling.initScale(scale_vector);
  2029. scaling *= mTransform;
  2030. mTransform = scaling;
  2031. mTransform.condition();
  2032. }
  2033. domMatrix* matrix = daeSafeCast<domMatrix>(element);
  2034. if (matrix)
  2035. {
  2036. domFloat4x4 dom_value = matrix->getValue();
  2037. LLMatrix4 matrix_transform;
  2038. for (S32 i = 0; i < 4; i++)
  2039. {
  2040. for (S32 j = 0; j < 4; j++)
  2041. {
  2042. matrix_transform.mMatrix[i][j] = dom_value[i + j * 4];
  2043. }
  2044. }
  2045. matrix_transform *= mTransform;
  2046. mTransform = matrix_transform;
  2047. mTransform.condition();
  2048. }
  2049. domInstance_geometry* instance_geo =
  2050. daeSafeCast<domInstance_geometry>(element);
  2051. if (instance_geo)
  2052. {
  2053. domGeometry* geo =
  2054. daeSafeCast<domGeometry>(instance_geo->getUrl().getElement());
  2055. if (geo)
  2056. {
  2057. domMesh* mesh =
  2058. daeSafeCast<domMesh>(geo->getDescendant(daeElement::matchType(domMesh::ID())));
  2059. if (mesh)
  2060. {
  2061. std::vector<LLPointer<LLModel> >::iterator i = mModelsMap[mesh].begin();
  2062. while (i != mModelsMap[mesh].end())
  2063. {
  2064. LLModel* model = *i;
  2065. LLMatrix4 transformation = mTransform;
  2066. if (mTransform.determinant() < 0)
  2067. {
  2068. // Negative scales are not supported
  2069. llwarns << "Negative scale detected, unsupported transform. domInstance_geometry: "
  2070. << getElementLabel(instance_geo) << llendl;
  2071. LLSD args;
  2072. args["Message"] = "NegativeScaleTrans";
  2073. args["LABEL"] = getElementLabel(instance_geo);
  2074. mWarningsArray.append(args);
  2075. bad_element = true;
  2076. }
  2077. LLModelLoader::material_map materials = getMaterials(model,
  2078. instance_geo,
  2079. dae);
  2080. // Adjust the transformation to compensate for mesh
  2081. // normalization
  2082. LLVector3 mesh_scale_vector;
  2083. LLVector3 mesh_translation_vector;
  2084. model->getNormalizedScaleTranslation(mesh_scale_vector,
  2085. mesh_translation_vector);
  2086. LLMatrix4 mesh_translation;
  2087. mesh_translation.setTranslation(mesh_translation_vector);
  2088. mesh_translation *= transformation;
  2089. transformation = mesh_translation;
  2090. LLMatrix4 mesh_scale;
  2091. mesh_scale.initScale(mesh_scale_vector);
  2092. mesh_scale *= transformation;
  2093. transformation = mesh_scale;
  2094. if (transformation.determinant() < 0)
  2095. {
  2096. // Negative scales are not supported
  2097. llwarns << "Negative scale detected, unsupported post-normalization transform. domInstance_geometry: "
  2098. << getElementLabel(instance_geo) << llendl;
  2099. LLSD args;
  2100. args["Message"] = "NegativeScaleNormTrans";
  2101. args["LABEL"] = getElementLabel(instance_geo);
  2102. mWarningsArray.append(args);
  2103. bad_element = true;
  2104. }
  2105. std::string label;
  2106. if (model->mLabel.empty())
  2107. {
  2108. label = getLodlessLabel(instance_geo);
  2109. llassert(!label.empty());
  2110. if (model->mSubmodelID)
  2111. {
  2112. label += (char)((int)'a' + model->mSubmodelID);
  2113. }
  2114. model->mLabel = label + lod_suffix[mLod];
  2115. }
  2116. else
  2117. {
  2118. // Do not change model's name if possible, it will play
  2119. // havoc with scenes that already use said model.
  2120. size_t ext_pos = getSuffixPosition(model->mLabel);
  2121. if (ext_pos != std::string::npos)
  2122. {
  2123. label = model->mLabel.substr(0, ext_pos);
  2124. }
  2125. else
  2126. {
  2127. label = model->mLabel;
  2128. }
  2129. }
  2130. mScene[transformation].emplace_back(model, label,
  2131. transformation,
  2132. materials);
  2133. stretch_extents(model, transformation, mExtents[0],
  2134. mExtents[1], mFirstTransform);
  2135. ++i;
  2136. }
  2137. }
  2138. }
  2139. else
  2140. {
  2141. llwarns << "Unable to resolve geometry URL." << llendl;
  2142. LLSD args;
  2143. args["Message"] = "CantResolveGeometryUrl";
  2144. mWarningsArray.append(args);
  2145. bad_element = true;
  2146. }
  2147. }
  2148. domInstance_node* instance_node = daeSafeCast<domInstance_node>(element);
  2149. if (instance_node)
  2150. {
  2151. daeElement* instance = instance_node->getUrl().getElement();
  2152. if (instance)
  2153. {
  2154. processElement(instance,bad_element, dae);
  2155. }
  2156. }
  2157. // Process children
  2158. daeTArray<daeSmartRef<daeElement> > children = element->getChildren();
  2159. for (S32 i = 0, child_count = children.getCount(); i < child_count; ++i)
  2160. {
  2161. processElement(children[i], bad_element, dae);
  2162. }
  2163. if (pushed_mat)
  2164. {
  2165. // This element was a node, restore transform before processing
  2166. // siblings
  2167. mTransform = saved_transform;
  2168. }
  2169. }
  2170. std::map<std::string, LLImportMaterial> LLDAELoader::getMaterials(LLModel* model,
  2171. domInstance_geometry* instance_geo,
  2172. DAE* dae)
  2173. {
  2174. std::map<std::string, LLImportMaterial> materials;
  2175. for (size_t i = 0; i < model->mMaterialList.size(); ++i)
  2176. {
  2177. LLImportMaterial import_material;
  2178. domInstance_material* instance_mat = NULL;
  2179. domBind_material::domTechnique_common* technique =
  2180. daeSafeCast<domBind_material::domTechnique_common>(instance_geo->getDescendant(daeElement::matchType(domBind_material::domTechnique_common::ID())));
  2181. if (technique)
  2182. {
  2183. daeTArray<daeSmartRef<domInstance_material> > inst_materials = technique->getChildrenByType<domInstance_material>();
  2184. for (S32 j = 0, cnt = inst_materials.getCount(); j < cnt; ++j)
  2185. {
  2186. std::string symbol(inst_materials[j]->getSymbol());
  2187. if (symbol == model->mMaterialList[i])
  2188. {
  2189. // Found the binding
  2190. instance_mat = inst_materials[j];
  2191. break;
  2192. }
  2193. }
  2194. }
  2195. if (instance_mat)
  2196. {
  2197. domMaterial* material =
  2198. daeSafeCast<domMaterial>(instance_mat->getTarget().getElement());
  2199. if (material)
  2200. {
  2201. domInstance_effect* instance_effect =
  2202. daeSafeCast<domInstance_effect>(material->getDescendant(daeElement::matchType(domInstance_effect::ID())));
  2203. if (instance_effect)
  2204. {
  2205. domEffect* effect =
  2206. daeSafeCast<domEffect>(instance_effect->getUrl().getElement());
  2207. if (effect)
  2208. {
  2209. domProfile_COMMON* profile =
  2210. daeSafeCast<domProfile_COMMON>(effect->getDescendant(daeElement::matchType(domProfile_COMMON::ID())));
  2211. if (profile)
  2212. {
  2213. import_material = profileToMaterial(profile, dae);
  2214. }
  2215. }
  2216. }
  2217. }
  2218. }
  2219. import_material.mBinding = model->mMaterialList[i];
  2220. materials[model->mMaterialList[i]] = import_material;
  2221. }
  2222. return materials;
  2223. }
  2224. LLImportMaterial LLDAELoader::profileToMaterial(domProfile_COMMON* material,
  2225. DAE* dae)
  2226. {
  2227. LLImportMaterial mat;
  2228. mat.mFullbright = false;
  2229. daeElement* diffuse = material->getDescendant("diffuse");
  2230. if (diffuse)
  2231. {
  2232. domCommon_color_or_texture_type_complexType::domTexture* texture =
  2233. daeSafeCast<domCommon_color_or_texture_type_complexType::domTexture>(diffuse->getDescendant("texture"));
  2234. if (texture)
  2235. {
  2236. domCommon_newparam_type_Array newparams = material->getNewparam_array();
  2237. if (newparams.getCount())
  2238. {
  2239. for (S32 i = 0, cnt = newparams.getCount(); i < cnt; ++i)
  2240. {
  2241. domFx_surface_common* surface = newparams[i]->getSurface();
  2242. if (surface)
  2243. {
  2244. domFx_surface_init_common* init =
  2245. surface->getFx_surface_init_common();
  2246. if (init)
  2247. {
  2248. domFx_surface_init_from_common_Array init_from =
  2249. init->getInit_from_array();
  2250. if ((S32)init_from.getCount() > i)
  2251. {
  2252. domImage* image =
  2253. daeSafeCast<domImage>(init_from[i]->getValue().getElement());
  2254. if (image)
  2255. {
  2256. // We only support init_from now - embedded
  2257. // data will come later
  2258. domImage::domInit_from* initfm;
  2259. initfm = image->getInit_from();
  2260. if (initfm)
  2261. {
  2262. mat.mDiffuseMapFilename =
  2263. cdom::uriToNativePath(initfm->getValue().str());
  2264. mat.mDiffuseMapLabel =
  2265. getElementLabel(material);
  2266. }
  2267. }
  2268. }
  2269. }
  2270. }
  2271. }
  2272. }
  2273. else if (texture->getTexture())
  2274. {
  2275. domImage* image = NULL;
  2276. dae->getDatabase()->getElement((daeElement**)&image, 0,
  2277. texture->getTexture(),
  2278. COLLADA_TYPE_IMAGE);
  2279. if (image)
  2280. {
  2281. // We only support init_from now - embedded data will come
  2282. // later
  2283. domImage::domInit_from* init = image->getInit_from();
  2284. if (init)
  2285. {
  2286. std::string img_path_val =
  2287. cdom::uriToNativePath(init->getValue().str());
  2288. #if LL_WINDOWS
  2289. // Work-around DOM tendency to resort to UNC names
  2290. // which are only confusing for downstream...
  2291. std::string::iterator i = img_path_val.begin();
  2292. while (*i == '\\')
  2293. {
  2294. ++i;
  2295. }
  2296. mat.mDiffuseMapFilename.assign(i, img_path_val.end());
  2297. #else
  2298. mat.mDiffuseMapFilename = img_path_val;
  2299. #endif
  2300. mat.mDiffuseMapLabel = getElementLabel(material);
  2301. }
  2302. }
  2303. }
  2304. }
  2305. domCommon_color_or_texture_type_complexType::domColor* color =
  2306. daeSafeCast<domCommon_color_or_texture_type_complexType::domColor>(diffuse->getDescendant("color"));
  2307. if (color)
  2308. {
  2309. domFx_color_common domfx_color = color->getValue();
  2310. LLColor4 value = LLColor4(domfx_color[0], domfx_color[1],
  2311. domfx_color[2], domfx_color[3]);
  2312. mat.mDiffuseColor = value;
  2313. }
  2314. }
  2315. daeElement* emission = material->getDescendant("emission");
  2316. if (emission)
  2317. {
  2318. LLColor4 emission_color = getDaeColor(emission);
  2319. if (((emission_color[0] + emission_color[1] +
  2320. emission_color[2]) / 3.f) > 0.25f)
  2321. {
  2322. mat.mFullbright = true;
  2323. }
  2324. }
  2325. return mat;
  2326. }
  2327. // Try to get a decent label for this element
  2328. std::string LLDAELoader::getElementLabel(daeElement* element)
  2329. {
  2330. std::string name;
  2331. if (!element)
  2332. {
  2333. return name;
  2334. }
  2335. // If we have a name attribute, use it
  2336. name = element->getAttribute("name");
  2337. if (name.length())
  2338. {
  2339. return name;
  2340. }
  2341. // If we have an ID attribute, use it
  2342. if (element->getID())
  2343. {
  2344. return std::string(element->getID());
  2345. }
  2346. // If we have a parent, use it
  2347. daeElement* parent = element->getParent();
  2348. std::string index_string;
  2349. if (parent)
  2350. {
  2351. // Retrieve index to distinguish items inside same parent
  2352. size_t ind = 0;
  2353. parent->getChildren().find(element, ind);
  2354. if (ind > 0)
  2355. {
  2356. index_string = "_" + boost::lexical_cast<std::string>(ind);
  2357. }
  2358. // If parent has a name or ID, use it
  2359. std::string name = parent->getAttribute("name");
  2360. if (!name.length())
  2361. {
  2362. name = std::string(parent->getID());
  2363. }
  2364. if (name.length())
  2365. {
  2366. // Make sure that index will not mix up with pre-named LOD
  2367. // extensions
  2368. size_t ext_pos = getSuffixPosition(name);
  2369. if (ext_pos == std::string::npos)
  2370. {
  2371. return name + index_string;
  2372. }
  2373. else
  2374. {
  2375. return name.insert(ext_pos, index_string);
  2376. }
  2377. }
  2378. }
  2379. // Try to use our type
  2380. daeString element_name = element->getElementName();
  2381. if (element_name)
  2382. {
  2383. return std::string(element_name) + index_string;
  2384. }
  2385. // If all else fails, use "object"
  2386. return "object" + index_string;
  2387. }
  2388. //static
  2389. size_t LLDAELoader::getSuffixPosition(const std::string& label)
  2390. {
  2391. if (label.find("_LOD") != std::string::npos ||
  2392. label.find("_PHYS") != std::string::npos)
  2393. {
  2394. return label.rfind('_');
  2395. }
  2396. return std::string::npos;
  2397. }
  2398. //static
  2399. std::string LLDAELoader::getLodlessLabel(daeElement* element)
  2400. {
  2401. std::string label = getElementLabel(element);
  2402. size_t ext_pos = getSuffixPosition(label);
  2403. if (ext_pos != std::string::npos)
  2404. {
  2405. return label.substr(0, ext_pos);
  2406. }
  2407. return label;
  2408. }
  2409. LLColor4 LLDAELoader::getDaeColor(daeElement* element)
  2410. {
  2411. LLColor4 value;
  2412. domCommon_color_or_texture_type_complexType::domColor* color =
  2413. daeSafeCast<domCommon_color_or_texture_type_complexType::domColor>(element->getDescendant("color"));
  2414. if (color)
  2415. {
  2416. domFx_color_common domfx_color = color->getValue();
  2417. value = LLColor4(domfx_color[0], domfx_color[1], domfx_color[2],
  2418. domfx_color[3]);
  2419. }
  2420. return value;
  2421. }
  2422. bool LLDAELoader::addVolumeFacesFromDomMesh(LLModel* modelp, domMesh* meshp,
  2423. LLSD& log_msg)
  2424. {
  2425. if (!modelp || !meshp) return false;
  2426. LLModel::EModelStatus status = LLModel::NO_ERRORS;
  2427. domTriangles_Array& tris = meshp->getTriangles_array();
  2428. for (U32 i = 0, count = tris.getCount(); i < count; ++i)
  2429. {
  2430. domTrianglesRef& tri = tris.get(i);
  2431. status = load_face_from_dom_tris(modelp->getVolumeFaces(),
  2432. modelp->getMaterialList(), tri,
  2433. log_msg);
  2434. modelp->mStatus = status;
  2435. if (status != LLModel::NO_ERRORS)
  2436. {
  2437. modelp->clearFacesAndMaterials();
  2438. return false;
  2439. }
  2440. }
  2441. domPolylist_Array& polys = meshp->getPolylist_array();
  2442. for (U32 i = 0, count = polys.getCount(); i < count; ++i)
  2443. {
  2444. domPolylistRef& poly = polys.get(i);
  2445. status = load_face_from_dom_polylist(modelp->getVolumeFaces(),
  2446. modelp->getMaterialList(), poly,
  2447. log_msg);
  2448. if (status != LLModel::NO_ERRORS)
  2449. {
  2450. modelp->clearFacesAndMaterials();
  2451. return false;
  2452. }
  2453. }
  2454. domPolygons_Array& polygons = meshp->getPolygons_array();
  2455. for (U32 i = 0, poly_count = polygons.getCount(); i < poly_count; ++i)
  2456. {
  2457. domPolygonsRef& poly = polygons.get(i);
  2458. status = load_face_from_dom_polygons(modelp->getVolumeFaces(),
  2459. modelp->getMaterialList(), poly);
  2460. if (status != LLModel::NO_ERRORS)
  2461. {
  2462. modelp->clearFacesAndMaterials();
  2463. return false;
  2464. }
  2465. }
  2466. // If we are missing normals, do a quick and dirty calculation of them.
  2467. // Use the normals of each vertex' connected faces and sum them up. Should
  2468. // the user select "Generate normals" from the mesh upload floater, more
  2469. // accurate normals will replace these.
  2470. LLVolume::face_list_t vol_faces = modelp->getVolumeFaces();
  2471. for (LLVolume::face_list_t::iterator it = vol_faces.begin(),
  2472. end = vol_faces.end();
  2473. it != end; ++it)
  2474. {
  2475. LLVolumeFace& face = *it;
  2476. if (face.mNormals || !face.mIndices || face.mNumIndices % 3)
  2477. {
  2478. continue;
  2479. }
  2480. face.mNormals = face.mPositions + face.mNumVertices;
  2481. for (S32 i = 0, count = face.mNumVertices; i < count; ++i)
  2482. {
  2483. face.mNormals[i].clear();
  2484. }
  2485. for (S32 i = 0, count = face.mNumIndices; i < count; )
  2486. {
  2487. LLVector4a v0(face.mPositions[face.mIndices[i]]);
  2488. LLVector4a v1(face.mPositions[face.mIndices[i + 1]]);
  2489. LLVector4a v2(face.mPositions[face.mIndices[i + 2]]);
  2490. LLVector4a normal;
  2491. v2.sub(v1);
  2492. v1.sub(v0);
  2493. normal.setCross3(v1, v2);
  2494. normal.normalize3();
  2495. face.mNormals[face.mIndices[i++]].add(normal);
  2496. face.mNormals[face.mIndices[i++]].add(normal);
  2497. face.mNormals[face.mIndices[i++]].add(normal);
  2498. }
  2499. for (S32 i = 0, count = face.mNumVertices; i < count; ++i)
  2500. {
  2501. face.mNormals[i].normalize3();
  2502. }
  2503. }
  2504. return status == LLModel::NO_ERRORS;
  2505. }
  2506. // Diff version supports creating multiple models when material counts spill
  2507. // over the 8 face server-side limit
  2508. //static
  2509. bool LLDAELoader::loadModelsFromDomMesh(domMesh* mesh,
  2510. std::vector<LLModel*>& models_out,
  2511. U32 submodel_limit)
  2512. {
  2513. LLVolumeParams volume_params;
  2514. volume_params.setType(LL_PCODE_PROFILE_SQUARE, LL_PCODE_PATH_LINE);
  2515. models_out.clear();
  2516. LLModel* ret = new LLModel(volume_params, 0.f);
  2517. std::string model_name = getLodlessLabel(mesh);
  2518. ret->mLabel = model_name + lod_suffix[mLod];
  2519. llassert(!ret->mLabel.empty());
  2520. ret->clearFacesAndMaterials();
  2521. // Get the whole set of volume faces
  2522. addVolumeFacesFromDomMesh(ret, mesh, mWarningsArray);
  2523. U32 volume_faces = ret->getNumVolumeFaces();
  2524. // Side-steps all manner of issues when splitting models and matching lower
  2525. // LOD materials to base models
  2526. ret->sortVolumeFacesByMaterialName();
  2527. #if !LL_NORMALIZE_ALL_MODELS
  2528. bool normalized = false;
  2529. #endif
  2530. S32 submodel_id = 0;
  2531. // Remove all faces that definitely would not fit into one model and
  2532. // sub-model limit.
  2533. U32 face_limit = (submodel_limit + 1) * LL_SCULPT_MESH_MAX_FACES;
  2534. if (face_limit < volume_faces)
  2535. {
  2536. ret->setNumVolumeFaces(face_limit);
  2537. }
  2538. LLVolume::face_list_t remainder;
  2539. do
  2540. {
  2541. // Ensure we do this once with the whole gang and not per-model
  2542. #if !LL_NORMALIZE_ALL_MODELS
  2543. if (!normalized && !mNoNormalize)
  2544. {
  2545. normalized = true;
  2546. ret->normalizeVolumeFaces();
  2547. }
  2548. #endif
  2549. ret->trimVolumeFacesToSize(LL_SCULPT_MESH_MAX_FACES, &remainder);
  2550. // Remove unused/redundant vertices after normalizing
  2551. if (!mNoOptimize)
  2552. {
  2553. ret->remapVolumeFaces();
  2554. }
  2555. volume_faces = remainder.size();
  2556. models_out.push_back(ret);
  2557. // If we have left-over volume faces, create another model to absorb
  2558. // them...
  2559. if (volume_faces)
  2560. {
  2561. LLModel* next = new LLModel(volume_params, 0.f);
  2562. next->mSubmodelID = ++submodel_id;
  2563. next->mLabel = model_name + (char)((int)'a' + next->mSubmodelID) +
  2564. lod_suffix[mLod];
  2565. next->getVolumeFaces() = remainder;
  2566. next->mNormalizedScale = ret->mNormalizedScale;
  2567. next->mNormalizedTranslation = ret->mNormalizedTranslation;
  2568. if ((S32)ret->mMaterialList.size() > LL_SCULPT_MESH_MAX_FACES)
  2569. {
  2570. next->mMaterialList.assign(ret->mMaterialList.begin() +
  2571. LL_SCULPT_MESH_MAX_FACES,
  2572. ret->mMaterialList.end());
  2573. }
  2574. ret = next;
  2575. }
  2576. remainder.clear();
  2577. }
  2578. while (volume_faces);
  2579. return true;
  2580. }