llavatarappearance.cpp 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  1. /**
  2. * @File llavatarappearance.cpp
  3. * @brief Implementation of LLAvatarAppearance class
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, 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 "boost/lexical_cast.hpp"
  34. #include "boost/tokenizer.hpp"
  35. #include "llavatarappearance.h"
  36. #include "llavatarappearancedefines.h"
  37. #include "llavatarjointmesh.h"
  38. #include "lldir.h"
  39. #include "llpolymesh.h"
  40. #include "llpolymorph.h"
  41. #include "llpolyskeletaldistortion.h"
  42. #include "llstl.h"
  43. #include "lltexglobalcolor.h"
  44. #include "llwearabledata.h"
  45. #include "imageids.h"
  46. using namespace LLAvatarAppearanceDefines;
  47. // Constants
  48. const std::string AVATAR_DEFAULT_CHAR = "avatar_lad.xml";
  49. const LLColor4 DUMMY_COLOR = LLColor4(0.5f, 0.5f, 0.5f, 1.f);
  50. // Static variables
  51. LLAvatarSkeletonInfo* LLAvatarAppearance::sAvatarSkeletonInfo = NULL;
  52. LLAvatarAppearance::LLAvatarXmlInfo* LLAvatarAppearance::sAvatarXmlInfo = NULL;
  53. //------------------------------------------------------------------------
  54. // LLAvatarBoneInfo class
  55. // Trans/Scale/Rot etc. info about each avatar bone. Used by
  56. // LLVOAvatarSkeleton.
  57. //------------------------------------------------------------------------
  58. class LLAvatarBoneInfo
  59. {
  60. friend class LLAvatarAppearance;
  61. friend class LLAvatarSkeletonInfo;
  62. public:
  63. LLAvatarBoneInfo()
  64. : mIsJoint(false)
  65. {
  66. }
  67. ~LLAvatarBoneInfo()
  68. {
  69. std::for_each(mChildList.begin(), mChildList.end(), DeletePointer());
  70. mChildList.clear();
  71. }
  72. bool parseXml(LLXmlTreeNode* node);
  73. private:
  74. LLVector3 mPos;
  75. LLVector3 mEnd;
  76. LLVector3 mRot;
  77. LLVector3 mScale;
  78. LLVector3 mPivot;
  79. std::string mName;
  80. std::string mSupport;
  81. std::string mAliases;
  82. bool mIsJoint;
  83. typedef std::vector<LLAvatarBoneInfo*> child_list_t;
  84. child_list_t mChildList;
  85. };
  86. bool LLAvatarBoneInfo::parseXml(LLXmlTreeNode* node)
  87. {
  88. if (node->hasName("bone"))
  89. {
  90. mIsJoint = true;
  91. static LLStdStringHandle name_string =
  92. LLXmlTree::addAttributeString("name");
  93. if (!node->getFastAttributeString(name_string, mName))
  94. {
  95. llwarns << "Bone without name" << llendl;
  96. return false;
  97. }
  98. static LLStdStringHandle aliases_string =
  99. LLXmlTree::addAttributeString("aliases");
  100. // Note: aliases are not required.
  101. node->getFastAttributeString(aliases_string, mAliases);
  102. }
  103. else if (node->hasName("collision_volume"))
  104. {
  105. mIsJoint = false;
  106. static LLStdStringHandle name_string =
  107. LLXmlTree::addAttributeString("name");
  108. if (!node->getFastAttributeString(name_string, mName))
  109. {
  110. mName = "Collision Volume";
  111. }
  112. }
  113. else
  114. {
  115. llwarns << "Invalid node " << node->getName() << llendl;
  116. return false;
  117. }
  118. static LLStdStringHandle pos_string = LLXmlTree::addAttributeString("pos");
  119. if (!node->getFastAttributeVector3(pos_string, mPos))
  120. {
  121. llwarns << "Bone without position" << llendl;
  122. return false;
  123. }
  124. static LLStdStringHandle rot_string = LLXmlTree::addAttributeString("rot");
  125. if (!node->getFastAttributeVector3(rot_string, mRot))
  126. {
  127. llwarns << "Bone without rotation" << llendl;
  128. return false;
  129. }
  130. static LLStdStringHandle scale_string =
  131. LLXmlTree::addAttributeString("scale");
  132. if (!node->getFastAttributeVector3(scale_string, mScale))
  133. {
  134. llwarns << "Bone without scale" << llendl;
  135. return false;
  136. }
  137. static LLStdStringHandle end_string = LLXmlTree::addAttributeString("end");
  138. if (!node->getFastAttributeVector3(end_string, mEnd))
  139. {
  140. llwarns << "Bone without end" << llendl;
  141. mEnd = LLVector3(0.0f, 0.0f, 0.0f);
  142. }
  143. static LLStdStringHandle support_string =
  144. LLXmlTree::addAttributeString("support");
  145. if (!node->getFastAttributeString(support_string, mSupport))
  146. {
  147. llwarns << "Bone without support" << llendl;
  148. mSupport = "base";
  149. }
  150. if (mIsJoint)
  151. {
  152. static LLStdStringHandle pivot_string =
  153. LLXmlTree::addAttributeString("pivot");
  154. if (!node->getFastAttributeVector3(pivot_string, mPivot))
  155. {
  156. llwarns << "Bone without pivot" << llendl;
  157. return false;
  158. }
  159. }
  160. // Parse children
  161. for (LLXmlTreeNode* child = node->getFirstChild(); child;
  162. child = node->getNextChild())
  163. {
  164. LLAvatarBoneInfo* child_info = new LLAvatarBoneInfo;
  165. if (!child_info->parseXml(child))
  166. {
  167. delete child_info;
  168. return false;
  169. }
  170. mChildList.push_back(child_info);
  171. }
  172. return true;
  173. }
  174. //------------------------------------------------------------------------
  175. // LLAvatarSkeletonInfo class
  176. // Overall avatar skeleton
  177. //------------------------------------------------------------------------
  178. class LLAvatarSkeletonInfo
  179. {
  180. friend class LLAvatarAppearance;
  181. public:
  182. LLAvatarSkeletonInfo()
  183. : mNumBones(0),
  184. mNumCollisionVolumes(0)
  185. {
  186. }
  187. ~LLAvatarSkeletonInfo()
  188. {
  189. std::for_each(mBoneInfoList.begin(), mBoneInfoList.end(),
  190. DeletePointer());
  191. mBoneInfoList.clear();
  192. }
  193. bool parseXml(LLXmlTreeNode* node);
  194. S32 getNumBones() const { return mNumBones; }
  195. S32 getNumCollisionVolumes() const { return mNumCollisionVolumes; }
  196. private:
  197. S32 mNumBones;
  198. S32 mNumCollisionVolumes;
  199. typedef std::vector<LLAvatarBoneInfo*> bone_info_list_t;
  200. bone_info_list_t mBoneInfoList;
  201. };
  202. bool LLAvatarSkeletonInfo::parseXml(LLXmlTreeNode* node)
  203. {
  204. static LLStdStringHandle num_bones_string =
  205. LLXmlTree::addAttributeString("num_bones");
  206. if (!node->getFastAttributeS32(num_bones_string, mNumBones))
  207. {
  208. llwarns << "Couldn't find number of bones." << llendl;
  209. return false;
  210. }
  211. static LLStdStringHandle num_collision_volumes_string =
  212. LLXmlTree::addAttributeString("num_collision_volumes");
  213. node->getFastAttributeS32(num_collision_volumes_string,
  214. mNumCollisionVolumes);
  215. LLXmlTreeNode* child;
  216. for (child = node->getFirstChild(); child; child = node->getNextChild())
  217. {
  218. LLAvatarBoneInfo* info = new LLAvatarBoneInfo;
  219. if (!info->parseXml(child))
  220. {
  221. delete info;
  222. llwarns << "Error parsing bone in skeleton file" << llendl;
  223. return false;
  224. }
  225. mBoneInfoList.push_back(info);
  226. }
  227. return true;
  228. }
  229. // Makes aliases for joint and pushes them to map.
  230. void LLAvatarAppearance::makeJointAliases(LLAvatarBoneInfo* bone_info)
  231. {
  232. if (!bone_info || !bone_info->mIsJoint)
  233. {
  234. return;
  235. }
  236. std::string bone_name = bone_info->mName;
  237. // Actual name is a valid alias; add it.
  238. mJointAliasMap[bone_name] = bone_name;
  239. std::string aliases = bone_info->mAliases;
  240. boost::char_separator<char> sep(" ");
  241. boost::tokenizer<boost::char_separator<char> > tok(aliases, sep);
  242. for (boost::tokenizer<boost::char_separator<char> >::iterator
  243. i = tok.begin(), end = tok.end();
  244. i != end; ++i)
  245. {
  246. if (mJointAliasMap.find(*i) != mJointAliasMap.end())
  247. {
  248. llwarns << "Avatar skeleton joint alias \"" << *i
  249. << "\" remapped from \"" << mJointAliasMap[*i]
  250. << "\" to \"" << bone_name << "\"" << llendl;
  251. }
  252. mJointAliasMap[*i] = bone_name;
  253. }
  254. for (LLAvatarBoneInfo::child_list_t::const_iterator
  255. iter = bone_info->mChildList.begin(),
  256. end = bone_info->mChildList.end();
  257. iter != end; ++iter)
  258. {
  259. makeJointAliases(*iter);
  260. }
  261. }
  262. const joint_alias_map_t& LLAvatarAppearance::getJointAliases()
  263. {
  264. if (mJointAliasMap.empty())
  265. {
  266. for (LLAvatarSkeletonInfo::bone_info_list_t::const_iterator
  267. iter = sAvatarSkeletonInfo->mBoneInfoList.begin(),
  268. end = sAvatarSkeletonInfo->mBoneInfoList.end();
  269. iter != end; ++iter)
  270. {
  271. makeJointAliases(*iter);
  272. }
  273. }
  274. // Also accept the name with spaces substituted with underscores. This
  275. // gives a mechanism for referencing such joints in Collada files which do
  276. // not allow spaces.
  277. std::string underscored;
  278. for (LLAvatarXmlInfo::attachment_info_list_t::iterator
  279. it = sAvatarXmlInfo->mAttachmentInfoList.begin(),
  280. end = sAvatarXmlInfo->mAttachmentInfoList.end();
  281. it != end; ++it)
  282. {
  283. LLAvatarXmlInfo::LLAvatarAttachmentInfo* info = *it;
  284. if (info)
  285. {
  286. const std::string& bone_name = info->mName;
  287. underscored = bone_name;
  288. LLStringUtil::replaceChar(underscored, ' ', '_');
  289. if (underscored != bone_name)
  290. {
  291. mJointAliasMap[underscored] = bone_name;
  292. }
  293. }
  294. }
  295. return mJointAliasMap;
  296. }
  297. LLJoint* LLAvatarAppearance::getSkeletonJoint(S32 num)
  298. {
  299. return num >= 0 && num < (S32)mSkeleton.size() ? mSkeleton[num] : NULL;
  300. }
  301. //-----------------------------------------------------------------------------
  302. // LLAvatarXmlInfo sub-class
  303. //-----------------------------------------------------------------------------
  304. LLAvatarAppearance::LLAvatarXmlInfo::LLAvatarXmlInfo()
  305. : mTexSkinColorInfo(NULL),
  306. mTexHairColorInfo(NULL),
  307. mTexEyeColorInfo(NULL)
  308. {
  309. }
  310. LLAvatarAppearance::LLAvatarXmlInfo::~LLAvatarXmlInfo()
  311. {
  312. std::for_each(mMeshInfoList.begin(), mMeshInfoList.end(), DeletePointer());
  313. mMeshInfoList.clear();
  314. std::for_each(mSkeletalDistortionInfoList.begin(),
  315. mSkeletalDistortionInfoList.end(), DeletePointer());
  316. mSkeletalDistortionInfoList.clear();
  317. std::for_each(mAttachmentInfoList.begin(), mAttachmentInfoList.end(),
  318. DeletePointer());
  319. mAttachmentInfoList.clear();
  320. delete_and_clear(mTexSkinColorInfo);
  321. delete_and_clear(mTexHairColorInfo);
  322. delete_and_clear(mTexEyeColorInfo);
  323. std::for_each(mLayerInfoList.begin(), mLayerInfoList.end(),
  324. DeletePointer());
  325. mLayerInfoList.clear();
  326. std::for_each(mDriverInfoList.begin(), mDriverInfoList.end(),
  327. DeletePointer());
  328. mDriverInfoList.clear();
  329. std::for_each(mMorphMaskInfoList.begin(), mMorphMaskInfoList.end(),
  330. DeletePointer());
  331. mMorphMaskInfoList.clear();
  332. }
  333. bool LLAvatarAppearance::LLAvatarXmlInfo::parseXmlSkeletonNode(LLXmlTreeNode* root)
  334. {
  335. LLXmlTreeNode* node = root->getChildByName("skeleton");
  336. if (!node)
  337. {
  338. llwarns << "avatar file: missing <skeleton>" << llendl;
  339. return false;
  340. }
  341. LLXmlTreeNode* child;
  342. // SKELETON DISTORTIONS
  343. for (child = node->getChildByName("param"); child;
  344. child = node->getNextNamedChild())
  345. {
  346. if (!child->getChildByName("param_skeleton"))
  347. {
  348. if (child->getChildByName("param_morph"))
  349. {
  350. llwarns << "Cannot specify morph param in skeleton definition."
  351. << llendl;
  352. }
  353. else
  354. {
  355. llwarns << "Unknown param type." << llendl;
  356. }
  357. return false;
  358. }
  359. LLPolySkeletalDistortionInfo* info = new LLPolySkeletalDistortionInfo;
  360. if (!info->parseXml(child))
  361. {
  362. delete info;
  363. return false;
  364. }
  365. mSkeletalDistortionInfoList.push_back(info);
  366. }
  367. // ATTACHMENT POINTS
  368. for (child = node->getChildByName("attachment_point"); child;
  369. child = node->getNextNamedChild())
  370. {
  371. LLAvatarAttachmentInfo* info = new LLAvatarAttachmentInfo();
  372. static LLStdStringHandle name_string =
  373. LLXmlTree::addAttributeString("name");
  374. if (!child->getFastAttributeString(name_string, info->mName))
  375. {
  376. llwarns << "No name supplied for attachment point." << llendl;
  377. delete info;
  378. return false;
  379. }
  380. static LLStdStringHandle joint_string =
  381. LLXmlTree::addAttributeString("joint");
  382. if (!child->getFastAttributeString(joint_string, info->mJointName))
  383. {
  384. llwarns << "No bone declared in attachment point " << info->mName
  385. << llendl;
  386. delete info;
  387. return false;
  388. }
  389. info->mJointKey = LLJoint::getKey(info->mJointName);
  390. static LLStdStringHandle position_string =
  391. LLXmlTree::addAttributeString("position");
  392. if (child->getFastAttributeVector3(position_string, info->mPosition))
  393. {
  394. info->mHasPosition = true;
  395. }
  396. static LLStdStringHandle rotation_string =
  397. LLXmlTree::addAttributeString("rotation");
  398. if (child->getFastAttributeVector3(rotation_string, info->mRotationEuler))
  399. {
  400. info->mHasRotation = true;
  401. }
  402. static LLStdStringHandle group_string =
  403. LLXmlTree::addAttributeString("group");
  404. if (child->getFastAttributeS32(group_string, info->mGroup))
  405. {
  406. if (info->mGroup == -1)
  407. info->mGroup = -1111; // -1 = none parsed, < -1 = bad value
  408. }
  409. static LLStdStringHandle id_string =
  410. LLXmlTree::addAttributeString("id");
  411. if (!child->getFastAttributeS32(id_string, info->mAttachmentID))
  412. {
  413. llwarns << "No id supplied for attachment point " << info->mName
  414. << llendl;
  415. delete info;
  416. return false;
  417. }
  418. static LLStdStringHandle slot_string =
  419. LLXmlTree::addAttributeString("pie_slice");
  420. child->getFastAttributeS32(slot_string, info->mPieMenuSlice);
  421. static LLStdStringHandle visible_in_first_person_string =
  422. LLXmlTree::addAttributeString("visible_in_first_person");
  423. child->getFastAttributeBool(visible_in_first_person_string,
  424. info->mVisibleFirstPerson);
  425. static LLStdStringHandle hud_attachment_string =
  426. LLXmlTree::addAttributeString("hud");
  427. child->getFastAttributeBool(hud_attachment_string,
  428. info->mIsHUDAttachment);
  429. mAttachmentInfoList.push_back(info);
  430. }
  431. return true;
  432. }
  433. // Parses <mesh> nodes from XML tree
  434. bool LLAvatarAppearance::LLAvatarXmlInfo::parseXmlMeshNodes(LLXmlTreeNode* root)
  435. {
  436. for (LLXmlTreeNode* node = root->getChildByName("mesh"); node;
  437. node = root->getNextNamedChild())
  438. {
  439. LLAvatarMeshInfo* info = new LLAvatarMeshInfo;
  440. // attribute: type
  441. static LLStdStringHandle type_string =
  442. LLXmlTree::addAttributeString("type");
  443. if (!node->getFastAttributeString(type_string, info->mType))
  444. {
  445. llwarns << "Avatar file: <mesh> is missing type attribute. Ignoring element."
  446. << llendl;
  447. delete info;
  448. return false; // Ignore this element
  449. }
  450. static LLStdStringHandle lod_string =
  451. LLXmlTree::addAttributeString("lod");
  452. if (!node->getFastAttributeS32(lod_string, info->mLOD))
  453. {
  454. llwarns << "Avatar file: <mesh> is missing lod attribute. Ignoring element."
  455. << llendl;
  456. delete info;
  457. return false; // Ignore this element
  458. }
  459. static LLStdStringHandle filename_str =
  460. LLXmlTree::addAttributeString("file_name");
  461. if (!node->getFastAttributeString(filename_str, info->mMeshFileName))
  462. {
  463. llwarns << "Avatar file: <mesh> is missing file_name attribute. Ignoring: "
  464. << info->mType << llendl;
  465. delete info;
  466. return false; // Ignore this element
  467. }
  468. static LLStdStringHandle reference_string =
  469. LLXmlTree::addAttributeString("reference");
  470. node->getFastAttributeString(reference_string, info->mReferenceMeshName);
  471. // Attribute: min_pixel_area
  472. static LLStdStringHandle min_pixel_area_string =
  473. LLXmlTree::addAttributeString("min_pixel_area");
  474. static LLStdStringHandle min_pixel_width_string =
  475. LLXmlTree::addAttributeString("min_pixel_width");
  476. if (!node->getFastAttributeF32(min_pixel_area_string,
  477. info->mMinPixelArea))
  478. {
  479. F32 min_pixel_area = 0.1f;
  480. if (node->getFastAttributeF32(min_pixel_width_string, min_pixel_area))
  481. {
  482. // this is square root of pixel area (sensible to use linear
  483. // space in defining lods)
  484. min_pixel_area = min_pixel_area * min_pixel_area;
  485. }
  486. info->mMinPixelArea = min_pixel_area;
  487. }
  488. // Parse visual params for this node only if we haven't already
  489. for (LLXmlTreeNode* child = node->getChildByName("param"); child;
  490. child = node->getNextNamedChild())
  491. {
  492. if (!child->getChildByName("param_morph"))
  493. {
  494. if (child->getChildByName("param_skeleton"))
  495. {
  496. llwarns << "Cannot specify skeleton param in a mesh definition."
  497. << llendl;
  498. }
  499. else
  500. {
  501. llwarns << "Unknown param type." << llendl;
  502. }
  503. return false;
  504. }
  505. LLPolyMorphTargetInfo* morphinfo = new LLPolyMorphTargetInfo();
  506. if (!morphinfo->parseXml(child))
  507. {
  508. delete morphinfo;
  509. delete info;
  510. return false;
  511. }
  512. bool shared = false;
  513. static LLStdStringHandle shared_string =
  514. LLXmlTree::addAttributeString("shared");
  515. child->getFastAttributeBool(shared_string, shared);
  516. info->mPolyMorphTargetInfoList.emplace_back(morphinfo, shared);
  517. }
  518. mMeshInfoList.push_back(info);
  519. }
  520. return true;
  521. }
  522. // Parses <global_color> nodes from XML tree
  523. bool LLAvatarAppearance::LLAvatarXmlInfo::parseXmlColorNodes(LLXmlTreeNode* root)
  524. {
  525. for (LLXmlTreeNode* color_node = root->getChildByName("global_color");
  526. color_node; color_node = root->getNextNamedChild())
  527. {
  528. std::string global_color_name;
  529. static LLStdStringHandle name_string =
  530. LLXmlTree::addAttributeString("name");
  531. if (color_node->getFastAttributeString(name_string, global_color_name))
  532. {
  533. if (global_color_name == "skin_color")
  534. {
  535. if (mTexSkinColorInfo)
  536. {
  537. llwarns << "avatar file: multiple instances of skin_color"
  538. << llendl;
  539. return false;
  540. }
  541. mTexSkinColorInfo = new LLTexGlobalColorInfo;
  542. if (!mTexSkinColorInfo->parseXml(color_node))
  543. {
  544. delete_and_clear(mTexSkinColorInfo);
  545. llwarns << "avatar file: mTexSkinColor->parseXml() failed"
  546. << llendl;
  547. return false;
  548. }
  549. }
  550. else if (global_color_name == "hair_color")
  551. {
  552. if (mTexHairColorInfo)
  553. {
  554. llwarns << "avatar file: multiple instances of hair_color"
  555. << llendl;
  556. return false;
  557. }
  558. mTexHairColorInfo = new LLTexGlobalColorInfo;
  559. if (!mTexHairColorInfo->parseXml(color_node))
  560. {
  561. delete_and_clear(mTexHairColorInfo);
  562. llwarns << "avatar file: mTexHairColor->parseXml() failed"
  563. << llendl;
  564. return false;
  565. }
  566. }
  567. else if (global_color_name == "eye_color")
  568. {
  569. if (mTexEyeColorInfo)
  570. {
  571. llwarns << "avatar file: multiple instances of eye_color"
  572. << llendl;
  573. return false;
  574. }
  575. mTexEyeColorInfo = new LLTexGlobalColorInfo;
  576. if (!mTexEyeColorInfo->parseXml(color_node))
  577. {
  578. delete_and_clear(mTexEyeColorInfo);
  579. llwarns << "avatar file: mTexEyeColor->parseXml() failed"
  580. << llendl;
  581. return false;
  582. }
  583. }
  584. }
  585. }
  586. return true;
  587. }
  588. // Parses <layer_set> nodes from XML tree
  589. bool LLAvatarAppearance::LLAvatarXmlInfo::parseXmlLayerNodes(LLXmlTreeNode* root)
  590. {
  591. for (LLXmlTreeNode* layer_node = root->getChildByName("layer_set");
  592. layer_node; layer_node = root->getNextNamedChild())
  593. {
  594. LLTexLayerSetInfo* layer_info = new LLTexLayerSetInfo();
  595. if (layer_info->parseXml(layer_node))
  596. {
  597. mLayerInfoList.push_back(layer_info);
  598. }
  599. else
  600. {
  601. llwarns << "avatar file: layer_set->parseXml() failed" << llendl;
  602. delete layer_info;
  603. return false;
  604. }
  605. }
  606. return true;
  607. }
  608. // Parses <driver_parameters> nodes from XML tree
  609. bool LLAvatarAppearance::LLAvatarXmlInfo::parseXmlDriverNodes(LLXmlTreeNode* root)
  610. {
  611. LLXmlTreeNode* driver = root->getChildByName("driver_parameters");
  612. if (driver)
  613. {
  614. for (LLXmlTreeNode* grand_child = driver->getChildByName("param");
  615. grand_child;
  616. grand_child = driver->getNextNamedChild())
  617. {
  618. if (grand_child->getChildByName("param_driver"))
  619. {
  620. LLDriverParamInfo* driver_info = new LLDriverParamInfo();
  621. if (driver_info->parseXml(grand_child))
  622. {
  623. mDriverInfoList.push_back(driver_info);
  624. }
  625. else
  626. {
  627. delete driver_info;
  628. llwarns << "avatar file: driver_param->parseXml() failed"
  629. << llendl;
  630. return false;
  631. }
  632. }
  633. }
  634. }
  635. return true;
  636. }
  637. // Parses <driver_parameters> nodes from XML tree
  638. bool LLAvatarAppearance::LLAvatarXmlInfo::parseXmlMorphNodes(LLXmlTreeNode* root)
  639. {
  640. LLXmlTreeNode* masks = root->getChildByName("morph_masks");
  641. if (!masks)
  642. {
  643. return false;
  644. }
  645. for (LLXmlTreeNode* grand_child = masks->getChildByName("mask");
  646. grand_child; grand_child = masks->getNextNamedChild())
  647. {
  648. LLAvatarMorphInfo* info = new LLAvatarMorphInfo();
  649. static LLStdStringHandle name_string =
  650. LLXmlTree::addAttributeString("morph_name");
  651. if (!grand_child->getFastAttributeString(name_string, info->mName))
  652. {
  653. llwarns << "No name supplied for morph mask." << llendl;
  654. delete info;
  655. return false;
  656. }
  657. static LLStdStringHandle region_string =
  658. LLXmlTree::addAttributeString("body_region");
  659. if (!grand_child->getFastAttributeString(region_string, info->mRegion))
  660. {
  661. llwarns << "No region supplied for morph mask." << llendl;
  662. delete info;
  663. return false;
  664. }
  665. static LLStdStringHandle layer_string =
  666. LLXmlTree::addAttributeString("layer");
  667. if (!grand_child->getFastAttributeString(layer_string, info->mLayer))
  668. {
  669. llwarns << "No layer supplied for morph mask." << llendl;
  670. delete info;
  671. return false;
  672. }
  673. // optional parameter. don't throw a warning if not present.
  674. static LLStdStringHandle invert_string =
  675. LLXmlTree::addAttributeString("invert");
  676. grand_child->getFastAttributeBool(invert_string, info->mInvert);
  677. mMorphMaskInfoList.push_back(info);
  678. }
  679. return true;
  680. }
  681. //-----------------------------------------------------------------------------
  682. // LLMaskedMorph sub-class
  683. //-----------------------------------------------------------------------------
  684. //virtual
  685. LLAvatarAppearance::LLMaskedMorph::LLMaskedMorph(LLVisualParam* morph_target,
  686. bool invert,
  687. const std::string& layer)
  688. : mMorphTarget(morph_target),
  689. mInvert(invert),
  690. mLayer(layer)
  691. {
  692. LLPolyMorphTarget* target = morph_target->asPolyMorphTarget();
  693. if (target)
  694. {
  695. target->addPendingMorphMask();
  696. }
  697. }
  698. //-----------------------------------------------------------------------------
  699. // LLAvatarAppearance class
  700. //-----------------------------------------------------------------------------
  701. LLAvatarAppearance::LLAvatarAppearance(LLWearableData* wearable_data)
  702. : LLCharacter(),
  703. mIsDummy(false),
  704. mIsBuilt(false),
  705. mTexSkinColor(NULL),
  706. mTexHairColor(NULL),
  707. mTexEyeColor(NULL),
  708. mNumBones(0),
  709. mPelvisToFoot(0.f),
  710. mHeadOffset(),
  711. mRoot(NULL),
  712. mWearableData(wearable_data)
  713. {
  714. llassert_always(mWearableData);
  715. mBakedTextureDatas.resize(BAKED_NUM_INDICES);
  716. for (U32 i = 0, count = mBakedTextureDatas.size(); i < count; ++i)
  717. {
  718. mBakedTextureDatas[i].mLastTextureID = IMG_DEFAULT_AVATAR;
  719. mBakedTextureDatas[i].mTexLayerSet = NULL;
  720. mBakedTextureDatas[i].mIsLoaded = false;
  721. mBakedTextureDatas[i].mIsUsed = false;
  722. mBakedTextureDatas[i].mMaskTexName = 0;
  723. mBakedTextureDatas[i].mTextureIndex =
  724. LLAvatarAppearanceDictionary::bakedToLocalTextureIndex((EBakedTextureIndex)i);
  725. }
  726. }
  727. //virtual
  728. void LLAvatarAppearance::initInstance()
  729. {
  730. // Initialize joint, mesh and shape members
  731. mRoot = createAvatarJoint();
  732. mRoot->setName("mRoot");
  733. for (LLAvatarAppearanceDictionary::MeshEntries::const_iterator
  734. iter = gAvatarAppDictp->getMeshEntries().begin(),
  735. end = gAvatarAppDictp->getMeshEntries().end();
  736. iter != end; ++iter)
  737. {
  738. const EMeshIndex mesh_index = iter->first;
  739. const LLAvatarAppearanceDictionary::MeshEntry* mesh_dict = iter->second;
  740. LLAvatarJoint* joint = createAvatarJoint();
  741. joint->setName(mesh_dict->mName);
  742. joint->setMeshID(mesh_index);
  743. mMeshLOD.push_back(joint);
  744. for (U32 lod = 0, count = mesh_dict->mLOD; lod < count; ++lod)
  745. {
  746. LLAvatarJointMesh* mesh = createAvatarJointMesh();
  747. std::string mesh_name = "m" + mesh_dict->mName +
  748. boost::lexical_cast<std::string>(lod);
  749. // We pre-pended an m - need to capitalize first character for
  750. // camelCase
  751. mesh_name[1] = toupper(mesh_name[1]);
  752. mesh->setName(mesh_name);
  753. mesh->setMeshID(mesh_index);
  754. mesh->setPickName(mesh_dict->mPickName);
  755. mesh->setIsTransparent(false);
  756. switch ((S32)mesh_index)
  757. {
  758. case MESH_ID_HAIR:
  759. mesh->setIsTransparent(true);
  760. break;
  761. case MESH_ID_SKIRT:
  762. mesh->setIsTransparent(true);
  763. break;
  764. case MESH_ID_EYEBALL_LEFT:
  765. case MESH_ID_EYEBALL_RIGHT:
  766. mesh->setSpecular(LLColor4(1.f, 1.f, 1.f, 1.f), 1.f);
  767. }
  768. joint->mMeshParts.push_back(mesh);
  769. }
  770. }
  771. // Associate baked textures with meshes
  772. for (LLAvatarAppearanceDictionary::MeshEntries::const_iterator
  773. it1 = gAvatarAppDictp->getMeshEntries().begin(),
  774. end1 = gAvatarAppDictp->getMeshEntries().end();
  775. it1 != end1; ++it1)
  776. {
  777. const EMeshIndex mesh_index = it1->first;
  778. const LLAvatarAppearanceDictionary::MeshEntry* mesh_dict = it1->second;
  779. const EBakedTextureIndex btex_idx = mesh_dict->mBakedID;
  780. // Skip it if there is no associated baked texture.
  781. if (btex_idx == BAKED_NUM_INDICES) continue;
  782. for (avatar_joint_mesh_list_t::iterator
  783. it2 = mMeshLOD[mesh_index]->mMeshParts.begin(),
  784. end2 = mMeshLOD[mesh_index]->mMeshParts.end();
  785. it2 != end2; ++it2)
  786. {
  787. LLAvatarJointMesh* mesh = *it2;
  788. mBakedTextureDatas[(S32)btex_idx].mJointMeshes.push_back(mesh);
  789. }
  790. }
  791. buildCharacter();
  792. }
  793. //virtual
  794. LLAvatarAppearance::~LLAvatarAppearance()
  795. {
  796. for (U32 i = 0, count = mBakedTextureDatas.size(); i < count; ++i)
  797. {
  798. if (mBakedTextureDatas[i].mTexLayerSet)
  799. {
  800. delete mBakedTextureDatas[i].mTexLayerSet;
  801. mBakedTextureDatas[i].mTexLayerSet = NULL;
  802. }
  803. mBakedTextureDatas[i].mJointMeshes.clear();
  804. for (morph_list_t::iterator
  805. iter = mBakedTextureDatas[i].mMaskedMorphs.begin(),
  806. end = mBakedTextureDatas[i].mMaskedMorphs.end();
  807. iter != end; ++iter)
  808. {
  809. LLMaskedMorph* masked_morph = *iter;
  810. delete masked_morph;
  811. }
  812. }
  813. if (mRoot)
  814. {
  815. mRoot->removeAllChildren();
  816. delete mRoot;
  817. mRoot = NULL;
  818. }
  819. mJointMap.clear();
  820. clearSkeleton();
  821. clearCollisionVolumes();
  822. delete_and_clear(mTexSkinColor);
  823. delete_and_clear(mTexHairColor);
  824. delete_and_clear(mTexEyeColor);
  825. std::for_each(mPolyMeshes.begin(), mPolyMeshes.end(),
  826. DeletePairedPointer());
  827. mPolyMeshes.clear();
  828. for (avatar_joint_list_t::iterator iter = mMeshLOD.begin(),
  829. end = mMeshLOD.end();
  830. iter != end; ++iter)
  831. {
  832. LLAvatarJoint* joint = *iter;
  833. std::for_each(joint->mMeshParts.begin(), joint->mMeshParts.end(),
  834. DeletePointer());
  835. joint->mMeshParts.clear();
  836. }
  837. std::for_each(mMeshLOD.begin(), mMeshLOD.end(), DeletePointer());
  838. mMeshLOD.clear();
  839. delete_and_clear(mRoot);
  840. }
  841. //static
  842. void LLAvatarAppearance::initClass(const std::string& lad_file,
  843. const std::string& skel_file)
  844. {
  845. std::string avatar_file = lad_file.empty() ? AVATAR_DEFAULT_CHAR
  846. : lad_file;
  847. avatar_file = gDirUtil.getFullPath(LL_PATH_CHARACTER, avatar_file);
  848. LLXmlTree lad_xml_tree;
  849. if (!lad_xml_tree.parseFile(avatar_file, false))
  850. {
  851. llerrs << "Problem reading avatar configuration file:" << avatar_file
  852. << llendl;
  853. }
  854. // Now sanity check xml file
  855. LLXmlTreeNode* root = lad_xml_tree.getRoot();
  856. if (!root)
  857. {
  858. llerrs << "No root node found in avatar configuration file: "
  859. << avatar_file << llendl;
  860. }
  861. // <linden_avatar version="M.n"> (root)
  862. if (!root->hasName("linden_avatar"))
  863. {
  864. llerrs << "Invalid avatar file header: " << avatar_file << llendl;
  865. }
  866. std::string version;
  867. static LLStdStringHandle version_string =
  868. LLXmlTree::addAttributeString("version");
  869. if (!root->getFastAttributeString(version_string, version) ||
  870. (version != "1.0" && version != "2.0"))
  871. {
  872. llerrs << "Invalid avatar file version: " << version << " in file: "
  873. << avatar_file << llendl;
  874. }
  875. S32 wearable_def_version = 1;
  876. static LLStdStringHandle wearable_version_string =
  877. LLXmlTree::addAttributeString("wearable_definition_version");
  878. root->getFastAttributeS32(wearable_version_string,
  879. wearable_def_version);
  880. LLWearable::setCurrentDefinitionVersion(wearable_def_version);
  881. std::string mesh_file_name;
  882. LLXmlTreeNode* skeleton_node = root->getChildByName("skeleton");
  883. if (!skeleton_node)
  884. {
  885. llerrs << "No skeleton in avatar configuration file: " << avatar_file
  886. << llendl;
  887. }
  888. std::string skeleton_file_name = skel_file;
  889. if (skel_file.empty())
  890. {
  891. static LLStdStringHandle filename_str =
  892. LLXmlTree::addAttributeString("file_name");
  893. if (!skeleton_node->getFastAttributeString(filename_str,
  894. skeleton_file_name))
  895. {
  896. llerrs << "No file name in skeleton node in avatar config file: "
  897. << avatar_file << llendl;
  898. }
  899. }
  900. std::string skeleton_path = gDirUtil.getFullPath(LL_PATH_CHARACTER,
  901. skeleton_file_name);
  902. LLXmlTree skel_xml_tree;
  903. if (!parseSkeletonFile(skeleton_path, skel_xml_tree))
  904. {
  905. llerrs << "Error parsing skeleton file: " << skeleton_path << llendl;
  906. }
  907. // Process XML data
  908. // avatar_skeleton.xml
  909. if (sAvatarSkeletonInfo)
  910. {
  911. // This can happen if a login attempt failed
  912. delete sAvatarSkeletonInfo;
  913. }
  914. sAvatarSkeletonInfo = new LLAvatarSkeletonInfo;
  915. if (!sAvatarSkeletonInfo->parseXml(skel_xml_tree.getRoot()))
  916. {
  917. llerrs << "Error parsing skeleton XML file: " << skeleton_path
  918. << llendl;
  919. }
  920. // Parse avatar_lad.xml
  921. // Release old sAvatarXmlInfo if any (this can happen if a login attempt
  922. // failed)
  923. delete_and_clear(sAvatarXmlInfo);
  924. sAvatarXmlInfo = new LLAvatarXmlInfo;
  925. if (!sAvatarXmlInfo->parseXmlSkeletonNode(root))
  926. {
  927. llerrs << "Error parsing skeleton node in avatar XML file: "
  928. << skeleton_path << llendl;
  929. }
  930. if (!sAvatarXmlInfo->parseXmlMeshNodes(root))
  931. {
  932. llerrs << "Error parsing skeleton node in avatar XML file: "
  933. << skeleton_path << llendl;
  934. }
  935. if (!sAvatarXmlInfo->parseXmlColorNodes(root))
  936. {
  937. llerrs << "Error parsing skeleton node in avatar XML file: "
  938. << skeleton_path << llendl;
  939. }
  940. if (!sAvatarXmlInfo->parseXmlLayerNodes(root))
  941. {
  942. llerrs << "Error parsing skeleton node in avatar XML file: "
  943. << skeleton_path << llendl;
  944. }
  945. if (!sAvatarXmlInfo->parseXmlDriverNodes(root))
  946. {
  947. llerrs << "Error parsing skeleton node in avatar XML file: "
  948. << skeleton_path << llendl;
  949. }
  950. if (!sAvatarXmlInfo->parseXmlMorphNodes(root))
  951. {
  952. llerrs << "Error parsing skeleton node in avatar XML file: "
  953. << skeleton_path << llendl;
  954. }
  955. }
  956. void LLAvatarAppearance::cleanupClass()
  957. {
  958. delete_and_clear(sAvatarXmlInfo);
  959. delete_and_clear(sAvatarSkeletonInfo);
  960. }
  961. // The viewer can only suggest a good size for the agent, the simulator will
  962. // keep it inside a reasonable range.
  963. void LLAvatarAppearance::computeBodySize(bool force)
  964. {
  965. LLVector3 pelvis_scale = mPelvisp->getScale();
  966. // Some of the joints have not been cached
  967. LLVector3 skull = mSkullp->getPosition();
  968. LLVector3 neck = mNeckp->getPosition();
  969. LLVector3 neck_scale = mNeckp->getScale();
  970. LLVector3 chest = mChestp->getPosition();
  971. LLVector3 chest_scale = mChestp->getScale();
  972. // The rest of the joints have been cached
  973. LLVector3 head = mHeadp->getPosition();
  974. LLVector3 head_scale = mHeadp->getScale();
  975. LLVector3 torso = mTorsop->getPosition();
  976. LLVector3 torso_scale = mTorsop->getScale();
  977. LLVector3 hip = mHipLeftp->getPosition();
  978. LLVector3 hip_scale = mHipLeftp->getScale();
  979. LLVector3 knee = mKneeLeftp->getPosition();
  980. LLVector3 knee_scale = mKneeLeftp->getScale();
  981. LLVector3 ankle = mAnkleLeftp->getPosition();
  982. LLVector3 ankle_scale = mAnkleLeftp->getScale();
  983. LLVector3 foot = mFootLeftp->getPosition();
  984. mPelvisToFoot = hip.mV[VZ] * pelvis_scale.mV[VZ] -
  985. knee.mV[VZ] * hip_scale.mV[VZ] -
  986. ankle.mV[VZ] * knee_scale.mV[VZ] -
  987. foot.mV[VZ] * ankle_scale.mV[VZ];
  988. LLVector3 new_body_size;
  989. new_body_size.mV[VZ] = mPelvisToFoot +
  990. // The sqrt(2) correction below is an approximate
  991. // correction to get to the top of the head
  992. F_SQRT2 * (skull.mV[VZ] * head_scale.mV[VZ]) +
  993. head.mV[VZ] * neck_scale.mV[VZ] +
  994. neck.mV[VZ] * chest_scale.mV[VZ] +
  995. chest.mV[VZ] * torso_scale.mV[VZ] +
  996. torso.mV[VZ] * pelvis_scale.mV[VZ];
  997. // *TODO: measure the real depth and width
  998. new_body_size.mV[VX] = DEFAULT_AGENT_DEPTH;
  999. new_body_size.mV[VY] = DEFAULT_AGENT_WIDTH;
  1000. mAvatarOffset.mV[VX] = mAvatarOffset.mV[VY] = 0.0f;
  1001. F32 old_offset = mAvatarOffset.mV[VZ];
  1002. mAvatarOffset.mV[VZ] = getVisualParamWeight(AVATAR_HOVER);
  1003. bool offset_changed = old_offset != mAvatarOffset.mV[VZ];
  1004. if (new_body_size != mBodySize || offset_changed)
  1005. {
  1006. mBodySize = new_body_size;
  1007. bodySizeChanged();
  1008. }
  1009. }
  1010. bool LLAvatarAppearance::parseSkeletonFile(const std::string& filename,
  1011. LLXmlTree& skel_xml_tree)
  1012. {
  1013. // Parse the file
  1014. if (!skel_xml_tree.parseFile(filename, false))
  1015. {
  1016. llerrs << "Cannot parse skeleton file: " << filename << llendl;
  1017. }
  1018. // Now sanity-check the XML
  1019. LLXmlTreeNode* root = skel_xml_tree.getRoot();
  1020. if (!root)
  1021. {
  1022. llerrs << "No root node found in avatar skeleton file: " << filename
  1023. << llendl;
  1024. }
  1025. if (!root->hasName("linden_skeleton"))
  1026. {
  1027. llerrs << "Invalid avatar skeleton file header: " << filename
  1028. << llendl;
  1029. }
  1030. std::string version;
  1031. static LLStdStringHandle version_string =
  1032. LLXmlTree::addAttributeString("version");
  1033. if (!root->getFastAttributeString(version_string, version) ||
  1034. (version != "1.0" && version != "2.0"))
  1035. {
  1036. llerrs << "Invalid avatar skeleton file version: " << version
  1037. << " in file: " << filename << llendl;
  1038. }
  1039. return true;
  1040. }
  1041. bool LLAvatarAppearance::setupBone(const LLAvatarBoneInfo* info,
  1042. LLJoint* parent,
  1043. S32& volume_num,
  1044. S32& joint_num)
  1045. {
  1046. if (!info)
  1047. {
  1048. llwarns << "NULL avatar bone info pointer passed." << llendl;
  1049. return false;
  1050. }
  1051. LLJoint* joint = NULL;
  1052. LL_DEBUGS("Avatar") << "Bone info. Name: " << info->mName
  1053. << " - Joint: " << (info->mIsJoint ? "yes" : "no")
  1054. << " - Volume number: " << volume_num
  1055. << " - Joint number: " << joint_num << LL_ENDL;
  1056. if (info->mIsJoint)
  1057. {
  1058. joint = getCharacterJoint(joint_num);
  1059. if (!joint)
  1060. {
  1061. llwarns << "Too many bones" << llendl;
  1062. return false;
  1063. }
  1064. joint->setName(info->mName);
  1065. }
  1066. else // Collision volume
  1067. {
  1068. if (volume_num >= (S32)mCollisionVolumes.size())
  1069. {
  1070. llwarns << "Too many collision volumes" << llendl;
  1071. return false;
  1072. }
  1073. joint = mCollisionVolumes[volume_num];
  1074. joint->setName(info->mName);
  1075. }
  1076. // Add to parent
  1077. if (parent && joint->getParent() != parent)
  1078. {
  1079. parent->addChild(joint);
  1080. }
  1081. joint->setPosition(info->mPos);
  1082. joint->setDefaultPosition(info->mPos);
  1083. joint->setRotation(mayaQ(info->mRot.mV[VX], info->mRot.mV[VY],
  1084. info->mRot.mV[VZ], LLQuaternion::XYZ));
  1085. joint->setScale(info->mScale);
  1086. joint->setDefaultScale(info->mScale);
  1087. joint->setSupport(info->mSupport);
  1088. joint->setEnd(info->mEnd);
  1089. if (info->mIsJoint)
  1090. {
  1091. joint->setSkinOffset(info->mPivot);
  1092. joint->setJointNum(joint_num++);
  1093. joint->setIsBone(true);
  1094. }
  1095. else // collision volume
  1096. {
  1097. joint->setJointNum(mNumBones + volume_num++);
  1098. }
  1099. // Setup children
  1100. for (LLAvatarBoneInfo::child_list_t::const_iterator
  1101. iter = info->mChildList.begin(), end = info->mChildList.end();
  1102. iter != end; ++iter)
  1103. {
  1104. LLAvatarBoneInfo* child_info = *iter;
  1105. if (!setupBone(child_info, joint, volume_num, joint_num))
  1106. {
  1107. return false;
  1108. }
  1109. }
  1110. return true;
  1111. }
  1112. bool LLAvatarAppearance::allocateCharacterJoints(U32 num)
  1113. {
  1114. if (num != (U32)mSkeleton.size())
  1115. {
  1116. clearSkeleton();
  1117. mSkeleton = avatar_joint_list_t(num, NULL);
  1118. mNumBones = num;
  1119. }
  1120. return true;
  1121. }
  1122. bool LLAvatarAppearance::buildSkeleton(const LLAvatarSkeletonInfo* info)
  1123. {
  1124. if (!info)
  1125. {
  1126. llwarns << "NULL avatar skeleton info pointer passed." << llendl;
  1127. return false;
  1128. }
  1129. LL_DEBUGS("Avatar") << "Sketeton info. Bones: " << info->mNumBones
  1130. << " - Collision volumes: "
  1131. << info->mNumCollisionVolumes << LL_ENDL;
  1132. // Allocate joints
  1133. if (!allocateCharacterJoints(info->mNumBones))
  1134. {
  1135. llerrs << "Cannot allocate " << info->mNumBones << " joints" << llendl;
  1136. }
  1137. // Allocate volumes
  1138. if (info->mNumCollisionVolumes)
  1139. {
  1140. if (!allocateCollisionVolumes(info->mNumCollisionVolumes))
  1141. {
  1142. llerrs << "Cannot allocate " << info->mNumCollisionVolumes
  1143. << " collision volumes" << llendl;
  1144. }
  1145. }
  1146. S32 current_joint_num = 0;
  1147. S32 current_volume_num = 0;
  1148. for (LLAvatarSkeletonInfo::bone_info_list_t::const_iterator
  1149. iter = info->mBoneInfoList.begin(), end = info->mBoneInfoList.end();
  1150. iter != end; ++iter)
  1151. {
  1152. LLAvatarBoneInfo* bone_info = *iter;
  1153. if (!setupBone(bone_info, NULL, current_volume_num, current_joint_num))
  1154. {
  1155. llerrs << "Error parsing bone in skeleton file" << llendl;
  1156. }
  1157. }
  1158. return true;
  1159. }
  1160. void LLAvatarAppearance::clearSkeleton()
  1161. {
  1162. std::for_each(mSkeleton.begin(), mSkeleton.end(), DeletePointer());
  1163. mSkeleton.clear();
  1164. }
  1165. void LLAvatarAppearance::addPelvisFixup(F32 fixup, const LLUUID& mesh_id)
  1166. {
  1167. if (mesh_id.notNull())
  1168. {
  1169. LLVector3 pos(0.f, 0.f, fixup);
  1170. mPelvisFixups.add(mesh_id, pos);
  1171. }
  1172. }
  1173. void LLAvatarAppearance::removePelvisFixup(const LLUUID& mesh_id)
  1174. {
  1175. mPelvisFixups.remove(mesh_id);
  1176. }
  1177. bool LLAvatarAppearance::hasPelvisFixup(F32& fixup, LLUUID& mesh_id) const
  1178. {
  1179. LLVector3 pos;
  1180. if (mPelvisFixups.findActiveOverride(mesh_id, pos))
  1181. {
  1182. fixup = pos[2];
  1183. return true;
  1184. }
  1185. return false;
  1186. }
  1187. bool LLAvatarAppearance::hasPelvisFixup(F32& fixup) const
  1188. {
  1189. LLUUID mesh_id;
  1190. return hasPelvisFixup(fixup, mesh_id);
  1191. }
  1192. // Deferred initialization and rebuild of the avatar.
  1193. void LLAvatarAppearance::buildCharacter()
  1194. {
  1195. // Remove all references to our existing skeleton so we can rebuild it
  1196. deactivateAllMotions();
  1197. // Remove all of mRoot's children
  1198. mRoot->removeAllChildren();
  1199. mJointMap.clear();
  1200. mIsBuilt = false;
  1201. // Clear mesh data
  1202. for (avatar_joint_list_t::iterator it1 = mMeshLOD.begin(),
  1203. end1 = mMeshLOD.end();
  1204. it1 != end1; ++it1)
  1205. {
  1206. LLAvatarJoint* joint = *it1;
  1207. for (avatar_joint_mesh_list_t::iterator
  1208. it2 = joint->mMeshParts.begin(),
  1209. end2 = joint->mMeshParts.end();
  1210. it2 != end2; ++it2)
  1211. {
  1212. LLAvatarJointMesh* mesh = *it2;
  1213. mesh->setMesh(NULL);
  1214. }
  1215. }
  1216. // (Re)load our skeleton and meshes
  1217. LLTimer timer;
  1218. if (!loadAvatar())
  1219. {
  1220. if (isSelf())
  1221. {
  1222. llerrs << "Unable to load user's avatar" << llendl;
  1223. }
  1224. llwarns << "Unable to load other's avatar" << llendl;
  1225. return;
  1226. }
  1227. LL_DEBUGS("Avatar") << "Avatar load took " << timer.getElapsedTimeF32()
  1228. << " seconds." << LL_ENDL;
  1229. // Initialize "well known" joint pointers
  1230. mPelvisp = mRoot->findJoint(LL_JOINT_KEY_PELVIS);
  1231. mTorsop = mRoot->findJoint(LL_JOINT_KEY_TORSO);
  1232. mChestp = mRoot->findJoint(LL_JOINT_KEY_CHEST);
  1233. mNeckp = mRoot->findJoint(LL_JOINT_KEY_NECK);
  1234. mHeadp = mRoot->findJoint(LL_JOINT_KEY_HEAD);
  1235. mSkullp = mRoot->findJoint(LL_JOINT_KEY_SKULL);
  1236. mHipLeftp = mRoot->findJoint(LL_JOINT_KEY_HIPLEFT);
  1237. mHipRightp = mRoot->findJoint(LL_JOINT_KEY_HIPRIGHT);
  1238. mKneeLeftp = mRoot->findJoint(LL_JOINT_KEY_KNEELEFT);
  1239. mKneeRightp = mRoot->findJoint(LL_JOINT_KEY_KNEERIGHT);
  1240. mAnkleLeftp = mRoot->findJoint(LL_JOINT_KEY_ANKLELEFT);
  1241. mAnkleRightp = mRoot->findJoint(LL_JOINT_KEY_ANKLERIGHT);
  1242. mFootLeftp = mRoot->findJoint(LL_JOINT_KEY_FOOTLEFT);
  1243. mFootRightp = mRoot->findJoint(LL_JOINT_KEY_FOOTRIGHT);
  1244. mWristLeftp = mRoot->findJoint(LL_JOINT_KEY_WRISTLEFT);
  1245. mWristRightp = mRoot->findJoint(LL_JOINT_KEY_WRISTRIGHT);
  1246. mEyeLeftp = mRoot->findJoint(LL_JOINT_KEY_EYELEFT);
  1247. mEyeRightp = mRoot->findJoint(LL_JOINT_KEY_EYERIGHT);
  1248. // Make sure "well known" pointers exist
  1249. if (!(mPelvisp && mTorsop && mChestp && mNeckp && mHeadp && mSkullp &&
  1250. mHipLeftp && mHipRightp && mKneeLeftp && mKneeRightp &&
  1251. mAnkleLeftp && mAnkleRightp && mFootLeftp && mFootRightp &&
  1252. mWristLeftp && mWristRightp && mEyeLeftp && mEyeRightp))
  1253. {
  1254. llerrs << "Failed to create avatar." << llendl;
  1255. }
  1256. // Initialize the pelvis
  1257. mPelvisp->setPosition(LLVector3::zero);
  1258. mIsBuilt = true;
  1259. }
  1260. bool LLAvatarAppearance::loadAvatar()
  1261. {
  1262. // avatar_skeleton.xml
  1263. if (!buildSkeleton(sAvatarSkeletonInfo))
  1264. {
  1265. llwarns << "Avatar file: buildSkeleton() failed" << llendl;
  1266. return false;
  1267. }
  1268. if (LLJoint::sAvatarJointAliasMap.empty())
  1269. {
  1270. LLJoint::sAvatarJointAliasMap = getJointAliases();
  1271. LL_DEBUGS("Avatar") << "Avatar skeleton joints aliases:";
  1272. for (joint_alias_map_t::const_iterator
  1273. it = LLJoint::sAvatarJointAliasMap.begin(),
  1274. end = LLJoint::sAvatarJointAliasMap.end();
  1275. it != end; ++it)
  1276. {
  1277. LL_CONT << "\n " << it->first << " -> " << it->second;
  1278. }
  1279. LL_CONT << LL_ENDL;
  1280. }
  1281. // avatar_lad.xml : <skeleton>
  1282. if (!loadSkeletonNode())
  1283. {
  1284. llerrs << "Avatar file: loadNodeSkeleton() failed" << llendl;
  1285. }
  1286. // avatar_lad.xml : <mesh>
  1287. if (!loadMeshNodes())
  1288. {
  1289. llerrs << "Avatar file: loadNodeMesh() failed" << llendl;
  1290. }
  1291. // avatar_lad.xml : <global_color>
  1292. if (sAvatarXmlInfo->mTexSkinColorInfo)
  1293. {
  1294. mTexSkinColor = new LLTexGlobalColor(this);
  1295. if (!mTexSkinColor->setInfo(sAvatarXmlInfo->mTexSkinColorInfo))
  1296. {
  1297. llerrs << "Avatar file: mTexSkinColor->setInfo() failed" << llendl;
  1298. }
  1299. }
  1300. else
  1301. {
  1302. llerrs << "<global_color> name=\"skin_color\" not found" << llendl;
  1303. }
  1304. if (sAvatarXmlInfo->mTexHairColorInfo)
  1305. {
  1306. mTexHairColor = new LLTexGlobalColor(this);
  1307. if (!mTexHairColor->setInfo(sAvatarXmlInfo->mTexHairColorInfo))
  1308. {
  1309. llerrs << "Avatar file: mTexHairColor->setInfo() failed" << llendl;
  1310. }
  1311. }
  1312. else
  1313. {
  1314. llerrs << "<global_color> name=\"hair_color\" not found" << llendl;
  1315. }
  1316. if (sAvatarXmlInfo->mTexEyeColorInfo)
  1317. {
  1318. mTexEyeColor = new LLTexGlobalColor(this);
  1319. if (!mTexEyeColor->setInfo(sAvatarXmlInfo->mTexEyeColorInfo))
  1320. {
  1321. llerrs << "Avatar file: mTexEyeColor->setInfo() failed" << llendl;
  1322. }
  1323. }
  1324. else
  1325. {
  1326. llerrs << "<global_color> name=\"eye_color\" not found" << llendl;
  1327. }
  1328. // avatar_lad.xml : <layer_set>
  1329. if (sAvatarXmlInfo->mLayerInfoList.empty())
  1330. {
  1331. llerrs << "Avatar file: missing <layer_set> node" << llendl;
  1332. }
  1333. if (sAvatarXmlInfo->mMorphMaskInfoList.empty())
  1334. {
  1335. llerrs << "Avatar file: missing <morph_masks> node" << llendl;
  1336. }
  1337. // avatar_lad.xml : <morph_masks>
  1338. for (LLAvatarXmlInfo::morph_info_list_t::iterator
  1339. iter = sAvatarXmlInfo->mMorphMaskInfoList.begin(),
  1340. end = sAvatarXmlInfo->mMorphMaskInfoList.end();
  1341. iter != end; ++iter)
  1342. {
  1343. LLAvatarXmlInfo::LLAvatarMorphInfo* info = *iter;
  1344. EBakedTextureIndex baked =
  1345. LLAvatarAppearanceDictionary::findBakedByRegionName(info->mRegion);
  1346. if (baked != BAKED_NUM_INDICES)
  1347. {
  1348. LLVisualParam* morph_param = getVisualParam(info->mName.c_str());
  1349. if (morph_param)
  1350. {
  1351. addMaskedMorph(baked, morph_param, info->mInvert,
  1352. info->mLayer);
  1353. }
  1354. }
  1355. }
  1356. loadLayersets();
  1357. // avatar_lad.xml : <driver_parameters>
  1358. for (LLAvatarXmlInfo::driver_info_list_t::iterator
  1359. iter = sAvatarXmlInfo->mDriverInfoList.begin(),
  1360. end = sAvatarXmlInfo->mDriverInfoList.end();
  1361. iter != end; ++iter)
  1362. {
  1363. LLDriverParamInfo* info = *iter;
  1364. LLDriverParam* driver_param = new LLDriverParam(this);
  1365. if (driver_param->setInfo(info))
  1366. {
  1367. addVisualParam(driver_param);
  1368. driver_param->setParamLocation(isSelf() ? LOC_AV_SELF
  1369. : LOC_AV_OTHER);
  1370. LLVisualParam*(LLAvatarAppearance::*avatar_function)(S32)const =
  1371. &LLAvatarAppearance::getVisualParam;
  1372. if (!driver_param->linkDrivenParams(boost::bind(avatar_function,
  1373. (LLAvatarAppearance*)this,
  1374. _1),
  1375. false))
  1376. {
  1377. llwarns << "Could not link driven params for avatar "
  1378. << getID().asString() << " param id: "
  1379. << driver_param->getID() << llendl;
  1380. continue;
  1381. }
  1382. }
  1383. else
  1384. {
  1385. delete driver_param;
  1386. llwarns << "driver_param->setInfo() failed" << llendl;
  1387. return false;
  1388. }
  1389. }
  1390. return true;
  1391. }
  1392. // Loads <skeleton> node from XML tree
  1393. bool LLAvatarAppearance::loadSkeletonNode()
  1394. {
  1395. mRoot->addChild(mSkeleton[0]);
  1396. // Make meshes children before calling parent version of the function
  1397. for (avatar_joint_list_t::iterator iter = mMeshLOD.begin(),
  1398. end = mMeshLOD.end();
  1399. iter != end; ++iter)
  1400. {
  1401. LLAvatarJoint* joint = *iter;
  1402. joint->mUpdateXform = false;
  1403. joint->setMeshesToChildren();
  1404. }
  1405. mRoot->addChild(mMeshLOD[MESH_ID_HEAD]);
  1406. mRoot->addChild(mMeshLOD[MESH_ID_EYELASH]);
  1407. mRoot->addChild(mMeshLOD[MESH_ID_UPPER_BODY]);
  1408. mRoot->addChild(mMeshLOD[MESH_ID_LOWER_BODY]);
  1409. mRoot->addChild(mMeshLOD[MESH_ID_SKIRT]);
  1410. LLJoint* skull = mRoot->findJoint(LL_JOINT_KEY_SKULL);
  1411. if (skull)
  1412. {
  1413. skull->addChild(mMeshLOD[MESH_ID_HAIR]);
  1414. }
  1415. LLJoint* l_eye = mRoot->findJoint(LL_JOINT_KEY_EYELEFT);
  1416. if (l_eye)
  1417. {
  1418. l_eye->addChild(mMeshLOD[MESH_ID_EYEBALL_LEFT]);
  1419. }
  1420. LLJoint* r_eye = mRoot->findJoint(LL_JOINT_KEY_EYERIGHT);
  1421. if (r_eye)
  1422. {
  1423. r_eye->addChild(mMeshLOD[MESH_ID_EYEBALL_RIGHT]);
  1424. }
  1425. // SKELETAL DISTORTIONS
  1426. for (LLAvatarXmlInfo::skeletal_distortion_info_list_t::iterator
  1427. iter = sAvatarXmlInfo->mSkeletalDistortionInfoList.begin(),
  1428. end = sAvatarXmlInfo->mSkeletalDistortionInfoList.end();
  1429. iter != end; ++iter)
  1430. {
  1431. LLPolySkeletalDistortionInfo* info =
  1432. (LLPolySkeletalDistortionInfo*)*iter;
  1433. LLPolySkeletalDistortion* param = new LLPolySkeletalDistortion(this);
  1434. if (!param->setInfo(info))
  1435. {
  1436. delete param;
  1437. return false;
  1438. }
  1439. addVisualParam(param);
  1440. param->setParamLocation(isSelf() ? LOC_AV_SELF : LOC_AV_OTHER);
  1441. }
  1442. return true;
  1443. }
  1444. // Loads <mesh> nodes from XML tree
  1445. bool LLAvatarAppearance::loadMeshNodes()
  1446. {
  1447. for (LLAvatarXmlInfo::mesh_info_list_t::const_iterator
  1448. it1 = sAvatarXmlInfo->mMeshInfoList.begin(),
  1449. end1 = sAvatarXmlInfo->mMeshInfoList.end();
  1450. it1 != end1; ++it1)
  1451. {
  1452. const LLAvatarXmlInfo::LLAvatarMeshInfo* info = *it1;
  1453. const std::string &type = info->mType;
  1454. S32 lod = info->mLOD;
  1455. U8 mesh_id = 0;
  1456. bool found_mesh_id = false;
  1457. for (LLAvatarAppearanceDictionary::MeshEntries::const_iterator
  1458. it2 = gAvatarAppDictp->getMeshEntries().begin(),
  1459. end2 = gAvatarAppDictp->getMeshEntries().end();
  1460. it2 != end2; ++it2)
  1461. {
  1462. const EMeshIndex mesh_index = it2->first;
  1463. const LLAvatarAppearanceDictionary::MeshEntry* mesh_dict =
  1464. it2->second;
  1465. if (type.compare(mesh_dict->mName) == 0)
  1466. {
  1467. mesh_id = mesh_index;
  1468. found_mesh_id = true;
  1469. break;
  1470. }
  1471. }
  1472. if (!found_mesh_id)
  1473. {
  1474. llwarns << "Ignoring unrecognized mesh type: " << type << llendl;
  1475. return false;
  1476. }
  1477. if (lod >= (S32)mMeshLOD[mesh_id]->mMeshParts.size())
  1478. {
  1479. llwarns << "Avatar file: <mesh> has invalid lod setting " << lod
  1480. << llendl;
  1481. return false;
  1482. }
  1483. LLAvatarJointMesh* mesh = mMeshLOD[mesh_id]->mMeshParts[lod];
  1484. // If this is not set to white (1.0), avatars will *ALWAYS* be darker
  1485. // than their surroundings. Do not touch !!!
  1486. mesh->setColor(LLColor4::white);
  1487. LLPolyMesh* poly_mesh;
  1488. if (!info->mReferenceMeshName.empty())
  1489. {
  1490. polymesh_map_t::const_iterator it3 =
  1491. mPolyMeshes.find(info->mReferenceMeshName);
  1492. if (it3 == mPolyMeshes.end())
  1493. {
  1494. // This should never happen
  1495. llwarns << "Could not find avatar mesh: "
  1496. << info->mReferenceMeshName << llendl;
  1497. return false;
  1498. }
  1499. poly_mesh = LLPolyMesh::getMesh(info->mMeshFileName, it3->second);
  1500. if (poly_mesh)
  1501. {
  1502. poly_mesh->setAvatar(this);
  1503. }
  1504. }
  1505. else
  1506. {
  1507. poly_mesh = LLPolyMesh::getMesh(info->mMeshFileName);
  1508. if (poly_mesh)
  1509. {
  1510. poly_mesh->setAvatar(this);
  1511. }
  1512. }
  1513. if (!poly_mesh)
  1514. {
  1515. llwarns << "Failed to load mesh of type " << type << llendl;
  1516. return false;
  1517. }
  1518. // Multimap insert
  1519. mPolyMeshes.emplace(info->mMeshFileName, poly_mesh);
  1520. mesh->setMesh(poly_mesh);
  1521. mesh->setLOD(info->mMinPixelArea);
  1522. for (LLAvatarXmlInfo::LLAvatarMeshInfo::morph_info_list_t::const_iterator
  1523. it4 = info->mPolyMorphTargetInfoList.begin(),
  1524. end4 = info->mPolyMorphTargetInfoList.end();
  1525. it4 != end4; ++it4)
  1526. {
  1527. const LLAvatarXmlInfo::LLAvatarMeshInfo::morph_info_pair_t* info_pair =
  1528. &(*it4);
  1529. LLPolyMorphTarget* param = new LLPolyMorphTarget(mesh->getMesh());
  1530. if (!param->setInfo((LLPolyMorphTargetInfo*)info_pair->first))
  1531. {
  1532. delete param;
  1533. return false;
  1534. }
  1535. if (info_pair->second)
  1536. {
  1537. addSharedVisualParam(param);
  1538. }
  1539. else
  1540. {
  1541. addVisualParam(param);
  1542. }
  1543. param->setParamLocation(isSelf() ? LOC_AV_SELF : LOC_AV_OTHER);
  1544. }
  1545. }
  1546. return true;
  1547. }
  1548. bool LLAvatarAppearance::loadLayersets()
  1549. {
  1550. bool success = true;
  1551. for (LLAvatarXmlInfo::layer_info_list_t::const_iterator
  1552. it1 = sAvatarXmlInfo->mLayerInfoList.begin(),
  1553. end1 = sAvatarXmlInfo->mLayerInfoList.end();
  1554. it1 != end1; ++it1)
  1555. {
  1556. LLTexLayerSetInfo* layerset_info = *it1;
  1557. if (isSelf())
  1558. {
  1559. // Construct a layerset for each one specified in avatar_lad.xml
  1560. // and initialize it as such.
  1561. LLTexLayerSet* layer_set = createTexLayerSet();
  1562. if (!layer_set->setInfo(layerset_info))
  1563. {
  1564. delete layer_set;
  1565. llwarns << "avatar file: layer_set->setInfo() failed"
  1566. << llendl;
  1567. return false;
  1568. }
  1569. // Scan baked textures and associate the layerset with the
  1570. // appropriate one
  1571. EBakedTextureIndex baked_index = BAKED_NUM_INDICES;
  1572. for (LLAvatarAppearanceDictionary::BakedTextures::const_iterator
  1573. it2 = gAvatarAppDictp->getBakedTextures().begin(),
  1574. end2 = gAvatarAppDictp->getBakedTextures().end();
  1575. it2 != end2; ++it2)
  1576. {
  1577. const LLAvatarAppearanceDictionary::BakedEntry* baked_dict =
  1578. it2->second;
  1579. if (layer_set->isBodyRegion(baked_dict->mName))
  1580. {
  1581. baked_index = it2->first;
  1582. // Ensure both structures are aware of each other
  1583. mBakedTextureDatas[baked_index].mTexLayerSet = layer_set;
  1584. layer_set->setBakedTexIndex(baked_index);
  1585. break;
  1586. }
  1587. }
  1588. // If no baked texture was found, warn and cleanup
  1589. if (baked_index == BAKED_NUM_INDICES)
  1590. {
  1591. llwarns << "<layer_set> has invalid body_region attribute"
  1592. << llendl;
  1593. delete layer_set;
  1594. return false;
  1595. }
  1596. // Scan morph masks and let any affected layers know they have an
  1597. // associated morph
  1598. for (LLAvatarAppearance::morph_list_t::const_iterator
  1599. it3 = mBakedTextureDatas[baked_index].mMaskedMorphs.begin(),
  1600. end3 = mBakedTextureDatas[baked_index].mMaskedMorphs.end();
  1601. it3 != end3; ++it3)
  1602. {
  1603. LLMaskedMorph* morph = *it3;
  1604. LLTexLayerInterface* layer =
  1605. layer_set->findLayerByName(morph->mLayer);
  1606. if (layer)
  1607. {
  1608. layer->setHasMorph(true);
  1609. }
  1610. else
  1611. {
  1612. llwarns << "Could not find layer named " << morph->mLayer
  1613. << " to set morph flag" << llendl;
  1614. success = false;
  1615. }
  1616. }
  1617. }
  1618. else // !isSelf()
  1619. {
  1620. // Construct a layerset for each one specified in avatar_lad.xml
  1621. // and initialize it as such.
  1622. LLTexLayerSetInfo* layerset_info = *it1;
  1623. layerset_info->createVisualParams(this);
  1624. }
  1625. }
  1626. return success;
  1627. }
  1628. LLJoint* LLAvatarAppearance::getCharacterJoint(U32 num)
  1629. {
  1630. if ((S32)num < 0 || num >= (U32)mSkeleton.size())
  1631. {
  1632. return NULL;
  1633. }
  1634. if (!mSkeleton[num])
  1635. {
  1636. mSkeleton[num] = createAvatarJoint();
  1637. }
  1638. return mSkeleton[num];
  1639. }
  1640. LLVector3 LLAvatarAppearance::getVolumePos(S32 joint_index,
  1641. LLVector3& volume_offset)
  1642. {
  1643. if (joint_index < 0 || joint_index > (S32)mCollisionVolumes.size())
  1644. {
  1645. return LLVector3::zero;
  1646. }
  1647. return mCollisionVolumes[joint_index]->getVolumePos(volume_offset);
  1648. }
  1649. LLJoint* LLAvatarAppearance::findCollisionVolume(S32 volume_id)
  1650. {
  1651. if (volume_id < 0 || volume_id > (S32)mCollisionVolumes.size())
  1652. {
  1653. return NULL;
  1654. }
  1655. return mCollisionVolumes[volume_id];
  1656. }
  1657. S32 LLAvatarAppearance::getCollisionVolumeID(std::string& name)
  1658. {
  1659. for (S32 i = 0, count = mCollisionVolumes.size(); i < count; ++i)
  1660. {
  1661. if (mCollisionVolumes[i]->getName() == name)
  1662. {
  1663. return i;
  1664. }
  1665. }
  1666. return -1;
  1667. }
  1668. LLPolyMesh* LLAvatarAppearance::getHeadMesh()
  1669. {
  1670. return mMeshLOD[MESH_ID_HEAD]->mMeshParts[0]->getMesh();
  1671. }
  1672. LLPolyMesh* LLAvatarAppearance::getUpperBodyMesh()
  1673. {
  1674. return mMeshLOD[MESH_ID_UPPER_BODY]->mMeshParts[0]->getMesh();
  1675. }
  1676. LLPolyMesh* LLAvatarAppearance::getMesh(S32 which)
  1677. {
  1678. return mMeshLOD[which]->mMeshParts[0]->getMesh();
  1679. }
  1680. // Adds a morph mask to the appropriate baked texture structure
  1681. void LLAvatarAppearance::addMaskedMorph(EBakedTextureIndex index,
  1682. LLVisualParam* morph_target,
  1683. bool invert, const std::string& layer)
  1684. {
  1685. if (index < BAKED_NUM_INDICES)
  1686. {
  1687. LLMaskedMorph* morph = new LLMaskedMorph(morph_target, invert, layer);
  1688. mBakedTextureDatas[index].mMaskedMorphs.push_front(morph);
  1689. }
  1690. }
  1691. //static
  1692. bool LLAvatarAppearance::teToColorParams(ETextureIndex te, U32* param_name)
  1693. {
  1694. switch (te)
  1695. {
  1696. case TEX_UPPER_SHIRT:
  1697. param_name[0] = 803; // shirt_red
  1698. param_name[1] = 804; // shirt_green
  1699. param_name[2] = 805; // shirt_blue
  1700. break;
  1701. case TEX_LOWER_PANTS:
  1702. param_name[0] = 806; // pants_red
  1703. param_name[1] = 807; // pants_green
  1704. param_name[2] = 808; // pants_blue
  1705. break;
  1706. case TEX_LOWER_SHOES:
  1707. param_name[0] = 812; // shoes_red
  1708. param_name[1] = 813; // shoes_green
  1709. param_name[2] = 817; // shoes_blue
  1710. break;
  1711. case TEX_LOWER_SOCKS:
  1712. param_name[0] = 818; // socks_red
  1713. param_name[1] = 819; // socks_green
  1714. param_name[2] = 820; // socks_blue
  1715. break;
  1716. case TEX_UPPER_JACKET:
  1717. case TEX_LOWER_JACKET:
  1718. param_name[0] = 834; // jacket_red
  1719. param_name[1] = 835; // jacket_green
  1720. param_name[2] = 836; // jacket_blue
  1721. break;
  1722. case TEX_UPPER_GLOVES:
  1723. param_name[0] = 827; // gloves_red
  1724. param_name[1] = 829; // gloves_green
  1725. param_name[2] = 830; // gloves_blue
  1726. break;
  1727. case TEX_UPPER_UNDERSHIRT:
  1728. param_name[0] = 821; // undershirt_red
  1729. param_name[1] = 822; // undershirt_green
  1730. param_name[2] = 823; // undershirt_blue
  1731. break;
  1732. case TEX_LOWER_UNDERPANTS:
  1733. param_name[0] = 824; // underpants_red
  1734. param_name[1] = 825; // underpants_green
  1735. param_name[2] = 826; // underpants_blue
  1736. break;
  1737. case TEX_SKIRT:
  1738. param_name[0] = 921; // skirt_red
  1739. param_name[1] = 922; // skirt_green
  1740. param_name[2] = 923; // skirt_blue
  1741. break;
  1742. case TEX_HEAD_TATTOO:
  1743. case TEX_LOWER_TATTOO:
  1744. case TEX_UPPER_TATTOO:
  1745. param_name[0] = 1071; // tattoo_red
  1746. param_name[1] = 1072; // tattoo_green
  1747. param_name[2] = 1073; // tattoo_blue
  1748. break;
  1749. case TEX_HEAD_UNIVERSAL_TATTOO:
  1750. case TEX_UPPER_UNIVERSAL_TATTOO:
  1751. case TEX_LOWER_UNIVERSAL_TATTOO:
  1752. case TEX_HAIR_TATTOO:
  1753. case TEX_EYES_TATTOO:
  1754. case TEX_LEFT_ARM_TATTOO:
  1755. case TEX_LEFT_LEG_TATTOO:
  1756. case TEX_SKIRT_TATTOO:
  1757. case TEX_AUX1_TATTOO:
  1758. case TEX_AUX2_TATTOO:
  1759. case TEX_AUX3_TATTOO:
  1760. param_name[0] = 1238; // tattoo_universal_red
  1761. param_name[1] = 1239; // tattoo_universal_green
  1762. param_name[2] = 1240; // tattoo_universal_blue
  1763. break;
  1764. default:
  1765. llassert(false);
  1766. return false;
  1767. }
  1768. return true;
  1769. }
  1770. void LLAvatarAppearance::setClothesColor(ETextureIndex te,
  1771. const LLColor4& new_color,
  1772. bool upload_bake)
  1773. {
  1774. U32 param_name[3];
  1775. if (teToColorParams(te, param_name))
  1776. {
  1777. setVisualParamWeight(param_name[0], new_color.mV[VX], upload_bake);
  1778. setVisualParamWeight(param_name[1], new_color.mV[VY], upload_bake);
  1779. setVisualParamWeight(param_name[2], new_color.mV[VZ], upload_bake);
  1780. }
  1781. }
  1782. LLColor4 LLAvatarAppearance::getClothesColor(ETextureIndex te)
  1783. {
  1784. LLColor4 color;
  1785. U32 param_name[3];
  1786. if (teToColorParams(te, param_name))
  1787. {
  1788. color.mV[VX] = getVisualParamWeight(param_name[0]);
  1789. color.mV[VY] = getVisualParamWeight(param_name[1]);
  1790. color.mV[VZ] = getVisualParamWeight(param_name[2]);
  1791. }
  1792. return color;
  1793. }
  1794. //static
  1795. LLColor4 LLAvatarAppearance::getDummyColor()
  1796. {
  1797. return DUMMY_COLOR;
  1798. }
  1799. LLColor4 LLAvatarAppearance::getGlobalColor(const std::string& col_name) const
  1800. {
  1801. if (col_name == "skin_color" && mTexSkinColor)
  1802. {
  1803. return mTexSkinColor->getColor();
  1804. }
  1805. else if (col_name == "hair_color" && mTexHairColor)
  1806. {
  1807. return mTexHairColor->getColor();
  1808. }
  1809. if (col_name == "eye_color" && mTexEyeColor)
  1810. {
  1811. return mTexEyeColor->getColor();
  1812. }
  1813. else
  1814. {
  1815. return LLColor4(0.f, 1.f, 1.f, 1.f); // good debugging color
  1816. }
  1817. }
  1818. // Unlike most wearable functions, this works for both self and others.
  1819. //virtual
  1820. bool LLAvatarAppearance::isWearingWearableType(LLWearableType::EType t) const
  1821. {
  1822. return mWearableData && mWearableData->getWearableCount(t) > 0;
  1823. }
  1824. LLTexLayerSet* LLAvatarAppearance::getAvatarLayerSet(EBakedTextureIndex i) const
  1825. {
  1826. return mBakedTextureDatas[i].mTexLayerSet;
  1827. }
  1828. void LLAvatarAppearance::clearCollisionVolumes()
  1829. {
  1830. std::for_each(mCollisionVolumes.begin(), mCollisionVolumes.end(),
  1831. DeletePointer());
  1832. mCollisionVolumes.clear();
  1833. }
  1834. bool LLAvatarAppearance::allocateCollisionVolumes(U32 num)
  1835. {
  1836. if (num != (U32)mCollisionVolumes.size())
  1837. {
  1838. clearCollisionVolumes();
  1839. mCollisionVolumes.reserve(num);
  1840. try
  1841. {
  1842. for (U32 i = 0; i < num; ++i)
  1843. {
  1844. LLAvatarJointCollisionVolume* cv =
  1845. new LLAvatarJointCollisionVolume();
  1846. mCollisionVolumes.push_back(cv);
  1847. }
  1848. }
  1849. catch (...)
  1850. {
  1851. LLMemory::allocationFailed();
  1852. llwarns << "Failed to allocate collision volumes" << llendl;
  1853. clearCollisionVolumes();
  1854. return false;
  1855. }
  1856. }
  1857. return true;
  1858. }