llpolyskeletaldistortion.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * @file llpolyskeletaldistortion.cpp
  3. * @brief Implementation of LLPolySkeletalDistortion classes
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llpolyskeletaldistortion.h"
  33. #include "llavatarappearance.h"
  34. #include "llavatarjoint.h"
  35. #include "llfasttimer.h"
  36. #include "llpolymorph.h"
  37. #include "llpreprocessor.h"
  38. #include "llwearable.h"
  39. //-----------------------------------------------------------------------------
  40. // LLPolySkeletalDistortionInfo() class
  41. //-----------------------------------------------------------------------------
  42. bool LLPolySkeletalDistortionInfo::parseXml(LLXmlTreeNode* node)
  43. {
  44. llassert(node->hasName("param") && node->getChildByName("param_skeleton"));
  45. if (!LLViewerVisualParamInfo::parseXml(node))
  46. {
  47. return false;
  48. }
  49. LLXmlTreeNode* skel_param = node->getChildByName("param_skeleton");
  50. if (!skel_param)
  51. {
  52. llwarns << "Failed to getChildByName(\"param_skeleton\")"
  53. << llendl;
  54. return false;
  55. }
  56. for (LLXmlTreeNode* bone = skel_param->getFirstChild(); bone;
  57. bone = skel_param->getNextChild())
  58. {
  59. if (bone->hasName("bone"))
  60. {
  61. std::string name;
  62. LLVector3 scale;
  63. LLVector3 pos;
  64. bool haspos = false;
  65. static LLStdStringHandle name_string = LLXmlTree::addAttributeString("name");
  66. if (!bone->getFastAttributeString(name_string, name))
  67. {
  68. llwarns << "No bone name specified for skeletal param."
  69. << llendl;
  70. continue;
  71. }
  72. static LLStdStringHandle scale_string = LLXmlTree::addAttributeString("scale");
  73. if (!bone->getFastAttributeVector3(scale_string, scale))
  74. {
  75. llwarns << "No scale specified for bone " << name << "."
  76. << llendl;
  77. continue;
  78. }
  79. // optional offset deformation (translation)
  80. static LLStdStringHandle offset_string = LLXmlTree::addAttributeString("offset");
  81. if (bone->getFastAttributeVector3(offset_string, pos))
  82. {
  83. haspos = true;
  84. }
  85. mBoneInfoList.emplace_back(name, scale, pos, haspos);
  86. }
  87. else
  88. {
  89. llwarns << "Unrecognized element " << bone->getName()
  90. << " in skeletal distortion" << llendl;
  91. continue;
  92. }
  93. }
  94. return true;
  95. }
  96. //-----------------------------------------------------------------------------
  97. // LLPolySkeletalDistortion() class
  98. //-----------------------------------------------------------------------------
  99. LLPolySkeletalDistortion::LLPolySkeletalDistortion(LLAvatarAppearance* avatarp)
  100. : LLViewerVisualParam(),
  101. mDefaultVec(),
  102. mJointScales(),
  103. mJointOffsets(),
  104. mAvatar(avatarp)
  105. {
  106. mDefaultVec.splat(0.001f);
  107. }
  108. LLPolySkeletalDistortion::LLPolySkeletalDistortion(const LLPolySkeletalDistortion& other)
  109. : LLViewerVisualParam(other),
  110. mDefaultVec(other.mDefaultVec),
  111. mJointScales(other.mJointScales),
  112. mJointOffsets(other.mJointOffsets),
  113. mAvatar(other.mAvatar)
  114. {
  115. }
  116. bool LLPolySkeletalDistortion::setInfo(LLPolySkeletalDistortionInfo* info)
  117. {
  118. if (info->mID < 0)
  119. {
  120. return false;
  121. }
  122. mInfo = info;
  123. mID = info->mID;
  124. setWeight(getDefaultWeight(), false);
  125. for (LLPolySkeletalDistortionInfo::bone_info_list_t::iterator
  126. iter = getInfo()->mBoneInfoList.begin(),
  127. end = getInfo()->mBoneInfoList.end();
  128. iter != end; ++iter)
  129. {
  130. LLPolySkeletalBoneInfo* bone_info = &(*iter);
  131. if (!bone_info) continue; // Paranoia
  132. LLJoint* joint = mAvatar->getJoint(bone_info->mJointKey);
  133. if (!joint)
  134. {
  135. // There is no point continuing after this error since it means
  136. // that either the skeleton or lad file is broken.
  137. llwarns << "Joint " << LLJoint::getName(bone_info->mJointKey)
  138. << " not found." << llendl;
  139. return false;
  140. }
  141. // Store it
  142. mJointScales[joint] = bone_info->mScaleDeformation;
  143. // Apply to children that need to inherit it
  144. for (S32 i = 0, count = joint->mChildren.size(); i < count; ++i)
  145. {
  146. LLAvatarJoint* child_joint = joint->mChildren[i]->asAvatarJoint();
  147. if (child_joint && child_joint->inheritScale())
  148. {
  149. LLVector3 deformation(child_joint->getScale());
  150. deformation.scaleVec(bone_info->mScaleDeformation);
  151. mJointScales[child_joint] = deformation;
  152. }
  153. }
  154. if (bone_info->mHasPositionDeformation)
  155. {
  156. mJointOffsets[joint] = bone_info->mPositionDeformation;
  157. }
  158. }
  159. return true;
  160. }
  161. //virtual
  162. LLViewerVisualParam* LLPolySkeletalDistortion::cloneParam(LLWearable* wearable) const
  163. {
  164. return new LLPolySkeletalDistortion(*this);
  165. }
  166. void LLPolySkeletalDistortion::apply(ESex avatar_sex)
  167. {
  168. LL_FAST_TIMER(FTM_POLYSKELETAL_DISTORTION_APPLY);
  169. F32 effective_weight = (getSex() & avatar_sex) ? mCurWeight
  170. : getDefaultWeight();
  171. for (joint_vec_map_t::iterator iter = mJointScales.begin(),
  172. end = mJointScales.end();
  173. iter != end; ++iter)
  174. {
  175. LLJoint* joint = iter->first;
  176. if (!joint) continue; // Paranoia
  177. const LLVector3& scale_delta = iter->second;
  178. LLVector3 new_scale = joint->getScale() +
  179. (effective_weight - mLastWeight) * scale_delta;
  180. joint->setScale(new_scale, true);
  181. }
  182. for (joint_vec_map_t::iterator iter = mJointOffsets.begin(),
  183. end = mJointOffsets.end();
  184. iter != end; ++iter)
  185. {
  186. LLJoint* joint = iter->first;
  187. if (!joint) continue; // Paranoia
  188. const LLVector3& pos_delta = iter->second;
  189. LLVector3 new_pos = joint->getPosition() +
  190. effective_weight * pos_delta -
  191. mLastWeight * pos_delta;
  192. joint->setPosition(new_pos, true);
  193. }
  194. if (mLastWeight != mCurWeight && !mIsAnimating)
  195. {
  196. mAvatar->bumpSkeletonSerialNum();
  197. }
  198. mLastWeight = mCurWeight;
  199. }
  200. //-----------------------------------------------------------------------------
  201. LLPolyMorphData* clone_morph_param_duplicate(const LLPolyMorphData* src_data,
  202. const std::string& name)
  203. {
  204. LLPolyMorphData* cloned_morph_data = new LLPolyMorphData(*src_data);
  205. if (!cloned_morph_data || !cloned_morph_data->isSuccesfullyAllocated())
  206. {
  207. llwarns << "Failure to clone parameter !" << llendl;
  208. if (cloned_morph_data)
  209. {
  210. delete cloned_morph_data;
  211. }
  212. return NULL;
  213. }
  214. cloned_morph_data->mName = name;
  215. for (U32 v = 0, count = cloned_morph_data->mNumIndices; v < count; ++v)
  216. {
  217. cloned_morph_data->mCoords[v] = src_data->mCoords[v];
  218. cloned_morph_data->mNormals[v] = src_data->mNormals[v];
  219. cloned_morph_data->mBinormals[v] = src_data->mBinormals[v];
  220. }
  221. return cloned_morph_data;
  222. }
  223. LLPolyMorphData* clone_morph_param_direction(const LLPolyMorphData* src_data,
  224. const LLVector3& direction,
  225. const std::string& name)
  226. {
  227. LLPolyMorphData* cloned_morph_data = new LLPolyMorphData(*src_data);
  228. if (!cloned_morph_data || !cloned_morph_data->isSuccesfullyAllocated())
  229. {
  230. llwarns << "Failure to clone parameter !" << llendl;
  231. if (cloned_morph_data)
  232. {
  233. delete cloned_morph_data;
  234. }
  235. return NULL;
  236. }
  237. cloned_morph_data->mName = name;
  238. LLVector4a dir;
  239. dir.load3(direction.mV);
  240. for (U32 v = 0, count = cloned_morph_data->mNumIndices; v < count; ++v)
  241. {
  242. cloned_morph_data->mCoords[v] = dir;
  243. cloned_morph_data->mNormals[v].clear();
  244. cloned_morph_data->mBinormals[v].clear();
  245. }
  246. return cloned_morph_data;
  247. }
  248. LLPolyMorphData* clone_morph_param_cleavage(const LLPolyMorphData* src_data,
  249. F32 scale, const std::string& name)
  250. {
  251. LLPolyMorphData* cloned_morph_data = new LLPolyMorphData(*src_data);
  252. if (!cloned_morph_data || !cloned_morph_data->isSuccesfullyAllocated())
  253. {
  254. llwarns << "Failure to clone parameter !" << llendl;
  255. if (cloned_morph_data)
  256. {
  257. delete cloned_morph_data;
  258. }
  259. return NULL;
  260. }
  261. cloned_morph_data->mName = name;
  262. LLVector4a sc;
  263. sc.splat(scale);
  264. LLVector4a nsc;
  265. nsc.set(scale, -scale, scale, scale);
  266. for (U32 v = 0, count = cloned_morph_data->mNumIndices; v < count; ++v)
  267. {
  268. if (cloned_morph_data->mCoords[v][1] < 0)
  269. {
  270. cloned_morph_data->mCoords[v].setMul(src_data->mCoords[v], nsc);
  271. cloned_morph_data->mNormals[v].setMul(src_data->mNormals[v], nsc);
  272. cloned_morph_data->mBinormals[v].setMul(src_data->mBinormals[v], nsc);
  273. }
  274. else
  275. {
  276. cloned_morph_data->mCoords[v].setMul(src_data->mCoords[v], sc);
  277. cloned_morph_data->mNormals[v].setMul(src_data->mNormals[v], sc);
  278. cloned_morph_data->mBinormals[v].setMul(src_data->mBinormals[v], sc);
  279. }
  280. }
  281. return cloned_morph_data;
  282. }