PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llpolymesh.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 1261 lines | 848 code | 167 blank | 246 comment | 131 complexity | fa264e75344f8d8ed76adceb4988e824 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llpolymesh.cpp
  3. * @brief Implementation of LLPolyMesh class
  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. //-----------------------------------------------------------------------------
  27. // Header Files
  28. //-----------------------------------------------------------------------------
  29. #include "llviewerprecompiledheaders.h"
  30. #include "llfasttimer.h"
  31. #include "llmemory.h"
  32. #include "llviewercontrol.h"
  33. #include "llxmltree.h"
  34. #include "llvoavatar.h"
  35. #include "llwearable.h"
  36. #include "lldir.h"
  37. #include "llvolume.h"
  38. #include "llendianswizzle.h"
  39. #include "llpolymesh.h"
  40. #define HEADER_ASCII "Linden Mesh 1.0"
  41. #define HEADER_BINARY "Linden Binary Mesh 1.0"
  42. extern LLControlGroup gSavedSettings; // read only
  43. LLPolyMorphData *clone_morph_param_duplicate(const LLPolyMorphData *src_data,
  44. const std::string &name);
  45. LLPolyMorphData *clone_morph_param_direction(const LLPolyMorphData *src_data,
  46. const LLVector3 &direction,
  47. const std::string &name);
  48. LLPolyMorphData *clone_morph_param_cleavage(const LLPolyMorphData *src_data,
  49. F32 scale,
  50. const std::string &name);
  51. //-----------------------------------------------------------------------------
  52. // Global table of loaded LLPolyMeshes
  53. //-----------------------------------------------------------------------------
  54. LLPolyMesh::LLPolyMeshSharedDataTable LLPolyMesh::sGlobalSharedMeshList;
  55. //-----------------------------------------------------------------------------
  56. // LLPolyMeshSharedData()
  57. //-----------------------------------------------------------------------------
  58. LLPolyMeshSharedData::LLPolyMeshSharedData()
  59. {
  60. mNumVertices = 0;
  61. mBaseCoords = NULL;
  62. mBaseNormals = NULL;
  63. mBaseBinormals = NULL;
  64. mTexCoords = NULL;
  65. mDetailTexCoords = NULL;
  66. mWeights = NULL;
  67. mHasWeights = FALSE;
  68. mHasDetailTexCoords = FALSE;
  69. mNumFaces = 0;
  70. mFaces = NULL;
  71. mNumJointNames = 0;
  72. mJointNames = NULL;
  73. mTriangleIndices = NULL;
  74. mNumTriangleIndices = 0;
  75. mReferenceData = NULL;
  76. mLastIndexOffset = -1;
  77. }
  78. //-----------------------------------------------------------------------------
  79. // ~LLPolyMeshSharedData()
  80. //-----------------------------------------------------------------------------
  81. LLPolyMeshSharedData::~LLPolyMeshSharedData()
  82. {
  83. freeMeshData();
  84. for_each(mMorphData.begin(), mMorphData.end(), DeletePointer());
  85. mMorphData.clear();
  86. }
  87. //-----------------------------------------------------------------------------
  88. // setupLOD()
  89. //-----------------------------------------------------------------------------
  90. void LLPolyMeshSharedData::setupLOD(LLPolyMeshSharedData* reference_data)
  91. {
  92. mReferenceData = reference_data;
  93. if (reference_data)
  94. {
  95. mBaseCoords = reference_data->mBaseCoords;
  96. mBaseNormals = reference_data->mBaseNormals;
  97. mBaseBinormals = reference_data->mBaseBinormals;
  98. mTexCoords = reference_data->mTexCoords;
  99. mDetailTexCoords = reference_data->mDetailTexCoords;
  100. mWeights = reference_data->mWeights;
  101. mHasWeights = reference_data->mHasWeights;
  102. mHasDetailTexCoords = reference_data->mHasDetailTexCoords;
  103. }
  104. }
  105. //-----------------------------------------------------------------------------
  106. // LLPolyMeshSharedData::freeMeshData()
  107. //-----------------------------------------------------------------------------
  108. void LLPolyMeshSharedData::freeMeshData()
  109. {
  110. if (!mReferenceData)
  111. {
  112. mNumVertices = 0;
  113. delete [] mBaseCoords;
  114. mBaseCoords = NULL;
  115. delete [] mBaseNormals;
  116. mBaseNormals = NULL;
  117. delete [] mBaseBinormals;
  118. mBaseBinormals = NULL;
  119. delete [] mTexCoords;
  120. mTexCoords = NULL;
  121. delete [] mDetailTexCoords;
  122. mDetailTexCoords = NULL;
  123. delete [] mWeights;
  124. mWeights = NULL;
  125. }
  126. mNumFaces = 0;
  127. delete [] mFaces;
  128. mFaces = NULL;
  129. mNumJointNames = 0;
  130. delete [] mJointNames;
  131. mJointNames = NULL;
  132. delete [] mTriangleIndices;
  133. mTriangleIndices = NULL;
  134. // mVertFaceMap.deleteAllData();
  135. }
  136. // compate_int is used by the qsort function to sort the index array
  137. int compare_int(const void *a, const void *b);
  138. //-----------------------------------------------------------------------------
  139. // genIndices()
  140. //-----------------------------------------------------------------------------
  141. void LLPolyMeshSharedData::genIndices(S32 index_offset)
  142. {
  143. if (index_offset == mLastIndexOffset)
  144. {
  145. return;
  146. }
  147. delete []mTriangleIndices;
  148. mTriangleIndices = new U32[mNumTriangleIndices];
  149. S32 cur_index = 0;
  150. for (S32 i = 0; i < mNumFaces; i++)
  151. {
  152. mTriangleIndices[cur_index] = mFaces[i][0] + index_offset;
  153. cur_index++;
  154. mTriangleIndices[cur_index] = mFaces[i][1] + index_offset;
  155. cur_index++;
  156. mTriangleIndices[cur_index] = mFaces[i][2] + index_offset;
  157. cur_index++;
  158. }
  159. mLastIndexOffset = index_offset;
  160. }
  161. //--------------------------------------------------------------------
  162. // LLPolyMeshSharedData::getNumKB()
  163. //--------------------------------------------------------------------
  164. U32 LLPolyMeshSharedData::getNumKB()
  165. {
  166. U32 num_kb = sizeof(LLPolyMesh);
  167. if (!isLOD())
  168. {
  169. num_kb += mNumVertices *
  170. ( sizeof(LLVector3) + // coords
  171. sizeof(LLVector3) + // normals
  172. sizeof(LLVector2) ); // texCoords
  173. }
  174. if (mHasDetailTexCoords && !isLOD())
  175. {
  176. num_kb += mNumVertices * sizeof(LLVector2); // detailTexCoords
  177. }
  178. if (mHasWeights && !isLOD())
  179. {
  180. num_kb += mNumVertices * sizeof(float); // weights
  181. }
  182. num_kb += mNumFaces * sizeof(LLPolyFace); // faces
  183. num_kb /= 1024;
  184. return num_kb;
  185. }
  186. //-----------------------------------------------------------------------------
  187. // LLPolyMeshSharedData::allocateVertexData()
  188. //-----------------------------------------------------------------------------
  189. BOOL LLPolyMeshSharedData::allocateVertexData( U32 numVertices )
  190. {
  191. U32 i;
  192. mBaseCoords = new LLVector3[ numVertices ];
  193. mBaseNormals = new LLVector3[ numVertices ];
  194. mBaseBinormals = new LLVector3[ numVertices ];
  195. mTexCoords = new LLVector2[ numVertices ];
  196. mDetailTexCoords = new LLVector2[ numVertices ];
  197. mWeights = new F32[ numVertices ];
  198. for (i = 0; i < numVertices; i++)
  199. {
  200. mWeights[i] = 0.f;
  201. }
  202. mNumVertices = numVertices;
  203. return TRUE;
  204. }
  205. //-----------------------------------------------------------------------------
  206. // LLPolyMeshSharedData::allocateFaceData()
  207. //-----------------------------------------------------------------------------
  208. BOOL LLPolyMeshSharedData::allocateFaceData( U32 numFaces )
  209. {
  210. mFaces = new LLPolyFace[ numFaces ];
  211. mNumFaces = numFaces;
  212. mNumTriangleIndices = mNumFaces * 3;
  213. return TRUE;
  214. }
  215. //-----------------------------------------------------------------------------
  216. // LLPolyMeshSharedData::allocateJointNames()
  217. //-----------------------------------------------------------------------------
  218. BOOL LLPolyMeshSharedData::allocateJointNames( U32 numJointNames )
  219. {
  220. mJointNames = new std::string[ numJointNames ];
  221. mNumJointNames = numJointNames;
  222. return TRUE;
  223. }
  224. //--------------------------------------------------------------------
  225. // LLPolyMeshSharedData::loadMesh()
  226. //--------------------------------------------------------------------
  227. BOOL LLPolyMeshSharedData::loadMesh( const std::string& fileName )
  228. {
  229. //-------------------------------------------------------------------------
  230. // Open the file
  231. //-------------------------------------------------------------------------
  232. if(fileName.empty())
  233. {
  234. llerrs << "Filename is Empty!" << llendl;
  235. return FALSE;
  236. }
  237. LLFILE* fp = LLFile::fopen(fileName, "rb"); /*Flawfinder: ignore*/
  238. if (!fp)
  239. {
  240. llerrs << "can't open: " << fileName << llendl;
  241. return FALSE;
  242. }
  243. //-------------------------------------------------------------------------
  244. // Read a chunk
  245. //-------------------------------------------------------------------------
  246. char header[128]; /*Flawfinder: ignore*/
  247. if (fread(header, sizeof(char), 128, fp) != 128)
  248. {
  249. llwarns << "Short read" << llendl;
  250. }
  251. //-------------------------------------------------------------------------
  252. // Check for proper binary header
  253. //-------------------------------------------------------------------------
  254. BOOL status = FALSE;
  255. if ( strncmp(header, HEADER_BINARY, strlen(HEADER_BINARY)) == 0 ) /*Flawfinder: ignore*/
  256. {
  257. lldebugs << "Loading " << fileName << llendl;
  258. //----------------------------------------------------------------
  259. // File Header (seek past it)
  260. //----------------------------------------------------------------
  261. fseek(fp, 24, SEEK_SET);
  262. //----------------------------------------------------------------
  263. // HasWeights
  264. //----------------------------------------------------------------
  265. U8 hasWeights;
  266. size_t numRead = fread(&hasWeights, sizeof(U8), 1, fp);
  267. if (numRead != 1)
  268. {
  269. llerrs << "can't read HasWeights flag from " << fileName << llendl;
  270. return FALSE;
  271. }
  272. if (!isLOD())
  273. {
  274. mHasWeights = (hasWeights==0) ? FALSE : TRUE;
  275. }
  276. //----------------------------------------------------------------
  277. // HasDetailTexCoords
  278. //----------------------------------------------------------------
  279. U8 hasDetailTexCoords;
  280. numRead = fread(&hasDetailTexCoords, sizeof(U8), 1, fp);
  281. if (numRead != 1)
  282. {
  283. llerrs << "can't read HasDetailTexCoords flag from " << fileName << llendl;
  284. return FALSE;
  285. }
  286. //----------------------------------------------------------------
  287. // Position
  288. //----------------------------------------------------------------
  289. LLVector3 position;
  290. numRead = fread(position.mV, sizeof(float), 3, fp);
  291. llendianswizzle(position.mV, sizeof(float), 3);
  292. if (numRead != 3)
  293. {
  294. llerrs << "can't read Position from " << fileName << llendl;
  295. return FALSE;
  296. }
  297. setPosition( position );
  298. //----------------------------------------------------------------
  299. // Rotation
  300. //----------------------------------------------------------------
  301. LLVector3 rotationAngles;
  302. numRead = fread(rotationAngles.mV, sizeof(float), 3, fp);
  303. llendianswizzle(rotationAngles.mV, sizeof(float), 3);
  304. if (numRead != 3)
  305. {
  306. llerrs << "can't read RotationAngles from " << fileName << llendl;
  307. return FALSE;
  308. }
  309. U8 rotationOrder;
  310. numRead = fread(&rotationOrder, sizeof(U8), 1, fp);
  311. if (numRead != 1)
  312. {
  313. llerrs << "can't read RotationOrder from " << fileName << llendl;
  314. return FALSE;
  315. }
  316. rotationOrder = 0;
  317. setRotation( mayaQ( rotationAngles.mV[0],
  318. rotationAngles.mV[1],
  319. rotationAngles.mV[2],
  320. (LLQuaternion::Order)rotationOrder ) );
  321. //----------------------------------------------------------------
  322. // Scale
  323. //----------------------------------------------------------------
  324. LLVector3 scale;
  325. numRead = fread(scale.mV, sizeof(float), 3, fp);
  326. llendianswizzle(scale.mV, sizeof(float), 3);
  327. if (numRead != 3)
  328. {
  329. llerrs << "can't read Scale from " << fileName << llendl;
  330. return FALSE;
  331. }
  332. setScale( scale );
  333. //-------------------------------------------------------------------------
  334. // Release any existing mesh geometry
  335. //-------------------------------------------------------------------------
  336. freeMeshData();
  337. U16 numVertices = 0;
  338. //----------------------------------------------------------------
  339. // NumVertices
  340. //----------------------------------------------------------------
  341. if (!isLOD())
  342. {
  343. numRead = fread(&numVertices, sizeof(U16), 1, fp);
  344. llendianswizzle(&numVertices, sizeof(U16), 1);
  345. if (numRead != 1)
  346. {
  347. llerrs << "can't read NumVertices from " << fileName << llendl;
  348. return FALSE;
  349. }
  350. allocateVertexData( numVertices );
  351. //----------------------------------------------------------------
  352. // Coords
  353. //----------------------------------------------------------------
  354. numRead = fread(mBaseCoords, 3*sizeof(float), numVertices, fp);
  355. llendianswizzle(mBaseCoords, sizeof(float), 3*numVertices);
  356. if (numRead != numVertices)
  357. {
  358. llerrs << "can't read Coordinates from " << fileName << llendl;
  359. return FALSE;
  360. }
  361. //----------------------------------------------------------------
  362. // Normals
  363. //----------------------------------------------------------------
  364. numRead = fread(mBaseNormals, 3*sizeof(float), numVertices, fp);
  365. llendianswizzle(mBaseNormals, sizeof(float), 3*numVertices);
  366. if (numRead != numVertices)
  367. {
  368. llerrs << " can't read Normals from " << fileName << llendl;
  369. return FALSE;
  370. }
  371. //----------------------------------------------------------------
  372. // Binormals
  373. //----------------------------------------------------------------
  374. numRead = fread(mBaseBinormals, 3*sizeof(float), numVertices, fp);
  375. llendianswizzle(mBaseBinormals, sizeof(float), 3*numVertices);
  376. if (numRead != numVertices)
  377. {
  378. llerrs << " can't read Binormals from " << fileName << llendl;
  379. return FALSE;
  380. }
  381. //----------------------------------------------------------------
  382. // TexCoords
  383. //----------------------------------------------------------------
  384. numRead = fread(mTexCoords, 2*sizeof(float), numVertices, fp);
  385. llendianswizzle(mTexCoords, sizeof(float), 2*numVertices);
  386. if (numRead != numVertices)
  387. {
  388. llerrs << "can't read TexCoords from " << fileName << llendl;
  389. return FALSE;
  390. }
  391. //----------------------------------------------------------------
  392. // DetailTexCoords
  393. //----------------------------------------------------------------
  394. if (mHasDetailTexCoords)
  395. {
  396. numRead = fread(mDetailTexCoords, 2*sizeof(float), numVertices, fp);
  397. llendianswizzle(mDetailTexCoords, sizeof(float), 2*numVertices);
  398. if (numRead != numVertices)
  399. {
  400. llerrs << "can't read DetailTexCoords from " << fileName << llendl;
  401. return FALSE;
  402. }
  403. }
  404. //----------------------------------------------------------------
  405. // Weights
  406. //----------------------------------------------------------------
  407. if (mHasWeights)
  408. {
  409. numRead = fread(mWeights, sizeof(float), numVertices, fp);
  410. llendianswizzle(mWeights, sizeof(float), numVertices);
  411. if (numRead != numVertices)
  412. {
  413. llerrs << "can't read Weights from " << fileName << llendl;
  414. return FALSE;
  415. }
  416. }
  417. }
  418. //----------------------------------------------------------------
  419. // NumFaces
  420. //----------------------------------------------------------------
  421. U16 numFaces;
  422. numRead = fread(&numFaces, sizeof(U16), 1, fp);
  423. llendianswizzle(&numFaces, sizeof(U16), 1);
  424. if (numRead != 1)
  425. {
  426. llerrs << "can't read NumFaces from " << fileName << llendl;
  427. return FALSE;
  428. }
  429. allocateFaceData( numFaces );
  430. //----------------------------------------------------------------
  431. // Faces
  432. //----------------------------------------------------------------
  433. U32 i;
  434. U32 numTris = 0;
  435. for (i = 0; i < numFaces; i++)
  436. {
  437. S16 face[3];
  438. numRead = fread(face, sizeof(U16), 3, fp);
  439. llendianswizzle(face, sizeof(U16), 3);
  440. if (numRead != 3)
  441. {
  442. llerrs << "can't read Face[" << i << "] from " << fileName << llendl;
  443. return FALSE;
  444. }
  445. if (mReferenceData)
  446. {
  447. llassert(face[0] < mReferenceData->mNumVertices);
  448. llassert(face[1] < mReferenceData->mNumVertices);
  449. llassert(face[2] < mReferenceData->mNumVertices);
  450. }
  451. if (isLOD())
  452. {
  453. // store largest index in case of LODs
  454. for (S32 j = 0; j < 3; j++)
  455. {
  456. if (face[j] > mNumVertices - 1)
  457. {
  458. mNumVertices = face[j] + 1;
  459. }
  460. }
  461. }
  462. mFaces[i][0] = face[0];
  463. mFaces[i][1] = face[1];
  464. mFaces[i][2] = face[2];
  465. // S32 j;
  466. // for(j = 0; j < 3; j++)
  467. // {
  468. // LLDynamicArray<S32> *face_list = mVertFaceMap.getIfThere(face[j]);
  469. // if (!face_list)
  470. // {
  471. // face_list = new LLDynamicArray<S32>;
  472. // mVertFaceMap.addData(face[j], face_list);
  473. // }
  474. // face_list->put(i);
  475. // }
  476. numTris++;
  477. }
  478. lldebugs << "verts: " << numVertices
  479. << ", faces: " << numFaces
  480. << ", tris: " << numTris
  481. << llendl;
  482. //----------------------------------------------------------------
  483. // NumSkinJoints
  484. //----------------------------------------------------------------
  485. if (!isLOD())
  486. {
  487. U16 numSkinJoints = 0;
  488. if ( mHasWeights )
  489. {
  490. numRead = fread(&numSkinJoints, sizeof(U16), 1, fp);
  491. llendianswizzle(&numSkinJoints, sizeof(U16), 1);
  492. if (numRead != 1)
  493. {
  494. llerrs << "can't read NumSkinJoints from " << fileName << llendl;
  495. return FALSE;
  496. }
  497. allocateJointNames( numSkinJoints );
  498. }
  499. //----------------------------------------------------------------
  500. // SkinJoints
  501. //----------------------------------------------------------------
  502. for (i=0; i < numSkinJoints; i++)
  503. {
  504. char jointName[64+1];
  505. numRead = fread(jointName, sizeof(jointName)-1, 1, fp);
  506. jointName[sizeof(jointName)-1] = '\0'; // ensure nul-termination
  507. if (numRead != 1)
  508. {
  509. llerrs << "can't read Skin[" << i << "].Name from " << fileName << llendl;
  510. return FALSE;
  511. }
  512. std::string *jn = &mJointNames[i];
  513. *jn = jointName;
  514. }
  515. //-------------------------------------------------------------------------
  516. // look for morph section
  517. //-------------------------------------------------------------------------
  518. char morphName[64+1];
  519. morphName[sizeof(morphName)-1] = '\0'; // ensure nul-termination
  520. while(fread(&morphName, sizeof(char), 64, fp) == 64)
  521. {
  522. if (!strcmp(morphName, "End Morphs"))
  523. {
  524. // we reached the end of the morphs
  525. break;
  526. }
  527. LLPolyMorphData* morph_data = new LLPolyMorphData(std::string(morphName));
  528. BOOL result = morph_data->loadBinary(fp, this);
  529. if (!result)
  530. {
  531. delete morph_data;
  532. continue;
  533. }
  534. mMorphData.insert(morph_data);
  535. if (!strcmp(morphName, "Breast_Female_Cleavage"))
  536. {
  537. mMorphData.insert(clone_morph_param_cleavage(morph_data,
  538. .75f,
  539. "Breast_Physics_LeftRight_Driven"));
  540. }
  541. if (!strcmp(morphName, "Breast_Female_Cleavage"))
  542. {
  543. mMorphData.insert(clone_morph_param_duplicate(morph_data,
  544. "Breast_Physics_InOut_Driven"));
  545. }
  546. if (!strcmp(morphName, "Breast_Gravity"))
  547. {
  548. mMorphData.insert(clone_morph_param_duplicate(morph_data,
  549. "Breast_Physics_UpDown_Driven"));
  550. }
  551. if (!strcmp(morphName, "Big_Belly_Torso"))
  552. {
  553. mMorphData.insert(clone_morph_param_direction(morph_data,
  554. LLVector3(0,0,0.05f),
  555. "Belly_Physics_Torso_UpDown_Driven"));
  556. }
  557. if (!strcmp(morphName, "Big_Belly_Legs"))
  558. {
  559. mMorphData.insert(clone_morph_param_direction(morph_data,
  560. LLVector3(0,0,0.05f),
  561. "Belly_Physics_Legs_UpDown_Driven"));
  562. }
  563. if (!strcmp(morphName, "skirt_belly"))
  564. {
  565. mMorphData.insert(clone_morph_param_direction(morph_data,
  566. LLVector3(0,0,0.05f),
  567. "Belly_Physics_Skirt_UpDown_Driven"));
  568. }
  569. if (!strcmp(morphName, "Small_Butt"))
  570. {
  571. mMorphData.insert(clone_morph_param_direction(morph_data,
  572. LLVector3(0,0,0.05f),
  573. "Butt_Physics_UpDown_Driven"));
  574. }
  575. if (!strcmp(morphName, "Small_Butt"))
  576. {
  577. mMorphData.insert(clone_morph_param_direction(morph_data,
  578. LLVector3(0,0.03f,0),
  579. "Butt_Physics_LeftRight_Driven"));
  580. }
  581. }
  582. S32 numRemaps;
  583. if (fread(&numRemaps, sizeof(S32), 1, fp) == 1)
  584. {
  585. llendianswizzle(&numRemaps, sizeof(S32), 1);
  586. for (S32 i = 0; i < numRemaps; i++)
  587. {
  588. S32 remapSrc;
  589. S32 remapDst;
  590. if (fread(&remapSrc, sizeof(S32), 1, fp) != 1)
  591. {
  592. llerrs << "can't read source vertex in vertex remap data" << llendl;
  593. break;
  594. }
  595. if (fread(&remapDst, sizeof(S32), 1, fp) != 1)
  596. {
  597. llerrs << "can't read destination vertex in vertex remap data" << llendl;
  598. break;
  599. }
  600. llendianswizzle(&remapSrc, sizeof(S32), 1);
  601. llendianswizzle(&remapDst, sizeof(S32), 1);
  602. mSharedVerts[remapSrc] = remapDst;
  603. }
  604. }
  605. }
  606. status = TRUE;
  607. }
  608. else
  609. {
  610. llerrs << "invalid mesh file header: " << fileName << llendl;
  611. status = FALSE;
  612. }
  613. if (0 == mNumJointNames)
  614. {
  615. allocateJointNames(1);
  616. }
  617. fclose( fp );
  618. return status;
  619. }
  620. //-----------------------------------------------------------------------------
  621. // getSharedVert()
  622. //-----------------------------------------------------------------------------
  623. const S32 *LLPolyMeshSharedData::getSharedVert(S32 vert)
  624. {
  625. if (mSharedVerts.count(vert) > 0)
  626. {
  627. return &mSharedVerts[vert];
  628. }
  629. return NULL;
  630. }
  631. //-----------------------------------------------------------------------------
  632. // getUV()
  633. //-----------------------------------------------------------------------------
  634. const LLVector2 &LLPolyMeshSharedData::getUVs(U32 index)
  635. {
  636. // TODO: convert all index variables to S32
  637. llassert((S32)index < mNumVertices);
  638. return mTexCoords[index];
  639. }
  640. //-----------------------------------------------------------------------------
  641. // LLPolyMesh()
  642. //-----------------------------------------------------------------------------
  643. LLPolyMesh::LLPolyMesh(LLPolyMeshSharedData *shared_data, LLPolyMesh *reference_mesh)
  644. {
  645. LLMemType mt(LLMemType::MTYPE_AVATAR_MESH);
  646. llassert(shared_data);
  647. mSharedData = shared_data;
  648. mReferenceMesh = reference_mesh;
  649. mAvatarp = NULL;
  650. mVertexData = NULL;
  651. mCurVertexCount = 0;
  652. mFaceIndexCount = 0;
  653. mFaceIndexOffset = 0;
  654. mFaceVertexCount = 0;
  655. mFaceVertexOffset = 0;
  656. if (shared_data->isLOD() && reference_mesh)
  657. {
  658. mCoords = reference_mesh->mCoords;
  659. mNormals = reference_mesh->mNormals;
  660. mScaledNormals = reference_mesh->mScaledNormals;
  661. mBinormals = reference_mesh->mBinormals;
  662. mScaledBinormals = reference_mesh->mScaledBinormals;
  663. mTexCoords = reference_mesh->mTexCoords;
  664. mClothingWeights = reference_mesh->mClothingWeights;
  665. }
  666. else
  667. {
  668. // Allocate memory without initializing every vector
  669. // NOTE: This makes asusmptions about the size of LLVector[234]
  670. int nverts = mSharedData->mNumVertices;
  671. int nfloats = nverts * (2*4 + 3*3 + 2 + 4);
  672. //use 16 byte aligned vertex data to make LLPolyMesh SSE friendly
  673. mVertexData = (F32*) ll_aligned_malloc_16(nfloats*4);
  674. int offset = 0;
  675. mCoords = (LLVector4*)(mVertexData + offset); offset += 4*nverts;
  676. mNormals = (LLVector4*)(mVertexData + offset); offset += 4*nverts;
  677. mClothingWeights = (LLVector4*)(mVertexData + offset); offset += 4*nverts;
  678. mTexCoords = (LLVector2*)(mVertexData + offset); offset += 2*nverts;
  679. // these members don't need to be 16-byte aligned, but the first one might be
  680. // read during an aligned memcpy of mTexCoords
  681. mScaledNormals = (LLVector3*)(mVertexData + offset); offset += 3*nverts;
  682. mBinormals = (LLVector3*)(mVertexData + offset); offset += 3*nverts;
  683. mScaledBinormals = (LLVector3*)(mVertexData + offset); offset += 3*nverts;
  684. initializeForMorph();
  685. }
  686. }
  687. //-----------------------------------------------------------------------------
  688. // ~LLPolyMesh()
  689. //-----------------------------------------------------------------------------
  690. LLPolyMesh::~LLPolyMesh()
  691. {
  692. S32 i;
  693. for (i = 0; i < mJointRenderData.count(); i++)
  694. {
  695. delete mJointRenderData[i];
  696. mJointRenderData[i] = NULL;
  697. }
  698. ll_aligned_free_16(mVertexData);
  699. }
  700. //-----------------------------------------------------------------------------
  701. // LLPolyMesh::getMesh()
  702. //-----------------------------------------------------------------------------
  703. LLPolyMesh *LLPolyMesh::getMesh(const std::string &name, LLPolyMesh* reference_mesh)
  704. {
  705. //-------------------------------------------------------------------------
  706. // search for an existing mesh by this name
  707. //-------------------------------------------------------------------------
  708. LLPolyMeshSharedData* meshSharedData = get_if_there(sGlobalSharedMeshList, name, (LLPolyMeshSharedData*)NULL);
  709. if (meshSharedData)
  710. {
  711. // llinfos << "Polymesh " << name << " found in global mesh table." << llendl;
  712. LLPolyMesh *poly_mesh = new LLPolyMesh(meshSharedData, reference_mesh);
  713. return poly_mesh;
  714. }
  715. //-------------------------------------------------------------------------
  716. // if not found, create a new one, add it to the list
  717. //-------------------------------------------------------------------------
  718. std::string full_path;
  719. full_path = gDirUtilp->getExpandedFilename(LL_PATH_CHARACTER,name);
  720. LLPolyMeshSharedData *mesh_data = new LLPolyMeshSharedData();
  721. if (reference_mesh)
  722. {
  723. mesh_data->setupLOD(reference_mesh->getSharedData());
  724. }
  725. if ( ! mesh_data->loadMesh( full_path ) )
  726. {
  727. delete mesh_data;
  728. return NULL;
  729. }
  730. LLPolyMesh *poly_mesh = new LLPolyMesh(mesh_data, reference_mesh);
  731. // llinfos << "Polymesh " << name << " added to global mesh table." << llendl;
  732. sGlobalSharedMeshList[name] = poly_mesh->mSharedData;
  733. return poly_mesh;
  734. }
  735. //-----------------------------------------------------------------------------
  736. // LLPolyMesh::freeAllMeshes()
  737. //-----------------------------------------------------------------------------
  738. void LLPolyMesh::freeAllMeshes()
  739. {
  740. // delete each item in the global lists
  741. for_each(sGlobalSharedMeshList.begin(), sGlobalSharedMeshList.end(), DeletePairedPointer());
  742. sGlobalSharedMeshList.clear();
  743. }
  744. LLPolyMeshSharedData *LLPolyMesh::getSharedData() const
  745. {
  746. return mSharedData;
  747. }
  748. //--------------------------------------------------------------------
  749. // LLPolyMesh::dumpDiagInfo()
  750. //--------------------------------------------------------------------
  751. void LLPolyMesh::dumpDiagInfo()
  752. {
  753. // keep track of totals
  754. U32 total_verts = 0;
  755. U32 total_faces = 0;
  756. U32 total_kb = 0;
  757. std::string buf;
  758. llinfos << "-----------------------------------------------------" << llendl;
  759. llinfos << " Global PolyMesh Table (DEBUG only)" << llendl;
  760. llinfos << " Verts Faces Mem(KB) Name" << llendl;
  761. llinfos << "-----------------------------------------------------" << llendl;
  762. // print each loaded mesh, and it's memory usage
  763. for(LLPolyMeshSharedDataTable::iterator iter = sGlobalSharedMeshList.begin();
  764. iter != sGlobalSharedMeshList.end(); ++iter)
  765. {
  766. const std::string& mesh_name = iter->first;
  767. LLPolyMeshSharedData* mesh = iter->second;
  768. S32 num_verts = mesh->mNumVertices;
  769. S32 num_faces = mesh->mNumFaces;
  770. U32 num_kb = mesh->getNumKB();
  771. buf = llformat("%8d %8d %8d %s", num_verts, num_faces, num_kb, mesh_name.c_str());
  772. llinfos << buf << llendl;
  773. total_verts += num_verts;
  774. total_faces += num_faces;
  775. total_kb += num_kb;
  776. }
  777. llinfos << "-----------------------------------------------------" << llendl;
  778. buf = llformat("%8d %8d %8d TOTAL", total_verts, total_faces, total_kb );
  779. llinfos << buf << llendl;
  780. llinfos << "-----------------------------------------------------" << llendl;
  781. }
  782. //-----------------------------------------------------------------------------
  783. // getWritableCoords()
  784. //-----------------------------------------------------------------------------
  785. LLVector4 *LLPolyMesh::getWritableCoords()
  786. {
  787. return mCoords;
  788. }
  789. //-----------------------------------------------------------------------------
  790. // getWritableNormals()
  791. //-----------------------------------------------------------------------------
  792. LLVector4 *LLPolyMesh::getWritableNormals()
  793. {
  794. return mNormals;
  795. }
  796. //-----------------------------------------------------------------------------
  797. // getWritableBinormals()
  798. //-----------------------------------------------------------------------------
  799. LLVector3 *LLPolyMesh::getWritableBinormals()
  800. {
  801. return mBinormals;
  802. }
  803. //-----------------------------------------------------------------------------
  804. // getWritableClothingWeights()
  805. //-----------------------------------------------------------------------------
  806. LLVector4 *LLPolyMesh::getWritableClothingWeights()
  807. {
  808. return mClothingWeights;
  809. }
  810. //-----------------------------------------------------------------------------
  811. // getWritableTexCoords()
  812. //-----------------------------------------------------------------------------
  813. LLVector2 *LLPolyMesh::getWritableTexCoords()
  814. {
  815. return mTexCoords;
  816. }
  817. //-----------------------------------------------------------------------------
  818. // getScaledNormals()
  819. //-----------------------------------------------------------------------------
  820. LLVector3 *LLPolyMesh::getScaledNormals()
  821. {
  822. return mScaledNormals;
  823. }
  824. //-----------------------------------------------------------------------------
  825. // getScaledBinormals()
  826. //-----------------------------------------------------------------------------
  827. LLVector3 *LLPolyMesh::getScaledBinormals()
  828. {
  829. return mScaledBinormals;
  830. }
  831. //-----------------------------------------------------------------------------
  832. // initializeForMorph()
  833. //-----------------------------------------------------------------------------
  834. void LLPolyMesh::initializeForMorph()
  835. {
  836. for (U32 i = 0; i < mSharedData->mNumVertices; ++i)
  837. {
  838. mCoords[i] = LLVector4(mSharedData->mBaseCoords[i]);
  839. mNormals[i] = LLVector4(mSharedData->mBaseNormals[i]);
  840. }
  841. memcpy(mScaledNormals, mSharedData->mBaseNormals, sizeof(LLVector3) * mSharedData->mNumVertices); /*Flawfinder: ignore*/
  842. memcpy(mBinormals, mSharedData->mBaseBinormals, sizeof(LLVector3) * mSharedData->mNumVertices); /*Flawfinder: ignore*/
  843. memcpy(mScaledBinormals, mSharedData->mBaseBinormals, sizeof(LLVector3) * mSharedData->mNumVertices); /*Flawfinder: ignore*/
  844. memcpy(mTexCoords, mSharedData->mTexCoords, sizeof(LLVector2) * mSharedData->mNumVertices); /*Flawfinder: ignore*/
  845. memset(mClothingWeights, 0, sizeof(LLVector4) * mSharedData->mNumVertices);
  846. }
  847. //-----------------------------------------------------------------------------
  848. // getMorphData()
  849. //-----------------------------------------------------------------------------
  850. LLPolyMorphData* LLPolyMesh::getMorphData(const std::string& morph_name)
  851. {
  852. if (!mSharedData)
  853. return NULL;
  854. for (LLPolyMeshSharedData::morphdata_list_t::iterator iter = mSharedData->mMorphData.begin();
  855. iter != mSharedData->mMorphData.end(); ++iter)
  856. {
  857. LLPolyMorphData *morph_data = *iter;
  858. if (morph_data->getName() == morph_name)
  859. {
  860. return morph_data;
  861. }
  862. }
  863. return NULL;
  864. }
  865. //-----------------------------------------------------------------------------
  866. // removeMorphData()
  867. //-----------------------------------------------------------------------------
  868. // // erasing but not deleting seems bad, but fortunately we don't actually use this...
  869. // void LLPolyMesh::removeMorphData(LLPolyMorphData *morph_target)
  870. // {
  871. // if (!mSharedData)
  872. // return;
  873. // mSharedData->mMorphData.erase(morph_target);
  874. // }
  875. //-----------------------------------------------------------------------------
  876. // deleteAllMorphData()
  877. //-----------------------------------------------------------------------------
  878. // void LLPolyMesh::deleteAllMorphData()
  879. // {
  880. // if (!mSharedData)
  881. // return;
  882. // for_each(mSharedData->mMorphData.begin(), mSharedData->mMorphData.end(), DeletePointer());
  883. // mSharedData->mMorphData.clear();
  884. // }
  885. //-----------------------------------------------------------------------------
  886. // getWritableWeights()
  887. //-----------------------------------------------------------------------------
  888. F32* LLPolyMesh::getWritableWeights() const
  889. {
  890. return mSharedData->mWeights;
  891. }
  892. //-----------------------------------------------------------------------------
  893. // LLPolySkeletalDistortionInfo()
  894. //-----------------------------------------------------------------------------
  895. LLPolySkeletalDistortionInfo::LLPolySkeletalDistortionInfo()
  896. {
  897. }
  898. BOOL LLPolySkeletalDistortionInfo::parseXml(LLXmlTreeNode* node)
  899. {
  900. llassert( node->hasName( "param" ) && node->getChildByName( "param_skeleton" ) );
  901. if (!LLViewerVisualParamInfo::parseXml(node))
  902. return FALSE;
  903. LLXmlTreeNode* skeletalParam = node->getChildByName("param_skeleton");
  904. if (NULL == skeletalParam)
  905. {
  906. llwarns << "Failed to getChildByName(\"param_skeleton\")"
  907. << llendl;
  908. return FALSE;
  909. }
  910. for( LLXmlTreeNode* bone = skeletalParam->getFirstChild(); bone; bone = skeletalParam->getNextChild() )
  911. {
  912. if (bone->hasName("bone"))
  913. {
  914. std::string name;
  915. LLVector3 scale;
  916. LLVector3 pos;
  917. BOOL haspos = FALSE;
  918. static LLStdStringHandle name_string = LLXmlTree::addAttributeString("name");
  919. if (!bone->getFastAttributeString(name_string, name))
  920. {
  921. llwarns << "No bone name specified for skeletal param." << llendl;
  922. continue;
  923. }
  924. static LLStdStringHandle scale_string = LLXmlTree::addAttributeString("scale");
  925. if (!bone->getFastAttributeVector3(scale_string, scale))
  926. {
  927. llwarns << "No scale specified for bone " << name << "." << llendl;
  928. continue;
  929. }
  930. // optional offset deformation (translation)
  931. static LLStdStringHandle offset_string = LLXmlTree::addAttributeString("offset");
  932. if (bone->getFastAttributeVector3(offset_string, pos))
  933. {
  934. haspos = TRUE;
  935. }
  936. mBoneInfoList.push_back(LLPolySkeletalBoneInfo(name, scale, pos, haspos));
  937. }
  938. else
  939. {
  940. llwarns << "Unrecognized element " << bone->getName() << " in skeletal distortion" << llendl;
  941. continue;
  942. }
  943. }
  944. return TRUE;
  945. }
  946. //-----------------------------------------------------------------------------
  947. // LLPolySkeletalDistortion()
  948. //-----------------------------------------------------------------------------
  949. LLPolySkeletalDistortion::LLPolySkeletalDistortion(LLVOAvatar *avatarp)
  950. {
  951. mAvatar = avatarp;
  952. mDefaultVec.setVec(0.001f, 0.001f, 0.001f);
  953. }
  954. //-----------------------------------------------------------------------------
  955. // ~LLPolySkeletalDistortion()
  956. //-----------------------------------------------------------------------------
  957. LLPolySkeletalDistortion::~LLPolySkeletalDistortion()
  958. {
  959. }
  960. BOOL LLPolySkeletalDistortion::setInfo(LLPolySkeletalDistortionInfo *info)
  961. {
  962. llassert(mInfo == NULL);
  963. if (info->mID < 0)
  964. return FALSE;
  965. mInfo = info;
  966. mID = info->mID;
  967. setWeight(getDefaultWeight(), FALSE );
  968. LLPolySkeletalDistortionInfo::bone_info_list_t::iterator iter;
  969. for (iter = getInfo()->mBoneInfoList.begin(); iter != getInfo()->mBoneInfoList.end(); iter++)
  970. {
  971. LLPolySkeletalBoneInfo *bone_info = &(*iter);
  972. LLJoint* joint = mAvatar->getJoint(bone_info->mBoneName);
  973. if (!joint)
  974. {
  975. llwarns << "Joint " << bone_info->mBoneName << " not found." << llendl;
  976. continue;
  977. }
  978. if (mJointScales.find(joint) != mJointScales.end())
  979. {
  980. llwarns << "Scale deformation already supplied for joint " << joint->getName() << "." << llendl;
  981. }
  982. // store it
  983. mJointScales[joint] = bone_info->mScaleDeformation;
  984. // apply to children that need to inherit it
  985. for (LLJoint::child_list_t::iterator iter = joint->mChildren.begin();
  986. iter != joint->mChildren.end(); ++iter)
  987. {
  988. LLViewerJoint* child_joint = (LLViewerJoint*)(*iter);
  989. if (child_joint->inheritScale())
  990. {
  991. LLVector3 childDeformation = LLVector3(child_joint->getScale());
  992. childDeformation.scaleVec(bone_info->mScaleDeformation);
  993. mJointScales[child_joint] = childDeformation;
  994. }
  995. }
  996. if (bone_info->mHasPositionDeformation)
  997. {
  998. if (mJointOffsets.find(joint) != mJointOffsets.end())
  999. {
  1000. llwarns << "Offset deformation already supplied for joint " << joint->getName() << "." << llendl;
  1001. }
  1002. mJointOffsets[joint] = bone_info->mPositionDeformation;
  1003. }
  1004. }
  1005. return TRUE;
  1006. }
  1007. /*virtual*/ LLViewerVisualParam* LLPolySkeletalDistortion::cloneParam(LLWearable* wearable) const
  1008. {
  1009. LLPolySkeletalDistortion *new_param = new LLPolySkeletalDistortion(mAvatar);
  1010. *new_param = *this;
  1011. return new_param;
  1012. }
  1013. //-----------------------------------------------------------------------------
  1014. // apply()
  1015. //-----------------------------------------------------------------------------
  1016. void LLPolySkeletalDistortion::apply( ESex avatar_sex )
  1017. {
  1018. F32 effective_weight = ( getSex() & avatar_sex ) ? mCurWeight : getDefaultWeight();
  1019. LLJoint* joint;
  1020. joint_vec_map_t::iterator iter;
  1021. for (iter = mJointScales.begin();
  1022. iter != mJointScales.end();
  1023. iter++)
  1024. {
  1025. joint = iter->first;
  1026. LLVector3 newScale = joint->getScale();
  1027. LLVector3 scaleDelta = iter->second;
  1028. newScale = newScale + (effective_weight * scaleDelta) - (mLastWeight * scaleDelta);
  1029. joint->setScale(newScale);
  1030. }
  1031. for (iter = mJointOffsets.begin();
  1032. iter != mJointOffsets.end();
  1033. iter++)
  1034. {
  1035. joint = iter->first;
  1036. LLVector3 newPosition = joint->getPosition();
  1037. LLVector3 positionDelta = iter->second;
  1038. newPosition = newPosition + (effective_weight * positionDelta) - (mLastWeight * positionDelta);
  1039. joint->setPosition(newPosition);
  1040. }
  1041. if (mLastWeight != mCurWeight && !mIsAnimating)
  1042. {
  1043. mAvatar->setSkeletonSerialNum(mAvatar->getSkeletonSerialNum() + 1);
  1044. }
  1045. mLastWeight = mCurWeight;
  1046. }
  1047. LLPolyMorphData *clone_morph_param_duplicate(const LLPolyMorphData *src_data,
  1048. const std::string &name)
  1049. {
  1050. LLPolyMorphData* cloned_morph_data = new LLPolyMorphData(*src_data);
  1051. cloned_morph_data->mName = name;
  1052. for (U32 v=0; v < cloned_morph_data->mNumIndices; v++)
  1053. {
  1054. cloned_morph_data->mCoords[v] = src_data->mCoords[v];
  1055. cloned_morph_data->mNormals[v] = src_data->mNormals[v];
  1056. cloned_morph_data->mBinormals[v] = src_data->mBinormals[v];
  1057. }
  1058. return cloned_morph_data;
  1059. }
  1060. LLPolyMorphData *clone_morph_param_direction(const LLPolyMorphData *src_data,
  1061. const LLVector3 &direction,
  1062. const std::string &name)
  1063. {
  1064. LLPolyMorphData* cloned_morph_data = new LLPolyMorphData(*src_data);
  1065. cloned_morph_data->mName = name;
  1066. for (U32 v=0; v < cloned_morph_data->mNumIndices; v++)
  1067. {
  1068. cloned_morph_data->mCoords[v] = direction;
  1069. cloned_morph_data->mNormals[v] = LLVector3(0,0,0);
  1070. cloned_morph_data->mBinormals[v] = LLVector3(0,0,0);
  1071. }
  1072. return cloned_morph_data;
  1073. }
  1074. LLPolyMorphData *clone_morph_param_cleavage(const LLPolyMorphData *src_data,
  1075. F32 scale,
  1076. const std::string &name)
  1077. {
  1078. LLPolyMorphData* cloned_morph_data = new LLPolyMorphData(*src_data);
  1079. cloned_morph_data->mName = name;
  1080. for (U32 v=0; v < cloned_morph_data->mNumIndices; v++)
  1081. {
  1082. cloned_morph_data->mCoords[v] = src_data->mCoords[v]*scale;
  1083. cloned_morph_data->mNormals[v] = src_data->mNormals[v]*scale;
  1084. cloned_morph_data->mBinormals[v] = src_data->mBinormals[v]*scale;
  1085. if (cloned_morph_data->mCoords[v][1] < 0)
  1086. {
  1087. cloned_morph_data->mCoords[v][1] *= -1;
  1088. cloned_morph_data->mNormals[v][1] *= -1;
  1089. cloned_morph_data->mBinormals[v][1] *= -1;
  1090. }
  1091. }
  1092. return cloned_morph_data;
  1093. }
  1094. // End