/indra/llprimitive/llmodel.h

https://bitbucket.org/lindenlab/viewer-beta/ · C++ Header · 281 lines · 190 code · 53 blank · 38 comment · 4 complexity · 6b54840555f5f3495af5de76a89f5a55 MD5 · raw file

  1. /**
  2. * @file llmodel.h
  3. * @brief Model handling class definitions
  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. #ifndef LL_LLMODEL_H
  27. #define LL_LLMODEL_H
  28. #include "llpointer.h"
  29. #include "llvolume.h"
  30. #include "v4math.h"
  31. #include "m4math.h"
  32. class daeElement;
  33. class domMesh;
  34. #define MAX_MODEL_FACES 8
  35. class LLMeshSkinInfo
  36. {
  37. public:
  38. LLUUID mMeshID;
  39. std::vector<std::string> mJointNames;
  40. std::vector<LLMatrix4> mInvBindMatrix;
  41. std::vector<LLMatrix4> mAlternateBindMatrix;
  42. std::map<std::string, U32> mJointMap;
  43. LLMeshSkinInfo() { }
  44. LLMeshSkinInfo(LLSD& data);
  45. void fromLLSD(LLSD& data);
  46. LLSD asLLSD(bool include_joints) const;
  47. LLMatrix4 mBindShapeMatrix;
  48. float mPelvisOffset;
  49. };
  50. class LLModel : public LLVolume
  51. {
  52. public:
  53. enum
  54. {
  55. LOD_IMPOSTOR = 0,
  56. LOD_LOW,
  57. LOD_MEDIUM,
  58. LOD_HIGH,
  59. LOD_PHYSICS,
  60. NUM_LODS
  61. };
  62. enum EModelStatus
  63. {
  64. NO_ERRORS = 0,
  65. VERTEX_NUMBER_OVERFLOW, //vertex number is >= 65535.
  66. BAD_ELEMENT,
  67. INVALID_STATUS
  68. } ;
  69. //convex_hull_decomposition is a vector of convex hulls
  70. //each convex hull is a set of points
  71. typedef std::vector<std::vector<LLVector3> > convex_hull_decomposition;
  72. typedef std::vector<LLVector3> hull;
  73. class PhysicsMesh
  74. {
  75. public:
  76. std::vector<LLVector3> mPositions;
  77. std::vector<LLVector3> mNormals;
  78. void clear()
  79. {
  80. mPositions.clear();
  81. mNormals.clear();
  82. }
  83. bool empty() const
  84. {
  85. return mPositions.empty();
  86. }
  87. };
  88. class Decomposition
  89. {
  90. public:
  91. Decomposition() { }
  92. Decomposition(LLSD& data);
  93. void fromLLSD(LLSD& data);
  94. LLSD asLLSD() const;
  95. bool hasHullList() const;
  96. void merge(const Decomposition* rhs);
  97. LLUUID mMeshID;
  98. LLModel::convex_hull_decomposition mHull;
  99. LLModel::hull mBaseHull;
  100. std::vector<LLModel::PhysicsMesh> mMesh;
  101. LLModel::PhysicsMesh mBaseHullMesh;
  102. LLModel::PhysicsMesh mPhysicsShapeMesh;
  103. };
  104. LLModel(LLVolumeParams& params, F32 detail);
  105. ~LLModel();
  106. bool loadModel(std::istream& is);
  107. bool loadSkinInfo(LLSD& header, std::istream& is);
  108. bool loadDecomposition(LLSD& header, std::istream& is);
  109. static LLSD writeModel(
  110. std::ostream& ostr,
  111. LLModel* physics,
  112. LLModel* high,
  113. LLModel* medium,
  114. LLModel* low,
  115. LLModel* imposotr,
  116. const LLModel::Decomposition& decomp,
  117. BOOL upload_skin,
  118. BOOL upload_joints,
  119. BOOL nowrite = FALSE,
  120. BOOL as_slm = FALSE);
  121. static LLSD writeModelToStream(
  122. std::ostream& ostr,
  123. LLSD& mdl,
  124. BOOL nowrite = FALSE, BOOL as_slm = FALSE);
  125. static LLModel* loadModelFromDomMesh(domMesh* mesh);
  126. static std::string getElementLabel(daeElement* element);
  127. std::string getName() const;
  128. std::string getMetric() const {return mMetric;}
  129. EModelStatus getStatus() const {return mStatus;}
  130. static std::string getStatusString(U32 status) ;
  131. void appendFaces(LLModel* model, LLMatrix4& transform, LLMatrix4& normal_transform);
  132. void appendFace(const LLVolumeFace& src_face, std::string src_material, LLMatrix4& mat, LLMatrix4& norm_mat);
  133. void setNumVolumeFaces(S32 count);
  134. void setVolumeFaceData(
  135. S32 f,
  136. LLStrider<LLVector3> pos,
  137. LLStrider<LLVector3> norm,
  138. LLStrider<LLVector2> tc,
  139. LLStrider<U16> ind,
  140. U32 num_verts,
  141. U32 num_indices);
  142. void generateNormals(F32 angle_cutoff);
  143. void addFace(const LLVolumeFace& face);
  144. void normalizeVolumeFaces();
  145. void optimizeVolumeFaces();
  146. void offsetMesh( const LLVector3& pivotPoint );
  147. void getNormalizedScaleTranslation(LLVector3& scale_out, LLVector3& translation_out);
  148. //reorder face list based on mMaterialList in this and reference so
  149. //order matches that of reference (material ordering touchup)
  150. bool matchMaterialOrder(LLModel* ref, int& refFaceCnt, int& modelFaceCnt );
  151. bool isMaterialListSubset( LLModel* ref );
  152. bool needToAddFaces( LLModel* ref, int& refFaceCnt, int& modelFaceCnt );
  153. std::vector<std::string> mMaterialList;
  154. //data used for skin weights
  155. class JointWeight
  156. {
  157. public:
  158. S32 mJointIdx;
  159. F32 mWeight;
  160. JointWeight()
  161. {
  162. mJointIdx = 0;
  163. mWeight = 0.f;
  164. }
  165. JointWeight(S32 idx, F32 weight)
  166. : mJointIdx(idx), mWeight(weight)
  167. {
  168. }
  169. bool operator<(const JointWeight& rhs) const
  170. {
  171. if (mWeight == rhs.mWeight)
  172. {
  173. return mJointIdx < rhs.mJointIdx;
  174. }
  175. return mWeight < rhs.mWeight;
  176. }
  177. };
  178. struct CompareWeightGreater
  179. {
  180. bool operator()(const JointWeight& lhs, const JointWeight& rhs)
  181. {
  182. return rhs < lhs; // strongest = first
  183. }
  184. };
  185. //Are the doubles the same w/in epsilon specified tolerance
  186. bool areEqual( double a, double b )
  187. {
  188. const float epsilon = 1e-5f;
  189. return (fabs((a - b)) < epsilon) ? true : false ;
  190. }
  191. //Make sure that we return false for any values that are within the tolerance for equivalence
  192. bool jointPositionalLookup( const LLVector3& a, const LLVector3& b )
  193. {
  194. return ( areEqual( a[0],b[0]) && areEqual( a[1],b[1] ) && areEqual( a[2],b[2]) ) ? true : false;
  195. }
  196. //copy of position array for this model -- mPosition[idx].mV[X,Y,Z]
  197. std::vector<LLVector3> mPosition;
  198. //map of positions to skin weights --- mSkinWeights[pos].mV[0..4] == <joint_index>.<weight>
  199. //joint_index corresponds to mJointList
  200. typedef std::vector<JointWeight> weight_list;
  201. typedef std::map<LLVector3, weight_list > weight_map;
  202. weight_map mSkinWeights;
  203. //get list of weight influences closest to given position
  204. weight_list& getJointInfluences(const LLVector3& pos);
  205. LLMeshSkinInfo mSkinInfo;
  206. std::string mRequestedLabel; // name requested in UI, if any.
  207. std::string mLabel; // name computed from dae.
  208. std::string mMetric; // user-supplied metric data for upload
  209. LLVector3 mNormalizedScale;
  210. LLVector3 mNormalizedTranslation;
  211. float mPelvisOffset;
  212. // convex hull decomposition
  213. S32 mDecompID;
  214. void setConvexHullDecomposition(
  215. const convex_hull_decomposition& decomp);
  216. void updateHullCenters();
  217. LLVector3 mCenterOfHullCenters;
  218. std::vector<LLVector3> mHullCenter;
  219. U32 mHullPoints;
  220. //ID for storing this model in a .slm file
  221. S32 mLocalID;
  222. Decomposition mPhysics;
  223. EModelStatus mStatus ;
  224. protected:
  225. void addVolumeFacesFromDomMesh(domMesh* mesh);
  226. virtual BOOL createVolumeFacesFromDomMesh(domMesh *mesh);
  227. };
  228. #endif //LL_LLMODEL_H