llpolymesh.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /**
  2. * @file llpolymesh.h
  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. #ifndef LL_LLPOLYMESHINTERFACE_H
  33. #define LL_LLPOLYMESHINTERFACE_H
  34. #include "linden_common.h"
  35. #include "lljoint.h"
  36. #include "llpolymorph.h"
  37. #include "llquaternion.h"
  38. #include "llstl.h"
  39. #include "llvector2.h"
  40. #include "llvector3.h"
  41. class LLAvatarAppearance;
  42. class LLSkinJoint;
  43. class LLWearable;
  44. //-----------------------------------------------------------------------------
  45. // LLPolyFace
  46. // A set of 4 vertex indices.
  47. // An LLPolyFace can represent either a triangle or quad.
  48. // If the last index is -1, it's a triangle.
  49. //-----------------------------------------------------------------------------
  50. typedef S32 LLPolyFace[3];
  51. //struct PrimitiveGroup;
  52. //-----------------------------------------------------------------------------
  53. // LLPolyMesh
  54. // A polyhedra consisting of any number of triangles and quads.
  55. // All instances contain a set of faces, and optionally may include
  56. // faces grouped into named face sets.
  57. //-----------------------------------------------------------------------------
  58. class LLPolyMorphTarget;
  59. class LLPolyMeshSharedData
  60. {
  61. friend class LLPolyMesh;
  62. protected:
  63. LOG_CLASS(LLPolyMeshSharedData);
  64. public:
  65. LLPolyMeshSharedData();
  66. ~LLPolyMeshSharedData();
  67. private:
  68. void setupLOD(LLPolyMeshSharedData* reference_data);
  69. // Frees all mesh memory resources
  70. void freeMeshData();
  71. LL_INLINE void setPosition(const LLVector3& pos) { mPosition = pos; }
  72. LL_INLINE void setRotation(const LLQuaternion& rot) { mRotation = rot; }
  73. LL_INLINE void setScale(const LLVector3& scale) { mScale = scale; }
  74. bool allocateVertexData(U32 numVertices);
  75. // Free the vertex data
  76. void freeVertexData();
  77. bool allocateFaceData(U32 numFaces);
  78. bool allocateJointNames(U32 numJointNames);
  79. // Retrieve the number of KB of memory used by this instance
  80. U32 getNumKB();
  81. // Load mesh data from file
  82. bool loadMesh(const std::string& fileName);
  83. public:
  84. #if 0 // Dead code
  85. void genIndices(S32 offset);
  86. #endif
  87. const LLVector2& getUVs(U32 index);
  88. const S32* getSharedVert(S32 vert);
  89. LL_INLINE bool isLOD() { return mReferenceData != NULL; }
  90. private:
  91. // Transform data
  92. LLQuaternion mRotation;
  93. LLVector3 mPosition;
  94. LLVector3 mScale;
  95. // Vertex data
  96. LLVector4a* mBaseCoords;
  97. LLVector4a* mBaseNormals;
  98. LLVector4a* mBaseBinormals;
  99. LLVector2* mTexCoords;
  100. LLVector2* mDetailTexCoords;
  101. F32* mWeights;
  102. S32 mNumVertices;
  103. bool mHasWeights;
  104. bool mHasDetailTexCoords;
  105. // Face data
  106. LLPolyFace* mFaces;
  107. S32 mNumFaces;
  108. // Face set data
  109. U32 mNumJointNames;
  110. std::string* mJointNames;
  111. // Morph targets
  112. typedef std::set<LLPolyMorphData*> morphdata_list_t;
  113. morphdata_list_t mMorphData;
  114. std::map<S32, S32> mSharedVerts;
  115. LLPolyMeshSharedData* mReferenceData;
  116. S32 mLastIndexOffset;
  117. public:
  118. // Temporarily...
  119. // Triangle indices
  120. U32 mNumTriangleIndices;
  121. U32* mTriangleIndices;
  122. };
  123. class LLJointRenderData
  124. {
  125. public:
  126. LLJointRenderData(const LLMatrix4* world_matrix, LLSkinJoint* skin_joint)
  127. : mWorldMatrix(world_matrix),
  128. mSkinJoint(skin_joint)
  129. {
  130. }
  131. public:
  132. const LLMatrix4* mWorldMatrix;
  133. LLSkinJoint* mSkinJoint;
  134. };
  135. class LLPolyMesh
  136. {
  137. protected:
  138. LOG_CLASS(LLPolyMesh);
  139. public:
  140. // Constructor
  141. LLPolyMesh(LLPolyMeshSharedData* shared_data, LLPolyMesh* reference_mesh);
  142. // Destructor
  143. ~LLPolyMesh();
  144. // Requests a mesh by name.
  145. // If the mesh already exists in the global mesh table, it is returned,
  146. // otherwise it is loaded from file, added to the table, and returned.
  147. static LLPolyMesh* getMesh(const std::string& name,
  148. LLPolyMesh* reference_mesh = NULL);
  149. // Frees all loaded meshes.
  150. // This should only be called once you know there are no outstanding
  151. // references to these objects. Generally, upon exit of the application.
  152. static void freeAllMeshes();
  153. //--------------------------------------------------------------------
  154. // Transform Data Access
  155. //--------------------------------------------------------------------
  156. // Get position
  157. LL_INLINE const LLVector3& getPosition()
  158. {
  159. llassert (mSharedData);
  160. return mSharedData->mPosition;
  161. }
  162. // Get rotation
  163. LL_INLINE const LLQuaternion& getRotation()
  164. {
  165. llassert (mSharedData);
  166. return mSharedData->mRotation;
  167. }
  168. // Get scale
  169. LL_INLINE const LLVector3& getScale()
  170. {
  171. llassert (mSharedData);
  172. return mSharedData->mScale;
  173. }
  174. //--------------------------------------------------------------------
  175. // Vertex Data Access
  176. //--------------------------------------------------------------------
  177. // Get number of vertices
  178. LL_INLINE U32 getNumVertices()
  179. {
  180. llassert (mSharedData);
  181. return mSharedData->mNumVertices;
  182. }
  183. // Returns whether or not the mesh has detail texture coords
  184. LL_INLINE bool hasDetailTexCoords()
  185. {
  186. llassert (mSharedData);
  187. return mSharedData->mHasDetailTexCoords;
  188. }
  189. // Returns whether or not the mesh has vertex weights
  190. LL_INLINE bool hasWeights() const
  191. {
  192. llassert (mSharedData);
  193. return mSharedData->mHasWeights;
  194. }
  195. // Get coords
  196. LL_INLINE const LLVector4a* getCoords() const { return mCoords; }
  197. // non const version
  198. LL_INLINE LLVector4a* getWritableCoords() { return mCoords; }
  199. // Get normals
  200. LL_INLINE const LLVector4a* getNormals() const { return mNormals; }
  201. // Get binormals
  202. LL_INLINE const LLVector4a* getBinormals() const { return mBinormals; }
  203. // Get base mesh coords
  204. LL_INLINE const LLVector4a* getBaseCoords() const
  205. {
  206. llassert(mSharedData);
  207. return mSharedData->mBaseCoords;
  208. }
  209. // Get base mesh normals
  210. LL_INLINE const LLVector4a* getBaseNormals() const
  211. {
  212. llassert(mSharedData);
  213. return mSharedData->mBaseNormals;
  214. }
  215. // Get base mesh binormals
  216. LL_INLINE const LLVector4a* getBaseBinormals() const
  217. {
  218. llassert(mSharedData);
  219. return mSharedData->mBaseBinormals;
  220. }
  221. // intermediate morphed normals and output normals
  222. LL_INLINE LLVector4a* getWritableNormals() { return mNormals; }
  223. LL_INLINE LLVector4a* getScaledNormals() { return mScaledNormals; }
  224. LL_INLINE LLVector4a* getWritableBinormals() { return mBinormals; }
  225. LL_INLINE LLVector4a* getScaledBinormals() { return mScaledBinormals; }
  226. // Get texCoords
  227. LL_INLINE const LLVector2* getTexCoords() const
  228. {
  229. return mTexCoords;
  230. }
  231. // non const version
  232. LL_INLINE LLVector2* getWritableTexCoords() { return mTexCoords; }
  233. // Get detailTexCoords
  234. LL_INLINE const LLVector2* getDetailTexCoords() const
  235. {
  236. llassert (mSharedData);
  237. return mSharedData->mDetailTexCoords;
  238. }
  239. // Get weights
  240. LL_INLINE const F32* getWeights() const
  241. {
  242. llassert (mSharedData);
  243. return mSharedData->mWeights;
  244. }
  245. LL_INLINE F32* getWritableWeights() const
  246. {
  247. llassert (mSharedData);
  248. return mSharedData->mWeights;
  249. }
  250. LL_INLINE LLVector4a* getWritableClothingWeights() { return mClothingWeights; }
  251. LL_INLINE const LLVector4a* getClothingWeights()
  252. {
  253. return mClothingWeights;
  254. }
  255. //--------------------------------------------------------------------
  256. // Face Data Access
  257. //--------------------------------------------------------------------
  258. // Get number of faces
  259. LL_INLINE S32 getNumFaces()
  260. {
  261. llassert (mSharedData);
  262. return mSharedData->mNumFaces;
  263. }
  264. // Get faces
  265. LL_INLINE LLPolyFace* getFaces()
  266. {
  267. llassert (mSharedData);
  268. return mSharedData->mFaces;
  269. }
  270. LL_INLINE U32 getNumJointNames()
  271. {
  272. llassert (mSharedData);
  273. return mSharedData->mNumJointNames;
  274. }
  275. LL_INLINE std::string* getJointNames()
  276. {
  277. llassert (mSharedData);
  278. return mSharedData->mJointNames;
  279. }
  280. LLPolyMorphData* getMorphData(const std::string& morph_name);
  281. #if 0
  282. void removeMorphData(LLPolyMorphData* morph_target);
  283. void deleteAllMorphData();
  284. #endif
  285. LL_INLINE LLPolyMeshSharedData* getSharedData() const
  286. {
  287. return mSharedData;
  288. }
  289. LL_INLINE LLPolyMesh* getReferenceMesh() { return mReferenceMesh ? mReferenceMesh : this; }
  290. // Get indices
  291. LL_INLINE U32* getIndices() { return mSharedData ? mSharedData->mTriangleIndices : NULL; }
  292. LL_INLINE bool isLOD() { return mSharedData && mSharedData->isLOD(); }
  293. LL_INLINE void setAvatar(LLAvatarAppearance* av) { mAvatarp = av; }
  294. LL_INLINE LLAvatarAppearance* getAvatar() { return mAvatarp; }
  295. std::vector<LLJointRenderData*> mJointRenderData;
  296. U32 mFaceVertexOffset;
  297. U32 mFaceVertexCount;
  298. U32 mFaceIndexOffset;
  299. U32 mFaceIndexCount;
  300. U32 mCurVertexCount;
  301. private:
  302. void initializeForMorph();
  303. // Dumps diagnostic information about the global mesh table
  304. static void dumpDiagInfo();
  305. protected:
  306. // mesh data shared across all instances of a given mesh
  307. LLPolyMeshSharedData* mSharedData;
  308. // Single array of floats for allocation / deletion
  309. F32* mVertexData;
  310. // deformed vertices (resulting from application of morph targets)
  311. LLVector4a* mCoords;
  312. // deformed normals (resulting from application of morph targets)
  313. LLVector4a* mScaledNormals;
  314. // output normals (after normalization)
  315. LLVector4a* mNormals;
  316. // deformed binormals (resulting from application of morph targets)
  317. LLVector4a* mScaledBinormals;
  318. // output binormals (after normalization)
  319. LLVector4a* mBinormals;
  320. // weight values that mark verts as clothing/skin
  321. LLVector4a* mClothingWeights;
  322. // output texture coordinates
  323. LLVector2* mTexCoords;
  324. LLPolyMesh* mReferenceMesh;
  325. // global mesh list
  326. typedef std::map<std::string, LLPolyMeshSharedData*> LLPolyMeshSharedDataTable;
  327. static LLPolyMeshSharedDataTable sGlobalSharedMeshList;
  328. // Backlink only; don't make this an LLPointer.
  329. LLAvatarAppearance* mAvatarp;
  330. };
  331. #endif // LL_LLPOLYMESHINTERFACE_H