llavatarjointmesh.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /**
  2. * @file LLAvatarJointMesh.cpp
  3. * @brief Implementation of LLAvatarJointMesh 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. #include "linden_common.h"
  33. #include "llavatarjointmesh.h"
  34. #include "lltexlayer.h"
  35. #include "llmath.h"
  36. #include "llmatrix4a.h"
  37. #include "llrender.h"
  38. #include "llmatrix3.h"
  39. #include "llmatrix4.h"
  40. #include "llvector4.h"
  41. //-----------------------------------------------------------------------------
  42. // LLSkinJoint class
  43. //-----------------------------------------------------------------------------
  44. LLSkinJoint::LLSkinJoint()
  45. {
  46. mJoint = NULL;
  47. }
  48. LLSkinJoint::~LLSkinJoint()
  49. {
  50. mJoint = NULL;
  51. }
  52. //static
  53. LLAvatarJoint* LLSkinJoint::getBaseSkeletonAncestor(LLAvatarJoint* joint)
  54. {
  55. LLJoint* ancestor = joint->getParent();
  56. while (ancestor && ancestor->getParent() &&
  57. ancestor->getSupport() != LLJoint::SUPPORT_BASE)
  58. {
  59. LL_DEBUGS("Avatar") << "skipping non-base ancestor: "
  60. << ancestor->getName() << LL_ENDL;
  61. ancestor = ancestor->getParent();
  62. }
  63. return (LLAvatarJoint*)ancestor;
  64. }
  65. //static
  66. LLVector3 LLSkinJoint::totalSkinOffset(LLAvatarJoint* joint)
  67. {
  68. LLVector3 total_offset;
  69. while (joint)
  70. {
  71. if (joint->getSupport() == LLJoint::SUPPORT_BASE)
  72. {
  73. total_offset += joint->getSkinOffset();
  74. }
  75. joint = (LLAvatarJoint*)joint->getParent();
  76. }
  77. return total_offset;
  78. }
  79. bool LLSkinJoint::setupSkinJoint(LLAvatarJoint* joint)
  80. {
  81. mRootToJointSkinOffset.clear();
  82. // find the named joint
  83. mJoint = joint;
  84. if (!mJoint)
  85. {
  86. mRootToParentJointSkinOffset.clear();
  87. llwarns << "NULL joint !" << llendl;
  88. return false;
  89. }
  90. // compute the inverse root skin matrix
  91. mRootToJointSkinOffset = -totalSkinOffset(joint);
  92. mRootToParentJointSkinOffset = -totalSkinOffset(getBaseSkeletonAncestor(joint));
  93. return true;
  94. }
  95. //-----------------------------------------------------------------------------
  96. // LLAvatarJointMesh class
  97. //-----------------------------------------------------------------------------
  98. U32 LLAvatarJointMesh::sClothingMaskImageName = 0;
  99. LLColor4 LLAvatarJointMesh::sClothingInnerColor;
  100. LLAvatarJointMesh::LLAvatarJointMesh()
  101. : mTexture(NULL),
  102. mLayerSet(NULL),
  103. mTestImageName(0),
  104. mFaceIndexCount(0),
  105. mColor(LLColor4(1.f, 1.f, 1.f, 1.f)),
  106. mShiny(0.f),
  107. #if 0
  108. mCullBackFaces(true),
  109. #endif
  110. mFace(NULL),
  111. mMesh(NULL),
  112. mMeshID(0),
  113. mSkinJoints(NULL),
  114. mNumSkinJoints(0)
  115. {
  116. mUpdateXform = false;
  117. mValid = false;
  118. mIsTransparent = false;
  119. }
  120. LLAvatarJointMesh::~LLAvatarJointMesh()
  121. {
  122. mMesh = NULL;
  123. mTexture = NULL;
  124. freeSkinData();
  125. }
  126. bool LLAvatarJointMesh::allocateSkinData(U32 numSkinJoints)
  127. {
  128. mSkinJoints = new LLSkinJoint[numSkinJoints];
  129. mNumSkinJoints = numSkinJoints;
  130. return true;
  131. }
  132. void LLAvatarJointMesh::freeSkinData()
  133. {
  134. mNumSkinJoints = 0;
  135. if (mSkinJoints)
  136. {
  137. delete[] mSkinJoints;
  138. mSkinJoints = NULL;
  139. }
  140. }
  141. void LLAvatarJointMesh::getColor(F32* red, F32* green, F32* blue, F32* alpha)
  142. {
  143. *red = mColor[0];
  144. *green = mColor[1];
  145. *blue = mColor[2];
  146. *alpha = mColor[3];
  147. }
  148. void LLAvatarJointMesh::setColor(F32 red, F32 green, F32 blue, F32 alpha)
  149. {
  150. mColor[0] = red;
  151. mColor[1] = green;
  152. mColor[2] = blue;
  153. mColor[3] = alpha;
  154. }
  155. void LLAvatarJointMesh::setColor(const LLColor4& color)
  156. {
  157. mColor = color;
  158. }
  159. void LLAvatarJointMesh::setTexture(LLGLTexture* texture)
  160. {
  161. mTexture = texture;
  162. // texture and dynamic_texture are mutually exclusive
  163. if (texture)
  164. {
  165. mLayerSet = NULL;
  166. #if 0
  167. texture->bindTexture(0);
  168. texture->setClamp(true, true);
  169. #endif
  170. }
  171. }
  172. bool LLAvatarJointMesh::hasGLTexture() const
  173. {
  174. return mTexture.notNull() && mTexture->hasGLTexture();
  175. }
  176. // Sets the shape texture (takes precedence over normal texture)
  177. void LLAvatarJointMesh::setLayerSet(LLTexLayerSet* layer_set)
  178. {
  179. mLayerSet = layer_set;
  180. // texture and dynamic_texture are mutually exclusive
  181. if (layer_set)
  182. {
  183. mTexture = NULL;
  184. }
  185. }
  186. bool LLAvatarJointMesh::hasComposite() const
  187. {
  188. return mLayerSet && mLayerSet->hasComposite();
  189. }
  190. void LLAvatarJointMesh::setMesh(LLPolyMesh* mesh)
  191. {
  192. // Set the mesh pointer
  193. mMesh = mesh;
  194. // Release any existing skin joints
  195. freeSkinData();
  196. if (!mMesh)
  197. {
  198. return;
  199. }
  200. // Acquire the transform from the mesh object
  201. setPosition(mMesh->getPosition());
  202. setRotation(mMesh->getRotation());
  203. setScale(mMesh->getScale());
  204. // Create skin joints if necessary
  205. if (mMesh->hasWeights() && !mMesh->isLOD())
  206. {
  207. U32 num_joint_names = mMesh->getNumJointNames();
  208. allocateSkinData(num_joint_names);
  209. std::string* joint_names = mMesh->getJointNames();
  210. for (U32 i = 0; i < num_joint_names; ++i)
  211. {
  212. const std::string& name = joint_names[i];
  213. LLJoint* jointp = getRoot()->findAliasedJoint(name);
  214. if (jointp)
  215. {
  216. LLAvatarJoint* avjointp = jointp->asAvatarJoint();
  217. if (avjointp)
  218. {
  219. mSkinJoints[i].setupSkinJoint(avjointp);
  220. continue;
  221. }
  222. }
  223. llwarns << "Root joint for '" << name
  224. << "' is not an avatar joint !" << llendl;
  225. }
  226. }
  227. // Setup joint array
  228. if (!mMesh->isLOD())
  229. {
  230. LL_DEBUGS("Avatar") << getName() << " joint render entries: "
  231. << mMesh->mJointRenderData.size() << LL_ENDL;
  232. LLAvatarJoint* avjointp = getRoot()->asAvatarJoint();
  233. if (avjointp)
  234. {
  235. setupJoint(avjointp);
  236. }
  237. else
  238. {
  239. llwarns << "Root joint is not an avatar joint !" << llendl;
  240. }
  241. }
  242. }
  243. void LLAvatarJointMesh::setupJoint(LLAvatarJoint* current_joint)
  244. {
  245. for (U32 sj = 0; sj < mNumSkinJoints; ++sj)
  246. {
  247. LLSkinJoint& js = mSkinJoints[sj];
  248. if (js.mJoint != current_joint)
  249. {
  250. continue;
  251. }
  252. // We have found a skinjoint for this joint...
  253. LL_DEBUGS("Avatar") << "Mesh: " << getName() << " joint "
  254. << current_joint->getName()
  255. << " matches skinjoint " << sj << LL_ENDL;
  256. // Is the last joint in the array our parent ?
  257. // SL-287: we need to update this so that the results are the same if
  258. // additional extended-skeleton joints lay between this joint and the
  259. // original parent.
  260. LLJoint* ancestor = LLSkinJoint::getBaseSkeletonAncestor(current_joint);
  261. if (!ancestor)
  262. {
  263. llwarns << "cannot find an ancestor joint for: "
  264. << current_joint->getName() << ". Aborted." << llendl;
  265. continue;
  266. }
  267. if (!mMesh)
  268. {
  269. llwarns << "mMesh is NULL for joint: " << current_joint->getName()
  270. << ". Aborted." << llendl;
  271. continue;
  272. }
  273. std::vector<LLJointRenderData*>& jrd = mMesh->mJointRenderData;
  274. if (jrd.size() &&
  275. jrd.back()->mWorldMatrix == &ancestor->getWorldMatrix())
  276. {
  277. // ...then just add ourselves
  278. LL_DEBUGS("Avatar") << "adding joint #" << jrd.size() << ": "
  279. << js.mJoint->getName() << LL_ENDL;
  280. LLJoint* jointp = js.mJoint;
  281. jrd.push_back(new LLJointRenderData(&jointp->getWorldMatrix(),
  282. &js));
  283. }
  284. else
  285. {
  286. // ...otherwise add our parent and ourselves
  287. LL_DEBUGS("Avatar") << "adding ancestor joint #" << jrd.size()
  288. << ": " << ancestor->getName() << LL_ENDL;
  289. jrd.push_back(new LLJointRenderData(&ancestor->getWorldMatrix(),
  290. NULL));
  291. LL_DEBUGS("Avatar") << "adding joint #" << jrd.size() << ": "
  292. << current_joint->getName() << LL_ENDL;
  293. jrd.push_back(new LLJointRenderData(&current_joint->getWorldMatrix(),
  294. &js));
  295. }
  296. }
  297. // Depth-first traversal
  298. for (S32 i = 0, count = current_joint->mChildren.size(); i < count; ++i)
  299. {
  300. LLJoint* jointp = current_joint->mChildren[i];
  301. if (jointp) // Paranoia
  302. {
  303. LLAvatarJoint* avjointp = jointp->asAvatarJoint();
  304. if (avjointp)
  305. {
  306. setupJoint(avjointp);
  307. }
  308. }
  309. }
  310. }