llpolymesh.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. /**
  2. * @file llpolymesh.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 "llpolymesh.h"
  33. #include "llavatarappearance.h"
  34. #include "lldir.h"
  35. #include "llendianswizzle.h"
  36. #include "llfasttimer.h"
  37. #include "llvolume.h"
  38. #include "llwearable.h"
  39. #include "llxmltree.h"
  40. #define HEADER_ASCII "Linden Mesh 1.0"
  41. #define HEADER_BINARY "Linden Binary Mesh 1.0"
  42. LLPolyMorphData* clone_morph_param_duplicate(const LLPolyMorphData* src_data,
  43. const std::string& name);
  44. LLPolyMorphData* clone_morph_param_direction(const LLPolyMorphData* src_data,
  45. const LLVector3& direction,
  46. const std::string& name);
  47. LLPolyMorphData* clone_morph_param_cleavage(const LLPolyMorphData* src_data,
  48. F32 scale,
  49. const std::string& name);
  50. //-----------------------------------------------------------------------------
  51. // Global table of loaded LLPolyMeshes
  52. //-----------------------------------------------------------------------------
  53. LLPolyMesh::LLPolyMeshSharedDataTable LLPolyMesh::sGlobalSharedMeshList;
  54. //-----------------------------------------------------------------------------
  55. // LLPolyMeshSharedData class
  56. //-----------------------------------------------------------------------------
  57. LLPolyMeshSharedData::LLPolyMeshSharedData()
  58. {
  59. mNumVertices = 0;
  60. mBaseCoords = NULL;
  61. mBaseNormals = NULL;
  62. mBaseBinormals = NULL;
  63. mTexCoords = NULL;
  64. mDetailTexCoords = NULL;
  65. mWeights = NULL;
  66. mHasWeights = false;
  67. mHasDetailTexCoords = false;
  68. mNumFaces = 0;
  69. mFaces = NULL;
  70. mNumJointNames = 0;
  71. mJointNames = NULL;
  72. mTriangleIndices = NULL;
  73. mNumTriangleIndices = 0;
  74. mReferenceData = NULL;
  75. mLastIndexOffset = -1;
  76. }
  77. LLPolyMeshSharedData::~LLPolyMeshSharedData()
  78. {
  79. freeMeshData();
  80. for_each(mMorphData.begin(), mMorphData.end(), DeletePointer());
  81. mMorphData.clear();
  82. }
  83. void LLPolyMeshSharedData::setupLOD(LLPolyMeshSharedData* reference_data)
  84. {
  85. mReferenceData = reference_data;
  86. if (reference_data)
  87. {
  88. mBaseCoords = reference_data->mBaseCoords;
  89. mBaseNormals = reference_data->mBaseNormals;
  90. mBaseBinormals = reference_data->mBaseBinormals;
  91. mTexCoords = reference_data->mTexCoords;
  92. mDetailTexCoords = reference_data->mDetailTexCoords;
  93. mWeights = reference_data->mWeights;
  94. mHasWeights = reference_data->mHasWeights;
  95. mHasDetailTexCoords = reference_data->mHasDetailTexCoords;
  96. }
  97. }
  98. void LLPolyMeshSharedData::freeMeshData()
  99. {
  100. if (!mReferenceData)
  101. {
  102. freeVertexData();
  103. }
  104. mNumFaces = 0;
  105. if (mFaces)
  106. {
  107. delete[] mFaces;
  108. mFaces = NULL;
  109. }
  110. mNumJointNames = 0;
  111. if (mJointNames)
  112. {
  113. delete[] mJointNames;
  114. mJointNames = NULL;
  115. }
  116. if (mTriangleIndices)
  117. {
  118. delete[] mTriangleIndices;
  119. mTriangleIndices = NULL;
  120. }
  121. }
  122. // compate_int is used by the qsort function to sort the index array
  123. S32 compare_int(const void* a, const void* b);
  124. #if 0 // Dead code
  125. void LLPolyMeshSharedData::genIndices(S32 index_offset)
  126. {
  127. if (index_offset == mLastIndexOffset)
  128. {
  129. return;
  130. }
  131. if (mTriangleIndices)
  132. {
  133. delete[] mTriangleIndices;
  134. }
  135. mTriangleIndices = new U32[mNumTriangleIndices];
  136. S32 cur_index = 0;
  137. for (S32 i = 0; i < mNumFaces; ++i)
  138. {
  139. mTriangleIndices[cur_index++] = mFaces[i][0] + index_offset;
  140. mTriangleIndices[cur_index++] = mFaces[i][1] + index_offset;
  141. mTriangleIndices[cur_index++] = mFaces[i][2] + index_offset;
  142. }
  143. mLastIndexOffset = index_offset;
  144. }
  145. #endif
  146. U32 LLPolyMeshSharedData::getNumKB()
  147. {
  148. U32 bytes = sizeof(LLPolyMesh);
  149. if (!isLOD())
  150. {
  151. bytes += mNumVertices * (sizeof(LLVector3) + // coords
  152. sizeof(LLVector3) + // normals
  153. sizeof(LLVector2)); // texCoords
  154. }
  155. if (mHasDetailTexCoords && !isLOD())
  156. {
  157. bytes += mNumVertices * sizeof(LLVector2); // detailTexCoords
  158. }
  159. if (mHasWeights && !isLOD())
  160. {
  161. bytes += mNumVertices * sizeof(float); // weights
  162. }
  163. bytes += mNumFaces * sizeof(LLPolyFace); // faces
  164. return bytes / 1024;
  165. }
  166. bool LLPolyMeshSharedData::allocateVertexData(U32 n_vertices)
  167. {
  168. mBaseCoords =(LLVector4a*)allocate_volume_mem(n_vertices * sizeof(LLVector4a));
  169. if (!mBaseCoords)
  170. {
  171. freeVertexData();
  172. return false;
  173. }
  174. mBaseNormals = (LLVector4a*)allocate_volume_mem(n_vertices * sizeof(LLVector4a));
  175. if (!mBaseNormals)
  176. {
  177. freeVertexData();
  178. return false;
  179. }
  180. mBaseBinormals = (LLVector4a*)allocate_volume_mem(n_vertices * sizeof(LLVector4a));
  181. if (!mBaseBinormals)
  182. {
  183. freeVertexData();
  184. return false;
  185. }
  186. S32 tex_size = (n_vertices * sizeof(LLVector2) + 0xF) & ~0xF;
  187. mTexCoords = (LLVector2*)allocate_volume_mem(tex_size);
  188. if (!mTexCoords)
  189. {
  190. freeVertexData();
  191. return false;
  192. }
  193. mDetailTexCoords = (LLVector2*)allocate_volume_mem(n_vertices * sizeof(LLVector2));
  194. if (!mDetailTexCoords)
  195. {
  196. freeVertexData();
  197. return false;
  198. }
  199. mWeights = (F32*)allocate_volume_mem(n_vertices * sizeof(F32));
  200. if (!mWeights)
  201. {
  202. freeVertexData();
  203. return false;
  204. }
  205. for (U32 i = 0; i < n_vertices; ++i)
  206. {
  207. mBaseCoords[i].clear();
  208. mBaseNormals[i].clear();
  209. mBaseBinormals[i].clear();
  210. mTexCoords[i].clear();
  211. mWeights[i] = 0.f;
  212. }
  213. mNumVertices = n_vertices;
  214. return true;
  215. }
  216. void LLPolyMeshSharedData::freeVertexData()
  217. {
  218. mNumVertices = 0;
  219. if (mBaseCoords)
  220. {
  221. free_volume_mem(mBaseCoords);
  222. mBaseCoords = NULL;
  223. }
  224. if (mBaseNormals)
  225. {
  226. free_volume_mem(mBaseNormals);
  227. mBaseNormals = NULL;
  228. }
  229. if (mBaseBinormals)
  230. {
  231. free_volume_mem(mBaseBinormals);
  232. mBaseBinormals = NULL;
  233. }
  234. if (mTexCoords)
  235. {
  236. free_volume_mem(mTexCoords);
  237. mTexCoords = NULL;
  238. }
  239. if (mDetailTexCoords)
  240. {
  241. free_volume_mem(mDetailTexCoords);
  242. mDetailTexCoords = NULL;
  243. }
  244. if (mWeights)
  245. {
  246. free_volume_mem(mWeights);
  247. mWeights = NULL;
  248. }
  249. }
  250. bool LLPolyMeshSharedData::allocateFaceData(U32 n_faces)
  251. {
  252. mFaces = new LLPolyFace[n_faces];
  253. if (!mFaces)
  254. {
  255. return false;
  256. }
  257. mNumFaces = n_faces;
  258. mNumTriangleIndices = mNumFaces * 3;
  259. return true;
  260. }
  261. bool LLPolyMeshSharedData::allocateJointNames(U32 numJointNames)
  262. {
  263. mJointNames = new std::string[numJointNames];
  264. if (!mJointNames)
  265. {
  266. return false;
  267. }
  268. mNumJointNames = numJointNames;
  269. return true;
  270. }
  271. bool LLPolyMeshSharedData::loadMesh(const std::string& filename)
  272. {
  273. //-------------------------------------------------------------------------
  274. // Open the file
  275. //-------------------------------------------------------------------------
  276. if (filename.empty())
  277. {
  278. llwarns << "Filename is Empty !" << llendl;
  279. llassert(false);
  280. return false;
  281. }
  282. LLFILE* fp = LLFile::open(filename, "rb");
  283. if (!fp)
  284. {
  285. llwarns << "Cannot open: " << filename << llendl;
  286. llassert(false);
  287. return false;
  288. }
  289. //-------------------------------------------------------------------------
  290. // Read a chunk
  291. //-------------------------------------------------------------------------
  292. char header[128];
  293. if (fread(header, 1, 128, fp) != 128)
  294. {
  295. llwarns << "Short read" << llendl;
  296. }
  297. //-------------------------------------------------------------------------
  298. // Check for proper binary header
  299. //-------------------------------------------------------------------------
  300. bool status = false;
  301. if (strncmp(header, HEADER_BINARY, strlen(HEADER_BINARY)) == 0)
  302. {
  303. LL_DEBUGS("PolyMesh") << "Loading " << filename << LL_ENDL;
  304. //---------------------------------------------------------------------
  305. // File Header (seek past it)
  306. //---------------------------------------------------------------------
  307. fseek(fp, 24, SEEK_SET);
  308. //---------------------------------------------------------------------
  309. // HasWeights
  310. //---------------------------------------------------------------------
  311. U8 has_weights;
  312. size_t num_read = fread(&has_weights, sizeof(U8), 1, fp);
  313. if (num_read != 1)
  314. {
  315. llwarns << "Cannot read HasWeights flag from " << filename
  316. << llendl;
  317. llassert(false);
  318. goto abortion;
  319. }
  320. if (!isLOD())
  321. {
  322. mHasWeights = has_weights != 0;
  323. }
  324. //---------------------------------------------------------------------
  325. // HasDetailTexCoords
  326. //---------------------------------------------------------------------
  327. U8 has_detail_tcoords;
  328. num_read = fread(&has_detail_tcoords, sizeof(U8), 1, fp);
  329. if (num_read != 1)
  330. {
  331. llwarns << "Cannot read HasDetailTexCoords flag from " << filename
  332. << llendl;
  333. llassert(false);
  334. goto abortion;
  335. }
  336. //---------------------------------------------------------------------
  337. // Position
  338. //---------------------------------------------------------------------
  339. LLVector3 position;
  340. num_read = fread(position.mV, sizeof(float), 3, fp);
  341. llendianswizzle(position.mV, sizeof(float), 3);
  342. if (num_read != 3)
  343. {
  344. llwarns << "Cannot read Position from " << filename << llendl;
  345. llassert(false);
  346. goto abortion;
  347. }
  348. setPosition(position);
  349. //---------------------------------------------------------------------
  350. // Rotation
  351. //---------------------------------------------------------------------
  352. LLVector3 rot_angles;
  353. num_read = fread(rot_angles.mV, sizeof(float), 3, fp);
  354. llendianswizzle(rot_angles.mV, sizeof(float), 3);
  355. if (num_read != 3)
  356. {
  357. llwarns << "Cannot read RotationAngles from " << filename
  358. << llendl;
  359. llassert(false);
  360. goto abortion;
  361. }
  362. U8 rot_order;
  363. num_read = fread(&rot_order, sizeof(U8), 1, fp);
  364. if (num_read != 1)
  365. {
  366. llwarns << "Cannot read RotationOrder from " << filename
  367. << llendl;
  368. llassert(false);
  369. goto abortion;
  370. }
  371. rot_order = 0;
  372. setRotation(mayaQ(rot_angles.mV[0], rot_angles.mV[1], rot_angles.mV[2],
  373. (LLQuaternion::Order)rot_order));
  374. //---------------------------------------------------------------------
  375. // Scale
  376. //---------------------------------------------------------------------
  377. LLVector3 scale;
  378. num_read = fread(scale.mV, sizeof(float), 3, fp);
  379. llendianswizzle(scale.mV, sizeof(float), 3);
  380. if (num_read != 3)
  381. {
  382. llwarns << "Cannot read Scale from " << filename << llendl;
  383. llassert(false);
  384. goto abortion;
  385. }
  386. setScale(scale);
  387. //---------------------------------------------------------------------
  388. // Release any existing mesh geometry
  389. //---------------------------------------------------------------------
  390. freeMeshData();
  391. //---------------------------------------------------------------------
  392. // NumVertices
  393. //---------------------------------------------------------------------
  394. U16 n_vertices = 0;
  395. if (!isLOD())
  396. {
  397. num_read = fread(&n_vertices, sizeof(U16), 1, fp);
  398. llendianswizzle(&n_vertices, sizeof(U16), 1);
  399. if (num_read != 1)
  400. {
  401. llwarns << "Cannot read NumVertices from " << filename
  402. << llendl;
  403. llassert(false);
  404. goto abortion;
  405. }
  406. if (!allocateVertexData(n_vertices))
  407. {
  408. llwarns << "Can't allocate vertex data: out of memory ?"
  409. << llendl;
  410. llassert(false);
  411. goto abortion;
  412. }
  413. // Coords
  414. for (U16 i = 0; i < n_vertices; ++i)
  415. {
  416. num_read = fread(&mBaseCoords[i], sizeof(float), 3, fp);
  417. llendianswizzle(&mBaseCoords[i], sizeof(float), 3);
  418. if (num_read != 3)
  419. {
  420. llwarns << "Cannot read Coordinates from " << filename
  421. << llendl;
  422. llassert(false);
  423. goto abortion;
  424. }
  425. }
  426. // Normals
  427. for (U16 i = 0; i < n_vertices; ++i)
  428. {
  429. num_read = fread(&mBaseNormals[i], sizeof(float), 3, fp);
  430. llendianswizzle(&mBaseNormals[i], sizeof(float), 3);
  431. if (num_read != 3)
  432. {
  433. llwarns << "Cannot read Normals from " << filename
  434. << llendl;
  435. llassert(false);
  436. goto abortion;
  437. }
  438. }
  439. // Binormals
  440. for (U16 i = 0; i < n_vertices; ++i)
  441. {
  442. num_read = fread(&mBaseBinormals[i], sizeof(float), 3, fp);
  443. llendianswizzle(&mBaseBinormals[i], sizeof(float), 3);
  444. if (num_read != 3)
  445. {
  446. llwarns << "Cannot read Binormals from " << filename
  447. << llendl;
  448. llassert(false);
  449. goto abortion;
  450. }
  451. }
  452. // TexCoords
  453. num_read = fread(mTexCoords, 2 * sizeof(float), n_vertices, fp);
  454. llendianswizzle(mTexCoords, sizeof(float), 2 * n_vertices);
  455. if (num_read != n_vertices)
  456. {
  457. llwarns << "Cannot read TexCoords from " << filename << llendl;
  458. llassert(false);
  459. goto abortion;
  460. }
  461. // DetailTexCoords
  462. if (mHasDetailTexCoords)
  463. {
  464. num_read = fread(mDetailTexCoords, 2 * sizeof(float),
  465. n_vertices, fp);
  466. llendianswizzle(mDetailTexCoords, sizeof(float),
  467. 2 * n_vertices);
  468. if (num_read != n_vertices)
  469. {
  470. llwarns << "Cannot read DetailTexCoords from " << filename
  471. << llendl;
  472. llassert(false);
  473. goto abortion;
  474. }
  475. }
  476. // Weights
  477. if (mHasWeights)
  478. {
  479. num_read = fread(mWeights, sizeof(float), n_vertices, fp);
  480. llendianswizzle(mWeights, sizeof(float), n_vertices);
  481. if (num_read != n_vertices)
  482. {
  483. llwarns << "Cannot read Weights from " << filename
  484. << llendl;
  485. llassert(false);
  486. goto abortion;
  487. }
  488. }
  489. }
  490. //---------------------------------------------------------------------
  491. // NumFaces
  492. //---------------------------------------------------------------------
  493. U16 n_faces;
  494. num_read = fread(&n_faces, sizeof(U16), 1, fp);
  495. llendianswizzle(&n_faces, sizeof(U16), 1);
  496. if (num_read != 1)
  497. {
  498. llwarns << "Cannot read NumFaces from " << filename << llendl;
  499. llassert(false);
  500. goto abortion;
  501. }
  502. allocateFaceData(n_faces);
  503. //---------------------------------------------------------------------
  504. // Faces
  505. //---------------------------------------------------------------------
  506. U32 i;
  507. U32 n_tris = 0;
  508. for (i = 0; i < n_faces; ++i)
  509. {
  510. S16 face[3];
  511. num_read = fread(face, sizeof(U16), 3, fp);
  512. llendianswizzle(face, sizeof(U16), 3);
  513. if (num_read != 3)
  514. {
  515. llwarns << "Cannot read Face[" << i << "] from " << filename
  516. << llendl;
  517. llassert(false);
  518. goto abortion;
  519. }
  520. if (mReferenceData)
  521. {
  522. llassert(face[0] < mReferenceData->mNumVertices);
  523. llassert(face[1] < mReferenceData->mNumVertices);
  524. llassert(face[2] < mReferenceData->mNumVertices);
  525. }
  526. if (isLOD())
  527. {
  528. // store largest index in case of LODs
  529. for (S32 j = 0; j < 3; ++j)
  530. {
  531. if (face[j] > mNumVertices - 1)
  532. {
  533. mNumVertices = face[j] + 1;
  534. }
  535. }
  536. }
  537. mFaces[i][0] = face[0];
  538. mFaces[i][1] = face[1];
  539. mFaces[i][2] = face[2];
  540. ++n_tris;
  541. }
  542. LL_DEBUGS("PolyMesh") << "verts: " << n_vertices << ", faces: "
  543. << n_faces << ", tris: " << n_tris << LL_ENDL;
  544. //---------------------------------------------------------------------
  545. // NumSkinJoints
  546. //---------------------------------------------------------------------
  547. if (!isLOD())
  548. {
  549. U16 n_skin_joints = 0;
  550. if (mHasWeights)
  551. {
  552. num_read = fread(&n_skin_joints, sizeof(U16), 1, fp);
  553. llendianswizzle(&n_skin_joints, sizeof(U16), 1);
  554. if (num_read != 1)
  555. {
  556. llwarns << "Cannot read NumSkinJoints from " << filename
  557. << llendl;
  558. llassert(false);
  559. goto abortion;
  560. }
  561. allocateJointNames(n_skin_joints);
  562. }
  563. //----------------------------------------------------------------
  564. // SkinJoints
  565. //----------------------------------------------------------------
  566. char joint_name[65];
  567. for (i = 0; i < n_skin_joints; ++i)
  568. {
  569. num_read = fread(joint_name, 64, 1, fp);
  570. joint_name[64] = '\0'; // ensure nul-termination
  571. if (num_read != 1)
  572. {
  573. llwarns << "Cannot read Skin[" << i << "].Name from "
  574. << filename << llendl;
  575. llassert(false);
  576. goto abortion;
  577. }
  578. std::string* jn = &mJointNames[i];
  579. *jn = joint_name;
  580. }
  581. //-----------------------------------------------------------------
  582. // Look for morph section
  583. //-----------------------------------------------------------------
  584. char morph_name[65];
  585. morph_name[64] = '\0'; // Ensure nul-termination
  586. while (fread(morph_name, 1, 64, fp) == 64)
  587. {
  588. if (!strcmp(morph_name, "End Morphs"))
  589. {
  590. // We reached the end of the morphs
  591. break;
  592. }
  593. LLPolyMorphData* morph_data = new LLPolyMorphData(morph_name);
  594. if (!morph_data || !morph_data->isSuccesfullyAllocated())
  595. {
  596. llwarns << "Failure to allocate new morph data for "
  597. << morph_name << llendl;
  598. llassert(false);
  599. goto abortion;
  600. }
  601. if (!morph_data->loadBinary(fp, this))
  602. {
  603. delete morph_data;
  604. continue;
  605. }
  606. mMorphData.insert(morph_data);
  607. LLPolyMorphData* cloned_data;
  608. if (!strcmp(morph_name, "Breast_Female_Cleavage"))
  609. {
  610. cloned_data =
  611. clone_morph_param_cleavage(morph_data, 0.75f,
  612. "Breast_Physics_LeftRight_Driven");
  613. if (!cloned_data || !cloned_data->isSuccesfullyAllocated())
  614. {
  615. llwarns << "Failure to clone morph data" << llendl;
  616. llassert(false);
  617. goto abortion;
  618. }
  619. mMorphData.insert(cloned_data);
  620. cloned_data =
  621. clone_morph_param_duplicate(morph_data,
  622. "Breast_Physics_InOut_Driven");
  623. if (!cloned_data || !cloned_data->isSuccesfullyAllocated())
  624. {
  625. llwarns << "Failure to clone morph data" << llendl;
  626. llassert(false);
  627. goto abortion;
  628. }
  629. mMorphData.insert(cloned_data);
  630. }
  631. if (!strcmp(morph_name, "Breast_Gravity"))
  632. {
  633. cloned_data =
  634. clone_morph_param_duplicate(morph_data,
  635. "Breast_Physics_UpDown_Driven");
  636. if (!cloned_data || !cloned_data->isSuccesfullyAllocated())
  637. {
  638. llwarns << "Failure to clone morph data" << llendl;
  639. llassert(false);
  640. goto abortion;
  641. }
  642. mMorphData.insert(cloned_data);
  643. }
  644. if (!strcmp(morph_name, "Big_Belly_Torso"))
  645. {
  646. cloned_data =
  647. clone_morph_param_direction(morph_data,
  648. LLVector3(0, 0, 0.05f),
  649. "Belly_Physics_Torso_UpDown_Driven");
  650. if (!cloned_data || !cloned_data->isSuccesfullyAllocated())
  651. {
  652. llwarns << "Failure to clone morph data" << llendl;
  653. llassert(false);
  654. goto abortion;
  655. }
  656. mMorphData.insert(cloned_data);
  657. }
  658. if (!strcmp(morph_name, "Big_Belly_Legs"))
  659. {
  660. cloned_data =
  661. clone_morph_param_direction(morph_data,
  662. LLVector3(0, 0, 0.05f),
  663. "Belly_Physics_Legs_UpDown_Driven");
  664. if (!cloned_data || !cloned_data->isSuccesfullyAllocated())
  665. {
  666. llwarns << "Failure to clone morph data" << llendl;
  667. llassert(false);
  668. goto abortion;
  669. }
  670. mMorphData.insert(cloned_data);
  671. }
  672. if (!strcmp(morph_name, "skirt_belly"))
  673. {
  674. cloned_data =
  675. clone_morph_param_direction(morph_data,
  676. LLVector3(0, 0, 0.05f),
  677. "Belly_Physics_Skirt_UpDown_Driven");
  678. if (!cloned_data || !cloned_data->isSuccesfullyAllocated())
  679. {
  680. llwarns << "Failure to clone morph data" << llendl;
  681. llassert(false);
  682. goto abortion;
  683. }
  684. mMorphData.insert(cloned_data);
  685. }
  686. if (!strcmp(morph_name, "Small_Butt"))
  687. {
  688. cloned_data =
  689. clone_morph_param_direction(morph_data,
  690. LLVector3(0, 0, 0.05f),
  691. "Butt_Physics_UpDown_Driven");
  692. if (!cloned_data || !cloned_data->isSuccesfullyAllocated())
  693. {
  694. llwarns << "Failure to clone morph data" << llendl;
  695. llassert(false);
  696. goto abortion;
  697. }
  698. mMorphData.insert(cloned_data);
  699. cloned_data =
  700. clone_morph_param_direction(morph_data,
  701. LLVector3(0, 0.03f, 0),
  702. "Butt_Physics_LeftRight_Driven");
  703. if (!cloned_data || !cloned_data->isSuccesfullyAllocated())
  704. {
  705. llwarns << "Failure to clone morph data" << llendl;
  706. llassert(false);
  707. goto abortion;
  708. }
  709. mMorphData.insert(cloned_data);
  710. }
  711. }
  712. S32 n_remaps;
  713. if (fread(&n_remaps, sizeof(S32), 1, fp) == 1)
  714. {
  715. llendianswizzle(&n_remaps, sizeof(S32), 1);
  716. for (S32 i = 0; i < n_remaps; ++i)
  717. {
  718. S32 remap_src;
  719. S32 remap_dst;
  720. if (fread(&remap_src, sizeof(S32), 1, fp) != 1)
  721. {
  722. llwarns << "Cannot read source vertex in vertex remap data"
  723. << llendl;
  724. llassert(false);
  725. goto abortion;
  726. }
  727. if (fread(&remap_dst, sizeof(S32), 1, fp) != 1)
  728. {
  729. llwarns << "Cannot read destination vertex in vertex remap data"
  730. << llendl;
  731. llassert(false);
  732. goto abortion;
  733. }
  734. llendianswizzle(&remap_src, sizeof(S32), 1);
  735. llendianswizzle(&remap_dst, sizeof(S32), 1);
  736. mSharedVerts[remap_src] = remap_dst;
  737. }
  738. }
  739. }
  740. status = true;
  741. }
  742. else
  743. {
  744. llwarns << "Invalid mesh file header: " << filename << llendl;
  745. }
  746. if (mNumJointNames == 0)
  747. {
  748. allocateJointNames(1);
  749. }
  750. abortion:
  751. LLFile::close(fp);
  752. return status;
  753. }
  754. const S32* LLPolyMeshSharedData::getSharedVert(S32 vert)
  755. {
  756. if (mSharedVerts.count(vert) > 0)
  757. {
  758. return &mSharedVerts[vert];
  759. }
  760. return NULL;
  761. }
  762. const LLVector2 &LLPolyMeshSharedData::getUVs(U32 index)
  763. {
  764. // TODO: convert all index variables to S32
  765. llassert((S32)index < mNumVertices);
  766. return mTexCoords[index];
  767. }
  768. //-----------------------------------------------------------------------------
  769. // LLPolyMesh class
  770. //-----------------------------------------------------------------------------
  771. LLPolyMesh::LLPolyMesh(LLPolyMeshSharedData* shared_data,
  772. LLPolyMesh* reference_mesh)
  773. {
  774. llassert(shared_data);
  775. mSharedData = shared_data;
  776. mReferenceMesh = reference_mesh;
  777. mAvatarp = NULL;
  778. mVertexData = NULL;
  779. mCurVertexCount = 0;
  780. mFaceIndexCount = 0;
  781. mFaceIndexOffset = 0;
  782. mFaceVertexCount = 0;
  783. mFaceVertexOffset = 0;
  784. if (shared_data->isLOD() && reference_mesh)
  785. {
  786. mCoords = reference_mesh->mCoords;
  787. mNormals = reference_mesh->mNormals;
  788. mScaledNormals = reference_mesh->mScaledNormals;
  789. mBinormals = reference_mesh->mBinormals;
  790. mScaledBinormals = reference_mesh->mScaledBinormals;
  791. mTexCoords = reference_mesh->mTexCoords;
  792. mClothingWeights = reference_mesh->mClothingWeights;
  793. }
  794. else
  795. {
  796. // Allocate memory without initializing every vector
  797. // NOTE: This makes assumptions about the size of LLVector[234]
  798. S32 nverts = mSharedData->mNumVertices;
  799. // make sure it's an even number of verts for alignment
  800. nverts += nverts % 2;
  801. S32 nfloats = nverts * (4 + //coords
  802. 4 + //normals
  803. 4 + //weights
  804. 2 + //coords
  805. 4 + //scaled normals
  806. 4 + //binormals
  807. 4); //scaled binormals
  808. // use 16 byte aligned vertex data to make LLPolyMesh SSE friendly
  809. mVertexData = (F32*)allocate_volume_mem(nfloats * 4);
  810. if (!mVertexData)
  811. {
  812. llwarns << "Failure to allocate vertex data buffer !" << llendl;
  813. mCoords = mNormals = mClothingWeights = mScaledNormals =
  814. mBinormals = mScaledBinormals = NULL;
  815. mTexCoords = NULL;
  816. return;
  817. }
  818. S32 offset = 0;
  819. mCoords = (LLVector4a*)(mVertexData + offset); offset += 4 * nverts;
  820. mNormals = (LLVector4a*)(mVertexData + offset); offset += 4 * nverts;
  821. mClothingWeights = (LLVector4a*)(mVertexData + offset); offset += 4 * nverts;
  822. mTexCoords = (LLVector2*)(mVertexData + offset); offset += 2 * nverts;
  823. mScaledNormals = (LLVector4a*)(mVertexData + offset); offset += 4 * nverts;
  824. mBinormals = (LLVector4a*)(mVertexData + offset); offset += 4 * nverts;
  825. mScaledBinormals = (LLVector4a*)(mVertexData + offset); offset += 4 * nverts;
  826. initializeForMorph();
  827. }
  828. }
  829. LLPolyMesh::~LLPolyMesh()
  830. {
  831. delete_and_clear(mJointRenderData);
  832. if (mVertexData)
  833. {
  834. free_volume_mem(mVertexData);
  835. mVertexData = NULL;
  836. }
  837. }
  838. LLPolyMesh* LLPolyMesh::getMesh(const std::string& name,
  839. LLPolyMesh* reference_mesh)
  840. {
  841. //-------------------------------------------------------------------------
  842. // Search for an existing mesh by this name
  843. //-------------------------------------------------------------------------
  844. LLPolyMeshSharedData* meshSharedData = get_ptr_in_map(sGlobalSharedMeshList,
  845. name);
  846. if (meshSharedData)
  847. {
  848. LL_DEBUGS("PolyMesh") << "Polymesh " << name
  849. << " found in global mesh table." << LL_ENDL;
  850. LLPolyMesh* poly_mesh = new LLPolyMesh(meshSharedData, reference_mesh);
  851. return poly_mesh;
  852. }
  853. //-------------------------------------------------------------------------
  854. // If not found, create a new one, add it to the list
  855. //-------------------------------------------------------------------------
  856. std::string full_path = gDirUtil.getFullPath(LL_PATH_CHARACTER, name);
  857. LLPolyMeshSharedData* mesh_data = new LLPolyMeshSharedData();
  858. if (reference_mesh)
  859. {
  860. mesh_data->setupLOD(reference_mesh->getSharedData());
  861. }
  862. if (!mesh_data->loadMesh(full_path))
  863. {
  864. delete mesh_data;
  865. return NULL;
  866. }
  867. LLPolyMesh* poly_mesh = new LLPolyMesh(mesh_data, reference_mesh);
  868. LL_DEBUGS("PolyMesh") << "Polymesh " << name
  869. << " added to global mesh table." << LL_ENDL;
  870. sGlobalSharedMeshList[name] = poly_mesh->mSharedData;
  871. return poly_mesh;
  872. }
  873. void LLPolyMesh::freeAllMeshes()
  874. {
  875. // Delete each item in the global lists
  876. for_each(sGlobalSharedMeshList.begin(), sGlobalSharedMeshList.end(),
  877. DeletePairedPointer());
  878. sGlobalSharedMeshList.clear();
  879. }
  880. void LLPolyMesh::dumpDiagInfo()
  881. {
  882. // keep track of totals
  883. U32 total_verts = 0;
  884. U32 total_faces = 0;
  885. U32 total_kb = 0;
  886. std::string buf;
  887. llinfos << "-----------------------------------------------------"
  888. << llendl;
  889. llinfos << " Global PolyMesh Table (DEBUG only)" << llendl;
  890. llinfos << " Verts Faces Mem(KB) Name" << llendl;
  891. llinfos << "-----------------------------------------------------"
  892. << llendl;
  893. // print each loaded mesh, and it's memory usage
  894. for (LLPolyMeshSharedDataTable::iterator iter = sGlobalSharedMeshList.begin();
  895. iter != sGlobalSharedMeshList.end(); ++iter)
  896. {
  897. const std::string& mesh_name = iter->first;
  898. LLPolyMeshSharedData* mesh = iter->second;
  899. S32 num_verts = mesh->mNumVertices;
  900. S32 num_faces = mesh->mNumFaces;
  901. U32 num_kb = mesh->getNumKB();
  902. buf = llformat("%8d %8d %8d %s", num_verts, num_faces, num_kb,
  903. mesh_name.c_str());
  904. llinfos << buf << llendl;
  905. total_verts += num_verts;
  906. total_faces += num_faces;
  907. total_kb += num_kb;
  908. }
  909. llinfos << "-----------------------------------------------------"
  910. << llendl;
  911. buf = llformat("%8d %8d %8d TOTAL", total_verts, total_faces, total_kb);
  912. llinfos << buf << llendl;
  913. llinfos << "-----------------------------------------------------"
  914. << llendl;
  915. }
  916. void LLPolyMesh::initializeForMorph()
  917. {
  918. S32 num_vertices = mSharedData->mNumVertices;
  919. LLVector4a::memcpyNonAliased16((F32*)mCoords,
  920. (F32*)mSharedData->mBaseCoords,
  921. sizeof(LLVector4a) * num_vertices);
  922. LLVector4a::memcpyNonAliased16((F32*)mNormals,
  923. (F32*)mSharedData->mBaseNormals,
  924. sizeof(LLVector4a) * num_vertices);
  925. LLVector4a::memcpyNonAliased16((F32*)mScaledNormals,
  926. (F32*)mSharedData->mBaseNormals,
  927. sizeof(LLVector4a) * num_vertices);
  928. LLVector4a::memcpyNonAliased16((F32*)mBinormals,
  929. (F32*)mSharedData->mBaseNormals,
  930. sizeof(LLVector4a) * num_vertices);
  931. LLVector4a::memcpyNonAliased16((F32*)mScaledBinormals,
  932. (F32*)mSharedData->mBaseNormals,
  933. sizeof(LLVector4a) * num_vertices);
  934. LLVector4a::memcpyNonAliased16((F32*)mTexCoords,
  935. (F32*)mSharedData->mTexCoords,
  936. sizeof(LLVector2) *
  937. (num_vertices + num_vertices % 2));
  938. for (S32 i = 0; i < num_vertices; ++i)
  939. {
  940. mClothingWeights[i].clear();
  941. }
  942. }
  943. LLPolyMorphData* LLPolyMesh::getMorphData(const std::string& morph_name)
  944. {
  945. if (mSharedData)
  946. {
  947. for (LLPolyMeshSharedData::morphdata_list_t::iterator
  948. iter = mSharedData->mMorphData.begin(),
  949. end = mSharedData->mMorphData.end();
  950. iter != end; ++iter)
  951. {
  952. LLPolyMorphData* morph_data = *iter;
  953. if (morph_data->getName() == morph_name)
  954. {
  955. return morph_data;
  956. }
  957. }
  958. }
  959. return NULL;
  960. }
  961. #if 0
  962. // Erasing but not deleting seems bad, but fortunately we do not actually use
  963. // this...
  964. void LLPolyMesh::removeMorphData(LLPolyMorphData* morph_target)
  965. {
  966. if (mSharedData)
  967. {
  968. mSharedData->mMorphData.erase(morph_target);
  969. }
  970. }
  971. void LLPolyMesh::deleteAllMorphData()
  972. {
  973. if (mSharedData)
  974. {
  975. for_each(mSharedData->mMorphData.begin(),
  976. mSharedData->mMorphData.end(), DeletePointer());
  977. mSharedData->mMorphData.clear();
  978. }
  979. }
  980. #endif