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

/indra/newview/llvoavatar.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 2117 lines | 1501 code | 311 blank | 305 comment | 187 complexity | b8371880310954f4104c8e87c8f309e3 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @File llvoavatar.cpp
  3. * @brief Implementation of LLVOAvatar class which is a derivation of LLViewerObject
  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. #if LL_MSVC
  27. // disable warning about boost::lexical_cast returning uninitialized data
  28. // when it fails to parse the string
  29. #pragma warning (disable:4701)
  30. #endif
  31. #include "llviewerprecompiledheaders.h"
  32. #include "llvoavatar.h"
  33. #include <stdio.h>
  34. #include <ctype.h>
  35. #include "llaudioengine.h"
  36. #include "noise.h"
  37. #include "sound_ids.h"
  38. #include "raytrace.h"
  39. #include "llagent.h" // Get state values from here
  40. #include "llagentcamera.h"
  41. #include "llagentwearables.h"
  42. #include "llanimationstates.h"
  43. #include "llavatarnamecache.h"
  44. #include "llavatarpropertiesprocessor.h"
  45. #include "llphysicsmotion.h"
  46. #include "llviewercontrol.h"
  47. #include "llcallingcard.h" // IDEVO for LLAvatarTracker
  48. #include "lldrawpoolavatar.h"
  49. #include "lldriverparam.h"
  50. #include "lleditingmotion.h"
  51. #include "llemote.h"
  52. //#include "llfirstuse.h"
  53. #include "llfloatertools.h"
  54. #include "llheadrotmotion.h"
  55. #include "llhudeffecttrail.h"
  56. #include "llhudmanager.h"
  57. #include "llhudnametag.h"
  58. #include "llhudtext.h" // for mText/mDebugText
  59. #include "llkeyframefallmotion.h"
  60. #include "llkeyframestandmotion.h"
  61. #include "llkeyframewalkmotion.h"
  62. #include "llmanipscale.h" // for get_default_max_prim_scale()
  63. #include "llmeshrepository.h"
  64. #include "llmutelist.h"
  65. #include "llmoveview.h"
  66. #include "llnotificationsutil.h"
  67. #include "llquantize.h"
  68. #include "llrand.h"
  69. #include "llregionhandle.h"
  70. #include "llresmgr.h"
  71. #include "llselectmgr.h"
  72. #include "llsprite.h"
  73. #include "lltargetingmotion.h"
  74. #include "lltexlayer.h"
  75. #include "lltoolmorph.h"
  76. #include "llviewercamera.h"
  77. #include "llviewertexturelist.h"
  78. #include "llviewermenu.h"
  79. #include "llviewerobjectlist.h"
  80. #include "llviewerparcelmgr.h"
  81. #include "llviewershadermgr.h"
  82. #include "llviewerstats.h"
  83. #include "llvoavatarself.h"
  84. #include "llvovolume.h"
  85. #include "llworld.h"
  86. #include "pipeline.h"
  87. #include "llviewershadermgr.h"
  88. #include "llsky.h"
  89. #include "llanimstatelabels.h"
  90. #include "lltrans.h"
  91. #include "llappearancemgr.h"
  92. #include "llgesturemgr.h" //needed to trigger the voice gesticulations
  93. #include "llvoiceclient.h"
  94. #include "llvoicevisualizer.h" // Ventrella
  95. #include "lldebugmessagebox.h"
  96. extern F32 SPEED_ADJUST_MAX;
  97. extern F32 SPEED_ADJUST_MAX_SEC;
  98. extern F32 ANIM_SPEED_MAX;
  99. extern F32 ANIM_SPEED_MIN;
  100. #if LL_MSVC
  101. // disable boost::lexical_cast warning
  102. #pragma warning (disable:4702)
  103. #endif
  104. #include <boost/lexical_cast.hpp>
  105. // #define OUTPUT_BREAST_DATA
  106. using namespace LLVOAvatarDefines;
  107. //-----------------------------------------------------------------------------
  108. // Global constants
  109. //-----------------------------------------------------------------------------
  110. const LLUUID ANIM_AGENT_BODY_NOISE = LLUUID("9aa8b0a6-0c6f-9518-c7c3-4f41f2c001ad"); //"body_noise"
  111. const LLUUID ANIM_AGENT_BREATHE_ROT = LLUUID("4c5a103e-b830-2f1c-16bc-224aa0ad5bc8"); //"breathe_rot"
  112. const LLUUID ANIM_AGENT_EDITING = LLUUID("2a8eba1d-a7f8-5596-d44a-b4977bf8c8bb"); //"editing"
  113. const LLUUID ANIM_AGENT_EYE = LLUUID("5c780ea8-1cd1-c463-a128-48c023f6fbea"); //"eye"
  114. const LLUUID ANIM_AGENT_FLY_ADJUST = LLUUID("db95561f-f1b0-9f9a-7224-b12f71af126e"); //"fly_adjust"
  115. const LLUUID ANIM_AGENT_HAND_MOTION = LLUUID("ce986325-0ba7-6e6e-cc24-b17c4b795578"); //"hand_motion"
  116. const LLUUID ANIM_AGENT_HEAD_ROT = LLUUID("e6e8d1dd-e643-fff7-b238-c6b4b056a68d"); //"head_rot"
  117. const LLUUID ANIM_AGENT_PELVIS_FIX = LLUUID("0c5dd2a2-514d-8893-d44d-05beffad208b"); //"pelvis_fix"
  118. const LLUUID ANIM_AGENT_TARGET = LLUUID("0e4896cb-fba4-926c-f355-8720189d5b55"); //"target"
  119. const LLUUID ANIM_AGENT_WALK_ADJUST = LLUUID("829bc85b-02fc-ec41-be2e-74cc6dd7215d"); //"walk_adjust"
  120. const LLUUID ANIM_AGENT_PHYSICS_MOTION = LLUUID("7360e029-3cb8-ebc4-863e-212df440d987"); //"physics_motion"
  121. //-----------------------------------------------------------------------------
  122. // Constants
  123. //-----------------------------------------------------------------------------
  124. const std::string AVATAR_DEFAULT_CHAR = "avatar";
  125. const S32 MIN_PIXEL_AREA_FOR_COMPOSITE = 1024;
  126. const F32 SHADOW_OFFSET_AMT = 0.03f;
  127. const F32 DELTA_TIME_MIN = 0.01f; // we clamp measured deltaTime to this
  128. const F32 DELTA_TIME_MAX = 0.2f; // range to insure stability of computations.
  129. const F32 PELVIS_LAG_FLYING = 0.22f;// pelvis follow half life while flying
  130. const F32 PELVIS_LAG_WALKING = 0.4f; // ...while walking
  131. const F32 PELVIS_LAG_MOUSELOOK = 0.15f;
  132. const F32 MOUSELOOK_PELVIS_FOLLOW_FACTOR = 0.5f;
  133. const F32 PELVIS_LAG_WHEN_FOLLOW_CAM_IS_ON = 0.0001f; // not zero! - something gets divided by this!
  134. const F32 PELVIS_ROT_THRESHOLD_SLOW = 60.0f; // amount of deviation allowed between
  135. const F32 PELVIS_ROT_THRESHOLD_FAST = 2.0f; // the pelvis and the view direction
  136. // when moving fast & slow
  137. const F32 TORSO_NOISE_AMOUNT = 1.0f; // Amount of deviation from up-axis, in degrees
  138. const F32 TORSO_NOISE_SPEED = 0.2f; // Time scale factor on torso noise.
  139. const F32 BREATHE_ROT_MOTION_STRENGTH = 0.05f;
  140. const F32 BREATHE_SCALE_MOTION_STRENGTH = 0.005f;
  141. const F32 MIN_SHADOW_HEIGHT = 0.f;
  142. const F32 MAX_SHADOW_HEIGHT = 0.3f;
  143. const S32 MIN_REQUIRED_PIXEL_AREA_BODY_NOISE = 10000;
  144. const S32 MIN_REQUIRED_PIXEL_AREA_BREATHE = 10000;
  145. const S32 MIN_REQUIRED_PIXEL_AREA_PELVIS_FIX = 40;
  146. const S32 TEX_IMAGE_SIZE_SELF = 512;
  147. const S32 TEX_IMAGE_AREA_SELF = TEX_IMAGE_SIZE_SELF * TEX_IMAGE_SIZE_SELF;
  148. const S32 TEX_IMAGE_SIZE_OTHER = 512 / 4; // The size of local textures for other (!isSelf()) avatars
  149. const F32 HEAD_MOVEMENT_AVG_TIME = 0.9f;
  150. const S32 MORPH_MASK_REQUESTED_DISCARD = 0;
  151. // Discard level at which to switch to baked textures
  152. // Should probably be 4 or 3, but didn't want to change it while change other logic - SJB
  153. const S32 SWITCH_TO_BAKED_DISCARD = 5;
  154. const F32 FOOT_COLLIDE_FUDGE = 0.04f;
  155. const F32 HOVER_EFFECT_MAX_SPEED = 3.f;
  156. const F32 HOVER_EFFECT_STRENGTH = 0.f;
  157. const F32 UNDERWATER_EFFECT_STRENGTH = 0.1f;
  158. const F32 UNDERWATER_FREQUENCY_DAMP = 0.33f;
  159. const F32 APPEARANCE_MORPH_TIME = 0.65f;
  160. const F32 TIME_BEFORE_MESH_CLEANUP = 5.f; // seconds
  161. const S32 AVATAR_RELEASE_THRESHOLD = 10; // number of avatar instances before releasing memory
  162. const F32 FOOT_GROUND_COLLISION_TOLERANCE = 0.25f;
  163. const F32 AVATAR_LOD_TWEAK_RANGE = 0.7f;
  164. const S32 MAX_BUBBLE_CHAT_LENGTH = DB_CHAT_MSG_STR_LEN;
  165. const S32 MAX_BUBBLE_CHAT_UTTERANCES = 12;
  166. const F32 CHAT_FADE_TIME = 8.0;
  167. const F32 BUBBLE_CHAT_TIME = CHAT_FADE_TIME * 3.f;
  168. const LLColor4 DUMMY_COLOR = LLColor4(0.5,0.5,0.5,1.0);
  169. enum ERenderName
  170. {
  171. RENDER_NAME_NEVER,
  172. RENDER_NAME_ALWAYS,
  173. RENDER_NAME_FADE
  174. };
  175. //-----------------------------------------------------------------------------
  176. // Callback data
  177. //-----------------------------------------------------------------------------
  178. struct LLTextureMaskData
  179. {
  180. LLTextureMaskData( const LLUUID& id ) :
  181. mAvatarID(id),
  182. mLastDiscardLevel(S32_MAX)
  183. {}
  184. LLUUID mAvatarID;
  185. S32 mLastDiscardLevel;
  186. };
  187. /*********************************************************************************
  188. ** **
  189. ** Begin private LLVOAvatar Support classes
  190. **
  191. **/
  192. //------------------------------------------------------------------------
  193. // LLVOBoneInfo
  194. // Trans/Scale/Rot etc. info about each avatar bone. Used by LLVOAvatarSkeleton.
  195. //------------------------------------------------------------------------
  196. class LLVOAvatarBoneInfo
  197. {
  198. friend class LLVOAvatar;
  199. friend class LLVOAvatarSkeletonInfo;
  200. public:
  201. LLVOAvatarBoneInfo() : mIsJoint(FALSE) {}
  202. ~LLVOAvatarBoneInfo()
  203. {
  204. std::for_each(mChildList.begin(), mChildList.end(), DeletePointer());
  205. }
  206. BOOL parseXml(LLXmlTreeNode* node);
  207. private:
  208. std::string mName;
  209. BOOL mIsJoint;
  210. LLVector3 mPos;
  211. LLVector3 mRot;
  212. LLVector3 mScale;
  213. LLVector3 mPivot;
  214. typedef std::vector<LLVOAvatarBoneInfo*> child_list_t;
  215. child_list_t mChildList;
  216. };
  217. //------------------------------------------------------------------------
  218. // LLVOAvatarSkeletonInfo
  219. // Overall avatar skeleton
  220. //------------------------------------------------------------------------
  221. class LLVOAvatarSkeletonInfo
  222. {
  223. friend class LLVOAvatar;
  224. public:
  225. LLVOAvatarSkeletonInfo() :
  226. mNumBones(0), mNumCollisionVolumes(0) {}
  227. ~LLVOAvatarSkeletonInfo()
  228. {
  229. std::for_each(mBoneInfoList.begin(), mBoneInfoList.end(), DeletePointer());
  230. }
  231. BOOL parseXml(LLXmlTreeNode* node);
  232. S32 getNumBones() const { return mNumBones; }
  233. S32 getNumCollisionVolumes() const { return mNumCollisionVolumes; }
  234. private:
  235. S32 mNumBones;
  236. S32 mNumCollisionVolumes;
  237. typedef std::vector<LLVOAvatarBoneInfo*> bone_info_list_t;
  238. bone_info_list_t mBoneInfoList;
  239. };
  240. //-----------------------------------------------------------------------------
  241. // class LLBodyNoiseMotion
  242. //-----------------------------------------------------------------------------
  243. class LLBodyNoiseMotion :
  244. public LLMotion
  245. {
  246. public:
  247. // Constructor
  248. LLBodyNoiseMotion(const LLUUID &id)
  249. : LLMotion(id)
  250. {
  251. mName = "body_noise";
  252. mTorsoState = new LLJointState;
  253. }
  254. // Destructor
  255. virtual ~LLBodyNoiseMotion() { }
  256. public:
  257. //-------------------------------------------------------------------------
  258. // functions to support MotionController and MotionRegistry
  259. //-------------------------------------------------------------------------
  260. // static constructor
  261. // all subclasses must implement such a function and register it
  262. static LLMotion *create(const LLUUID &id) { return new LLBodyNoiseMotion(id); }
  263. public:
  264. //-------------------------------------------------------------------------
  265. // animation callbacks to be implemented by subclasses
  266. //-------------------------------------------------------------------------
  267. // motions must specify whether or not they loop
  268. virtual BOOL getLoop() { return TRUE; }
  269. // motions must report their total duration
  270. virtual F32 getDuration() { return 0.0; }
  271. // motions must report their "ease in" duration
  272. virtual F32 getEaseInDuration() { return 0.0; }
  273. // motions must report their "ease out" duration.
  274. virtual F32 getEaseOutDuration() { return 0.0; }
  275. // motions must report their priority
  276. virtual LLJoint::JointPriority getPriority() { return LLJoint::HIGH_PRIORITY; }
  277. virtual LLMotionBlendType getBlendType() { return ADDITIVE_BLEND; }
  278. // called to determine when a motion should be activated/deactivated based on avatar pixel coverage
  279. virtual F32 getMinPixelArea() { return MIN_REQUIRED_PIXEL_AREA_BODY_NOISE; }
  280. // run-time (post constructor) initialization,
  281. // called after parameters have been set
  282. // must return true to indicate success and be available for activation
  283. virtual LLMotionInitStatus onInitialize(LLCharacter *character)
  284. {
  285. if( !mTorsoState->setJoint( character->getJoint("mTorso") ))
  286. {
  287. return STATUS_FAILURE;
  288. }
  289. mTorsoState->setUsage(LLJointState::ROT);
  290. addJointState( mTorsoState );
  291. return STATUS_SUCCESS;
  292. }
  293. // called when a motion is activated
  294. // must return TRUE to indicate success, or else
  295. // it will be deactivated
  296. virtual BOOL onActivate() { return TRUE; }
  297. // called per time step
  298. // must return TRUE while it is active, and
  299. // must return FALSE when the motion is completed.
  300. virtual BOOL onUpdate(F32 time, U8* joint_mask)
  301. {
  302. F32 nx[2];
  303. nx[0]=time*TORSO_NOISE_SPEED;
  304. nx[1]=0.0f;
  305. F32 ny[2];
  306. ny[0]=0.0f;
  307. ny[1]=time*TORSO_NOISE_SPEED;
  308. F32 noiseX = noise2(nx);
  309. F32 noiseY = noise2(ny);
  310. F32 rx = TORSO_NOISE_AMOUNT * DEG_TO_RAD * noiseX / 0.42f;
  311. F32 ry = TORSO_NOISE_AMOUNT * DEG_TO_RAD * noiseY / 0.42f;
  312. LLQuaternion tQn;
  313. tQn.setQuat( rx, ry, 0.0f );
  314. mTorsoState->setRotation( tQn );
  315. return TRUE;
  316. }
  317. // called when a motion is deactivated
  318. virtual void onDeactivate() {}
  319. private:
  320. //-------------------------------------------------------------------------
  321. // joint states to be animated
  322. //-------------------------------------------------------------------------
  323. LLPointer<LLJointState> mTorsoState;
  324. };
  325. //-----------------------------------------------------------------------------
  326. // class LLBreatheMotionRot
  327. //-----------------------------------------------------------------------------
  328. class LLBreatheMotionRot :
  329. public LLMotion
  330. {
  331. public:
  332. // Constructor
  333. LLBreatheMotionRot(const LLUUID &id) :
  334. LLMotion(id),
  335. mBreatheRate(1.f),
  336. mCharacter(NULL)
  337. {
  338. mName = "breathe_rot";
  339. mChestState = new LLJointState;
  340. }
  341. // Destructor
  342. virtual ~LLBreatheMotionRot() {}
  343. public:
  344. //-------------------------------------------------------------------------
  345. // functions to support MotionController and MotionRegistry
  346. //-------------------------------------------------------------------------
  347. // static constructor
  348. // all subclasses must implement such a function and register it
  349. static LLMotion *create(const LLUUID &id) { return new LLBreatheMotionRot(id); }
  350. public:
  351. //-------------------------------------------------------------------------
  352. // animation callbacks to be implemented by subclasses
  353. //-------------------------------------------------------------------------
  354. // motions must specify whether or not they loop
  355. virtual BOOL getLoop() { return TRUE; }
  356. // motions must report their total duration
  357. virtual F32 getDuration() { return 0.0; }
  358. // motions must report their "ease in" duration
  359. virtual F32 getEaseInDuration() { return 0.0; }
  360. // motions must report their "ease out" duration.
  361. virtual F32 getEaseOutDuration() { return 0.0; }
  362. // motions must report their priority
  363. virtual LLJoint::JointPriority getPriority() { return LLJoint::MEDIUM_PRIORITY; }
  364. virtual LLMotionBlendType getBlendType() { return NORMAL_BLEND; }
  365. // called to determine when a motion should be activated/deactivated based on avatar pixel coverage
  366. virtual F32 getMinPixelArea() { return MIN_REQUIRED_PIXEL_AREA_BREATHE; }
  367. // run-time (post constructor) initialization,
  368. // called after parameters have been set
  369. // must return true to indicate success and be available for activation
  370. virtual LLMotionInitStatus onInitialize(LLCharacter *character)
  371. {
  372. mCharacter = character;
  373. BOOL success = true;
  374. if ( !mChestState->setJoint( character->getJoint( "mChest" ) ) ) { success = false; }
  375. if ( success )
  376. {
  377. mChestState->setUsage(LLJointState::ROT);
  378. addJointState( mChestState );
  379. }
  380. if ( success )
  381. {
  382. return STATUS_SUCCESS;
  383. }
  384. else
  385. {
  386. return STATUS_FAILURE;
  387. }
  388. }
  389. // called when a motion is activated
  390. // must return TRUE to indicate success, or else
  391. // it will be deactivated
  392. virtual BOOL onActivate() { return TRUE; }
  393. // called per time step
  394. // must return TRUE while it is active, and
  395. // must return FALSE when the motion is completed.
  396. virtual BOOL onUpdate(F32 time, U8* joint_mask)
  397. {
  398. mBreatheRate = 1.f;
  399. F32 breathe_amt = (sinf(mBreatheRate * time) * BREATHE_ROT_MOTION_STRENGTH);
  400. mChestState->setRotation(LLQuaternion(breathe_amt, LLVector3(0.f, 1.f, 0.f)));
  401. return TRUE;
  402. }
  403. // called when a motion is deactivated
  404. virtual void onDeactivate() {}
  405. private:
  406. //-------------------------------------------------------------------------
  407. // joint states to be animated
  408. //-------------------------------------------------------------------------
  409. LLPointer<LLJointState> mChestState;
  410. F32 mBreatheRate;
  411. LLCharacter* mCharacter;
  412. };
  413. //-----------------------------------------------------------------------------
  414. // class LLPelvisFixMotion
  415. //-----------------------------------------------------------------------------
  416. class LLPelvisFixMotion :
  417. public LLMotion
  418. {
  419. public:
  420. // Constructor
  421. LLPelvisFixMotion(const LLUUID &id)
  422. : LLMotion(id), mCharacter(NULL)
  423. {
  424. mName = "pelvis_fix";
  425. mPelvisState = new LLJointState;
  426. }
  427. // Destructor
  428. virtual ~LLPelvisFixMotion() { }
  429. public:
  430. //-------------------------------------------------------------------------
  431. // functions to support MotionController and MotionRegistry
  432. //-------------------------------------------------------------------------
  433. // static constructor
  434. // all subclasses must implement such a function and register it
  435. static LLMotion *create(const LLUUID& id) { return new LLPelvisFixMotion(id); }
  436. public:
  437. //-------------------------------------------------------------------------
  438. // animation callbacks to be implemented by subclasses
  439. //-------------------------------------------------------------------------
  440. // motions must specify whether or not they loop
  441. virtual BOOL getLoop() { return TRUE; }
  442. // motions must report their total duration
  443. virtual F32 getDuration() { return 0.0; }
  444. // motions must report their "ease in" duration
  445. virtual F32 getEaseInDuration() { return 0.5f; }
  446. // motions must report their "ease out" duration.
  447. virtual F32 getEaseOutDuration() { return 0.5f; }
  448. // motions must report their priority
  449. virtual LLJoint::JointPriority getPriority() { return LLJoint::LOW_PRIORITY; }
  450. virtual LLMotionBlendType getBlendType() { return NORMAL_BLEND; }
  451. // called to determine when a motion should be activated/deactivated based on avatar pixel coverage
  452. virtual F32 getMinPixelArea() { return MIN_REQUIRED_PIXEL_AREA_PELVIS_FIX; }
  453. // run-time (post constructor) initialization,
  454. // called after parameters have been set
  455. // must return true to indicate success and be available for activation
  456. virtual LLMotionInitStatus onInitialize(LLCharacter *character)
  457. {
  458. mCharacter = character;
  459. if (!mPelvisState->setJoint( character->getJoint("mPelvis")))
  460. {
  461. return STATUS_FAILURE;
  462. }
  463. mPelvisState->setUsage(LLJointState::POS);
  464. addJointState( mPelvisState );
  465. return STATUS_SUCCESS;
  466. }
  467. // called when a motion is activated
  468. // must return TRUE to indicate success, or else
  469. // it will be deactivated
  470. virtual BOOL onActivate() { return TRUE; }
  471. // called per time step
  472. // must return TRUE while it is active, and
  473. // must return FALSE when the motion is completed.
  474. virtual BOOL onUpdate(F32 time, U8* joint_mask)
  475. {
  476. mPelvisState->setPosition(LLVector3::zero);
  477. return TRUE;
  478. }
  479. // called when a motion is deactivated
  480. virtual void onDeactivate() {}
  481. private:
  482. //-------------------------------------------------------------------------
  483. // joint states to be animated
  484. //-------------------------------------------------------------------------
  485. LLPointer<LLJointState> mPelvisState;
  486. LLCharacter* mCharacter;
  487. };
  488. /**
  489. **
  490. ** End LLVOAvatar Support classes
  491. ** **
  492. *********************************************************************************/
  493. //-----------------------------------------------------------------------------
  494. // Static Data
  495. //-----------------------------------------------------------------------------
  496. LLXmlTree LLVOAvatar::sXMLTree;
  497. LLXmlTree LLVOAvatar::sSkeletonXMLTree;
  498. LLVOAvatarSkeletonInfo* LLVOAvatar::sAvatarSkeletonInfo = NULL;
  499. LLVOAvatar::LLVOAvatarXmlInfo* LLVOAvatar::sAvatarXmlInfo = NULL;
  500. LLVOAvatarDictionary *LLVOAvatar::sAvatarDictionary = NULL;
  501. S32 LLVOAvatar::sFreezeCounter = 0;
  502. U32 LLVOAvatar::sMaxVisible = 12;
  503. F32 LLVOAvatar::sRenderDistance = 256.f;
  504. S32 LLVOAvatar::sNumVisibleAvatars = 0;
  505. S32 LLVOAvatar::sNumLODChangesThisFrame = 0;
  506. const LLUUID LLVOAvatar::sStepSoundOnLand("e8af4a28-aa83-4310-a7c4-c047e15ea0df");
  507. const LLUUID LLVOAvatar::sStepSounds[LL_MCODE_END] =
  508. {
  509. SND_STONE_RUBBER,
  510. SND_METAL_RUBBER,
  511. SND_GLASS_RUBBER,
  512. SND_WOOD_RUBBER,
  513. SND_FLESH_RUBBER,
  514. SND_RUBBER_PLASTIC,
  515. SND_RUBBER_RUBBER
  516. };
  517. S32 LLVOAvatar::sRenderName = RENDER_NAME_ALWAYS;
  518. BOOL LLVOAvatar::sRenderGroupTitles = TRUE;
  519. S32 LLVOAvatar::sNumVisibleChatBubbles = 0;
  520. BOOL LLVOAvatar::sDebugInvisible = FALSE;
  521. BOOL LLVOAvatar::sShowAttachmentPoints = FALSE;
  522. BOOL LLVOAvatar::sShowAnimationDebug = FALSE;
  523. BOOL LLVOAvatar::sShowFootPlane = FALSE;
  524. BOOL LLVOAvatar::sVisibleInFirstPerson = FALSE;
  525. F32 LLVOAvatar::sLODFactor = 1.f;
  526. F32 LLVOAvatar::sPhysicsLODFactor = 1.f;
  527. BOOL LLVOAvatar::sUseImpostors = FALSE;
  528. BOOL LLVOAvatar::sJointDebug = FALSE;
  529. F32 LLVOAvatar::sUnbakedTime = 0.f;
  530. F32 LLVOAvatar::sUnbakedUpdateTime = 0.f;
  531. F32 LLVOAvatar::sGreyTime = 0.f;
  532. F32 LLVOAvatar::sGreyUpdateTime = 0.f;
  533. //-----------------------------------------------------------------------------
  534. // Helper functions
  535. //-----------------------------------------------------------------------------
  536. static F32 calc_bouncy_animation(F32 x);
  537. //-----------------------------------------------------------------------------
  538. // LLVOAvatar()
  539. //-----------------------------------------------------------------------------
  540. LLVOAvatar::LLVOAvatar(const LLUUID& id,
  541. const LLPCode pcode,
  542. LLViewerRegion* regionp) :
  543. LLViewerObject(id, pcode, regionp),
  544. mIsDummy(FALSE),
  545. mSpecialRenderMode(0),
  546. mTurning(FALSE),
  547. mPelvisToFoot(0.f),
  548. mLastSkeletonSerialNum( 0 ),
  549. mHeadOffset(),
  550. mIsSitting(FALSE),
  551. mTimeVisible(),
  552. mTyping(FALSE),
  553. mMeshValid(FALSE),
  554. mVisible(FALSE),
  555. mWindFreq(0.f),
  556. mRipplePhase( 0.f ),
  557. mBelowWater(FALSE),
  558. mLastAppearanceBlendTime(0.f),
  559. mAppearanceAnimating(FALSE),
  560. mNameString(),
  561. mTitle(),
  562. mNameAway(false),
  563. mNameBusy(false),
  564. mNameMute(false),
  565. mNameAppearance(false),
  566. mNameFriend(false),
  567. mNameAlpha(0.f),
  568. mRenderGroupTitles(sRenderGroupTitles),
  569. mNameCloud(false),
  570. mFirstTEMessageReceived( FALSE ),
  571. mFirstAppearanceMessageReceived( FALSE ),
  572. mCulled( FALSE ),
  573. mVisibilityRank(0),
  574. mTexSkinColor( NULL ),
  575. mTexHairColor( NULL ),
  576. mTexEyeColor( NULL ),
  577. mNeedsSkin(FALSE),
  578. mLastSkinTime(0.f),
  579. mUpdatePeriod(1),
  580. mFullyLoaded(FALSE),
  581. mPreviousFullyLoaded(FALSE),
  582. mFullyLoadedInitialized(FALSE),
  583. mSupportsAlphaLayers(FALSE),
  584. mLoadedCallbacksPaused(FALSE),
  585. mHasPelvisOffset( FALSE ),
  586. mRenderUnloadedAvatar(LLCachedControl<bool>(gSavedSettings, "RenderUnloadedAvatar"))
  587. {
  588. LLMemType mt(LLMemType::MTYPE_AVATAR);
  589. //VTResume(); // VTune
  590. // mVoiceVisualizer is created by the hud effects manager and uses the HUD Effects pipeline
  591. const BOOL needsSendToSim = false; // currently, this HUD effect doesn't need to pack and unpack data to do its job
  592. mVoiceVisualizer = ( LLVoiceVisualizer *)LLHUDManager::getInstance()->createViewerEffect( LLHUDObject::LL_HUD_EFFECT_VOICE_VISUALIZER, needsSendToSim );
  593. lldebugs << "LLVOAvatar Constructor (0x" << this << ") id:" << mID << llendl;
  594. mPelvisp = NULL;
  595. mBakedTextureDatas.resize(BAKED_NUM_INDICES);
  596. for (U32 i = 0; i < mBakedTextureDatas.size(); i++ )
  597. {
  598. mBakedTextureDatas[i].mLastTextureIndex = IMG_DEFAULT_AVATAR;
  599. mBakedTextureDatas[i].mTexLayerSet = NULL;
  600. mBakedTextureDatas[i].mIsLoaded = false;
  601. mBakedTextureDatas[i].mIsUsed = false;
  602. mBakedTextureDatas[i].mMaskTexName = 0;
  603. mBakedTextureDatas[i].mTextureIndex = LLVOAvatarDictionary::bakedToLocalTextureIndex((EBakedTextureIndex)i);
  604. }
  605. mDirtyMesh = 2; // Dirty geometry, need to regenerate.
  606. mMeshTexturesDirty = FALSE;
  607. mHeadp = NULL;
  608. mIsBuilt = FALSE;
  609. mNumJoints = 0;
  610. mSkeleton = NULL;
  611. mNumCollisionVolumes = 0;
  612. mCollisionVolumes = NULL;
  613. // set up animation variables
  614. mSpeed = 0.f;
  615. setAnimationData("Speed", &mSpeed);
  616. mNeedsImpostorUpdate = TRUE;
  617. mNeedsAnimUpdate = TRUE;
  618. mImpostorDistance = 0;
  619. mImpostorPixelArea = 0;
  620. setNumTEs(TEX_NUM_INDICES);
  621. mbCanSelect = TRUE;
  622. mSignaledAnimations.clear();
  623. mPlayingAnimations.clear();
  624. mWasOnGroundLeft = FALSE;
  625. mWasOnGroundRight = FALSE;
  626. mTimeLast = 0.0f;
  627. mSpeedAccum = 0.0f;
  628. mRippleTimeLast = 0.f;
  629. mInAir = FALSE;
  630. mStepOnLand = TRUE;
  631. mStepMaterial = 0;
  632. mLipSyncActive = false;
  633. mOohMorph = NULL;
  634. mAahMorph = NULL;
  635. mCurrentGesticulationLevel = 0;
  636. mRuthTimer.reset();
  637. mRuthDebugTimer.reset();
  638. mDebugExistenceTimer.reset();
  639. mPelvisOffset = LLVector3(0.0f,0.0f,0.0f);
  640. mLastPelvisToFoot = 0.0f;
  641. mPelvisFixup = 0.0f;
  642. mLastPelvisFixup = 0.0f;
  643. }
  644. //------------------------------------------------------------------------
  645. // LLVOAvatar::~LLVOAvatar()
  646. //------------------------------------------------------------------------
  647. LLVOAvatar::~LLVOAvatar()
  648. {
  649. if (gSavedSettings.getBOOL("DebugAvatarRezTime"))
  650. {
  651. if (!mFullyLoaded)
  652. {
  653. llinfos << "REZTIME: [ " << (U32)mDebugExistenceTimer.getElapsedTimeF32() << "sec ] Avatar '" << getFullname() << "' left after " << (U32)mRuthDebugTimer.getElapsedTimeF32() << " seconds as cloud." << llendl;
  654. LLSD args;
  655. args["EXISTENCE"] = llformat("%d",(U32)mDebugExistenceTimer.getElapsedTimeF32());
  656. args["TIME"] = llformat("%d",(U32)mRuthDebugTimer.getElapsedTimeF32());
  657. args["NAME"] = getFullname();
  658. LLNotificationsUtil::add("AvatarRezLeftCloudNotification",args);
  659. }
  660. else
  661. {
  662. llinfos << "REZTIME: [ " << (U32)mDebugExistenceTimer.getElapsedTimeF32() << "sec ] Avatar '" << getFullname() << "' left." << llendl;
  663. LLSD args;
  664. args["EXISTENCE"] = llformat("%d",(U32)mDebugExistenceTimer.getElapsedTimeF32());
  665. args["NAME"] = getFullname();
  666. LLNotificationsUtil::add("AvatarRezLeftNotification",args);
  667. }
  668. }
  669. lldebugs << "LLVOAvatar Destructor (0x" << this << ") id:" << mID << llendl;
  670. mRoot.removeAllChildren();
  671. deleteAndClearArray(mSkeleton);
  672. deleteAndClearArray(mCollisionVolumes);
  673. mNumJoints = 0;
  674. for (U32 i = 0; i < mBakedTextureDatas.size(); i++)
  675. {
  676. deleteAndClear(mBakedTextureDatas[i].mTexLayerSet);
  677. mBakedTextureDatas[i].mMeshes.clear();
  678. for (morph_list_t::iterator iter2 = mBakedTextureDatas[i].mMaskedMorphs.begin();
  679. iter2 != mBakedTextureDatas[i].mMaskedMorphs.end(); iter2++)
  680. {
  681. LLMaskedMorph* masked_morph = (*iter2);
  682. delete masked_morph;
  683. }
  684. }
  685. std::for_each(mAttachmentPoints.begin(), mAttachmentPoints.end(), DeletePairedPointer());
  686. mAttachmentPoints.clear();
  687. deleteAndClear(mTexSkinColor);
  688. deleteAndClear(mTexHairColor);
  689. deleteAndClear(mTexEyeColor);
  690. std::for_each(mMeshes.begin(), mMeshes.end(), DeletePairedPointer());
  691. mMeshes.clear();
  692. for (std::vector<LLViewerJoint*>::iterator jointIter = mMeshLOD.begin();
  693. jointIter != mMeshLOD.end();
  694. ++jointIter)
  695. {
  696. LLViewerJoint* joint = (LLViewerJoint *) *jointIter;
  697. std::for_each(joint->mMeshParts.begin(), joint->mMeshParts.end(), DeletePointer());
  698. joint->mMeshParts.clear();
  699. }
  700. std::for_each(mMeshLOD.begin(), mMeshLOD.end(), DeletePointer());
  701. mMeshLOD.clear();
  702. mDead = TRUE;
  703. mAnimationSources.clear();
  704. LLLoadedCallbackEntry::cleanUpCallbackList(&mCallbackTextureList) ;
  705. lldebugs << "LLVOAvatar Destructor end" << llendl;
  706. }
  707. void LLVOAvatar::markDead()
  708. {
  709. if (mNameText)
  710. {
  711. mNameText->markDead();
  712. mNameText = NULL;
  713. sNumVisibleChatBubbles--;
  714. }
  715. mVoiceVisualizer->markDead();
  716. LLLoadedCallbackEntry::cleanUpCallbackList(&mCallbackTextureList) ;
  717. LLViewerObject::markDead();
  718. }
  719. BOOL LLVOAvatar::isFullyBaked()
  720. {
  721. if (mIsDummy) return TRUE;
  722. if (getNumTEs() == 0) return FALSE;
  723. for (U32 i = 0; i < mBakedTextureDatas.size(); i++)
  724. {
  725. if (!isTextureDefined(mBakedTextureDatas[i].mTextureIndex)
  726. && ( (i != BAKED_SKIRT) || isWearingWearableType(LLWearableType::WT_SKIRT) ) )
  727. {
  728. return FALSE;
  729. }
  730. }
  731. return TRUE;
  732. }
  733. void LLVOAvatar::deleteLayerSetCaches(bool clearAll)
  734. {
  735. for (U32 i = 0; i < mBakedTextureDatas.size(); i++)
  736. {
  737. if (mBakedTextureDatas[i].mTexLayerSet)
  738. {
  739. // ! BACKWARDS COMPATIBILITY !
  740. // Can be removed after hair baking is mandatory on the grid
  741. if ((i != BAKED_HAIR || isSelf()) && !clearAll)
  742. {
  743. mBakedTextureDatas[i].mTexLayerSet->deleteCaches();
  744. }
  745. }
  746. if (mBakedTextureDatas[i].mMaskTexName)
  747. {
  748. glDeleteTextures(1, (GLuint*)&(mBakedTextureDatas[i].mMaskTexName));
  749. mBakedTextureDatas[i].mMaskTexName = 0 ;
  750. }
  751. }
  752. }
  753. // static
  754. BOOL LLVOAvatar::areAllNearbyInstancesBaked(S32& grey_avatars)
  755. {
  756. BOOL res = TRUE;
  757. grey_avatars = 0;
  758. for (std::vector<LLCharacter*>::iterator iter = LLCharacter::sInstances.begin();
  759. iter != LLCharacter::sInstances.end(); ++iter)
  760. {
  761. LLVOAvatar* inst = (LLVOAvatar*) *iter;
  762. if( inst->isDead() )
  763. {
  764. continue;
  765. }
  766. else if( !inst->isFullyBaked() )
  767. {
  768. res = FALSE;
  769. if (inst->mHasGrey)
  770. {
  771. ++grey_avatars;
  772. }
  773. }
  774. }
  775. return res;
  776. }
  777. // static
  778. void LLVOAvatar::dumpBakedStatus()
  779. {
  780. LLVector3d camera_pos_global = gAgentCamera.getCameraPositionGlobal();
  781. for (std::vector<LLCharacter*>::iterator iter = LLCharacter::sInstances.begin();
  782. iter != LLCharacter::sInstances.end(); ++iter)
  783. {
  784. LLVOAvatar* inst = (LLVOAvatar*) *iter;
  785. llinfos << "Avatar ";
  786. LLNameValue* firstname = inst->getNVPair("FirstName");
  787. LLNameValue* lastname = inst->getNVPair("LastName");
  788. if( firstname )
  789. {
  790. llcont << firstname->getString();
  791. }
  792. if( lastname )
  793. {
  794. llcont << " " << lastname->getString();
  795. }
  796. llcont << " " << inst->mID;
  797. if( inst->isDead() )
  798. {
  799. llcont << " DEAD ("<< inst->getNumRefs() << " refs)";
  800. }
  801. if( inst->isSelf() )
  802. {
  803. llcont << " (self)";
  804. }
  805. F64 dist_to_camera = (inst->getPositionGlobal() - camera_pos_global).length();
  806. llcont << " " << dist_to_camera << "m ";
  807. llcont << " " << inst->mPixelArea << " pixels";
  808. if( inst->isVisible() )
  809. {
  810. llcont << " (visible)";
  811. }
  812. else
  813. {
  814. llcont << " (not visible)";
  815. }
  816. if( inst->isFullyBaked() )
  817. {
  818. llcont << " Baked";
  819. }
  820. else
  821. {
  822. llcont << " Unbaked (";
  823. for (LLVOAvatarDictionary::BakedTextures::const_iterator iter = LLVOAvatarDictionary::getInstance()->getBakedTextures().begin();
  824. iter != LLVOAvatarDictionary::getInstance()->getBakedTextures().end();
  825. ++iter)
  826. {
  827. const LLVOAvatarDictionary::BakedEntry *baked_dict = iter->second;
  828. const ETextureIndex index = baked_dict->mTextureIndex;
  829. if (!inst->isTextureDefined(index))
  830. {
  831. llcont << " " << LLVOAvatarDictionary::getInstance()->getTexture(index)->mName;
  832. }
  833. }
  834. llcont << " ) " << inst->getUnbakedPixelAreaRank();
  835. if( inst->isCulled() )
  836. {
  837. llcont << " culled";
  838. }
  839. }
  840. llcont << llendl;
  841. }
  842. }
  843. //static
  844. void LLVOAvatar::restoreGL()
  845. {
  846. if (!isAgentAvatarValid()) return;
  847. gAgentAvatarp->setCompositeUpdatesEnabled(TRUE);
  848. for (U32 i = 0; i < gAgentAvatarp->mBakedTextureDatas.size(); i++)
  849. {
  850. gAgentAvatarp->invalidateComposite(gAgentAvatarp->mBakedTextureDatas[i].mTexLayerSet, FALSE);
  851. }
  852. gAgentAvatarp->updateMeshTextures();
  853. }
  854. //static
  855. void LLVOAvatar::destroyGL()
  856. {
  857. deleteCachedImages();
  858. resetImpostors();
  859. }
  860. //static
  861. void LLVOAvatar::resetImpostors()
  862. {
  863. for (std::vector<LLCharacter*>::iterator iter = LLCharacter::sInstances.begin();
  864. iter != LLCharacter::sInstances.end(); ++iter)
  865. {
  866. LLVOAvatar* avatar = (LLVOAvatar*) *iter;
  867. avatar->mImpostor.release();
  868. }
  869. }
  870. // static
  871. void LLVOAvatar::deleteCachedImages(bool clearAll)
  872. {
  873. if (LLTexLayerSet::sHasCaches)
  874. {
  875. lldebugs << "Deleting layer set caches" << llendl;
  876. for (std::vector<LLCharacter*>::iterator iter = LLCharacter::sInstances.begin();
  877. iter != LLCharacter::sInstances.end(); ++iter)
  878. {
  879. LLVOAvatar* inst = (LLVOAvatar*) *iter;
  880. inst->deleteLayerSetCaches(clearAll);
  881. }
  882. LLTexLayerSet::sHasCaches = FALSE;
  883. }
  884. LLVOAvatarSelf::deleteScratchTextures();
  885. LLTexLayerStaticImageList::getInstance()->deleteCachedImages();
  886. }
  887. //------------------------------------------------------------------------
  888. // static
  889. // LLVOAvatar::initClass()
  890. //------------------------------------------------------------------------
  891. void LLVOAvatar::initClass()
  892. {
  893. std::string xmlFile;
  894. xmlFile = gDirUtilp->getExpandedFilename(LL_PATH_CHARACTER,AVATAR_DEFAULT_CHAR) + "_lad.xml";
  895. BOOL success = sXMLTree.parseFile( xmlFile, FALSE );
  896. if (!success)
  897. {
  898. llerrs << "Problem reading avatar configuration file:" << xmlFile << llendl;
  899. }
  900. // now sanity check xml file
  901. LLXmlTreeNode* root = sXMLTree.getRoot();
  902. if (!root)
  903. {
  904. llerrs << "No root node found in avatar configuration file: " << xmlFile << llendl;
  905. return;
  906. }
  907. //-------------------------------------------------------------------------
  908. // <linden_avatar version="1.0"> (root)
  909. //-------------------------------------------------------------------------
  910. if( !root->hasName( "linden_avatar" ) )
  911. {
  912. llerrs << "Invalid avatar file header: " << xmlFile << llendl;
  913. }
  914. std::string version;
  915. static LLStdStringHandle version_string = LLXmlTree::addAttributeString("version");
  916. if( !root->getFastAttributeString( version_string, version ) || (version != "1.0") )
  917. {
  918. llerrs << "Invalid avatar file version: " << version << " in file: " << xmlFile << llendl;
  919. }
  920. S32 wearable_def_version = 1;
  921. static LLStdStringHandle wearable_definition_version_string = LLXmlTree::addAttributeString("wearable_definition_version");
  922. root->getFastAttributeS32( wearable_definition_version_string, wearable_def_version );
  923. LLWearable::setCurrentDefinitionVersion( wearable_def_version );
  924. std::string mesh_file_name;
  925. LLXmlTreeNode* skeleton_node = root->getChildByName( "skeleton" );
  926. if (!skeleton_node)
  927. {
  928. llerrs << "No skeleton in avatar configuration file: " << xmlFile << llendl;
  929. return;
  930. }
  931. std::string skeleton_file_name;
  932. static LLStdStringHandle file_name_string = LLXmlTree::addAttributeString("file_name");
  933. if (!skeleton_node->getFastAttributeString(file_name_string, skeleton_file_name))
  934. {
  935. llerrs << "No file name in skeleton node in avatar config file: " << xmlFile << llendl;
  936. }
  937. std::string skeleton_path;
  938. skeleton_path = gDirUtilp->getExpandedFilename(LL_PATH_CHARACTER,skeleton_file_name);
  939. if (!parseSkeletonFile(skeleton_path))
  940. {
  941. llerrs << "Error parsing skeleton file: " << skeleton_path << llendl;
  942. }
  943. // Process XML data
  944. // avatar_skeleton.xml
  945. if (sAvatarSkeletonInfo)
  946. { //this can happen if a login attempt failed
  947. delete sAvatarSkeletonInfo;
  948. }
  949. sAvatarSkeletonInfo = new LLVOAvatarSkeletonInfo;
  950. if (!sAvatarSkeletonInfo->parseXml(sSkeletonXMLTree.getRoot()))
  951. {
  952. llerrs << "Error parsing skeleton XML file: " << skeleton_path << llendl;
  953. }
  954. // parse avatar_lad.xml
  955. if (sAvatarXmlInfo)
  956. { //this can happen if a login attempt failed
  957. deleteAndClear(sAvatarXmlInfo);
  958. }
  959. sAvatarXmlInfo = new LLVOAvatarXmlInfo;
  960. if (!sAvatarXmlInfo->parseXmlSkeletonNode(root))
  961. {
  962. llerrs << "Error parsing skeleton node in avatar XML file: " << skeleton_path << llendl;
  963. }
  964. if (!sAvatarXmlInfo->parseXmlMeshNodes(root))
  965. {
  966. llerrs << "Error parsing skeleton node in avatar XML file: " << skeleton_path << llendl;
  967. }
  968. if (!sAvatarXmlInfo->parseXmlColorNodes(root))
  969. {
  970. llerrs << "Error parsing skeleton node in avatar XML file: " << skeleton_path << llendl;
  971. }
  972. if (!sAvatarXmlInfo->parseXmlLayerNodes(root))
  973. {
  974. llerrs << "Error parsing skeleton node in avatar XML file: " << skeleton_path << llendl;
  975. }
  976. if (!sAvatarXmlInfo->parseXmlDriverNodes(root))
  977. {
  978. llerrs << "Error parsing skeleton node in avatar XML file: " << skeleton_path << llendl;
  979. }
  980. if (!sAvatarXmlInfo->parseXmlMorphNodes(root))
  981. {
  982. llerrs << "Error parsing skeleton node in avatar XML file: " << skeleton_path << llendl;
  983. }
  984. gAnimLibrary.animStateSetString(ANIM_AGENT_BODY_NOISE,"body_noise");
  985. gAnimLibrary.animStateSetString(ANIM_AGENT_BREATHE_ROT,"breathe_rot");
  986. gAnimLibrary.animStateSetString(ANIM_AGENT_PHYSICS_MOTION,"physics_motion");
  987. gAnimLibrary.animStateSetString(ANIM_AGENT_EDITING,"editing");
  988. gAnimLibrary.animStateSetString(ANIM_AGENT_EYE,"eye");
  989. gAnimLibrary.animStateSetString(ANIM_AGENT_FLY_ADJUST,"fly_adjust");
  990. gAnimLibrary.animStateSetString(ANIM_AGENT_HAND_MOTION,"hand_motion");
  991. gAnimLibrary.animStateSetString(ANIM_AGENT_HEAD_ROT,"head_rot");
  992. gAnimLibrary.animStateSetString(ANIM_AGENT_PELVIS_FIX,"pelvis_fix");
  993. gAnimLibrary.animStateSetString(ANIM_AGENT_TARGET,"target");
  994. gAnimLibrary.animStateSetString(ANIM_AGENT_WALK_ADJUST,"walk_adjust");
  995. }
  996. void LLVOAvatar::cleanupClass()
  997. {
  998. deleteAndClear(sAvatarXmlInfo);
  999. sSkeletonXMLTree.cleanup();
  1000. sXMLTree.cleanup();
  1001. }
  1002. void LLVOAvatar::initInstance(void)
  1003. {
  1004. //-------------------------------------------------------------------------
  1005. // initialize joint, mesh and shape members
  1006. //-------------------------------------------------------------------------
  1007. mRoot.setName( "mRoot" );
  1008. for (LLVOAvatarDictionary::Meshes::const_iterator iter = LLVOAvatarDictionary::getInstance()->getMeshes().begin();
  1009. iter != LLVOAvatarDictionary::getInstance()->getMeshes().end();
  1010. ++iter)
  1011. {
  1012. const EMeshIndex mesh_index = iter->first;
  1013. const LLVOAvatarDictionary::MeshEntry *mesh_dict = iter->second;
  1014. LLViewerJoint* joint = new LLViewerJoint();
  1015. joint->setName(mesh_dict->mName);
  1016. joint->setMeshID(mesh_index);
  1017. mMeshLOD.push_back(joint);
  1018. /* mHairLOD.setName("mHairLOD");
  1019. mHairMesh0.setName("mHairMesh0");
  1020. mHairMesh0.setMeshID(MESH_ID_HAIR);
  1021. mHairMesh1.setName("mHairMesh1"); */
  1022. for (U32 lod = 0; lod < mesh_dict->mLOD; lod++)
  1023. {
  1024. LLViewerJointMesh* mesh = new LLViewerJointMesh();
  1025. std::string mesh_name = "m" + mesh_dict->mName + boost::lexical_cast<std::string>(lod);
  1026. // We pre-pended an m - need to capitalize first character for camelCase
  1027. mesh_name[1] = toupper(mesh_name[1]);
  1028. mesh->setName(mesh_name);
  1029. mesh->setMeshID(mesh_index);
  1030. mesh->setPickName(mesh_dict->mPickName);
  1031. mesh->setIsTransparent(FALSE);
  1032. switch((int)mesh_index)
  1033. {
  1034. case MESH_ID_HAIR:
  1035. mesh->setIsTransparent(TRUE);
  1036. break;
  1037. case MESH_ID_SKIRT:
  1038. mesh->setIsTransparent(TRUE);
  1039. break;
  1040. case MESH_ID_EYEBALL_LEFT:
  1041. case MESH_ID_EYEBALL_RIGHT:
  1042. mesh->setSpecular( LLColor4( 1.0f, 1.0f, 1.0f, 1.0f ), 1.f );
  1043. break;
  1044. }
  1045. joint->mMeshParts.push_back(mesh);
  1046. }
  1047. }
  1048. //-------------------------------------------------------------------------
  1049. // associate baked textures with meshes
  1050. //-------------------------------------------------------------------------
  1051. for (LLVOAvatarDictionary::Meshes::const_iterator iter = LLVOAvatarDictionary::getInstance()->getMeshes().begin();
  1052. iter != LLVOAvatarDictionary::getInstance()->getMeshes().end();
  1053. ++iter)
  1054. {
  1055. const EMeshIndex mesh_index = iter->first;
  1056. const LLVOAvatarDictionary::MeshEntry *mesh_dict = iter->second;
  1057. const EBakedTextureIndex baked_texture_index = mesh_dict->mBakedID;
  1058. // Skip it if there's no associated baked texture.
  1059. if (baked_texture_index == BAKED_NUM_INDICES) continue;
  1060. for (std::vector<LLViewerJointMesh* >::iterator iter = mMeshLOD[mesh_index]->mMeshParts.begin();
  1061. iter != mMeshLOD[mesh_index]->mMeshParts.end();
  1062. ++iter)
  1063. {
  1064. LLViewerJointMesh* mesh = (LLViewerJointMesh*) *iter;
  1065. mBakedTextureDatas[(int)baked_texture_index].mMeshes.push_back(mesh);
  1066. }
  1067. }
  1068. //-------------------------------------------------------------------------
  1069. // register motions
  1070. //-------------------------------------------------------------------------
  1071. if (LLCharacter::sInstances.size() == 1)
  1072. {
  1073. LLKeyframeMotion::setVFS(gStaticVFS);
  1074. registerMotion( ANIM_AGENT_BUSY, LLNullMotion::create );
  1075. registerMotion( ANIM_AGENT_CROUCH, LLKeyframeStandMotion::create );
  1076. registerMotion( ANIM_AGENT_CROUCHWALK, LLKeyframeWalkMotion::create );
  1077. registerMotion( ANIM_AGENT_EXPRESS_AFRAID, LLEmote::create );
  1078. registerMotion( ANIM_AGENT_EXPRESS_ANGER, LLEmote::create );
  1079. registerMotion( ANIM_AGENT_EXPRESS_BORED, LLEmote::create );
  1080. registerMotion( ANIM_AGENT_EXPRESS_CRY, LLEmote::create );
  1081. registerMotion( ANIM_AGENT_EXPRESS_DISDAIN, LLEmote::create );
  1082. registerMotion( ANIM_AGENT_EXPRESS_EMBARRASSED, LLEmote::create );
  1083. registerMotion( ANIM_AGENT_EXPRESS_FROWN, LLEmote::create );
  1084. registerMotion( ANIM_AGENT_EXPRESS_KISS, LLEmote::create );
  1085. registerMotion( ANIM_AGENT_EXPRESS_LAUGH, LLEmote::create );
  1086. registerMotion( ANIM_AGENT_EXPRESS_OPEN_MOUTH, LLEmote::create );
  1087. registerMotion( ANIM_AGENT_EXPRESS_REPULSED, LLEmote::create );
  1088. registerMotion( ANIM_AGENT_EXPRESS_SAD, LLEmote::create );
  1089. registerMotion( ANIM_AGENT_EXPRESS_SHRUG, LLEmote::create );
  1090. registerMotion( ANIM_AGENT_EXPRESS_SMILE, LLEmote::create );
  1091. registerMotion( ANIM_AGENT_EXPRESS_SURPRISE, LLEmote::create );
  1092. registerMotion( ANIM_AGENT_EXPRESS_TONGUE_OUT, LLEmote::create );
  1093. registerMotion( ANIM_AGENT_EXPRESS_TOOTHSMILE, LLEmote::create );
  1094. registerMotion( ANIM_AGENT_EXPRESS_WINK, LLEmote::create );
  1095. registerMotion( ANIM_AGENT_EXPRESS_WORRY, LLEmote::create );
  1096. registerMotion( ANIM_AGENT_FEMALE_RUN_NEW, LLKeyframeWalkMotion::create );
  1097. registerMotion( ANIM_AGENT_FEMALE_WALK, LLKeyframeWalkMotion::create );
  1098. registerMotion( ANIM_AGENT_FEMALE_WALK_NEW, LLKeyframeWalkMotion::create );
  1099. registerMotion( ANIM_AGENT_RUN, LLKeyframeWalkMotion::create );
  1100. registerMotion( ANIM_AGENT_RUN_NEW, LLKeyframeWalkMotion::create );
  1101. registerMotion( ANIM_AGENT_STAND, LLKeyframeStandMotion::create );
  1102. registerMotion( ANIM_AGENT_STAND_1, LLKeyframeStandMotion::create );
  1103. registerMotion( ANIM_AGENT_STAND_2, LLKeyframeStandMotion::create );
  1104. registerMotion( ANIM_AGENT_STAND_3, LLKeyframeStandMotion::create );
  1105. registerMotion( ANIM_AGENT_STAND_4, LLKeyframeStandMotion::create );
  1106. registerMotion( ANIM_AGENT_STANDUP, LLKeyframeFallMotion::create );
  1107. registerMotion( ANIM_AGENT_TURNLEFT, LLKeyframeWalkMotion::create );
  1108. registerMotion( ANIM_AGENT_TURNRIGHT, LLKeyframeWalkMotion::create );
  1109. registerMotion( ANIM_AGENT_WALK, LLKeyframeWalkMotion::create );
  1110. registerMotion( ANIM_AGENT_WALK_NEW, LLKeyframeWalkMotion::create );
  1111. // motions without a start/stop bit
  1112. registerMotion( ANIM_AGENT_BODY_NOISE, LLBodyNoiseMotion::create );
  1113. registerMotion( ANIM_AGENT_BREATHE_ROT, LLBreatheMotionRot::create );
  1114. registerMotion( ANIM_AGENT_PHYSICS_MOTION, LLPhysicsMotionController::create );
  1115. registerMotion( ANIM_AGENT_EDITING, LLEditingMotion::create );
  1116. registerMotion( ANIM_AGENT_EYE, LLEyeMotion::create );
  1117. registerMotion( ANIM_AGENT_FEMALE_WALK, LLKeyframeWalkMotion::create );
  1118. registerMotion( ANIM_AGENT_FLY_ADJUST, LLFlyAdjustMotion::create );
  1119. registerMotion( ANIM_AGENT_HAND_MOTION, LLHandMotion::create );
  1120. registerMotion( ANIM_AGENT_HEAD_ROT, LLHeadRotMotion::create );
  1121. registerMotion( ANIM_AGENT_PELVIS_FIX, LLPelvisFixMotion::create );
  1122. registerMotion( ANIM_AGENT_SIT_FEMALE, LLKeyframeMotion::create );
  1123. registerMotion( ANIM_AGENT_TARGET, LLTargetingMotion::create );
  1124. registerMotion( ANIM_AGENT_WALK_ADJUST, LLWalkAdjustMotion::create );
  1125. }
  1126. buildCharacter();
  1127. // preload specific motions here
  1128. createMotion( ANIM_AGENT_CUSTOMIZE);
  1129. createMotion( ANIM_AGENT_CUSTOMIZE_DONE);
  1130. //VTPause(); // VTune
  1131. mVoiceVisualizer->setVoiceEnabled( LLVoiceClient::getInstance()->getVoiceEnabled( mID ) );
  1132. }
  1133. const LLVector3 LLVOAvatar::getRenderPosition() const
  1134. {
  1135. if (mDrawable.isNull() || mDrawable->getGeneration() < 0)
  1136. {
  1137. return getPositionAgent();
  1138. }
  1139. else if (isRoot())
  1140. {
  1141. if ( !mHasPelvisOffset )
  1142. {
  1143. return mDrawable->getPositionAgent();
  1144. }
  1145. else
  1146. {
  1147. //Apply a pelvis fixup (as defined by the avs skin)
  1148. LLVector3 pos = mDrawable->getPositionAgent();
  1149. pos[VZ] += mPelvisFixup;
  1150. return pos;
  1151. }
  1152. }
  1153. else
  1154. {
  1155. return getPosition() * mDrawable->getParent()->getRenderMatrix();
  1156. }
  1157. }
  1158. void LLVOAvatar::updateDrawable(BOOL force_damped)
  1159. {
  1160. clearChanged(SHIFTED);
  1161. }
  1162. void LLVOAvatar::onShift(const LLVector4a& shift_vector)
  1163. {
  1164. const LLVector3& shift = reinterpret_cast<const LLVector3&>(shift_vector);
  1165. mLastAnimExtents[0] += shift;
  1166. mLastAnimExtents[1] += shift;
  1167. mNeedsImpostorUpdate = TRUE;
  1168. mNeedsAnimUpdate = TRUE;
  1169. }
  1170. void LLVOAvatar::updateSpatialExtents(LLVector4a& newMin, LLVector4a &newMax)
  1171. {
  1172. if (isImpostor() && !needsImpostorUpdate())
  1173. {
  1174. LLVector3 delta = getRenderPosition() -
  1175. ((LLVector3(mDrawable->getPositionGroup().getF32ptr())-mImpostorOffset));
  1176. newMin.load3( (mLastAnimExtents[0] + delta).mV);
  1177. newMax.load3( (mLastAnimExtents[1] + delta).mV);
  1178. }
  1179. else
  1180. {
  1181. getSpatialExtents(newMin,newMax);
  1182. mLastAnimExtents[0].set(newMin.getF32ptr());
  1183. mLastAnimExtents[1].set(newMax.getF32ptr());
  1184. LLVector4a pos_group;
  1185. pos_group.setAdd(newMin,newMax);
  1186. pos_group.mul(0.5f);
  1187. mImpostorOffset = LLVector3(pos_group.getF32ptr())-getRenderPosition();
  1188. mDrawable->setPositionGroup(pos_group);
  1189. }
  1190. }
  1191. void LLVOAvatar::getSpatialExtents(LLVector4a& newMin, LLVector4a& newMax)
  1192. {
  1193. LLVector4a buffer(0.25f);
  1194. LLVector4a pos;
  1195. pos.load3(getRenderPosition().mV);
  1196. newMin.setSub(pos, buffer);
  1197. newMax.setAdd(pos, buffer);
  1198. float max_attachment_span = get_default_max_prim_scale() * 5.0f;
  1199. //stretch bounding box by joint positions
  1200. for (polymesh_map_t::iterator i = mMeshes.begin(); i != mMeshes.end(); ++i)
  1201. {
  1202. LLPolyMesh* mesh = i->second;
  1203. for (S32 joint_num = 0; joint_num < mesh->mJointRenderData.count(); joint_num++)
  1204. {
  1205. LLVector4a trans;
  1206. trans.load3( mesh->mJointRenderData[joint_num]->mWorldMatrix->getTranslation().mV);
  1207. update_min_max(newMin, newMax, trans);
  1208. }
  1209. }
  1210. LLVector4a center, size;
  1211. center.setAdd(newMin, newMax);
  1212. center.mul(0.5f);
  1213. size.setSub(newMax,newMin);
  1214. size.mul(0.5f);
  1215. mPixelArea = LLPipeline::calcPixelArea(center, size, *LLViewerCamera::getInstance());
  1216. //stretch bounding box by attachments
  1217. for (attachment_map_t::iterator iter = mAttachmentPoints.begin();
  1218. iter != mAttachmentPoints.end();
  1219. ++iter)
  1220. {
  1221. LLViewerJointAttachment* attachment = iter->second;
  1222. if (!attachment->getValid())
  1223. {
  1224. continue ;
  1225. }
  1226. for (LLViewerJointAttachment::attachedobjs_vec_t::iterator attachment_iter = attachment->mAttachedObjects.begin();
  1227. attachment_iter != attachment->mAttachedObjects.end();
  1228. ++attachment_iter)
  1229. {
  1230. const LLViewerObject* attached_object = (*attachment_iter);
  1231. if (attached_object && !attached_object->isHUDAttachment())
  1232. {
  1233. LLDrawable* drawable = attached_object->mDrawable;
  1234. if (drawable && !drawable->isState(LLDrawable::RIGGED))
  1235. {
  1236. LLSpatialBridge* bridge = drawable->getSpatialBridge();
  1237. if (bridge)
  1238. {
  1239. const LLVector4a* ext = bridge->getSpatialExtents();
  1240. LLVector4a distance;
  1241. distance.setSub(ext[1], ext[0]);
  1242. LLVector4a max_span(max_attachment_span);
  1243. S32 lt = distance.lessThan(max_span).getGatheredBits() & 0x7;
  1244. // Only add the prim to spatial extents calculations if it isn't a megaprim.
  1245. // max_attachment_span calculated at the start of the function
  1246. // (currently 5 times our max prim size)
  1247. if (lt == 0x7)
  1248. {
  1249. update_min_max(newMin,newMax,ext[0]);
  1250. update_min_max(newMin,newMax,ext[1]);
  1251. }
  1252. }
  1253. }
  1254. }
  1255. }
  1256. }
  1257. //pad bounding box
  1258. newMin.sub(buffer);
  1259. newMax.add(buffer);
  1260. }
  1261. //-----------------------------------------------------------------------------
  1262. // renderCollisionVolumes()
  1263. //-----------------------------------------------------------------------------
  1264. void LLVOAvatar::renderCollisionVolumes()
  1265. {
  1266. for (S32 i = 0; i < mNumCollisionVolumes; i++)
  1267. {
  1268. mCollisionVolumes[i].renderCollision();
  1269. }
  1270. if (mNameText.notNull())
  1271. {
  1272. LLVector3 unused;
  1273. mNameText->lineSegmentIntersect(LLVector3(0,0,0), LLVector3(0,0,1), unused, TRUE);
  1274. }
  1275. }
  1276. BOOL LLVOAvatar::lineSegmentIntersect(const LLVector3& start, const LLVector3& end,
  1277. S32 face,
  1278. BOOL pick_transparent,
  1279. S32* face_hit,
  1280. LLVector3* intersection,
  1281. LLVector2* tex_coord,
  1282. LLVector3* normal,
  1283. LLVector3* bi_normal)
  1284. {
  1285. if ((isSelf() && !gAgent.needsRenderAvatar()) || !LLPipeline::sPickAvatar)
  1286. {
  1287. return FALSE;
  1288. }
  1289. if (lineSegmentBoundingBox(start, end))
  1290. {
  1291. for (S32 i = 0; i < mNumCollisionVolumes; ++i)
  1292. {
  1293. mCollisionVolumes[i].updateWorldMatrix();
  1294. glh::matrix4f mat((F32*) mCollisionVolumes[i].getXform()->getWorldMatrix().mMatrix);
  1295. glh::matrix4f inverse = mat.inverse();
  1296. glh::matrix4f norm_mat = inverse.transpose();
  1297. glh::vec3f p1(start.mV);
  1298. glh::vec3f p2(end.mV);
  1299. inverse.mult_matrix_vec(p1);
  1300. inverse.mult_matrix_vec(p2);
  1301. LLVector3 position;
  1302. LLVector3 norm;
  1303. if (linesegment_sphere(LLVector3(p1.v), LLVector3(p2.v), LLVector3(0,0,0), 1.f, position, norm))
  1304. {
  1305. glh::vec3f res_pos(position.mV);
  1306. mat.mult_matrix_vec(res_pos);
  1307. norm.normalize();
  1308. glh::vec3f res_norm(norm.mV);
  1309. norm_mat.mult_matrix_dir(res_norm);
  1310. if (intersection)
  1311. {
  1312. *intersection = LLVector3(res_pos.v);
  1313. }
  1314. if (normal)
  1315. {
  1316. *normal = LLVector3(res_norm.v);
  1317. }
  1318. return TRUE;
  1319. }
  1320. }
  1321. if (isSelf())
  1322. {
  1323. for (attachment_map_t::iterator iter = mAttachmentPoints.begin();
  1324. iter != mAttachmentPoints.end();
  1325. ++iter)
  1326. {
  1327. LLViewerJointAttachment* attachment = iter->second;
  1328. for (LLViewerJointAttachment::attachedobjs_vec_t::iterator attachment_iter = attachment->mAttachedObjects.begin();
  1329. attachment_iter != attachment->mAttachedObjects.end();
  1330. ++attachment_iter)
  1331. {
  1332. LLViewerObject* attached_object = (*attachment_iter);
  1333. if (attached_object && !attached_object->isDead() && attachment->getValid())
  1334. {
  1335. LLDrawable* drawable = attached_object->mDrawable;
  1336. if (drawable->isState(LLDrawable::RIGGED))
  1337. { //regenerate octree for rigged attachment
  1338. gPipeline.markRebuild(mDrawable, LLDrawable::REBUILD_RIGGED, TRUE);
  1339. }
  1340. }
  1341. }
  1342. }
  1343. }
  1344. }
  1345. LLVector3 position;
  1346. if (mNameText.notNull() && mNameText->lineSegmentIntersect(start, end, position))
  1347. {
  1348. if (intersection)
  1349. {
  1350. *intersection = position;
  1351. }
  1352. return TRUE;
  1353. }
  1354. return FALSE;
  1355. }
  1356. LLViewerObject* LLVOAvatar::lineSegmentIntersectRiggedAttachments(const LLVector3& start, const LLVector3& end,
  1357. S32 face,
  1358. BOOL pick_transparent,
  1359. S32* face_hit,
  1360. LLVector3* intersection,
  1361. LLVector2* tex_coord,
  1362. LLVector3* normal,
  1363. LLVector3* bi_normal)
  1364. {
  1365. if (isSelf() && !gAgent.needsRenderAvatar())
  1366. {
  1367. return NULL;
  1368. }
  1369. LLViewerObject* hit = NULL;
  1370. if (lineSegmentBoundingBox(start, end))
  1371. {
  1372. LLVector3 local_end = end;
  1373. LLVector3 local_intersection;
  1374. for (attachment_map_t::iterator iter = mAttachmentPoints.begin();
  1375. iter != mAttachmentPoints.end();
  1376. ++iter)
  1377. {
  1378. LLViewerJointAttachment* attachment = iter->second;
  1379. for (LLViewerJointAttachment::attachedobjs_vec_t::iterator attachment_iter = attachment->mAttachedObjects.begin();
  1380. attachment_iter != attachment->mAttachedObjects.end();
  1381. ++attachment_iter)
  1382. {
  1383. LLViewerObject* attached_object = (*attachment_iter);
  1384. if (attached_object->lineSegmentIntersect(start, local_end, face, pick_transparent, face_hit, &local_intersection, tex_coord, normal, bi_normal))
  1385. {
  1386. local_end = local_intersection;
  1387. if (intersection)
  1388. {
  1389. *intersection = local_intersection;
  1390. }
  1391. hit = attached_object;
  1392. }
  1393. }
  1394. }
  1395. }
  1396. return hit;
  1397. }
  1398. //-----------------------------------------------------------------------------
  1399. // parseSkeletonFile()
  1400. //-----------------------------------------------------------------------------
  1401. BOOL LLVOAvatar::parseSkeletonFile(const std::string& filename)
  1402. {
  1403. LLMemType mt(LLMemType::MTYPE_AVATAR);
  1404. //-------------------------------------------------------------------------
  1405. // parse the file
  1406. //-------------------------------------------------------------------------
  1407. BOOL parsesuccess = sSkeletonXMLTree.parseFile( filename, FALSE );
  1408. if (!parsesuccess)
  1409. {
  1410. llerrs << "Can't parse skeleton file: " << filename << llendl;
  1411. return FALSE;
  1412. }
  1413. // now sanity check xml file
  1414. LLXmlTreeNode* root = sSkeletonXMLTree.getRoot();
  1415. if (!root)
  1416. {
  1417. llerrs << "No root node found in avatar skeleton file: " << filename << llendl;
  1418. return FALSE;
  1419. }
  1420. if( !root->hasName( "linden_skeleton" ) )
  1421. {
  1422. llerrs << "Invalid avatar skeleton file header: " << filename << llendl;
  1423. return FALSE;
  1424. }
  1425. std::string version;
  1426. static LLStdStringHandle version_string = LLXmlTree::addAttributeString("version");
  1427. if( !root->getFastAttributeString( version_string, version ) || (version != "1.0") )
  1428. {
  1429. llerrs << "Invalid avatar skeleton file version: " << version << " in file: " << filename << llendl;
  1430. return FALSE;
  1431. }
  1432. return TRUE;
  1433. }
  1434. //-----------------------------------------------------------------------------
  1435. // setupBone()
  1436. //-----------------------------------------------------------------------------
  1437. BOOL LLVOAvatar::setupBone(const LLVOAvatarBoneInfo* info, LLViewerJoint* parent, S32 &volume_num, S32 &joint_num)
  1438. {
  1439. LLMemType mt(LLMemType::MTYPE_AVATAR);
  1440. LLViewerJoint* joint = NULL;
  1441. if (info->mIsJoint)
  1442. {
  1443. joint = (LLViewerJoint*)getCharacterJoint(joint_num);
  1444. if (!joint)
  1445. {
  1446. llwarns << "Too many bones" << llendl;
  1447. return FALSE;
  1448. }
  1449. joint->setName( info->mName );
  1450. }
  1451. else // collision volume
  1452. {
  1453. if (volume_num >= (S32)mNumCollisionVolumes)
  1454. {
  1455. llwarns << "Too many bones" << llendl;
  1456. return FALSE;
  1457. }
  1458. joint = (LLViewerJoint*)(&mCollisionVolumes[volume_num]);
  1459. joint->setName( info->mName );
  1460. }
  1461. // add to parent
  1462. if (parent)
  1463. {
  1464. parent->addChild( joint );
  1465. }
  1466. joint->setPosition(info->mPos);
  1467. joint->setRotation(mayaQ(info->mRot.mV[VX], info->mRot.mV[VY],
  1468. info->mRot.mV[VZ], LLQuaternion::XYZ));
  1469. joint->setScale(info->mScale);
  1470. joint->setDefaultFromCurrentXform();
  1471. if (info->mIsJoint)
  1472. {
  1473. joint->setSkinOffset( info->mPivot );
  1474. joint_num++;
  1475. }
  1476. else // collision volume
  1477. {
  1478. volume_num++;
  1479. }
  1480. // setup children
  1481. LLVOAvatarBoneInfo::child_list_t::const_iterator iter;
  1482. for (iter = info->mChildList.begin(); iter != info->mChildList.end(); ++iter)
  1483. {
  1484. LLVOAvatarBoneInfo *child_info = *iter;
  1485. if (!setupBone(child_info, joint, volume_num, joint_num))
  1486. {
  1487. return FALSE;
  1488. }
  1489. }
  1490. return TRUE;
  1491. }
  1492. //-----------------------------------------------------------------------------
  1493. // buildSkeleton()
  1494. //-----------------------------------------------------------------------------
  1495. BOOL LLVOAvatar::buildSkeleton(const LLVOAvatarSkeletonInfo *info)
  1496. {
  1497. LLMemType mt(LLMemType::MTYPE_AVATAR);
  1498. //-------------------------------------------------------------------------
  1499. // allocate joints
  1500. //-------------------------------------------------------------------------
  1501. if (!allocateCharacterJoints(info->mNumBones))
  1502. {
  1503. llerrs << "Can't allocate " << info->mNumBones << " joints" << llendl;
  1504. return FALSE;
  1505. }
  1506. //-------------------------------------------------------------------------
  1507. // allocate volumes
  1508. //-------------------------------------------------------------------------
  1509. if (info->mNumCollisionVolumes)
  1510. {
  1511. if (!allocateCollisionVolumes(info->mNumCollisionVolumes))
  1512. {
  1513. llerrs << "Can't allocate " << info->mNumCollisionVolumes << " collision volumes" << llendl;
  1514. return FALSE;
  1515. }
  1516. }
  1517. S32 current_joint_num = 0;
  1518. S32 current_volume_num = 0;
  1519. LLVOAvatarSkeletonInfo::bone_info_list_t::const_iterator iter;
  1520. for (iter = info->mBoneInfoList.begin(); iter != info->mBoneInfoList.end(); ++iter)
  1521. {
  1522. LLVOAvatarBoneInfo *info = *iter;
  1523. if (!setupBone(info, NULL, current_volume_num, current_joint_num))
  1524. {
  1525. llerrs << "Error parsing bone in skeleton file" << llendl;
  1526. return FALSE;
  1527. }
  1528. }
  1529. return TRUE;
  1530. }
  1531. LLVOAvatar* LLVOAvatar::asAvatar()
  1532. {
  1533. return this;
  1534. }
  1535. //-----------------------------------------------------------------------------
  1536. // LLVOAvatar::startDefaultMotions()
  1537. //-----------------------------------------------------------------------------
  1538. void LLVOAvatar::startDefaultMotions()
  1539. {
  1540. //-------------------------------------------------------------------------
  1541. // start default motions
  1542. //-------------------------------------------------------------------------
  1543. startMotion( ANIM_AGENT_HEAD_ROT );
  1544. startMotion( ANIM_AGENT_EYE );
  1545. startMotion( ANIM_AGENT_BODY_NOISE );
  1546. startMotion( ANIM_AGENT_BREATHE_ROT );
  1547. startMotion( ANIM_AGENT_PHYSICS_MOTION );
  1548. startMotion( ANIM_AGENT_HAND_MOTION );
  1549. startMotion( ANIM_AGENT_PELVIS_FIX );
  1550. //-------------------------------------------------------------------------
  1551. // restart any currently active motions
  1552. //-------------------------------------------------------------------------
  1553. processAnimationStateChanges();
  1554. }
  1555. //-----------------------------------------------------------------------------
  1556. // LLVOAvatar::buildCharacter()
  1557. // Deferred initialization and rebuild of the avatar.
  1558. //-----------------------------------------------------------------------------
  1559. void LLVOAvatar::buildCharacter()
  1560. {
  1561. LLMemType mt(LLMemType::MTYPE_AVATAR);
  1562. //-------------------------------------------------------------------------
  1563. // remove all references to our existing skeleton
  1564. // so we can rebuild it
  1565. //-------------------------------------------------------------------------
  1566. flushAllMotions();
  1567. //-------------------------------------------------------------------------
  1568. // remove all of mRoot's children
  1569. //-------------------------------------------------------------------------
  1570. mRoot.removeAllChildren();
  1571. mIsBuilt = FALSE;
  1572. //-------------------------------------------------------------------------
  1573. // clear mesh data
  1574. //-------------------------------------------------------------------------
  1575. for (std::vector<LLViewerJoint*>::iterator jointIter = mMeshLOD.begin();
  1576. jointIter != mMeshLOD.end(); ++jointIter)
  1577. {
  1578. LLViewerJoint* joint = (LLViewerJoint*) *jointIter;
  1579. for (std::vector<LLViewerJointMesh*>::iterator meshIter = joint->mMeshParts.begin();
  1580. meshIter != joint->mMeshParts.end(); ++meshIter)
  1581. {
  1582. LLViewerJointMesh * mesh = (LLViewerJointMesh *) *meshIter;
  1583. mesh->setMesh(NULL);
  1584. }
  1585. }
  1586. //-------------------------------------------------------------------------
  1587. // (re)load our skeleton and meshes
  1588. //-------------------------------------------------------------------------
  1589. LLTimer timer;
  1590. BOOL status = loadAvatar();
  1591. stop_glerror();
  1592. // gPrintMessagesThisFrame = TRUE;
  1593. lldebugs << "Avatar load took " << timer.getElapsedTimeF32() << " seconds." << llendl;
  1594. if (!status)
  1595. {
  1596. if (isSelf())
  1597. {
  1598. llerrs << "Unable to load user's avatar" << llendl;
  1599. }
  1600. else
  1601. {
  1602. llwarns << "Unable to load other's avatar" << llendl;
  1603. }
  1604. return;
  1605. }
  1606. //-------------------------------------------------------------------------
  1607. // initialize "well known" joint pointers
  1608. //-------------------------------------------------------------------------
  1609. mPelvisp = (LLViewerJoint*)mRoot.findJoint("mPelvis");
  1610. mTorsop = (LLViewerJoint*)mRoot.findJoint("mTorso");
  1611. mChestp = (LLViewerJoint*)mRoot.findJoint("mChest");
  1612. mNeckp = (LLViewerJoint*)mRoot.findJoint("mNeck");
  1613. mHeadp = (LLViewerJoint*)mRoot.findJoint("mHead");
  1614. mSkullp = (LLViewerJoint*)mRoot.findJoint("mSkull");
  1615. mHipLeftp = (LLViewerJoint*)mRoot.findJoint("mHipLeft");
  1616. mHipRightp = (LLViewerJoint*)mRoot.findJoint("mHipRight");
  1617. mKneeLeftp = (LLViewerJoint*)mRoot.findJoint("mKneeLeft");
  1618. mKneeRightp = (LLViewerJoint*)mRoot.findJoint("mKneeRight");
  1619. mAnkleLeftp = (LLViewerJoint*)mRoot.findJoint("mAnkleLeft");
  1620. mAnkleRightp = (LLViewerJoint*)mRoot.findJoint("mAnkleRight");
  1621. mFootLeftp = (LLViewerJoint*)mRoot.findJoint("mFootLeft");
  1622. mFootRightp = (LLViewerJoint*)mRoot.findJoint("mFootRight");
  1623. mWristLeftp = (LLViewerJoint*)mRoot.findJoint("mWristLeft");
  1624. mWristRightp = (LLViewerJoint*)mRoot.findJoint("mWristRight");
  1625. mEyeLeftp = (LLViewerJoint*)mRoot.findJoint("mEyeLeft");
  1626. mEyeRightp = (LLViewerJoint*)mRoot.findJoint("mEyeRight");
  1627. //-------------------------------------------------------------------------
  1628. // Make sure "well known" pointers exist
  1629. //-------------------------------------------------------------------------
  1630. if (!(mPelvisp &&
  1631. mTorsop &&
  1632. mChestp &&
  1633. mNeckp &&
  1634. mHeadp &&
  1635. mSkullp &&
  1636. mHipLeftp &&
  1637. mHipRightp &&
  1638. mKneeLeftp &&
  1639. mKneeRightp &&
  1640. mAnkleLeftp &&
  1641. mAnkleRightp &&
  1642. mFootLeftp &&
  1643. mFootRightp &&
  1644. mWristLeftp &&
  1645. mWristRightp &&
  1646. mEyeLeftp &&
  1647. mEyeRightp))
  1648. {
  1649. llerrs << "Failed to create avatar." << llendl;
  1650. return;
  1651. }
  1652. //-------------------------------------------------------------------------
  1653. // initialize the pelvis
  1654. //-------------------------------------------------------------------------
  1655. mPelvisp->setPosition( LLVector3(0.0f, 0.0f, 0.0f) );
  1656. //-------------------------------------------------------------------------
  1657. // set head offset from pelvis
  1658. //-------------------------------------------------------------------------
  1659. updateHeadOffset();
  1660. //-------------------------------------------------------------------------
  1661. // initialize lip sync morph pointers
  1662. //-------------------------------------------------------------------------
  1663. mOohMorph = getVisualParam( "Lipsync_Ooh" );
  1664. mAahMorph = getVisualParam( "Lipsync_Aah" );
  1665. // If we don't have the Ooh morph, use the Kiss morph
  1666. if (!mOohMorph)
  1667. {
  1668. llwarns << "Missing 'Ooh' morph for lipsync, using fallback." << llendl;
  1669. mOohMorph = getVisualParam( "Express_Kiss" );
  1670. }
  1671. // If we don't have the Aah morph, use the Open Mouth morph
  1672. if (!mAahMorph)
  1673. {
  1674. llwarns << "Missing 'Aah' morph for lipsync, using fallback." << llendl;
  1675. mAahMorph = getVisualParam( "Express_Open_Mouth" );
  1676. }
  1677. startDefaultMotions();
  1678. //-------------------------------------------------------------------------
  1679. // restart any currently active motions
  1680. //-------------------------------------------------------------------------
  1681. processAnimationStateChanges();
  1682. mIsBuilt = TRUE;
  1683. stop_glerror();
  1684. mMeshValid = TRUE;
  1685. }
  1686. //-----------------------------------------------------------------------------
  1687. // releaseMeshData()
  1688. //-----------------------------------------------------------------------------
  1689. void LLVOAvatar::releaseMeshData()
  1690. {
  1691. LLMemType mt(LLMemType::MTYPE_AVATAR);
  1692. if (sInstances.size() < AVATAR_RELEASE_THRESHOLD || mIsDummy)
  1693. {
  1694. return;
  1695. }
  1696. //llinfos << "Releasing" << llendl;
  1697. // cleanup mesh data
  1698. for (std::vector<LLViewerJoint*>::iterator iter = mMeshLOD.begin();
  1699. iter != mMeshLOD.end();
  1700. ++iter)
  1701. {
  1702. LLViewerJoint* joint = (LLViewerJoint*) *iter;
  1703. joint->setValid(FALSE, TRUE);
  1704. }
  1705. //cleanup data
  1706. if (mDrawable.notNull())
  1707. {
  1708. LLFace* facep = mDrawable->getFace(0);
  1709. facep->setSize(0, 0);
  1710. for(S32 i = mNumInitFaces ; i < mDrawable->getNumFaces(); i++)
  1711. {
  1712. facep = mDrawable->getFace(i);
  1713. facep->setSize(0, 0);
  1714. }
  1715. }
  1716. for (attachment_map_t::iterator iter = mAttachmentPoints.begin();
  1717. iter != mAttachmentPoints.end();
  1718. ++iter)
  1719. {
  1720. LLViewerJointAttachment* attachment = iter->second;
  1721. if (!attachment->getIsHUDAttachment())
  1722. {
  1723. attachment->setAttachmentVisibility(FALSE);
  1724. }
  1725. }
  1726. mMeshValid = FALSE;
  1727. }
  1728. //-----------------------------------------------------------------------------
  1729. // restoreMeshData()
  1730. //-----------------------------------------------------------------------------
  1731. // virtual
  1732. void LLVOAvatar::restoreMeshData()
  1733. {
  1734. llassert(!isSelf());
  1735. LLMemType mt(LLMemType::MTYPE_AVATAR);
  1736. //llinfos << "Restoring" << llendl;
  1737. mMeshValid = TRUE;
  1738. updateJointLODs();
  1739. for (attachment_map_t::iterator iter = mAttachmentPoints.begin();
  1740. iter != mAttachmentPoints.end();
  1741. ++iter)
  1742. {
  1743. LLViewerJointAttachment* attachment = iter->second;
  1744. if (!attachment->getIsHUDAttachment())
  1745. {
  1746. attachment->setAttachmentVisibility(TRUE);
  1747. }
  1748. }
  1749. // force mesh update as LOD might not have changed to trigger this
  1750. gPipeline.markRebuild(mDrawable, LLDrawable::REBUILD_GEOMETRY, TRUE);
  1751. }
  1752. //-----------------------------------------------------------------------------
  1753. // updateMeshData()
  1754. //-----------------------------------------------------------------------------
  1755. void LLVOAvatar::updateMeshData()
  1756. {
  1757. if (mDrawable.notNull())
  1758. {
  1759. stop_glerror();
  1760. S32 f_num = 0 ;
  1761. const U32 VERTEX_NUMBER_THRESHOLD = 128 ;//small number of this means each part of an avatar has its own vertex buffer.
  1762. const S32 num_parts = mMeshLOD.size();
  1763. // this order is determined by number of LODS
  1764. // if a mesh earlier in this list changed LODs while a later mesh doesn't,
  1765. // the later mesh's index offset will be inaccurate
  1766. for(S32 part_index = 0 ; part_index < num_parts ;)
  1767. {
  1768. S32 j = part_index ;
  1769. U32 last_v_num = 0, num_vertices = 0 ;
  1770. U32 last_i_num = 0, num_indices = 0 ;
  1771. while(part_index < num_parts && num_vertices < VERTEX_NUMBER_THRESHOLD)
  1772. {
  1773. last_v_num = num_vertices ;
  1774. last_i_num = num_indices ;
  1775. mMeshLOD[part_index++]->updateFaceSizes(num_vertices, num_indices, mAdjustedPixelArea);
  1776. }
  1777. if(num_vertices < 1)//skip empty meshes
  1778. {
  1779. continue ;
  1780. }
  1781. if(last_v_num > 0)//put the last inserted part into next vertex buffer.
  1782. {
  1783. num_vertices = last_v_num ;
  1784. num_indices = last_i_num ;
  1785. part_index-- ;
  1786. }
  1787. LLFace* facep ;
  1788. if(f_num < mDrawable->getNumFaces())
  1789. {
  1790. facep = mDrawable->getFace(f_num);
  1791. }
  1792. else
  1793. {
  1794. facep = mDrawable->addFace(mDrawable->getFace(0)->getPool(), mDrawable->getFace(0)->getTexture()) ;
  1795. }
  1796. // resize immediately
  1797. facep->setSize(num_vertices, num_indices);
  1798. bool terse_update = false;
  1799. facep->setGeomIndex(0);
  1800. facep->setIndicesIndex(0);
  1801. LLVertexBuffer* buff = facep->getVertexBuffer();
  1802. if(!facep->getVertexBuffer())
  1803. {
  1804. buff = new LLVertexBufferAvatar();
  1805. buff->allocateBuffer(num_vertices, num_indices, TRUE);
  1806. facep->se