llpolymorph.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. /**
  2. * @file llpolymorph.cpp
  3. * @brief Implementation of LLPolyMesh 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 "llpolymorph.h"
  34. #include "llavatarappearance.h"
  35. #include "llavatarjoint.h"
  36. #include "lldate.h"
  37. #include "llendianswizzle.h"
  38. #include "llfasttimer.h"
  39. #include "llpolymesh.h"
  40. #include "llvolume.h"
  41. #include "llwearable.h"
  42. #include "llxmltree.h"
  43. constexpr F32 NORMAL_SOFTEN_FACTOR = 0.65f;
  44. //-----------------------------------------------------------------------------
  45. // LLPolyMorphData() class
  46. //-----------------------------------------------------------------------------
  47. LLPolyMorphData::LLPolyMorphData(const char* morph_name)
  48. : mName(morph_name),
  49. mNumIndices(0),
  50. mCurrentIndex(0),
  51. mTotalDistortion(0.f),
  52. mMaxDistortion(0.f),
  53. mVertexIndices(NULL),
  54. mCoords(NULL),
  55. mNormals(NULL),
  56. mBinormals(NULL),
  57. mTexCoords(NULL),
  58. mMesh(NULL),
  59. mSuccessfullyAllocated(true)
  60. {
  61. mAvgDistortion.clear();
  62. }
  63. LLPolyMorphData::LLPolyMorphData(const LLPolyMorphData& rhs)
  64. : mName(rhs.mName),
  65. mNumIndices(rhs.mNumIndices),
  66. mCurrentIndex(0),
  67. mTotalDistortion(rhs.mTotalDistortion),
  68. mAvgDistortion(rhs.mAvgDistortion),
  69. mMaxDistortion(rhs.mMaxDistortion),
  70. mVertexIndices(NULL),
  71. mCoords(NULL),
  72. mNormals(NULL),
  73. mBinormals(NULL),
  74. mTexCoords(NULL),
  75. mMesh(NULL),
  76. mSuccessfullyAllocated(false)
  77. {
  78. const S32 num_verts = mNumIndices;
  79. S32 vert_size = sizeof(LLVector4a) * num_verts;
  80. mCoords = (LLVector4a*)allocate_volume_mem(vert_size);
  81. if (!mCoords)
  82. {
  83. freeData();
  84. return;
  85. }
  86. mNormals = (LLVector4a*)allocate_volume_mem(vert_size);
  87. if (!mNormals)
  88. {
  89. freeData();
  90. return;
  91. }
  92. mBinormals = (LLVector4a*)allocate_volume_mem(vert_size);
  93. if (!mBinormals)
  94. {
  95. freeData();
  96. return;
  97. }
  98. S32 tex_size = (num_verts * sizeof(LLVector2) + 0xF) & ~0xF;
  99. mTexCoords = (LLVector2*)allocate_volume_mem(tex_size);
  100. if (!mTexCoords)
  101. {
  102. freeData();
  103. return;
  104. }
  105. mVertexIndices = (U32*)allocate_volume_mem(num_verts * sizeof(U32));
  106. if (!mVertexIndices)
  107. {
  108. freeData();
  109. return;
  110. }
  111. mSuccessfullyAllocated = true;
  112. for (S32 v = 0; v < num_verts; ++v)
  113. {
  114. mCoords[v] = rhs.mCoords[v];
  115. mNormals[v] = rhs.mNormals[v];
  116. mBinormals[v] = rhs.mBinormals[v];
  117. mTexCoords[v] = rhs.mTexCoords[v];
  118. mVertexIndices[v] = rhs.mVertexIndices[v];
  119. }
  120. }
  121. LLPolyMorphData::~LLPolyMorphData()
  122. {
  123. freeData();
  124. }
  125. bool LLPolyMorphData::loadBinary(LLFILE* fp, LLPolyMeshSharedData* mesh)
  126. {
  127. S32 num_verts;
  128. S32 num_read = fread(&num_verts, sizeof(S32), 1, fp);
  129. llendianswizzle(&num_verts, sizeof(S32), 1);
  130. if (num_read != 1)
  131. {
  132. llwarns << "Cannot read number of morph target vertices" << llendl;
  133. return false;
  134. }
  135. //-------------------------------------------------------------------------
  136. // Free any existing data
  137. //-------------------------------------------------------------------------
  138. freeData();
  139. //-------------------------------------------------------------------------
  140. // Allocate vertices
  141. //-------------------------------------------------------------------------
  142. S32 vert_size = sizeof(LLVector4a) * num_verts;
  143. mCoords = (LLVector4a*)allocate_volume_mem(vert_size);
  144. if (!mCoords)
  145. {
  146. freeData();
  147. return false;
  148. }
  149. mNormals = (LLVector4a*)allocate_volume_mem(vert_size);
  150. if (!mNormals)
  151. {
  152. freeData();
  153. return false;
  154. }
  155. mBinormals = (LLVector4a*)allocate_volume_mem(vert_size);
  156. if (!mBinormals)
  157. {
  158. freeData();
  159. return false;
  160. }
  161. S32 tex_size = (num_verts * sizeof(LLVector2) + 0xF) & ~0xF;
  162. mTexCoords = (LLVector2*)allocate_volume_mem(tex_size);
  163. if (!mTexCoords)
  164. {
  165. freeData();
  166. return false;
  167. }
  168. // Actually, we are allocating more space than we need for the skiplist
  169. mVertexIndices = (U32*)allocate_volume_mem(num_verts * sizeof(U32));
  170. if (!mVertexIndices)
  171. {
  172. freeData();
  173. return false;
  174. }
  175. mNumIndices = 0;
  176. mTotalDistortion = 0.f;
  177. mMaxDistortion = 0.f;
  178. mAvgDistortion.clear();
  179. mMesh = mesh;
  180. //-------------------------------------------------------------------------
  181. // Read vertices
  182. //-------------------------------------------------------------------------
  183. for (S32 v = 0; v < num_verts; ++v)
  184. {
  185. num_read = fread(&mVertexIndices[v], sizeof(U32), 1, fp);
  186. llendianswizzle(&mVertexIndices[v], sizeof(U32), 1);
  187. if (num_read != 1)
  188. {
  189. llwarns << "Cannot read morph target vertex number" << llendl;
  190. return false;
  191. }
  192. if (mVertexIndices[v] > 10000)
  193. {
  194. llwarns << "Bad morph index: " << mVertexIndices[v] << llendl;
  195. llassert(false);
  196. return false;
  197. }
  198. num_read = fread(&mCoords[v], sizeof(F32), 3, fp);
  199. llendianswizzle(&mCoords[v], sizeof(F32), 3);
  200. if (num_read != 3)
  201. {
  202. llwarns << "Cannot read morph target vertex coordinates" << llendl;
  203. return false;
  204. }
  205. F32 magnitude = mCoords[v].getLength3().getF32();
  206. mTotalDistortion += magnitude;
  207. LLVector4a t;
  208. t.setAbs(mCoords[v]);
  209. mAvgDistortion.add(t);
  210. if (magnitude > mMaxDistortion)
  211. {
  212. mMaxDistortion = magnitude;
  213. }
  214. num_read = fread(&mNormals[v], sizeof(F32), 3, fp);
  215. llendianswizzle(&mNormals[v], sizeof(F32), 3);
  216. if (num_read != 3)
  217. {
  218. llwarns << "Cannot read morph target normal" << llendl;
  219. return false;
  220. }
  221. num_read = fread(&mBinormals[v], sizeof(F32), 3, fp);
  222. llendianswizzle(&mBinormals[v], sizeof(F32), 3);
  223. if (num_read != 3)
  224. {
  225. llwarns << "Cannot read morph target binormal" << llendl;
  226. return false;
  227. }
  228. num_read = fread(&mTexCoords[v].mV, sizeof(F32), 2, fp);
  229. llendianswizzle(&mTexCoords[v].mV, sizeof(F32), 2);
  230. if (num_read != 2)
  231. {
  232. llwarns << "west_limit read morph target uv" << llendl;
  233. return false;
  234. }
  235. ++mNumIndices;
  236. }
  237. mAvgDistortion.mul(1.f / (F32)mNumIndices);
  238. mAvgDistortion.normalize3fast();
  239. return true;
  240. }
  241. void LLPolyMorphData::freeData()
  242. {
  243. if (mCoords)
  244. {
  245. free_volume_mem(mCoords);
  246. mCoords = NULL;
  247. }
  248. if (mNormals)
  249. {
  250. free_volume_mem(mNormals);
  251. mNormals = NULL;
  252. }
  253. if (mBinormals)
  254. {
  255. free_volume_mem(mBinormals);
  256. mBinormals = NULL;
  257. }
  258. if (mTexCoords)
  259. {
  260. free_volume_mem(mTexCoords);
  261. mTexCoords = NULL;
  262. }
  263. if (mVertexIndices)
  264. {
  265. free_volume_mem(mVertexIndices);
  266. mVertexIndices = NULL;
  267. }
  268. }
  269. //-----------------------------------------------------------------------------
  270. // LLPolyMorphTargetInfo() class
  271. //-----------------------------------------------------------------------------
  272. LLPolyMorphTargetInfo::LLPolyMorphTargetInfo()
  273. : mIsClothingMorph(false)
  274. {
  275. }
  276. bool LLPolyMorphTargetInfo::parseXml(LLXmlTreeNode* node)
  277. {
  278. llassert(node->hasName("param") && node->getChildByName("param_morph"));
  279. if (!LLViewerVisualParamInfo::parseXml(node))
  280. {
  281. return false;
  282. }
  283. // Get mixed-case name
  284. static LLStdStringHandle name_string =
  285. LLXmlTree::addAttributeString("name");
  286. if (!node->getFastAttributeString(name_string, mMorphName))
  287. {
  288. llwarns << "Avatar file: <param> is missing name attribute" << llendl;
  289. return false; // Continue, ignoring this tag
  290. }
  291. static LLStdStringHandle clothing_morph_string =
  292. LLXmlTree::addAttributeString("clothing_morph");
  293. node->getFastAttributeBool(clothing_morph_string, mIsClothingMorph);
  294. LLXmlTreeNode* paramNode = node->getChildByName("param_morph");
  295. if (!paramNode)
  296. {
  297. llwarns << "Failed to getChildByName(\"param_morph\")" << llendl;
  298. return false;
  299. }
  300. for (LLXmlTreeNode* child_node = paramNode->getFirstChild();
  301. child_node; child_node = paramNode->getNextChild())
  302. {
  303. static LLStdStringHandle name_string =
  304. LLXmlTree::addAttributeString("name");
  305. if (child_node->hasName("volume_morph"))
  306. {
  307. std::string volume_name;
  308. if (child_node->getFastAttributeString(name_string, volume_name))
  309. {
  310. LLVector3 scale;
  311. static LLStdStringHandle scale_string =
  312. LLXmlTree::addAttributeString("scale");
  313. child_node->getFastAttributeVector3(scale_string, scale);
  314. LLVector3 pos;
  315. static LLStdStringHandle pos_string =
  316. LLXmlTree::addAttributeString("pos");
  317. child_node->getFastAttributeVector3(pos_string, pos);
  318. mVolumeInfoList.emplace_back(volume_name, scale,pos);
  319. }
  320. }
  321. }
  322. return true;
  323. }
  324. //-----------------------------------------------------------------------------
  325. // LLPolyMorphTarget() class
  326. //-----------------------------------------------------------------------------
  327. LLPolyMorphTarget::LLPolyMorphTarget(LLPolyMesh* poly_mesh)
  328. : LLViewerVisualParam(),
  329. mMorphData(NULL),
  330. mMesh(poly_mesh),
  331. mVertMask(NULL),
  332. mLastSex(SEX_FEMALE),
  333. mNumMorphMasksPending(0),
  334. mVolumeMorphs()
  335. {
  336. }
  337. LLPolyMorphTarget::LLPolyMorphTarget(const LLPolyMorphTarget& other)
  338. : LLViewerVisualParam(other),
  339. mMorphData(other.mMorphData),
  340. mMesh(other.mMesh),
  341. mVertMask(other.mVertMask ? new LLPolyVertexMask(*other.mVertMask) : NULL),
  342. mLastSex(other.mLastSex),
  343. mNumMorphMasksPending(other.mNumMorphMasksPending),
  344. mVolumeMorphs(other.mVolumeMorphs)
  345. {
  346. }
  347. LLPolyMorphTarget::~LLPolyMorphTarget()
  348. {
  349. if (mVertMask)
  350. {
  351. delete mVertMask;
  352. mVertMask = NULL;
  353. }
  354. }
  355. bool LLPolyMorphTarget::setInfo(LLPolyMorphTargetInfo* info)
  356. {
  357. llassert(mInfo == NULL);
  358. if (info->mID < 0)
  359. {
  360. return false;
  361. }
  362. mInfo = info;
  363. mID = info->mID;
  364. setWeight(getDefaultWeight(), false);
  365. LLAvatarAppearance* avatarp = mMesh->getAvatar();
  366. if (!avatarp)
  367. {
  368. llwarns << "NULL avatar for this morph target !" << llendl;
  369. return false;
  370. }
  371. for (LLPolyMorphTargetInfo::volume_info_list_t::iterator
  372. iter = getInfo()->mVolumeInfoList.begin(),
  373. end = getInfo()->mVolumeInfoList.end();
  374. iter != end; ++iter)
  375. {
  376. LLPolyVolumeMorphInfo* volume_info = &(*iter);
  377. for (S32 i = 0, count = avatarp->mCollisionVolumes.size();
  378. i < count; ++i)
  379. {
  380. if (avatarp->mCollisionVolumes[i]->getName() == volume_info->mName)
  381. {
  382. mVolumeMorphs.emplace_back(avatarp->mCollisionVolumes[i],
  383. volume_info->mScale,
  384. volume_info->mPos);
  385. break;
  386. }
  387. }
  388. }
  389. std::string morph_param_name = getInfo()->mMorphName;
  390. mMorphData = mMesh->getMorphData(morph_param_name);
  391. if (!mMorphData)
  392. {
  393. const std::string driven_tag = "_Driven";
  394. U32 pos = morph_param_name.find(driven_tag);
  395. if (pos > 0)
  396. {
  397. morph_param_name = morph_param_name.substr(0, pos);
  398. mMorphData = mMesh->getMorphData(morph_param_name);
  399. }
  400. }
  401. if (!mMorphData)
  402. {
  403. llwarns << "No morph target named " << morph_param_name
  404. << " found in mesh." << llendl;
  405. return false; // Continue, ignoring this tag
  406. }
  407. return true;
  408. }
  409. //virtual
  410. LLViewerVisualParam* LLPolyMorphTarget::cloneParam(LLWearable* wearable) const
  411. {
  412. return new LLPolyMorphTarget(*this);
  413. }
  414. #if 0 // Unused methods
  415. LLVector4a LLPolyMorphTarget::getVertexDistortion(S32 requested_index,
  416. LLPolyMesh* mesh)
  417. {
  418. if (!mMorphData || mMesh != mesh) return LLVector4a::getZero();
  419. for (U32 index = 0, count = mMorphData->mNumIndices; index < count;
  420. ++index)
  421. {
  422. if (mMorphData->mVertexIndices[index] == (U32)requested_index)
  423. {
  424. return mMorphData->mCoords[index];
  425. }
  426. }
  427. return LLVector4a::getZero();
  428. }
  429. const LLVector4a* LLPolyMorphTarget::getFirstDistortion(U32* index,
  430. LLPolyMesh** poly_mesh)
  431. {
  432. if (!mMorphData)
  433. {
  434. return &LLVector4a::getZero();
  435. }
  436. mMorphData->mCurrentIndex = 0;
  437. if (!mMorphData->mNumIndices)
  438. {
  439. return NULL;
  440. }
  441. LLVector4a* result = &mMorphData->mCoords[mMorphData->mCurrentIndex];
  442. if (index)
  443. {
  444. *index = mMorphData->mVertexIndices[mMorphData->mCurrentIndex];
  445. }
  446. if (poly_mesh)
  447. {
  448. *poly_mesh = mMesh;
  449. }
  450. return result;
  451. }
  452. const LLVector4a* LLPolyMorphTarget::getNextDistortion(U32* index,
  453. LLPolyMesh** poly_mesh)
  454. {
  455. if (!mMorphData)
  456. {
  457. return &LLVector4a::getZero();
  458. }
  459. if (++mMorphData->mCurrentIndex >= mMorphData->mNumIndices)
  460. {
  461. return NULL;
  462. }
  463. LLVector4a* result = &mMorphData->mCoords[mMorphData->mCurrentIndex];
  464. if (index)
  465. {
  466. *index = mMorphData->mVertexIndices[mMorphData->mCurrentIndex];
  467. }
  468. if (poly_mesh)
  469. {
  470. *poly_mesh = mMesh;
  471. }
  472. return result;
  473. }
  474. F32 LLPolyMorphTarget::getTotalDistortion()
  475. {
  476. return mMorphData ? mMorphData->mTotalDistortion : 0.f;
  477. }
  478. const LLVector4a& LLPolyMorphTarget::getAvgDistortion()
  479. {
  480. return mMorphData ? mMorphData->mAvgDistortion : LLVector4a::getZero();
  481. }
  482. F32 LLPolyMorphTarget::getMaxDistortion()
  483. {
  484. return mMorphData ? mMorphData->mMaxDistortion : 0.f;
  485. }
  486. #endif
  487. void LLPolyMorphTarget::apply(ESex avatar_sex)
  488. {
  489. if (!mMorphData || mNumMorphMasksPending > 0)
  490. {
  491. return;
  492. }
  493. LL_FAST_TIMER(FTM_APPLY_MORPH_TARGET);
  494. mLastSex = avatar_sex;
  495. // Check for NaN condition (NaN is detected if a variable doesn't equal
  496. // itself.
  497. if (mCurWeight != mCurWeight)
  498. {
  499. mCurWeight = 0.f;
  500. }
  501. if (mLastWeight != mLastWeight)
  502. {
  503. mLastWeight = mCurWeight + .001f;
  504. }
  505. // Perform differential update of morph
  506. F32 delta_weight = (getSex() & avatar_sex) ? mCurWeight - mLastWeight
  507. : getDefaultWeight() - mLastWeight;
  508. // Store last weight
  509. mLastWeight += delta_weight;
  510. if (delta_weight != 0.f)
  511. {
  512. llassert(!mMesh->isLOD());
  513. LLVector4a* coords = mMesh->getWritableCoords();
  514. LLVector4a* scaled_normals = mMesh->getScaledNormals();
  515. LLVector4a* normals = mMesh->getWritableNormals();
  516. LLVector4a* scaled_binormals = mMesh->getScaledBinormals();
  517. LLVector4a* binormals = mMesh->getWritableBinormals();
  518. LLVector4a* clothing_weights = mMesh->getWritableClothingWeights();
  519. LLVector2* tex_coords = mMesh->getWritableTexCoords();
  520. F32* maskWeightArray = mVertMask ? mVertMask->getMorphMaskWeights()
  521. : NULL;
  522. for (U32 vert_index_morph = 0, count = mMorphData->mNumIndices;
  523. vert_index_morph < count; ++vert_index_morph)
  524. {
  525. S32 vert_index_mesh = mMorphData->mVertexIndices[vert_index_morph];
  526. F32 maskWeight = 1.f;
  527. if (maskWeightArray)
  528. {
  529. maskWeight = maskWeightArray[vert_index_morph];
  530. }
  531. LLVector4a pos = mMorphData->mCoords[vert_index_morph];
  532. pos.mul(delta_weight * maskWeight);
  533. coords[vert_index_mesh].add(pos);
  534. if (getInfo()->mIsClothingMorph && clothing_weights)
  535. {
  536. LLVector4a clothing_offset = mMorphData->mCoords[vert_index_morph];
  537. clothing_offset.mul(delta_weight * maskWeight);
  538. LLVector4a* clothing_weight = &clothing_weights[vert_index_mesh];
  539. clothing_weight->add(clothing_offset);
  540. clothing_weight->getF32ptr()[VW] = maskWeight;
  541. }
  542. // Calculate new normals based on half angles
  543. LLVector4a norm = mMorphData->mNormals[vert_index_morph];
  544. norm.mul(delta_weight * maskWeight * NORMAL_SOFTEN_FACTOR);
  545. scaled_normals[vert_index_mesh].add(norm);
  546. norm = scaled_normals[vert_index_mesh];
  547. // Guard against degenerate input data before we create NaNs below !
  548. norm.normalize3fast();
  549. normals[vert_index_mesh] = norm;
  550. // Calculate new binormals
  551. LLVector4a binorm = mMorphData->mBinormals[vert_index_morph];
  552. // Guard against degenerate input data before we create NaNs below !
  553. if (!binorm.isFinite3() ||
  554. binorm.dot3(binorm).getF32() <= F_APPROXIMATELY_ZERO)
  555. {
  556. binorm.set(1.f, 0.f, 0.f, 1.f);
  557. }
  558. binorm.mul(delta_weight * maskWeight * NORMAL_SOFTEN_FACTOR);
  559. scaled_binormals[vert_index_mesh].add(binorm);
  560. LLVector4a tangent;
  561. tangent.setCross3(scaled_binormals[vert_index_mesh], norm);
  562. LLVector4a& normalized_binormal = binormals[vert_index_mesh];
  563. normalized_binormal.setCross3(norm, tangent);
  564. normalized_binormal.normalize3fast();
  565. tex_coords[vert_index_mesh] += mMorphData->mTexCoords[vert_index_morph] *
  566. delta_weight * maskWeight;
  567. }
  568. // Now apply volume changes
  569. applyVolumeChanges(delta_weight);
  570. }
  571. if (mNext)
  572. {
  573. mNext->apply(avatar_sex);
  574. }
  575. }
  576. void LLPolyMorphTarget::applyMask(U8* mask_tex_data, S32 width, S32 height,
  577. S32 num_components, bool invert)
  578. {
  579. LLVector4a* clothing_weights =
  580. getInfo()->mIsClothingMorph ? mMesh->getWritableClothingWeights()
  581. : NULL;
  582. if (!mVertMask)
  583. {
  584. mVertMask = new LLPolyVertexMask(mMorphData);
  585. --mNumMorphMasksPending;
  586. }
  587. else
  588. {
  589. // Remove effect of previous mask
  590. F32* mask_weights = mVertMask ? mVertMask->getMorphMaskWeights() : NULL;
  591. if (mask_weights)
  592. {
  593. LLVector4a* coords = mMesh->getWritableCoords();
  594. LLVector4a* scaled_normals = mMesh->getScaledNormals();
  595. LLVector4a* scaled_binormals = mMesh->getScaledBinormals();
  596. LLVector2* tex_coords = mMesh->getWritableTexCoords();
  597. LLVector4Logical clothing_mask;
  598. clothing_mask.clear();
  599. clothing_mask.setElement<0>();
  600. clothing_mask.setElement<1>();
  601. clothing_mask.setElement<2>();
  602. for (U32 vert = 0, count = mMorphData->mNumIndices; vert < count;
  603. ++vert)
  604. {
  605. F32 last_weight = mLastWeight * mask_weights[vert];
  606. S32 out_vert = mMorphData->mVertexIndices[vert];
  607. // Remove effect of existing masked morph
  608. LLVector4a t;
  609. t = mMorphData->mCoords[vert];
  610. t.mul(last_weight);
  611. coords[out_vert].sub(t);
  612. t = mMorphData->mNormals[vert];
  613. t.mul(last_weight * NORMAL_SOFTEN_FACTOR);
  614. scaled_normals[out_vert].sub(t);
  615. t = mMorphData->mBinormals[vert];
  616. t.mul(last_weight * NORMAL_SOFTEN_FACTOR);
  617. scaled_binormals[out_vert].sub(t);
  618. tex_coords[out_vert] -= mMorphData->mTexCoords[vert] *
  619. last_weight;
  620. if (clothing_weights)
  621. {
  622. LLVector4a clothing_offset = mMorphData->mCoords[vert];
  623. clothing_offset.mul(last_weight);
  624. LLVector4a* clothing_weight = &clothing_weights[out_vert];
  625. LLVector4a t;
  626. t.setSub(*clothing_weight, clothing_offset);
  627. clothing_weight->setSelectWithMask(clothing_mask, t,
  628. *clothing_weight);
  629. }
  630. }
  631. }
  632. }
  633. // Set last weight to 0, since we've removed the effect of this morph
  634. mLastWeight = 0.f;
  635. mVertMask->generateMask(mask_tex_data, width, height, num_components,
  636. invert, clothing_weights);
  637. apply(mLastSex);
  638. }
  639. void LLPolyMorphTarget::applyVolumeChanges(F32 delta_weight)
  640. {
  641. // Apply volume changes
  642. for (volume_list_t::iterator iter = mVolumeMorphs.begin(),
  643. end = mVolumeMorphs.end();
  644. iter != end; ++iter)
  645. {
  646. LLPolyVolumeMorph* morph = &(*iter);
  647. if (!morph) continue; // Paranoia
  648. LLVector3 scale_delta = morph->mScale * delta_weight;
  649. LLVector3 pos_delta = morph->mPos * delta_weight;
  650. morph->mVolume->setScale(morph->mVolume->getScale() + scale_delta);
  651. morph->mVolume->setPosition(morph->mVolume->getPosition() + pos_delta);
  652. }
  653. }
  654. //-----------------------------------------------------------------------------
  655. // LLPolyVertexMask() class
  656. //-----------------------------------------------------------------------------
  657. LLPolyVertexMask::LLPolyVertexMask(LLPolyMorphData* morph_data)
  658. : mMorphData(NULL),
  659. mWeightsGenerated(false)
  660. {
  661. mWeights = (F32*)allocate_volume_mem(morph_data->mNumIndices *
  662. sizeof(F32));
  663. if (mWeights)
  664. {
  665. mMorphData = morph_data;
  666. }
  667. else
  668. {
  669. llwarns << "Failure to allocate memory for weights !" << llendl;
  670. }
  671. }
  672. LLPolyVertexMask::LLPolyVertexMask(const LLPolyVertexMask& other)
  673. : mWeights(NULL),
  674. mMorphData(other.mMorphData),
  675. mWeightsGenerated(other.mWeightsGenerated)
  676. {
  677. if (mMorphData && mMorphData->mNumIndices > 0)
  678. {
  679. mWeights = (F32*)allocate_volume_mem(other.mMorphData->mNumIndices *
  680. sizeof(F32));
  681. if (mWeights)
  682. {
  683. memcpy(mWeights, other.mWeights,
  684. sizeof(F32) * mMorphData->mNumIndices);
  685. }
  686. else
  687. {
  688. llwarns << "Failure to allocate memory for weights !" << llendl;
  689. }
  690. }
  691. else
  692. {
  693. llwarns << "Invalid morph data !" << llendl;
  694. llassert(false);
  695. }
  696. }
  697. LLPolyVertexMask::~LLPolyVertexMask()
  698. {
  699. if (mWeights)
  700. {
  701. free_volume_mem(mWeights);
  702. mWeights = NULL;
  703. }
  704. }
  705. void LLPolyVertexMask::generateMask(U8* mask_tex_data, S32 width, S32 height,
  706. S32 num_components, bool invert,
  707. LLVector4a* clothing_weights)
  708. {
  709. LLVector2 uv_coords;
  710. for (U32 index = 0, count = mMorphData->mNumIndices; index < count;
  711. ++index)
  712. {
  713. S32 vert_idx = mMorphData->mVertexIndices[index];
  714. const S32* shared_vert_idx =
  715. mMorphData->mMesh->getSharedVert(vert_idx);
  716. if (shared_vert_idx)
  717. {
  718. uv_coords = mMorphData->mMesh->getUVs(*shared_vert_idx);
  719. }
  720. else
  721. {
  722. uv_coords = mMorphData->mMesh->getUVs(vert_idx);
  723. }
  724. U32 s = llclamp((U32)(uv_coords.mV[VX] * (F32)(width - 1)), 0U,
  725. (U32)width - 1);
  726. U32 t = llclamp((U32)(uv_coords.mV[VY] * (F32)(height - 1)), 0U,
  727. (U32)height - 1);
  728. F32 weight;
  729. if (mask_tex_data)
  730. {
  731. weight = (F32)mask_tex_data[(t * width + s) * num_components +
  732. num_components - 1] / 255.f;
  733. }
  734. else
  735. {
  736. weight = 0.f;
  737. }
  738. if (invert)
  739. {
  740. weight = 1.f - weight;
  741. }
  742. mWeights[index] = weight;
  743. #if 0
  744. // Now apply step function
  745. mWeights[index] = mWeights[index] > 0.95f ? 1.f : 0.f;
  746. #endif
  747. if (clothing_weights)
  748. {
  749. clothing_weights[vert_idx].getF32ptr()[VW] = mWeights[index];
  750. }
  751. }
  752. mWeightsGenerated = true;
  753. }
  754. F32* LLPolyVertexMask::getMorphMaskWeights()
  755. {
  756. return mWeightsGenerated ? mWeights : NULL;
  757. }