llcharacter.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:17k
源码类别:

游戏引擎

开发平台:

C++ Builder

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