lldriverparam.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /**
  2. * @file lldriverparam.cpp
  3. * @brief A visual parameter that drives (controls) other visual parameters.
  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 "lldriverparam.h"
  34. #include "llavatarappearance.h"
  35. #include "llwearable.h"
  36. #include "llwearabledata.h"
  37. //-----------------------------------------------------------------------------
  38. // LLDriverParamInfo
  39. //-----------------------------------------------------------------------------
  40. LLDriverParamInfo::LLDriverParamInfo()
  41. : mDriverParam(NULL)
  42. {
  43. }
  44. bool LLDriverParamInfo::parseXml(LLXmlTreeNode* node)
  45. {
  46. llassert(node->hasName("param") && node->getChildByName("param_driver"));
  47. if (!LLViewerVisualParamInfo::parseXml(node))
  48. {
  49. return false;
  50. }
  51. LLXmlTreeNode* param_driver_node = node->getChildByName("param_driver");
  52. if (!param_driver_node)
  53. {
  54. return false;
  55. }
  56. for (LLXmlTreeNode* child = param_driver_node->getChildByName("driven");
  57. child; child = param_driver_node->getNextNamedChild())
  58. {
  59. S32 driven_id;
  60. static LLStdStringHandle id_string = LLXmlTree::addAttributeString("id");
  61. if (child->getFastAttributeS32(id_string, driven_id))
  62. {
  63. F32 min1 = mMinWeight;
  64. F32 max1 = mMaxWeight;
  65. F32 max2 = max1;
  66. F32 min2 = max1;
  67. // driven ________ //
  68. // ^ /| |\ //
  69. // | / | | \ //
  70. // | / | | \ //
  71. // | / | | \ //
  72. // | / | | \ //
  73. //-------|----|-------|----|-------> driver //
  74. // | min1 max1 max2 min2
  75. static LLStdStringHandle min1_string = LLXmlTree::addAttributeString("min1");
  76. child->getFastAttributeF32(min1_string, min1); // optional
  77. static LLStdStringHandle max1_string = LLXmlTree::addAttributeString("max1");
  78. child->getFastAttributeF32(max1_string, max1); // optional
  79. static LLStdStringHandle max2_string = LLXmlTree::addAttributeString("max2");
  80. child->getFastAttributeF32(max2_string, max2); // optional
  81. static LLStdStringHandle min2_string = LLXmlTree::addAttributeString("min2");
  82. child->getFastAttributeF32(min2_string, min2); // optional
  83. // Push these on the front of the deque, so that we can construct
  84. // them in order later (faster)
  85. mDrivenInfoList.emplace_front(driven_id, min1, max1, max2, min2);
  86. }
  87. else
  88. {
  89. llerrs << "<driven> Unable to resolve driven parameter: "
  90. << driven_id << llendl;
  91. return false;
  92. }
  93. }
  94. return true;
  95. }
  96. //-----------------------------------------------------------------------------
  97. // LLDriverParam
  98. //-----------------------------------------------------------------------------
  99. LLDriverParam::LLDriverParam(LLAvatarAppearance* appearance,
  100. LLWearable* wearable)
  101. : LLViewerVisualParam(),
  102. mDefaultVec(),
  103. mDriven(),
  104. mCurrentDistortionParam(NULL),
  105. mAvatarAppearance(appearance),
  106. mWearablep(wearable)
  107. {
  108. llassert(mAvatarAppearance);
  109. llassert(mWearablep == NULL || mAvatarAppearance->isSelf());
  110. mDefaultVec.clear();
  111. }
  112. LLDriverParam::LLDriverParam(const LLDriverParam& other)
  113. : LLViewerVisualParam(other),
  114. mDefaultVec(other.mDefaultVec),
  115. mDriven(other.mDriven),
  116. mCurrentDistortionParam(other.mCurrentDistortionParam),
  117. mAvatarAppearance(other.mAvatarAppearance),
  118. mWearablep(other.mWearablep)
  119. {
  120. llassert(mAvatarAppearance);
  121. llassert(mWearablep == NULL || mAvatarAppearance->isSelf());
  122. }
  123. bool LLDriverParam::setInfo(LLDriverParamInfo* info)
  124. {
  125. llassert(mInfo == NULL);
  126. if (info->mID < 0)
  127. {
  128. return false;
  129. }
  130. mInfo = info;
  131. mID = info->mID;
  132. info->mDriverParam = this;
  133. setWeight(getDefaultWeight(), false);
  134. return true;
  135. }
  136. //virtual
  137. LLViewerVisualParam* LLDriverParam::cloneParam(LLWearable* wearable) const
  138. {
  139. llassert(wearable);
  140. return new LLDriverParam(*this);
  141. }
  142. void LLDriverParam::setWeight(F32 weight, bool upload_bake)
  143. {
  144. F32 min_weight = getMinWeight();
  145. F32 max_weight = getMaxWeight();
  146. if (mIsAnimating)
  147. {
  148. // allow overshoot when animating
  149. mCurWeight = weight;
  150. }
  151. else
  152. {
  153. mCurWeight = llclamp(weight, min_weight, max_weight);
  154. }
  155. // driven ________
  156. // ^ /| |\ ^
  157. // | / | | \ |
  158. // | / | | \ |
  159. // | / | | \ |
  160. // | / | | \ |
  161. //-------|----|-------|----|-------> driver
  162. // | min1 max1 max2 min2
  163. for (entry_list_t::iterator iter = mDriven.begin(), end = mDriven.end();
  164. iter != end; ++iter)
  165. {
  166. LLDrivenEntry* driven = &(*iter);
  167. LLDrivenEntryInfo* info = driven->mInfo;
  168. LLViewerVisualParam* param = driven->mParam;
  169. F32 driven_weight = 0.f;
  170. F32 driven_min = param->getMinWeight();
  171. F32 driven_max = param->getMaxWeight();
  172. if (mIsAnimating)
  173. {
  174. // driven param doesn't interpolate (textures, for example)
  175. if (!param->getAnimating())
  176. {
  177. continue;
  178. }
  179. if (mCurWeight < info->mMin1)
  180. {
  181. if (info->mMin1 == min_weight)
  182. {
  183. if (info->mMin1 == info->mMax1)
  184. {
  185. driven_weight = driven_max;
  186. }
  187. else
  188. {
  189. // Up-slope extrapolation
  190. F32 t = (mCurWeight - info->mMin1) /
  191. (info->mMax1 - info->mMin1);
  192. driven_weight = driven_min +
  193. t * (driven_max - driven_min);
  194. }
  195. }
  196. else
  197. {
  198. driven_weight = driven_min;
  199. }
  200. setDrivenWeight(driven, driven_weight, upload_bake);
  201. continue;
  202. }
  203. else if (mCurWeight > info->mMin2)
  204. {
  205. if (info->mMin2 == max_weight)
  206. {
  207. if (info->mMin2 == info->mMax2)
  208. {
  209. driven_weight = driven_max;
  210. }
  211. else
  212. {
  213. // Down-slope extrapolation
  214. F32 t = (mCurWeight - info->mMax2) /
  215. (info->mMin2 - info->mMax2);
  216. driven_weight = driven_max +
  217. t * (driven_min - driven_max);
  218. }
  219. }
  220. else
  221. {
  222. driven_weight = driven_min;
  223. }
  224. setDrivenWeight(driven, driven_weight, upload_bake);
  225. continue;
  226. }
  227. }
  228. driven_weight = getDrivenWeight(driven, mCurWeight);
  229. setDrivenWeight(driven,driven_weight,upload_bake);
  230. }
  231. }
  232. #if 0 // Unused methods
  233. F32 LLDriverParam::getTotalDistortion()
  234. {
  235. F32 sum = 0.f;
  236. for (entry_list_t::iterator iter = mDriven.begin(), end = mDriven.end();
  237. iter != end; ++iter)
  238. {
  239. LLDrivenEntry* driven = &(*iter);
  240. sum += driven->mParam->getTotalDistortion();
  241. }
  242. return sum;
  243. }
  244. const LLVector4a& LLDriverParam::getAvgDistortion()
  245. {
  246. // It is actually incorrect to take the average of averages, but it is good
  247. // enough here.
  248. LLVector4a sum;
  249. sum.clear();
  250. S32 count = 0;
  251. for (entry_list_t::iterator iter = mDriven.begin(), end = mDriven.end();
  252. iter != end; ++iter)
  253. {
  254. LLDrivenEntry* driven = &(*iter);
  255. sum.add(driven->mParam->getAvgDistortion());
  256. ++count;
  257. }
  258. sum.mul(1.f / (F32)count);
  259. mDefaultVec = sum;
  260. return mDefaultVec;
  261. }
  262. F32 LLDriverParam::getMaxDistortion()
  263. {
  264. F32 max = 0.f;
  265. for (entry_list_t::iterator iter = mDriven.begin(), end = mDriven.end();
  266. iter != end; ++iter)
  267. {
  268. LLDrivenEntry* driven = &(*iter);
  269. F32 param_max = driven->mParam->getMaxDistortion();
  270. if (param_max > max)
  271. {
  272. max = param_max;
  273. }
  274. }
  275. return max;
  276. }
  277. LLVector4a LLDriverParam::getVertexDistortion(S32 index, LLPolyMesh* poly_mesh)
  278. {
  279. LLVector4a sum;
  280. sum.clear();
  281. for (entry_list_t::iterator iter = mDriven.begin(), end = mDriven.end();
  282. iter != end; ++iter)
  283. {
  284. LLDrivenEntry* driven = &(*iter);
  285. sum.add(driven->mParam->getVertexDistortion(index, poly_mesh));
  286. }
  287. return sum;
  288. }
  289. const LLVector4a* LLDriverParam::getFirstDistortion(U32* index,
  290. LLPolyMesh** poly_mesh)
  291. {
  292. mCurrentDistortionParam = NULL;
  293. const LLVector4a* v = NULL;
  294. for (entry_list_t::iterator iter = mDriven.begin(), end = mDriven.end();
  295. iter != end; ++iter)
  296. {
  297. LLDrivenEntry* driven = &(*iter);
  298. v = driven->mParam->getFirstDistortion(index, poly_mesh);
  299. if (v)
  300. {
  301. mCurrentDistortionParam = driven->mParam;
  302. break;
  303. }
  304. }
  305. return v;
  306. }
  307. const LLVector4a* LLDriverParam::getNextDistortion(U32* index,
  308. LLPolyMesh** poly_mesh)
  309. {
  310. llassert(mCurrentDistortionParam);
  311. if (!mCurrentDistortionParam)
  312. {
  313. return NULL;
  314. }
  315. LLDrivenEntry* driven = NULL;
  316. entry_list_t::iterator iter;
  317. entry_list_t::iterator end = mDriven.end();
  318. // Set mDriven iteration to the right point
  319. for (iter = mDriven.begin(); iter != end; ++iter)
  320. {
  321. driven = &(*iter);
  322. if (driven->mParam == mCurrentDistortionParam)
  323. {
  324. break;
  325. }
  326. }
  327. llassert(driven);
  328. if (!driven)
  329. {
  330. return NULL; // shouldn't happen, but...
  331. }
  332. // We are already in the middle of a param's distortions, so get the next
  333. // one.
  334. const LLVector4a* v = driven->mParam->getNextDistortion(index, poly_mesh);
  335. if (!v && iter != end)
  336. {
  337. // This param is finished, so start the next param. It might not have
  338. // any distortions, though, so we have to loop to find the next param
  339. // that does.
  340. for (++iter; iter != end; ++iter)
  341. {
  342. driven = &(*iter);
  343. v = driven->mParam->getFirstDistortion(index, poly_mesh);
  344. if (v)
  345. {
  346. mCurrentDistortionParam = driven->mParam;
  347. break;
  348. }
  349. }
  350. }
  351. return v;
  352. };
  353. #endif
  354. S32 LLDriverParam::getDrivenParamsCount() const
  355. {
  356. return mDriven.size();
  357. }
  358. const LLViewerVisualParam* LLDriverParam::getDrivenParam(S32 index) const
  359. {
  360. if (0 > index || index >= (S32)mDriven.size())
  361. {
  362. return NULL;
  363. }
  364. return mDriven[index].mParam;
  365. }
  366. void LLDriverParam::setAnimationTarget(F32 target_value, bool upload_bake)
  367. {
  368. LLVisualParam::setAnimationTarget(target_value, upload_bake);
  369. for (entry_list_t::iterator iter = mDriven.begin(), end = mDriven.end();
  370. iter != end; ++iter)
  371. {
  372. LLDrivenEntry* driven = &(*iter);
  373. F32 driven_weight = getDrivenWeight(driven, mTargetWeight);
  374. // this isn't normally necessary, as driver params handle interpolation
  375. // of their driven params but texture params need to know to assume
  376. // their final value at beginning of interpolation
  377. driven->mParam->setAnimationTarget(driven_weight, upload_bake);
  378. }
  379. }
  380. void LLDriverParam::stopAnimating(bool upload_bake)
  381. {
  382. LLVisualParam::stopAnimating(upload_bake);
  383. for (entry_list_t::iterator iter = mDriven.begin(), end = mDriven.end();
  384. iter != end; ++iter)
  385. {
  386. LLDrivenEntry* driven = &(*iter);
  387. driven->mParam->setAnimating(false);
  388. }
  389. }
  390. //virtual
  391. bool LLDriverParam::linkDrivenParams(visual_param_mapper mapper,
  392. bool only_cross_params)
  393. {
  394. bool success = true;
  395. for (LLDriverParamInfo::entry_info_list_t::iterator
  396. iter = getInfo()->mDrivenInfoList.begin(),
  397. end = getInfo()->mDrivenInfoList.end();
  398. iter != end; ++iter)
  399. {
  400. LLDrivenEntryInfo* driven_info = &(*iter);
  401. S32 driven_id = driven_info->mDrivenID;
  402. // Check for already existing links. Do not overwrite.
  403. bool found = false;
  404. for (entry_list_t::iterator driven_iter = mDriven.begin(),
  405. driven_end = mDriven.end();
  406. driven_iter != driven_end; ++driven_iter)
  407. {
  408. if (driven_iter->mInfo->mDrivenID == driven_id)
  409. {
  410. found = true;
  411. break;
  412. }
  413. }
  414. if (!found)
  415. {
  416. LLViewerVisualParam* param =
  417. (LLViewerVisualParam*)mapper(driven_id);
  418. if (param)
  419. {
  420. param->setParamLocation(this->getParamLocation());
  421. }
  422. if (param &&
  423. (!only_cross_params || param->getCrossWearable()))
  424. {
  425. mDriven.emplace_back(param, driven_info);
  426. }
  427. else
  428. {
  429. success = false;
  430. }
  431. }
  432. }
  433. return success;
  434. }
  435. void LLDriverParam::resetDrivenParams()
  436. {
  437. mDriven.clear();
  438. mDriven.reserve(getInfo()->mDrivenInfoList.size());
  439. }
  440. void LLDriverParam::updateCrossDrivenParams(LLWearableType::EType driven_type)
  441. {
  442. bool needs_update = getWearableType() == driven_type;
  443. // if the driver has a driven entry for the passed-in wearable type, we
  444. // need to refresh the value
  445. for (entry_list_t::iterator iter = mDriven.begin(), end = mDriven.end();
  446. iter != end; ++iter)
  447. {
  448. LLDrivenEntry* driven = &(*iter);
  449. LLViewerVisualParam* param = driven ? driven->mParam : NULL;
  450. if (param && param->getCrossWearable() &&
  451. param->getWearableType() == driven_type)
  452. {
  453. needs_update = true;
  454. }
  455. }
  456. if (needs_update)
  457. {
  458. LLWearableType::EType driver_type = (LLWearableType::EType)getWearableType();
  459. // If we've gotten here, we've added a new wearable of type "type"
  460. // Thus this wearable needs to get updates from the driver wearable.
  461. // The call to setVisualParamWeight seems redundant, but is necessary
  462. // as the number of driven wearables has changed since the last update.
  463. LLWearable* wearable;
  464. wearable = mAvatarAppearance->getWearableData()->getTopWearable(driver_type);
  465. if (wearable)
  466. {
  467. wearable->setVisualParamWeight(mID,
  468. wearable->getVisualParamWeight(mID),
  469. false);
  470. }
  471. }
  472. }
  473. F32 LLDriverParam::getDrivenWeight(const LLDrivenEntry* driven,
  474. F32 input_weight)
  475. {
  476. F32 min_weight = getMinWeight();
  477. F32 max_weight = getMaxWeight();
  478. const LLDrivenEntryInfo* info = driven->mInfo;
  479. F32 driven_weight = 0.f;
  480. F32 driven_min = driven->mParam->getMinWeight();
  481. F32 driven_max = driven->mParam->getMaxWeight();
  482. if (input_weight <= info->mMin1)
  483. {
  484. if (info->mMin1 == info->mMax1 && info->mMin1 <= min_weight)
  485. {
  486. driven_weight = driven_max;
  487. }
  488. else
  489. {
  490. driven_weight = driven_min;
  491. }
  492. }
  493. else if (input_weight <= info->mMax1)
  494. {
  495. F32 t = (input_weight - info->mMin1) / (info->mMax1 - info->mMin1);
  496. driven_weight = driven_min + t * (driven_max - driven_min);
  497. }
  498. else if (input_weight <= info->mMax2)
  499. {
  500. driven_weight = driven_max;
  501. }
  502. else if (input_weight <= info->mMin2)
  503. {
  504. F32 t = (input_weight - info->mMax2) / (info->mMin2 - info->mMax2);
  505. driven_weight = driven_max + t * (driven_min - driven_max);
  506. }
  507. else
  508. {
  509. if (info->mMax2 >= max_weight)
  510. {
  511. driven_weight = driven_max;
  512. }
  513. else
  514. {
  515. driven_weight = driven_min;
  516. }
  517. }
  518. return driven_weight;
  519. }
  520. void LLDriverParam::setDrivenWeight(LLDrivenEntry* driven, F32 driven_weight,
  521. bool upload_bake)
  522. {
  523. if (mWearablep && mAvatarAppearance->isValid() &&
  524. driven->mParam->getCrossWearable() &&
  525. mAvatarAppearance->getWearableData()->isOnTop(mWearablep))
  526. {
  527. // Call setWeight through LLVOAvatarSelf so other wearables can be
  528. // updated with the correct values
  529. mAvatarAppearance->setVisualParamWeight((LLVisualParam*)driven->mParam,
  530. driven_weight, upload_bake);
  531. }
  532. else
  533. {
  534. driven->mParam->setWeight(driven_weight, upload_bake);
  535. }
  536. }