llwearable.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /**
  2. * @file llwearable.cpp
  3. * @brief LLWearable class implementation
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2002-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 "llwearable.h"
  34. #include "llavatarappearance.h"
  35. #include "llavatarappearancedefines.h"
  36. #include "lllocaltextureobject.h"
  37. #include "lltexlayer.h"
  38. #include "lltexturemanagerbridge.h"
  39. #include "llvisualparam.h"
  40. using namespace LLAvatarAppearanceDefines;
  41. // Keep track of active wearables: LLWearableList needs this to avoid double-
  42. // free !!!
  43. fast_hset<LLWearable*> LLWearable::sWearableList;
  44. //static
  45. S32 LLWearable::sCurrentDefinitionVersion = 1;
  46. // Private local helper function
  47. static std::string terse_F32_to_string(F32 f)
  48. {
  49. std::string r = llformat("%.2f", f);
  50. S32 len = r.length();
  51. // "1.20" -> "1.2"
  52. // "24.00" -> "24."
  53. while (len > 0 && r[len - 1] == '0')
  54. {
  55. r.erase(--len, 1);
  56. }
  57. if (r[len - 1] == '.')
  58. {
  59. // "24." -> "24"
  60. r.erase(len - 1, 1);
  61. }
  62. else if (r[0] == '-' && r[1] == '0')
  63. {
  64. // "-0.59" -> "-.59"
  65. r.erase(1, 1);
  66. }
  67. else if (r[0] == '0')
  68. {
  69. // "0.59" -> ".59"
  70. r.erase(0, 1);
  71. }
  72. return r;
  73. }
  74. LLWearable::LLWearable()
  75. : mDefinitionVersion(-1),
  76. mName(),
  77. mDescription(),
  78. mPermissions(),
  79. mSaleInfo(),
  80. mType(LLWearableType::WT_NONE),
  81. mSavedVisualParamMap(),
  82. mVisualParamIndexMap(),
  83. mTEMap(),
  84. mSavedTEMap()
  85. {
  86. sWearableList.insert(this);
  87. }
  88. //virtual
  89. LLWearable::~LLWearable()
  90. {
  91. sWearableList.erase(this);
  92. for (visual_param_index_map_t::iterator it = mVisualParamIndexMap.begin();
  93. it != mVisualParamIndexMap.end(); ++it)
  94. {
  95. LLVisualParam* vp = it->second;
  96. if (vp)
  97. {
  98. vp->clearNextParam();
  99. delete vp;
  100. it->second = NULL;
  101. }
  102. }
  103. mVisualParamIndexMap.clear();
  104. destroyTextures();
  105. }
  106. const std::string& LLWearable::getTypeLabel() const
  107. {
  108. return LLWearableType::getTypeLabel(mType);
  109. }
  110. const std::string& LLWearable::getTypeName() const
  111. {
  112. return LLWearableType::getTypeName(mType);
  113. }
  114. LLAssetType::EType LLWearable::getAssetType() const
  115. {
  116. return LLWearableType::getAssetType(mType);
  117. }
  118. bool LLWearable::exportFile(const std::string& filename) const
  119. {
  120. llofstream ofs(filename.c_str(),
  121. std::ios_base::out | std::ios_base::trunc |
  122. std::ios_base::binary);
  123. return ofs.is_open() && exportStream(ofs);
  124. }
  125. //virtual
  126. bool LLWearable::exportStream(std::ostream& output_stream) const
  127. {
  128. if (!output_stream.good()) return false;
  129. // Header and version
  130. output_stream << "LLWearable version " << mDefinitionVersion << "\n";
  131. // Name
  132. output_stream << mName << "\n";
  133. // Description
  134. output_stream << mDescription << "\n";
  135. // Permissions
  136. if (!mPermissions.exportLegacyStream(output_stream))
  137. {
  138. return false;
  139. }
  140. // Sale info
  141. if (!mSaleInfo.exportLegacyStream(output_stream))
  142. {
  143. return false;
  144. }
  145. // Wearable type
  146. output_stream << "type " << (S32) getType() << "\n";
  147. // Parameters
  148. output_stream << "parameters " << mVisualParamIndexMap.size() << "\n";
  149. for (visual_param_index_map_t::const_iterator
  150. iter = mVisualParamIndexMap.begin(),
  151. end = mVisualParamIndexMap.end();
  152. iter != end; ++iter)
  153. {
  154. S32 param_id = iter->first;
  155. const LLVisualParam* param = iter->second;
  156. F32 param_weight = param->getWeight();
  157. output_stream << param_id << " " << terse_F32_to_string(param_weight)
  158. << "\n";
  159. }
  160. // Texture entries
  161. output_stream << "textures " << mTEMap.size() << "\n";
  162. for (te_map_t::const_iterator iter = mTEMap.begin(), end = mTEMap.end();
  163. iter != end; ++iter)
  164. {
  165. S32 te = iter->first;
  166. const LLUUID& image_id = iter->second->getID();
  167. output_stream << te << " " << image_id << "\n";
  168. }
  169. return true;
  170. }
  171. void LLWearable::createVisualParams(LLAvatarAppearance* avatarp)
  172. {
  173. for (LLViewerVisualParam* param =
  174. (LLViewerVisualParam*)avatarp->getFirstVisualParam();
  175. param; param = (LLViewerVisualParam*) avatarp->getNextVisualParam())
  176. {
  177. if (param->getWearableType() == mType)
  178. {
  179. LLVisualParam* clone_param = param->cloneParam(this);
  180. clone_param->setParamLocation(LOC_UNKNOWN);
  181. clone_param->setParamLocation(LOC_WEARABLE);
  182. addVisualParam(clone_param);
  183. }
  184. }
  185. // Resync driver parameters to point to the newly cloned driven
  186. // parameters
  187. for (visual_param_index_map_t::iterator
  188. param_iter = mVisualParamIndexMap.begin(),
  189. end = mVisualParamIndexMap.end();
  190. param_iter != end; ++param_iter)
  191. {
  192. LLVisualParam* param = param_iter->second;
  193. LLVisualParam*(LLWearable::*wearable_function)(S32)const =
  194. &LLWearable::getVisualParam;
  195. // need this line to disambiguate between versions of
  196. // LLCharacter::getVisualParam()
  197. LLVisualParam*(LLAvatarAppearance::*param_function)(S32)const =
  198. &LLAvatarAppearance::getVisualParam;
  199. param->resetDrivenParams();
  200. if (!param->linkDrivenParams(boost::bind(wearable_function,
  201. (LLWearable*)this, _1),
  202. false))
  203. {
  204. if (!param->linkDrivenParams(boost::bind(param_function, avatarp,
  205. _1),
  206. true))
  207. {
  208. #if 0 // Temporarily made into a debug message, till LL determines
  209. // whether non-linked params are a normal occurrence or not...
  210. llwarns << "Could not link driven params for wearable "
  211. << getName() << " id: " << param->getID() << llendl;
  212. #else
  213. LL_DEBUGS("Avatar") << "Could not link driven params for wearable "
  214. << getName() << " id: " << param->getID()
  215. << LL_ENDL;
  216. #endif
  217. continue;
  218. }
  219. }
  220. }
  221. }
  222. void LLWearable::createLayers(S32 te, LLAvatarAppearance* avatarp)
  223. {
  224. LLTexLayerSet* layer_set = NULL;
  225. const LLAvatarAppearanceDictionary::TextureEntry* texture_dict =
  226. gAvatarAppDictp->getTexture((ETextureIndex)te);
  227. if (texture_dict && texture_dict->mIsUsedByBakedTexture)
  228. {
  229. const EBakedTextureIndex baked_index =
  230. texture_dict->mBakedTextureIndex;
  231. layer_set = avatarp->getAvatarLayerSet(baked_index);
  232. }
  233. if (layer_set)
  234. {
  235. layer_set->cloneTemplates(mTEMap[te], (ETextureIndex)te, this);
  236. }
  237. else
  238. {
  239. llwarns << "Could not find layerset for texture entry " << te
  240. << " in wearable. mIsUsedByBakedTexture = "
  241. << texture_dict->mBakedTextureIndex << llendl;
  242. }
  243. }
  244. LLWearable::EImportResult LLWearable::importFile(const std::string& filename,
  245. LLAvatarAppearance* avatarp)
  246. {
  247. llifstream ifs(filename.c_str(), std::ios_base::in | std::ios_base::binary);
  248. return ifs.is_open() ? importStream(ifs, avatarp) : FAILURE;
  249. }
  250. //virtual
  251. LLWearable::EImportResult LLWearable::importStream(std::istream& input_stream,
  252. LLAvatarAppearance* avatarp)
  253. {
  254. if (!avatarp || !gTextureManagerBridgep)
  255. {
  256. return FAILURE;
  257. }
  258. // *NOTE: changing the type or size of this buffer will require changes in
  259. // the fscanf() code below.
  260. // We are using a local max buffer size here to avoid issues if MAX_STRING
  261. // size changes.
  262. constexpr U32 PARSE_BUFFER_SIZE = 2048;
  263. char buffer[PARSE_BUFFER_SIZE];
  264. char uuid_buffer[37];
  265. // This data is being generated on the viewer.
  266. // Impose some sane limits on parameter and texture counts.
  267. constexpr S32 MAX_WEARABLE_ASSET_TEXTURES = 100;
  268. constexpr S32 MAX_WEARABLE_ASSET_PARAMETERS = 1000;
  269. // Read header and version
  270. if (!getNextPopulatedLine(input_stream, buffer, PARSE_BUFFER_SIZE))
  271. {
  272. llwarns << "Failed to read wearable asset input stream." << llendl;
  273. return FAILURE;
  274. }
  275. if (sscanf(buffer, "LLWearable version %d\n", &mDefinitionVersion) != 1)
  276. {
  277. return BAD_HEADER;
  278. }
  279. // Temporary hack to allow wearables with definition version 24 to still
  280. // load. This should only affect lindens and NDA'd testers who have saved
  281. // wearables in 2.0 the extra check for version == 24 can be removed before
  282. // release, once internal testers have loaded these wearables again.
  283. // See hack pt 2 at bottom of function to ensure that these wearables get
  284. // re-saved with version definition 22.
  285. if (mDefinitionVersion > sCurrentDefinitionVersion &&
  286. mDefinitionVersion != 24)
  287. {
  288. llwarns << "Wearable asset has newer version (" << mDefinitionVersion
  289. << ") than XML (" << sCurrentDefinitionVersion << ")"
  290. << llendl;
  291. return FAILURE;
  292. }
  293. // Name may be empty
  294. if (!input_stream.good())
  295. {
  296. llwarns << "Bad wearable asset: early end of input stream while reading name"
  297. << llendl;
  298. return FAILURE;
  299. }
  300. input_stream.getline(buffer, PARSE_BUFFER_SIZE);
  301. mName = buffer;
  302. // Description may be empty
  303. if (!input_stream.good())
  304. {
  305. llwarns << "Bad wearable asset: early end of input stream while reading description"
  306. << llendl;
  307. return FAILURE;
  308. }
  309. input_stream.getline(buffer, PARSE_BUFFER_SIZE);
  310. mDescription = buffer;
  311. // Permissions may have extra empty lines before the correct line
  312. if (!getNextPopulatedLine(input_stream, buffer, PARSE_BUFFER_SIZE))
  313. {
  314. llwarns << "Bad wearable asset: early end of input stream while reading permissions"
  315. << llendl;
  316. return FAILURE;
  317. }
  318. S32 perm_version = -1;
  319. if (sscanf(buffer, " permissions %d\n", &perm_version) != 1 ||
  320. perm_version != 0)
  321. {
  322. llwarns << "Bad wearable asset: missing valid permissions" << llendl;
  323. return FAILURE;
  324. }
  325. if (!mPermissions.importLegacyStream(input_stream))
  326. {
  327. return FAILURE;
  328. }
  329. // Sale info
  330. if (!getNextPopulatedLine(input_stream, buffer, PARSE_BUFFER_SIZE))
  331. {
  332. llwarns << "Bad wearable asset: early end of input stream while reading sale info"
  333. << llendl;
  334. return FAILURE;
  335. }
  336. S32 sale_info_version = -1;
  337. if (sscanf(buffer, " sale_info %d\n", &sale_info_version) != 1 ||
  338. sale_info_version != 0)
  339. {
  340. llwarns << "Bad wearable asset: missing valid sale_info" << llendl;
  341. return FAILURE;
  342. }
  343. // Sale info used to contain next owner perm. It is now in the permissions.
  344. // Thus, we read that out, and fix legacy objects. It's possible this op
  345. // would fail, but it should pick up the vast majority of the tasks.
  346. bool has_perm_mask = false;
  347. U32 perm_mask = 0;
  348. if (!mSaleInfo.importLegacyStream(input_stream, has_perm_mask, perm_mask))
  349. {
  350. return FAILURE;
  351. }
  352. if (has_perm_mask)
  353. {
  354. // Fair use fix.
  355. if (!(perm_mask & PERM_COPY))
  356. {
  357. perm_mask |= PERM_TRANSFER;
  358. }
  359. mPermissions.setMaskNext(perm_mask);
  360. }
  361. // Wearable type
  362. if (!getNextPopulatedLine(input_stream, buffer, PARSE_BUFFER_SIZE))
  363. {
  364. llwarns << "Bad wearable asset: early end of input stream while reading type"
  365. << llendl;
  366. return FAILURE;
  367. }
  368. S32 type = -1;
  369. if (sscanf(buffer, "type %d\n", &type) != 1)
  370. {
  371. llwarns << "Bad wearable asset: cannot read type" << llendl;
  372. return FAILURE;
  373. }
  374. if (type >= 0 && type < LLWearableType::WT_COUNT)
  375. {
  376. setType((LLWearableType::EType)type, avatarp);
  377. }
  378. else
  379. {
  380. mType = LLWearableType::WT_COUNT;
  381. llwarns << "Bad wearable asset: bad type #" << type << llendl;
  382. return FAILURE;
  383. }
  384. // Parameters header
  385. if (!getNextPopulatedLine(input_stream, buffer, PARSE_BUFFER_SIZE))
  386. {
  387. llwarns << "Bad wearable asset: early end of input stream while reading parameters header. Type: "
  388. << LLWearableType::getTypeName(getType()) << llendl;
  389. return FAILURE;
  390. }
  391. S32 num_parameters = -1;
  392. if (sscanf(buffer, "parameters %d\n", &num_parameters) != 1)
  393. {
  394. llwarns << "Bad wearable asset: missing parameters block. Type: "
  395. << LLWearableType::getTypeName(getType()) << llendl;
  396. return FAILURE;
  397. }
  398. if (num_parameters > MAX_WEARABLE_ASSET_PARAMETERS)
  399. {
  400. llwarns << "Bad wearable asset: too many parameters: "
  401. << num_parameters << ". Type: "
  402. << LLWearableType::getTypeName(getType()) << llendl;
  403. return FAILURE;
  404. }
  405. S32 param_count = mVisualParamIndexMap.size();
  406. if (num_parameters > param_count)
  407. {
  408. llwarns << "Wearable parameter mismatch. Reading in "
  409. << num_parameters << " from file, but created " << param_count
  410. << " from avatar parameters. Type: "
  411. << LLWearableType::getTypeName(getType()) << llendl;
  412. }
  413. else if (num_parameters < param_count)
  414. {
  415. llinfos << "Old wearable detected. Reading in "
  416. << num_parameters << " from file, but created " << param_count
  417. << " from avatar parameters. Type: "
  418. << LLWearableType::getTypeName(getType()) << llendl;
  419. }
  420. // Parameters
  421. S32 i;
  422. for (i = 0; i < num_parameters; ++i)
  423. {
  424. if (!getNextPopulatedLine(input_stream, buffer, PARSE_BUFFER_SIZE))
  425. {
  426. llwarns << "Bad wearable asset: early end of input stream "
  427. << "while reading parameter #" << i << ". Type: "
  428. << LLWearableType::getTypeName(getType()) << llendl;
  429. return FAILURE;
  430. }
  431. S32 param_id = 0;
  432. F32 param_weight = 0.f;
  433. if (sscanf(buffer, "%d %f\n", &param_id, &param_weight) != 2)
  434. {
  435. llwarns << "Bad wearable asset: bad parameter, #" << i
  436. << ". Type: " << LLWearableType::getTypeName(getType())
  437. << llendl;
  438. return FAILURE;
  439. }
  440. mSavedVisualParamMap[param_id] = param_weight;
  441. }
  442. // Textures header
  443. if (!getNextPopulatedLine(input_stream, buffer, PARSE_BUFFER_SIZE))
  444. {
  445. llwarns << "Bad wearable asset: early end of input stream while reading textures header #"
  446. << i << ". Type: " << LLWearableType::getTypeName(getType())
  447. << llendl;
  448. return FAILURE;
  449. }
  450. S32 num_textures = -1;
  451. if (sscanf(buffer, "textures %d\n", &num_textures) != 1)
  452. {
  453. llwarns << "Bad wearable asset: missing textures block. Type: "
  454. << LLWearableType::getTypeName(getType()) << llendl;
  455. return FAILURE;
  456. }
  457. if (num_textures > MAX_WEARABLE_ASSET_TEXTURES)
  458. {
  459. llwarns << "Bad wearable asset: too many textures: " << num_textures
  460. << ". Type: " << LLWearableType::getTypeName(getType())
  461. << llendl;
  462. return FAILURE;
  463. }
  464. // Textures
  465. for (i = 0; i < num_textures; ++i)
  466. {
  467. if (!getNextPopulatedLine(input_stream, buffer, PARSE_BUFFER_SIZE))
  468. {
  469. llwarns << "Bad wearable asset: early end of input stream "
  470. << "while reading textures #" << i << ". Type: "
  471. << LLWearableType::getTypeName(getType()) << llendl;
  472. return FAILURE;
  473. }
  474. S32 te = 0;
  475. if (sscanf(buffer, "%d %36s\n", &te, uuid_buffer) != 2)
  476. {
  477. llwarns << "Bad wearable asset: bad texture, #" << i << ". Type: "
  478. << LLWearableType::getTypeName(getType()) << llendl;
  479. return FAILURE;
  480. }
  481. if (te >= TEX_NUM_INDICES)
  482. {
  483. llwarns << "Bad wearable asset: texture index too large " << te
  484. << ". Type: " << LLWearableType::getTypeName(getType())
  485. << llendl;
  486. return FAILURE;
  487. }
  488. if (!LLUUID::validate(uuid_buffer))
  489. {
  490. llwarns << "Bad wearable asset: bad texture uuid: " << uuid_buffer
  491. << ". Type: " << LLWearableType::getTypeName(getType())
  492. << llendl;
  493. return FAILURE;
  494. }
  495. LLUUID id = LLUUID(uuid_buffer);
  496. LLGLTexture* image = gTextureManagerBridgep->getFetchedTexture(id);
  497. if (mTEMap.find(te) != mTEMap.end())
  498. {
  499. delete mTEMap[te];
  500. }
  501. if (mSavedTEMap.find(te) != mSavedTEMap.end())
  502. {
  503. delete mSavedTEMap[te];
  504. }
  505. LLUUID textureid(uuid_buffer);
  506. mTEMap[te] = new LLLocalTextureObject(image, textureid);
  507. mSavedTEMap[te] = new LLLocalTextureObject(image, textureid);
  508. createLayers(te, avatarp);
  509. }
  510. // Copy all saved param values to working params
  511. revertValues();
  512. return SUCCESS;
  513. }
  514. bool LLWearable::getNextPopulatedLine(std::istream& input_stream, char* buffer,
  515. U32 buffer_size)
  516. {
  517. if (!input_stream.good())
  518. {
  519. return false;
  520. }
  521. do
  522. {
  523. input_stream.getline(buffer, buffer_size);
  524. }
  525. while (input_stream.good() && buffer[0] == '\0');
  526. return buffer[0] != '\0';
  527. }
  528. void LLWearable::setType(LLWearableType::EType type,
  529. LLAvatarAppearance* avatarp)
  530. {
  531. mType = type;
  532. createVisualParams(avatarp);
  533. }
  534. LLLocalTextureObject* LLWearable::getLocalTextureObject(S32 index)
  535. {
  536. te_map_t::iterator iter = mTEMap.find(index);
  537. if (iter != mTEMap.end())
  538. {
  539. LLLocalTextureObject* lto = iter->second;
  540. return lto;
  541. }
  542. return NULL;
  543. }
  544. const LLLocalTextureObject* LLWearable::getLocalTextureObject(S32 index) const
  545. {
  546. te_map_t::const_iterator iter = mTEMap.find(index);
  547. if (iter != mTEMap.end())
  548. {
  549. const LLLocalTextureObject* lto = iter->second;
  550. return lto;
  551. }
  552. return NULL;
  553. }
  554. void LLWearable::getLocalTextureListSeq(std::vector<LLLocalTextureObject*>& lv)
  555. {
  556. lv.clear();
  557. lv.reserve(mTEMap.size());
  558. for (te_map_t::const_iterator iter = mTEMap.begin(), end = mTEMap.end();
  559. iter != end; ++iter)
  560. {
  561. LLLocalTextureObject* ltop = iter->second;
  562. if (ltop)
  563. {
  564. lv.push_back(ltop);
  565. }
  566. }
  567. }
  568. LLLocalTextureObject* LLWearable::setLocalTextureObject(S32 index,
  569. LLLocalTextureObject& lto)
  570. {
  571. if (mTEMap.find(index) != mTEMap.end())
  572. {
  573. mTEMap.erase(index);
  574. }
  575. LLLocalTextureObject* clone_lto = new LLLocalTextureObject(lto);
  576. mTEMap[index] = clone_lto;
  577. return clone_lto;
  578. }
  579. // *FIXME: this triggers changes to driven params on avatar, potentially
  580. // clobbering baked appearance.
  581. void LLWearable::revertValues()
  582. {
  583. // Update saved settings so wearable is no longer dirty; non-driver params
  584. // first.
  585. for (param_map_t::const_iterator iter = mSavedVisualParamMap.begin(),
  586. end = mSavedVisualParamMap.end();
  587. iter != end; ++iter)
  588. {
  589. S32 id = iter->first;
  590. F32 value = iter->second;
  591. LLVisualParam* param = getVisualParam(id);
  592. if (param && !param->asDriverParam())
  593. {
  594. setVisualParamWeight(id, value, true);
  595. }
  596. }
  597. // Then update driver params
  598. for (param_map_t::const_iterator iter = mSavedVisualParamMap.begin(),
  599. end = mSavedVisualParamMap.end();
  600. iter != end; ++iter)
  601. {
  602. S32 id = iter->first;
  603. F32 value = iter->second;
  604. LLVisualParam* param = getVisualParam(id);
  605. if (param && param->asDriverParam())
  606. {
  607. setVisualParamWeight(id, value, true);
  608. }
  609. }
  610. // Make sure that saved values are sane
  611. for (param_map_t::const_iterator iter = mSavedVisualParamMap.begin(),
  612. end = mSavedVisualParamMap.end();
  613. iter != end; ++iter)
  614. {
  615. S32 id = iter->first;
  616. LLVisualParam* param = getVisualParam(id);
  617. if (param)
  618. {
  619. mSavedVisualParamMap[id] = param->getWeight();
  620. }
  621. }
  622. syncImages(mSavedTEMap, mTEMap);
  623. }
  624. void LLWearable::saveValues()
  625. {
  626. // Update saved settings so wearable is no longer dirty
  627. mSavedVisualParamMap.clear();
  628. for (visual_param_index_map_t::const_iterator
  629. iter = mVisualParamIndexMap.begin(),
  630. end = mVisualParamIndexMap.end();
  631. iter != end; ++iter)
  632. {
  633. S32 id = iter->first;
  634. LLVisualParam* wearable_param = iter->second;
  635. F32 value = wearable_param->getWeight();
  636. mSavedVisualParamMap[id] = value;
  637. }
  638. // Deep copy of mTEMap (copies only those tes that are current, filling in
  639. // defaults where needed)
  640. syncImages(mTEMap, mSavedTEMap);
  641. }
  642. void LLWearable::syncImages(te_map_t& src, te_map_t& dst)
  643. {
  644. // Deep copy of mTEMap (copies only those tes that are current, filling in
  645. // defaults where needed)
  646. for (S32 te = 0; te < TEX_NUM_INDICES; ++te)
  647. {
  648. if (LLAvatarAppearanceDictionary::getTEWearableType((ETextureIndex)te) == mType)
  649. {
  650. te_map_t::const_iterator iter = src.find(te);
  651. LLUUID image_id;
  652. LLGLTexture* image = NULL;
  653. LLLocalTextureObject* lto = NULL;
  654. if (iter != src.end())
  655. {
  656. // There is a Local Texture Object in the source image map. Use
  657. // this to populate the values to store in the destination
  658. // image map.
  659. lto = iter->second;
  660. image = lto->getImage();
  661. image_id = lto->getID();
  662. }
  663. else
  664. {
  665. // There is no Local Texture Object in the source image map.
  666. // Get the defaults values for populating the destination image
  667. // map.
  668. image_id = getDefaultTextureImageID((ETextureIndex) te);
  669. if (gTextureManagerBridgep)
  670. {
  671. image = gTextureManagerBridgep->getFetchedTexture(image_id);
  672. }
  673. }
  674. if (dst.find(te) != dst.end())
  675. {
  676. // There is already an entry in the destination map for the
  677. // texture. Just update its values.
  678. dst[te]->setImage(image);
  679. dst[te]->setID(image_id);
  680. }
  681. else
  682. {
  683. // No entry found in the destination map, we need to create a
  684. // new Local Texture Object
  685. dst[te] = new LLLocalTextureObject(image, image_id);
  686. }
  687. if (lto)
  688. {
  689. // If we pulled values from a Local Texture Object in the
  690. // source map, make sure the proper flags are set in the new
  691. // (or updated) entry in the destination map.
  692. dst[te]->setBakedReady(lto->getBakedReady());
  693. dst[te]->setDiscard(lto->getDiscard());
  694. }
  695. }
  696. }
  697. }
  698. void LLWearable::destroyTextures()
  699. {
  700. std::for_each(mTEMap.begin(), mTEMap.end(), DeletePairedPointer());
  701. mTEMap.clear();
  702. std::for_each(mSavedTEMap.begin(), mSavedTEMap.end(),
  703. DeletePairedPointer());
  704. mSavedTEMap.clear();
  705. }
  706. void LLWearable::addVisualParam(LLVisualParam* param)
  707. {
  708. S32 id = param->getID();
  709. if (mVisualParamIndexMap[id])
  710. {
  711. delete mVisualParamIndexMap[id];
  712. }
  713. param->setIsDummy(false);
  714. param->setParamLocation(LOC_WEARABLE);
  715. mVisualParamIndexMap[id] = param;
  716. mSavedVisualParamMap[id] = param->getDefaultWeight();
  717. }
  718. void LLWearable::setVisualParamWeight(S32 i, F32 value, bool upload_bake)
  719. {
  720. visual_param_index_map_t::iterator it = mVisualParamIndexMap.find(i);
  721. if (it == mVisualParamIndexMap.end())
  722. {
  723. llwarns << "Passed invalid parameter index #" << i << " for wearable: "
  724. << getName() << llendl;
  725. }
  726. it->second->setWeight(value, upload_bake);
  727. }
  728. F32 LLWearable::getVisualParamWeight(S32 i) const
  729. {
  730. visual_param_index_map_t::const_iterator it = mVisualParamIndexMap.find(i);
  731. if (it == mVisualParamIndexMap.end())
  732. {
  733. llwarns << "Passed invalid parameter index #" << i << " for wearable: "
  734. << getName() << llendl;
  735. return -1.f;
  736. }
  737. const LLVisualParam* wearable_param = it->second;
  738. return wearable_param->getWeight();
  739. }
  740. LLVisualParam* LLWearable::getVisualParam(S32 i) const
  741. {
  742. visual_param_index_map_t::const_iterator it = mVisualParamIndexMap.find(i);
  743. return it == mVisualParamIndexMap.end() ? NULL : it->second;
  744. }
  745. void LLWearable::getVisualParams(visual_param_vec_t& list)
  746. {
  747. list.reserve(mVisualParamIndexMap.size());
  748. // Add all visual params to the passed-in vector
  749. for (visual_param_index_map_t::iterator
  750. iter = mVisualParamIndexMap.begin(),
  751. end = mVisualParamIndexMap.end();
  752. iter != end; ++iter)
  753. {
  754. list.push_back(iter->second);
  755. }
  756. }
  757. void LLWearable::animateParams(F32 delta, bool upload_bake)
  758. {
  759. for (visual_param_index_map_t::iterator
  760. iter = mVisualParamIndexMap.begin(),
  761. end = mVisualParamIndexMap.end();
  762. iter != end; ++iter)
  763. {
  764. LLVisualParam* param = (LLVisualParam*)iter->second;
  765. param->animate(delta, upload_bake);
  766. }
  767. }
  768. LLColor4 LLWearable::getClothesColor(S32 te) const
  769. {
  770. LLColor4 color;
  771. U32 param_name[3];
  772. if (LLAvatarAppearance::teToColorParams((LLAvatarAppearanceDefines::ETextureIndex)te,
  773. param_name))
  774. {
  775. for (U8 index = 0; index < 3; ++index)
  776. {
  777. color.mV[index] = getVisualParamWeight(param_name[index]);
  778. }
  779. }
  780. return color;
  781. }
  782. void LLWearable::setClothesColor(S32 te, const LLColor4& new_color,
  783. bool upload_bake)
  784. {
  785. U32 param_name[3];
  786. if (LLAvatarAppearance::teToColorParams((LLAvatarAppearanceDefines::ETextureIndex)te,
  787. param_name))
  788. {
  789. for (U8 index = 0; index < 3; ++index)
  790. {
  791. setVisualParamWeight(param_name[index], new_color.mV[index],
  792. upload_bake);
  793. }
  794. }
  795. }
  796. void LLWearable::writeToAvatar(LLAvatarAppearance* avatarp)
  797. {
  798. if (!avatarp) return;
  799. // Pull params
  800. for (LLVisualParam* param = avatarp->getFirstVisualParam(); param;
  801. param = avatarp->getNextVisualParam())
  802. {
  803. if (param &&
  804. ((LLViewerVisualParam*)param)->getWearableType() == mType &&
  805. // Cross-wearable parameters are not authoritative, as they are
  806. // driven by a different wearable. So do not copy the values to
  807. // the avatar object if cross wearable. Cross wearable params get
  808. // their values from the avatar, they shouldn't write the other
  809. // way.
  810. !((LLViewerVisualParam*)param)->getCrossWearable())
  811. {
  812. S32 param_id = param->getID();
  813. F32 weight = getVisualParamWeight(param_id);
  814. avatarp->setVisualParamWeight(param_id, weight, false);
  815. }
  816. }
  817. }