llcharacter.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /**
  2. * @file llcharacter.cpp
  3. * @brief Implementation of LLCharacter 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 "llcharacter.h"
  34. #include "llfasttimer.h"
  35. #include "llstl.h"
  36. #include "llstring.h"
  37. #define SKEL_HEADER "Linden Skeleton 1.0"
  38. LLStringTable LLCharacter::sVisualParamNames(1024);
  39. std::vector<LLCharacter*> LLCharacter::sInstances;
  40. //static
  41. void LLCharacter::initClass()
  42. {
  43. // Let's avoid memory fragmentation over time...
  44. sInstances.reserve(256);
  45. }
  46. //static
  47. void LLCharacter::dumpStats()
  48. {
  49. llinfos << "LLCharacter: sInstances capacity reached: "
  50. << sInstances.capacity() << llendl;
  51. }
  52. LLCharacter::LLCharacter()
  53. : mPreferredPelvisHeight(0.f),
  54. mSex(SEX_FEMALE),
  55. mAppearanceSerialNum(0),
  56. mSkeletonSerialNum(0)
  57. {
  58. mMotionController.setCharacter(this);
  59. sInstances.push_back(this);
  60. mPauseRequest = new LLPauseRequestHandle();
  61. }
  62. LLCharacter::~LLCharacter()
  63. {
  64. for (LLVisualParam* param = getFirstVisualParam(); param;
  65. param = getNextVisualParam())
  66. {
  67. delete param;
  68. }
  69. mVisualParamIndexMap.clear();
  70. mVisualParamNameMap.clear();
  71. mAnimationData.clear();
  72. for (U32 i = 0, count = mDeferredDeletions.size(); i < count; ++i)
  73. {
  74. delete mDeferredDeletions[i];
  75. }
  76. mDeferredDeletions.clear();
  77. for (U32 i = 0, size = sInstances.size(); i < size; ++i)
  78. {
  79. if (sInstances[i] == this)
  80. {
  81. if (i != size - 1)
  82. {
  83. sInstances[i] = sInstances[size - 1];
  84. }
  85. sInstances.pop_back();
  86. break;
  87. }
  88. }
  89. }
  90. S32 LLCharacter::getVisualParamCountInGroup(const EVisualParamGroup group) const
  91. {
  92. S32 rtn = 0;
  93. for (visual_param_index_map_t::const_iterator
  94. iter = mVisualParamIndexMap.begin(),
  95. end = mVisualParamIndexMap.end();
  96. iter != end; )
  97. {
  98. if ((iter++)->second->getGroup() == group)
  99. {
  100. ++rtn;
  101. }
  102. }
  103. return rtn;
  104. }
  105. S32 LLCharacter::getVisualParamID(LLVisualParam* id)
  106. {
  107. for (visual_param_index_map_it_t iter = mVisualParamIndexMap.begin(),
  108. end = mVisualParamIndexMap.end();
  109. iter != end; ++iter)
  110. {
  111. if (iter->second == id)
  112. {
  113. return iter->first;
  114. }
  115. }
  116. return 0;
  117. }
  118. LLJoint* LLCharacter::getJoint(U32 key)
  119. {
  120. LLJoint* joint = NULL;
  121. LLJoint* root = getRootJoint();
  122. if (root)
  123. {
  124. joint = root->findJoint(key);
  125. }
  126. if (!joint)
  127. {
  128. llwarns << "Failed to find joint for joint key: " << key << llendl;
  129. }
  130. return joint;
  131. }
  132. bool LLCharacter::isMotionActive(const LLUUID& id)
  133. {
  134. LLMotion* motionp = mMotionController.findMotion(id);
  135. return motionp && mMotionController.isMotionActive(motionp);
  136. }
  137. //virtual
  138. void LLCharacter::updateMotions(e_update_t update_type)
  139. {
  140. if (update_type == HIDDEN_UPDATE)
  141. {
  142. LL_FAST_TIMER(FTM_UPDATE_HIDDEN_ANIMATION);
  143. mMotionController.updateMotionsMinimal();
  144. }
  145. else
  146. {
  147. LL_FAST_TIMER(FTM_UPDATE_ANIMATION);
  148. // Un-pause if the number of outstanding pause requests has dropped to
  149. // the initial one
  150. if (mMotionController.isPaused() && mPauseRequest->getNumRefs() == 1)
  151. {
  152. mMotionController.unpauseAllMotions();
  153. }
  154. bool force_update = (update_type == FORCE_UPDATE);
  155. {
  156. LL_FAST_TIMER(FTM_UPDATE_MOTIONS);
  157. mMotionController.updateMotions(force_update);
  158. }
  159. }
  160. }
  161. void LLCharacter::dumpCharacter(LLJoint* joint)
  162. {
  163. // Handle top level entry into recursion
  164. if (!joint)
  165. {
  166. llinfos << "DEBUG: Dumping Character @" << this << llendl;
  167. dumpCharacter(getRootJoint());
  168. llinfos << "DEBUG: Done." << llendl;
  169. return;
  170. }
  171. // Print joint info
  172. llinfos << "DEBUG: " << joint->getName() << " ("
  173. << (joint->getParent() ? joint->getParent()->getName()
  174. : std::string("ROOT")) << ")" << llendl;
  175. // Recurse
  176. for (LLJoint::child_list_t::iterator iter = joint->mChildren.begin(),
  177. end = joint->mChildren.end();
  178. iter != end; ++iter)
  179. {
  180. LLJoint* child_joint = *iter;
  181. dumpCharacter(child_joint);
  182. }
  183. }
  184. void* LLCharacter::getAnimationData(const std::string& name)
  185. {
  186. return get_ptr_in_map(mAnimationData, name);
  187. }
  188. bool LLCharacter::setVisualParamWeight(const LLVisualParam* which_param,
  189. F32 weight, bool upload_bake)
  190. {
  191. S32 index = which_param->getID();
  192. visual_param_index_map_it_t index_iter = mVisualParamIndexMap.find(index);
  193. if (index_iter != mVisualParamIndexMap.end())
  194. {
  195. index_iter->second->setWeight(weight, upload_bake);
  196. return true;
  197. }
  198. return false;
  199. }
  200. bool LLCharacter::setVisualParamWeight(const char* param_name, F32 weight,
  201. bool upload_bake)
  202. {
  203. std::string tname(param_name);
  204. LLStringUtil::toLower(tname);
  205. char* tableptr = sVisualParamNames.checkString(tname);
  206. if (tableptr)
  207. {
  208. visual_param_name_map_t::iterator name_iter;
  209. name_iter = mVisualParamNameMap.find(tableptr);
  210. if (name_iter != mVisualParamNameMap.end())
  211. {
  212. name_iter->second->setWeight(weight, upload_bake);
  213. return true;
  214. }
  215. }
  216. llwarns << "Invalid visual parameter: " << param_name << llendl;
  217. return false;
  218. }
  219. bool LLCharacter::setVisualParamWeight(S32 index, F32 weight, bool upload_bake)
  220. {
  221. visual_param_index_map_it_t index_iter = mVisualParamIndexMap.find(index);
  222. if (index_iter != mVisualParamIndexMap.end())
  223. {
  224. index_iter->second->setWeight(weight, upload_bake);
  225. return true;
  226. }
  227. llwarns << "Invalid visual parameter index: " << index << llendl;
  228. return false;
  229. }
  230. F32 LLCharacter::getVisualParamWeight(LLVisualParam* which_param)
  231. {
  232. S32 index = which_param->getID();
  233. visual_param_index_map_it_t index_iter = mVisualParamIndexMap.find(index);
  234. if (index_iter != mVisualParamIndexMap.end())
  235. {
  236. return index_iter->second->getWeight();
  237. }
  238. llwarns << "Invalid visual parameter*, index = " << index << llendl;
  239. return 0.f;
  240. }
  241. F32 LLCharacter::getVisualParamWeight(const char* param_name)
  242. {
  243. std::string tname(param_name);
  244. LLStringUtil::toLower(tname);
  245. char* tableptr = sVisualParamNames.checkString(tname);
  246. if (tableptr)
  247. {
  248. visual_param_name_map_t::iterator name_iter;
  249. name_iter = mVisualParamNameMap.find(tableptr);
  250. if (name_iter != mVisualParamNameMap.end())
  251. {
  252. return name_iter->second->getWeight();
  253. }
  254. }
  255. llwarns << "Invalid visual parameter: " << param_name << llendl;
  256. return 0.f;
  257. }
  258. F32 LLCharacter::getVisualParamWeight(S32 index)
  259. {
  260. visual_param_index_map_it_t index_iter = mVisualParamIndexMap.find(index);
  261. if (index_iter != mVisualParamIndexMap.end())
  262. {
  263. return index_iter->second->getWeight();
  264. }
  265. llwarns << "Invalid visual parameter index: " << index << llendl;
  266. return 0.f;
  267. }
  268. void LLCharacter::clearVisualParamWeights()
  269. {
  270. for (LLVisualParam* param = getFirstVisualParam(); param;
  271. param = getNextVisualParam())
  272. {
  273. if (param->isTweakable())
  274. {
  275. param->setWeight(param->getDefaultWeight(), false);
  276. }
  277. }
  278. }
  279. LLVisualParam* LLCharacter::getVisualParam(const char* param_name)
  280. {
  281. std::string tname(param_name);
  282. LLStringUtil::toLower(tname);
  283. char* tableptr = sVisualParamNames.checkString(tname);
  284. if (tableptr)
  285. {
  286. visual_param_name_map_t::iterator name_iter;
  287. name_iter = mVisualParamNameMap.find(tableptr);
  288. if (name_iter != mVisualParamNameMap.end())
  289. {
  290. return name_iter->second;
  291. }
  292. }
  293. llwarns << "Invalid visual parameter: " << param_name << llendl;
  294. return NULL;
  295. }
  296. void LLCharacter::addSharedVisualParam(LLVisualParam* param)
  297. {
  298. S32 index = param->getID();
  299. visual_param_index_map_it_t index_iter = mVisualParamIndexMap.find(index);
  300. LLVisualParam* current_param = 0;
  301. if (index_iter != mVisualParamIndexMap.end())
  302. {
  303. current_param = index_iter->second;
  304. }
  305. if (current_param)
  306. {
  307. LLVisualParam* next_param = current_param;
  308. while (next_param->getNextParam())
  309. {
  310. next_param = next_param->getNextParam();
  311. }
  312. next_param->setNextParam(param);
  313. }
  314. else
  315. {
  316. llwarns << "Shared visual parameter " << param->getName()
  317. << " does not already exist with ID " << param->getID()
  318. << llendl;
  319. }
  320. }
  321. void LLCharacter::addVisualParam(LLVisualParam* param)
  322. {
  323. S32 index = param->getID();
  324. // Add Index map
  325. std::pair<visual_param_index_map_it_t, bool> idxres =
  326. mVisualParamIndexMap.emplace(index, param);
  327. if (!idxres.second)
  328. {
  329. visual_param_index_map_it_t index_iter = idxres.first;
  330. LLVisualParam* old_param = (LLVisualParam*)index_iter->second;
  331. if (old_param != param)
  332. {
  333. if (old_param->getName() == param->getName())
  334. {
  335. llinfos << "New visual parameter '" << param->getName()
  336. << "' is replacing an existing one with the same ID and name."
  337. << llendl;
  338. }
  339. else
  340. {
  341. llwarns << "New visual parameter '" << param->getName()
  342. << "' is replacing an already existing visual parameter '"
  343. << old_param->getName() << "' with the same ID."
  344. << llendl;
  345. }
  346. index_iter->second = param;
  347. #if 0 // *HACK: deleting the old param now would cause a crash when
  348. // editing the Appearance after a "Rebuild character" action,
  349. // because the old param is still referenced in the wearables that
  350. // were loaded while the character rebuild happened... So, we
  351. // instead store the old param pointer in a vector, and delete it
  352. // only on character destruction. HB
  353. delete old_param;
  354. #else
  355. mDeferredDeletions.push_back(old_param);
  356. #endif
  357. }
  358. else
  359. {
  360. llwarns << "Visual parameter '" << param->getName()
  361. << "' already added !" << llendl;
  362. }
  363. }
  364. if (param->getInfo())
  365. {
  366. // Add name map
  367. std::string tname(param->getName());
  368. LLStringUtil::toLower(tname);
  369. char* tableptr = sVisualParamNames.addString(tname);
  370. std::pair<visual_param_name_map_t::iterator, bool> nameres =
  371. mVisualParamNameMap.emplace(tableptr, param);
  372. if (!nameres.second)
  373. {
  374. // Already exists, copy param
  375. visual_param_name_map_t::iterator name_iter = nameres.first;
  376. name_iter->second = param;
  377. }
  378. }
  379. }
  380. void LLCharacter::updateVisualParams()
  381. {
  382. for (LLVisualParam* param = getFirstVisualParam(); param;
  383. param = getNextVisualParam())
  384. {
  385. if (param->isAnimating())
  386. {
  387. continue;
  388. }
  389. // Only apply parameters whose effective weight has changed
  390. F32 effective_weight = (param->getSex() & mSex) ? param->getWeight()
  391. : param->getDefaultWeight();
  392. if (effective_weight != param->getLastWeight())
  393. {
  394. param->apply(mSex);
  395. }
  396. }
  397. }
  398. LLAnimPauseRequest LLCharacter::requestPause()
  399. {
  400. mMotionController.pauseAllMotions();
  401. return mPauseRequest;
  402. }