llcharacter.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. * @file llcharacter.h
  3. * @brief Implementation of LLCharacter class.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLCHARACTER_H
  33. #define LL_LLCHARACTER_H
  34. #include <string>
  35. #include "boost/container/flat_map.hpp"
  36. #include "lljoint.h"
  37. #include "llmotioncontroller.h"
  38. #include "llpointer.h"
  39. #include "llstringtable.h"
  40. #include "llthread.h"
  41. #include "llvisualparam.h"
  42. class LLPolyMesh;
  43. class LLPauseRequestHandle : public LLThreadSafeRefCount
  44. {
  45. public:
  46. LLPauseRequestHandle() = default;
  47. };
  48. typedef LLPointer<LLPauseRequestHandle> LLAnimPauseRequest;
  49. class LLCharacter
  50. {
  51. protected:
  52. LOG_CLASS(LLCharacter);
  53. public:
  54. LLCharacter();
  55. virtual ~LLCharacter();
  56. //-------------------------------------------------------------------------
  57. // LLCharacter Interface
  58. // These functions must be implemented by subclasses.
  59. //-------------------------------------------------------------------------
  60. // Gets the prefix to be used to lookup motion data files
  61. // from the viewer data directory
  62. virtual const char* getAnimationPrefix() = 0;
  63. // Gets the root joint of the character
  64. virtual LLJoint* getRootJoint() = 0;
  65. // Gets the specified joint. The default implementation does recursive
  66. // search, subclasses may optimize/cache results.
  67. virtual LLJoint* getJoint(U32 key);
  68. // Gets the position of the character
  69. virtual LLVector3 getCharacterPosition() = 0;
  70. // Gets the rotation of the character
  71. virtual LLQuaternion getCharacterRotation() = 0;
  72. // Gets the velocity of the character
  73. virtual LLVector3 getCharacterVelocity() = 0;
  74. // Gets the angular velocity of the character
  75. virtual LLVector3 getCharacterAngularVelocity() = 0;
  76. // Gets the height & normal of the ground under a point
  77. virtual void getGround(const LLVector3& inPos, LLVector3& out_pos,
  78. LLVector3& out_norm) = 0;
  79. // Skeleton joint accessor to support joint subclasses
  80. virtual LLJoint* getCharacterJoint(U32 i) = 0;
  81. // Gets the physics time dilation for the simulator
  82. virtual F32 getTimeDilation() = 0;
  83. // Gets current pixel area of this character
  84. virtual F32 getPixelArea() const = 0;
  85. // Gets the head mesh of the character
  86. virtual LLPolyMesh* getHeadMesh() = 0;
  87. // Gets the upper body mesh of the character
  88. virtual LLPolyMesh* getUpperBodyMesh() = 0;
  89. // Gets 'which' mesh for the character
  90. virtual LLPolyMesh* getMesh(S32 which) = 0;
  91. // Gets global coordinates from agent local coordinates
  92. virtual LLVector3d getPosGlobalFromAgent(const LLVector3& position) = 0;
  93. // Gets agent local coordinates from global coordinates
  94. virtual LLVector3 getPosAgentFromGlobal(const LLVector3d& position) = 0;
  95. // Updates all visual parameters for this character
  96. virtual void updateVisualParams();
  97. virtual void addDebugText(const std::string& text) = 0;
  98. virtual const LLUUID& getID() = 0;
  99. //-------------------------------------------------------------------------
  100. // End Interface
  101. //-------------------------------------------------------------------------
  102. // Registers a motion with the character. returns true if successful.
  103. LL_INLINE bool registerMotion(const LLUUID& id,
  104. LLMotionConstructor create) { return mMotionController.registerMotion(id, create); }
  105. LL_INLINE void removeMotion(const LLUUID& id) { mMotionController.removeMotion(id); }
  106. // Returns an instance of a registered motion, creating one if necessary
  107. // NOTE: always assign the result to a LLPointer !
  108. LLMotion* createMotion(const LLUUID& id) { return mMotionController.createMotion(id); }
  109. // Returns an existing instance of a registered motion
  110. LL_INLINE LLMotion* findMotion(const LLUUID& id) { return mMotionController.findMotion(id); }
  111. // Start a motion. Returns true if successful, false if an error occurred.
  112. LL_INLINE virtual bool startMotion(const LLUUID& id,
  113. F32 start_offset = 0.f) { return mMotionController.startMotion(id, start_offset); }
  114. // Stop a motion
  115. LL_INLINE virtual bool stopMotion(const LLUUID& id,
  116. bool immediatly = false) { return mMotionController.stopMotionLocally(id, immediatly); }
  117. // Is this motion active ?
  118. bool isMotionActive(const LLUUID& id);
  119. // Event handler for motion deactivation. Called when a motion has
  120. // completely stopped and has been deactivated. Subclasses may optionally
  121. // override this.
  122. LL_INLINE virtual void requestStopMotion(LLMotion* motion) {}
  123. // Periodic update function, steps the motion controller
  124. enum e_update_t { NORMAL_UPDATE, HIDDEN_UPDATE, FORCE_UPDATE };
  125. virtual void updateMotions(e_update_t update_type);
  126. LLAnimPauseRequest requestPause();
  127. LL_INLINE bool areAnimationsPaused() const { return mMotionController.isPaused(); }
  128. LL_INLINE void setAnimTimeFactorMultiplier(F32 factor) { mMotionController.mTimeFactorMultiplier = factor; }
  129. LL_INLINE F32 getAnimTimeFactorMultiplier() { return mMotionController.mTimeFactorMultiplier; }
  130. LL_INLINE void setTimeStep(F32 time_step) { mMotionController.setTimeStep(time_step); }
  131. LL_INLINE LLMotionController& getMotionController() { return mMotionController; }
  132. // Releases all motion instances which should result in no cached
  133. // references to character joint data. This is useful if a character
  134. // wants to rebuild its skeleton.
  135. LL_INLINE virtual void deactivateAllMotions() { mMotionController.deactivateAllMotions(); }
  136. // Flush only wipes active animations.
  137. LL_INLINE virtual void flushAllMotions() { mMotionController.flushAllMotions(); }
  138. // Dumps information for debugging
  139. virtual void dumpCharacter(LLJoint* joint = NULL);
  140. LL_INLINE virtual F32 getPreferredPelvisHeight() { return mPreferredPelvisHeight; }
  141. LL_INLINE virtual LLVector3 getVolumePos(S32 joint_index,
  142. LLVector3& offset) { return LLVector3::zero; }
  143. LL_INLINE virtual LLJoint* findCollisionVolume(S32 vol_id) { return NULL; }
  144. LL_INLINE virtual S32 getCollisionVolumeID(std::string& n) { return -1; }
  145. LL_INLINE void setAnimationData(const std::string& name,
  146. void* data) { mAnimationData[name] = data; }
  147. void* getAnimationData(const std::string& name);
  148. LL_INLINE void removeAnimationData(const std::string& name) { mAnimationData.erase(name); }
  149. void addVisualParam(LLVisualParam* param);
  150. void addSharedVisualParam(LLVisualParam* param);
  151. virtual bool setVisualParamWeight(const LLVisualParam* which_param,
  152. F32 weight, bool upload_bake = false);
  153. virtual bool setVisualParamWeight(const char* param_name, F32 weight,
  154. bool upload_bake = false);
  155. virtual bool setVisualParamWeight(S32 index, F32 weight,
  156. bool upload_bake = false);
  157. // Gets visual param weight by param or name
  158. F32 getVisualParamWeight(LLVisualParam* distortion);
  159. F32 getVisualParamWeight(const char* param_name);
  160. F32 getVisualParamWeight(S32 index);
  161. // set all morph weights to defaults
  162. void clearVisualParamWeights();
  163. // Visual parameter accessors
  164. LL_INLINE LLVisualParam* getFirstVisualParam()
  165. {
  166. mCurIterator = mVisualParamIndexMap.begin();
  167. return getNextVisualParam();
  168. }
  169. LL_INLINE LLVisualParam* getNextVisualParam()
  170. {
  171. return mCurIterator == mVisualParamIndexMap.end() ? NULL
  172. : (mCurIterator++)->second;
  173. }
  174. LL_INLINE LLVisualParam* getVisualParam(S32 id) const
  175. {
  176. visual_param_index_map_t::const_iterator iter = mVisualParamIndexMap.find(id);
  177. return iter == mVisualParamIndexMap.end() ? NULL : iter->second;
  178. }
  179. S32 getVisualParamCountInGroup(const EVisualParamGroup group) const;
  180. S32 getVisualParamID(LLVisualParam* id);
  181. LL_INLINE S32 getVisualParamCount() const { return (S32)mVisualParamIndexMap.size(); }
  182. LLVisualParam* getVisualParam(const char* name);
  183. LL_INLINE ESex getSex() const { return mSex; }
  184. LL_INLINE void setSex(ESex sex) { mSex = sex; }
  185. LL_INLINE U32 getAppearanceSerialNum() const { return mAppearanceSerialNum; }
  186. LL_INLINE void setAppearanceSerialNum(U32 num) { mAppearanceSerialNum = num; }
  187. LL_INLINE U32 getSkeletonSerialNum() const { return mSkeletonSerialNum; }
  188. LL_INLINE void bumpSkeletonSerialNum() { ++mSkeletonSerialNum; }
  189. static void initClass();
  190. static void dumpStats();
  191. LL_INLINE virtual void setHoverOffset(const LLVector3& hover_offset,
  192. bool send_update = true)
  193. {
  194. mHoverOffset = hover_offset;
  195. }
  196. LL_INLINE const LLVector3& getHoverOffset() const { return mHoverOffset; }
  197. public:
  198. static std::vector<LLCharacter*> sInstances;
  199. protected:
  200. LLMotionController mMotionController;
  201. typedef std::map<std::string, void*> animation_data_map_t;
  202. animation_data_map_t mAnimationData;
  203. LLAnimPauseRequest mPauseRequest;
  204. ESex mSex;
  205. U32 mAppearanceSerialNum;
  206. U32 mSkeletonSerialNum;
  207. F32 mPreferredPelvisHeight;
  208. private:
  209. LLVector3 mHoverOffset;
  210. // Visual parameters stuff
  211. // NOTE: do not replace with flat_hmap: this would not work porperly since
  212. // the visual parameters must stay ordered in the map... HB
  213. typedef boost::container::flat_map<S32,
  214. LLVisualParam*> visual_param_index_map_t;
  215. typedef visual_param_index_map_t::iterator visual_param_index_map_it_t;
  216. typedef boost::container::flat_map<char*,
  217. LLVisualParam*> visual_param_name_map_t;
  218. visual_param_index_map_it_t mCurIterator;
  219. visual_param_index_map_t mVisualParamIndexMap;
  220. visual_param_name_map_t mVisualParamNameMap;
  221. // Used to store replaced visual parameters that may still be referenced by
  222. // worn wearables (see LLCharacter::addVisualParam()). HB
  223. std::vector<LLVisualParam*> mDeferredDeletions;
  224. static LLStringTable sVisualParamNames;
  225. };
  226. #endif // LL_LLCHARACTER_H