PageRenderTime 21ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcharacter/llcharacter.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 503 lines | 319 code | 56 blank | 128 comment | 43 complexity | e0c3317be36170fd6ba4f687dc9cbda1 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llcharacter.cpp
  3. * @brief Implementation of LLCharacter class.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. //-----------------------------------------------------------------------------
  27. // Header Files
  28. //-----------------------------------------------------------------------------
  29. #include "linden_common.h"
  30. #include "llcharacter.h"
  31. #include "llstring.h"
  32. #define SKEL_HEADER "Linden Skeleton 1.0"
  33. LLStringTable LLCharacter::sVisualParamNames(1024);
  34. std::vector< LLCharacter* > LLCharacter::sInstances;
  35. BOOL LLCharacter::sAllowInstancesChange = TRUE ;
  36. //-----------------------------------------------------------------------------
  37. // LLCharacter()
  38. // Class Constructor
  39. //-----------------------------------------------------------------------------
  40. LLCharacter::LLCharacter()
  41. :
  42. mPreferredPelvisHeight( 0.f ),
  43. mSex( SEX_FEMALE ),
  44. mAppearanceSerialNum( 0 ),
  45. mSkeletonSerialNum( 0 )
  46. {
  47. llassert_always(sAllowInstancesChange) ;
  48. sInstances.push_back(this);
  49. mMotionController.setCharacter( this );
  50. mPauseRequest = new LLPauseRequestHandle();
  51. }
  52. //-----------------------------------------------------------------------------
  53. // ~LLCharacter()
  54. // Class Destructor
  55. //-----------------------------------------------------------------------------
  56. LLCharacter::~LLCharacter()
  57. {
  58. for (LLVisualParam *param = getFirstVisualParam();
  59. param;
  60. param = getNextVisualParam())
  61. {
  62. delete param;
  63. }
  64. U32 i ;
  65. U32 size = sInstances.size() ;
  66. for(i = 0 ; i < size ; i++)
  67. {
  68. if(sInstances[i] == this)
  69. {
  70. break ;
  71. }
  72. }
  73. llassert_always(i < size) ;
  74. llassert_always(sAllowInstancesChange) ;
  75. sInstances[i] = sInstances[size - 1] ;
  76. sInstances.pop_back() ;
  77. }
  78. //-----------------------------------------------------------------------------
  79. // getJoint()
  80. //-----------------------------------------------------------------------------
  81. LLJoint *LLCharacter::getJoint( const std::string &name )
  82. {
  83. LLJoint* joint = NULL;
  84. LLJoint *root = getRootJoint();
  85. if (root)
  86. {
  87. joint = root->findJoint(name);
  88. }
  89. if (!joint)
  90. {
  91. llwarns << "Failed to find joint." << llendl;
  92. }
  93. return joint;
  94. }
  95. //-----------------------------------------------------------------------------
  96. // registerMotion()
  97. //-----------------------------------------------------------------------------
  98. BOOL LLCharacter::registerMotion( const LLUUID& id, LLMotionConstructor create )
  99. {
  100. return mMotionController.registerMotion(id, create);
  101. }
  102. //-----------------------------------------------------------------------------
  103. // removeMotion()
  104. //-----------------------------------------------------------------------------
  105. void LLCharacter::removeMotion( const LLUUID& id )
  106. {
  107. mMotionController.removeMotion(id);
  108. }
  109. //-----------------------------------------------------------------------------
  110. // findMotion()
  111. //-----------------------------------------------------------------------------
  112. LLMotion* LLCharacter::findMotion( const LLUUID &id )
  113. {
  114. return mMotionController.findMotion( id );
  115. }
  116. //-----------------------------------------------------------------------------
  117. // createMotion()
  118. // NOTE: Always assign the result to a LLPointer!
  119. //-----------------------------------------------------------------------------
  120. LLMotion* LLCharacter::createMotion( const LLUUID &id )
  121. {
  122. return mMotionController.createMotion( id );
  123. }
  124. //-----------------------------------------------------------------------------
  125. // startMotion()
  126. //-----------------------------------------------------------------------------
  127. BOOL LLCharacter::startMotion(const LLUUID &id, F32 start_offset)
  128. {
  129. return mMotionController.startMotion(id, start_offset);
  130. }
  131. //-----------------------------------------------------------------------------
  132. // stopMotion()
  133. //-----------------------------------------------------------------------------
  134. BOOL LLCharacter::stopMotion(const LLUUID& id, BOOL stop_immediate)
  135. {
  136. return mMotionController.stopMotionLocally(id, stop_immediate);
  137. }
  138. //-----------------------------------------------------------------------------
  139. // isMotionActive()
  140. //-----------------------------------------------------------------------------
  141. BOOL LLCharacter::isMotionActive(const LLUUID& id)
  142. {
  143. LLMotion *motionp = mMotionController.findMotion(id);
  144. if (motionp)
  145. {
  146. return mMotionController.isMotionActive(motionp);
  147. }
  148. return FALSE;
  149. }
  150. //-----------------------------------------------------------------------------
  151. // onDeactivateMotion()
  152. //-----------------------------------------------------------------------------
  153. void LLCharacter::requestStopMotion( LLMotion* motion)
  154. {
  155. // llinfos << "DEBUG: Char::onDeactivateMotion( " << motionName << " )" << llendl;
  156. }
  157. //-----------------------------------------------------------------------------
  158. // updateMotions()
  159. //-----------------------------------------------------------------------------
  160. static LLFastTimer::DeclareTimer FTM_UPDATE_ANIMATION("Update Animation");
  161. static LLFastTimer::DeclareTimer FTM_UPDATE_HIDDEN_ANIMATION("Update Hidden Anim");
  162. void LLCharacter::updateMotions(e_update_t update_type)
  163. {
  164. if (update_type == HIDDEN_UPDATE)
  165. {
  166. LLFastTimer t(FTM_UPDATE_HIDDEN_ANIMATION);
  167. mMotionController.updateMotionsMinimal();
  168. }
  169. else
  170. {
  171. LLFastTimer t(FTM_UPDATE_ANIMATION);
  172. // unpause if the number of outstanding pause requests has dropped to the initial one
  173. if (mMotionController.isPaused() && mPauseRequest->getNumRefs() == 1)
  174. {
  175. mMotionController.unpauseAllMotions();
  176. }
  177. bool force_update = (update_type == FORCE_UPDATE);
  178. mMotionController.updateMotions(force_update);
  179. }
  180. }
  181. //-----------------------------------------------------------------------------
  182. // deactivateAllMotions()
  183. //-----------------------------------------------------------------------------
  184. void LLCharacter::deactivateAllMotions()
  185. {
  186. mMotionController.deactivateAllMotions();
  187. }
  188. //-----------------------------------------------------------------------------
  189. // flushAllMotions()
  190. //-----------------------------------------------------------------------------
  191. void LLCharacter::flushAllMotions()
  192. {
  193. mMotionController.flushAllMotions();
  194. }
  195. //-----------------------------------------------------------------------------
  196. // dumpCharacter()
  197. //-----------------------------------------------------------------------------
  198. void LLCharacter::dumpCharacter( LLJoint* joint )
  199. {
  200. // handle top level entry into recursion
  201. if (joint == NULL)
  202. {
  203. llinfos << "DEBUG: Dumping Character @" << this << llendl;
  204. dumpCharacter( getRootJoint() );
  205. llinfos << "DEBUG: Done." << llendl;
  206. return;
  207. }
  208. // print joint info
  209. llinfos << "DEBUG: " << joint->getName() << " (" << (joint->getParent()?joint->getParent()->getName():std::string("ROOT")) << ")" << llendl;
  210. // recurse
  211. for (LLJoint::child_list_t::iterator iter = joint->mChildren.begin();
  212. iter != joint->mChildren.end(); ++iter)
  213. {
  214. LLJoint* child_joint = *iter;
  215. dumpCharacter(child_joint);
  216. }
  217. }
  218. //-----------------------------------------------------------------------------
  219. // setAnimationData()
  220. //-----------------------------------------------------------------------------
  221. void LLCharacter::setAnimationData(std::string name, void *data)
  222. {
  223. mAnimationData[name] = data;
  224. }
  225. //-----------------------------------------------------------------------------
  226. // getAnimationData()
  227. //-----------------------------------------------------------------------------
  228. void* LLCharacter::getAnimationData(std::string name)
  229. {
  230. return get_if_there(mAnimationData, name, (void*)NULL);
  231. }
  232. //-----------------------------------------------------------------------------
  233. // removeAnimationData()
  234. //-----------------------------------------------------------------------------
  235. void LLCharacter::removeAnimationData(std::string name)
  236. {
  237. mAnimationData.erase(name);
  238. }
  239. //-----------------------------------------------------------------------------
  240. // setVisualParamWeight()
  241. //-----------------------------------------------------------------------------
  242. BOOL LLCharacter::setVisualParamWeight(LLVisualParam* which_param, F32 weight, BOOL upload_bake)
  243. {
  244. S32 index = which_param->getID();
  245. visual_param_index_map_t::iterator index_iter = mVisualParamIndexMap.find(index);
  246. if (index_iter != mVisualParamIndexMap.end())
  247. {
  248. index_iter->second->setWeight(weight, upload_bake);
  249. return TRUE;
  250. }
  251. return FALSE;
  252. }
  253. //-----------------------------------------------------------------------------
  254. // setVisualParamWeight()
  255. //-----------------------------------------------------------------------------
  256. BOOL LLCharacter::setVisualParamWeight(const char* param_name, F32 weight, BOOL upload_bake)
  257. {
  258. std::string tname(param_name);
  259. LLStringUtil::toLower(tname);
  260. char *tableptr = sVisualParamNames.checkString(tname);
  261. visual_param_name_map_t::iterator name_iter = mVisualParamNameMap.find(tableptr);
  262. if (name_iter != mVisualParamNameMap.end())
  263. {
  264. name_iter->second->setWeight(weight, upload_bake);
  265. return TRUE;
  266. }
  267. llwarns << "LLCharacter::setVisualParamWeight() Invalid visual parameter: " << param_name << llendl;
  268. return FALSE;
  269. }
  270. //-----------------------------------------------------------------------------
  271. // setVisualParamWeight()
  272. //-----------------------------------------------------------------------------
  273. BOOL LLCharacter::setVisualParamWeight(S32 index, F32 weight, BOOL upload_bake)
  274. {
  275. visual_param_index_map_t::iterator index_iter = mVisualParamIndexMap.find(index);
  276. if (index_iter != mVisualParamIndexMap.end())
  277. {
  278. index_iter->second->setWeight(weight, upload_bake);
  279. return TRUE;
  280. }
  281. llwarns << "LLCharacter::setVisualParamWeight() Invalid visual parameter index: " << index << llendl;
  282. return FALSE;
  283. }
  284. //-----------------------------------------------------------------------------
  285. // getVisualParamWeight()
  286. //-----------------------------------------------------------------------------
  287. F32 LLCharacter::getVisualParamWeight(LLVisualParam *which_param)
  288. {
  289. S32 index = which_param->getID();
  290. visual_param_index_map_t::iterator index_iter = mVisualParamIndexMap.find(index);
  291. if (index_iter != mVisualParamIndexMap.end())
  292. {
  293. return index_iter->second->getWeight();
  294. }
  295. else
  296. {
  297. llwarns << "LLCharacter::getVisualParamWeight() Invalid visual parameter*, index= " << index << llendl;
  298. return 0.f;
  299. }
  300. }
  301. //-----------------------------------------------------------------------------
  302. // getVisualParamWeight()
  303. //-----------------------------------------------------------------------------
  304. F32 LLCharacter::getVisualParamWeight(const char* param_name)
  305. {
  306. std::string tname(param_name);
  307. LLStringUtil::toLower(tname);
  308. char *tableptr = sVisualParamNames.checkString(tname);
  309. visual_param_name_map_t::iterator name_iter = mVisualParamNameMap.find(tableptr);
  310. if (name_iter != mVisualParamNameMap.end())
  311. {
  312. return name_iter->second->getWeight();
  313. }
  314. llwarns << "LLCharacter::getVisualParamWeight() Invalid visual parameter: " << param_name << llendl;
  315. return 0.f;
  316. }
  317. //-----------------------------------------------------------------------------
  318. // getVisualParamWeight()
  319. //-----------------------------------------------------------------------------
  320. F32 LLCharacter::getVisualParamWeight(S32 index)
  321. {
  322. visual_param_index_map_t::iterator index_iter = mVisualParamIndexMap.find(index);
  323. if (index_iter != mVisualParamIndexMap.end())
  324. {
  325. return index_iter->second->getWeight();
  326. }
  327. else
  328. {
  329. llwarns << "LLCharacter::getVisualParamWeight() Invalid visual parameter index: " << index << llendl;
  330. return 0.f;
  331. }
  332. }
  333. //-----------------------------------------------------------------------------
  334. // clearVisualParamWeights()
  335. //-----------------------------------------------------------------------------
  336. void LLCharacter::clearVisualParamWeights()
  337. {
  338. for (LLVisualParam *param = getFirstVisualParam();
  339. param;
  340. param = getNextVisualParam())
  341. {
  342. if (param->isTweakable())
  343. {
  344. param->setWeight( param->getDefaultWeight(), FALSE );
  345. }
  346. }
  347. }
  348. //-----------------------------------------------------------------------------
  349. // getVisualParam()
  350. //-----------------------------------------------------------------------------
  351. LLVisualParam* LLCharacter::getVisualParam(const char *param_name)
  352. {
  353. std::string tname(param_name);
  354. LLStringUtil::toLower(tname);
  355. char *tableptr = sVisualParamNames.checkString(tname);
  356. visual_param_name_map_t::iterator name_iter = mVisualParamNameMap.find(tableptr);
  357. if (name_iter != mVisualParamNameMap.end())
  358. {
  359. return name_iter->second;
  360. }
  361. llwarns << "LLCharacter::getVisualParam() Invalid visual parameter: " << param_name << llendl;
  362. return NULL;
  363. }
  364. //-----------------------------------------------------------------------------
  365. // addSharedVisualParam()
  366. //-----------------------------------------------------------------------------
  367. void LLCharacter::addSharedVisualParam(LLVisualParam *param)
  368. {
  369. S32 index = param->getID();
  370. visual_param_index_map_t::iterator index_iter = mVisualParamIndexMap.find(index);
  371. LLVisualParam* current_param = 0;
  372. if (index_iter != mVisualParamIndexMap.end())
  373. current_param = index_iter->second;
  374. if( current_param )
  375. {
  376. LLVisualParam* next_param = current_param;
  377. while(next_param->getNextParam())
  378. {
  379. next_param = next_param->getNextParam();
  380. }
  381. next_param->setNextParam(param);
  382. }
  383. else
  384. {
  385. llwarns << "Shared visual parameter " << param->getName() << " does not already exist with ID " <<
  386. param->getID() << llendl;
  387. }
  388. }
  389. //-----------------------------------------------------------------------------
  390. // addVisualParam()
  391. //-----------------------------------------------------------------------------
  392. void LLCharacter::addVisualParam(LLVisualParam *param)
  393. {
  394. S32 index = param->getID();
  395. // Add Index map
  396. std::pair<visual_param_index_map_t::iterator, bool> idxres;
  397. idxres = mVisualParamIndexMap.insert(visual_param_index_map_t::value_type(index, param));
  398. if (!idxres.second)
  399. {
  400. llwarns << "Visual parameter " << param->getName() << " already exists with same ID as " <<
  401. param->getName() << llendl;
  402. visual_param_index_map_t::iterator index_iter = idxres.first;
  403. index_iter->second = param;
  404. }
  405. if (param->getInfo())
  406. {
  407. // Add name map
  408. std::string tname(param->getName());
  409. LLStringUtil::toLower(tname);
  410. char *tableptr = sVisualParamNames.addString(tname);
  411. std::pair<visual_param_name_map_t::iterator, bool> nameres;
  412. nameres = mVisualParamNameMap.insert(visual_param_name_map_t::value_type(tableptr, param));
  413. if (!nameres.second)
  414. {
  415. // Already exists, copy param
  416. visual_param_name_map_t::iterator name_iter = nameres.first;
  417. name_iter->second = param;
  418. }
  419. }
  420. //llinfos << "Adding Visual Param '" << param->getName() << "' ( " << index << " )" << llendl;
  421. }
  422. //-----------------------------------------------------------------------------
  423. // updateVisualParams()
  424. //-----------------------------------------------------------------------------
  425. void LLCharacter::updateVisualParams()
  426. {
  427. for (LLVisualParam *param = getFirstVisualParam();
  428. param;
  429. param = getNextVisualParam())
  430. {
  431. if (param->isAnimating())
  432. {
  433. continue;
  434. }
  435. // only apply parameters whose effective weight has changed
  436. F32 effective_weight = ( param->getSex() & mSex ) ? param->getWeight() : param->getDefaultWeight();
  437. if (effective_weight != param->getLastWeight())
  438. {
  439. param->apply( mSex );
  440. }
  441. }
  442. }
  443. LLAnimPauseRequest LLCharacter::requestPause()
  444. {
  445. mMotionController.pauseAllMotions();
  446. return mPauseRequest;
  447. }