llmodelloader.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. * @file llmodelloader.h
  3. * @brief LLModelLoader class definition
  4. *
  5. * $LicenseInfo:firstyear=2004&license=viewergpl$
  6. *
  7. * Copyright (c) 2010, 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_LLMODELLOADER_H
  33. #define LL_LLMODELLOADER_H
  34. #include <list>
  35. #include <queue>
  36. #include "boost/function.hpp"
  37. #include "llmodel.h"
  38. #include "llthread.h"
  39. class LLMatrix4a;
  40. class LLJoint;
  41. constexpr S32 SLM_SUPPORTED_VERSION = 3;
  42. constexpr S32 NUM_LOD = 4;
  43. constexpr U32 LEGACY_RIG_FLAG_INVALID = 1;
  44. constexpr U32 LEGACY_RIG_FLAG_NO_JOINT = 2;
  45. constexpr U32 LEGACY_RIG_FLAG_TOO_MANY_JOINTS = 4;
  46. constexpr U32 LEGACY_RIG_FLAG_UNKNOWN_JOINT = 8;
  47. typedef std::map<std::string, LLMatrix4> JointTransformMap;
  48. typedef std::map<std::string, LLMatrix4>:: iterator JointTransformMapIt;
  49. typedef std::map<std::string, std::string> JointMap;
  50. typedef std::deque<std::string> JointNameSet;
  51. class LLModelLoader : public LLThread
  52. {
  53. protected:
  54. LOG_CLASS(LLModelLoader);
  55. public:
  56. typedef std::map<std::string, LLImportMaterial> material_map;
  57. typedef std::vector<LLPointer<LLModel> > model_list;
  58. typedef std::vector<LLModelInstance> model_instance_list_t;
  59. typedef std::map<LLMatrix4, model_instance_list_t> scene;
  60. // Callback with loaded model data and loaded LoD
  61. typedef boost::function<void (scene&, model_list&, S32, void*)> load_callback_t;
  62. // Function to provide joint lookup by name (within preview avi skeleton,
  63. // for example)
  64. typedef boost::function<LLJoint* (const std::string&, void*)> joint_lookup_func_t;
  65. // Function to load and associate material with all it's textures. The
  66. // returned value is the number of textures loaded intentionally non-const
  67. // so func can modify material to store platform-specific data
  68. typedef boost::function<U32 (LLImportMaterial&, void*)> texture_load_func_t;
  69. // Callback to inform client of state changes during the loading process
  70. // (errors are reported as state changes here as well)
  71. typedef boost::function<void (U32,void*)> state_callback_t;
  72. typedef enum
  73. {
  74. STARTING = 0,
  75. READING_FILE,
  76. CREATING_FACES,
  77. GENERATING_VERTEX_BUFFERS,
  78. GENERATING_LOD,
  79. DONE,
  80. WARNING_BIND_SHAPE_ORIENTATION,
  81. ERROR_PARSING, // Basically, loading failed
  82. ERROR_MATERIALS,
  83. ERROR_PASSWORD_REQUIRED,
  84. ERROR_NEED_MORE_MEMORY,
  85. ERROR_INVALID_FILE,
  86. ERROR_LOADER_SETUP,
  87. ERROR_INVALID_PARAMETERS,
  88. ERROR_OUT_OF_RANGE,
  89. ERROR_FILE_VERSION_INVALID,
  90. ERROR_LOD_MODEL_MISMATCH,
  91. ERROR_HIGH_LOD_MODEL_MISSING,
  92. // This error should always be last in this list, error code is passed
  93. // as ERROR_MODEL+error_code:
  94. ERROR_MODEL
  95. } eLoadState;
  96. LLModelLoader(const std::string& filename, S32 lod,
  97. load_callback_t load_cb,
  98. joint_lookup_func_t joint_lookup_func,
  99. texture_load_func_t texture_load_func,
  100. state_callback_t state_cb, void* userdata,
  101. JointTransformMap& joint_transform_map,
  102. JointNameSet& joints_from_nodes,
  103. JointMap& legal_joint_names, U32 max_joints_per_mesh);
  104. ~LLModelLoader() override;
  105. void run() override;
  106. LL_INLINE virtual void setNoNormalize() { mNoNormalize = true; }
  107. LL_INLINE virtual void setNoOptimize() { mNoOptimize = true; }
  108. static bool getSLMFilename(const std::string& model_filename,
  109. std::string& slm_filename);
  110. // Will try SLM or derived class OpenFile as appropriate
  111. virtual bool doLoadModel();
  112. // Derived classes need to provide their parsing of files here
  113. virtual bool openFile(const std::string& filename) = 0;
  114. bool loadFromSLM(const std::string& filename);
  115. void setLoadState(U32 state);
  116. void loadModelCallback();
  117. // Methods called in the main thread:
  118. void loadTextures();
  119. LL_INLINE bool areTexturesReady() { return mNumOfFetchingTextures <= 0; }
  120. // Determines the viability of an asset to be used as an avatar rig
  121. // (w or w/o joint upload caps)
  122. void critiqueRigForUploadApplicability(const std::vector<std::string>& joints);
  123. LL_INLINE bool isRigValidForJointPositionUpload() const
  124. {
  125. return mRigValidJointUpload;
  126. }
  127. LL_INLINE void setRigValidForJointPositionUpload(bool b)
  128. {
  129. mRigValidJointUpload = b;
  130. }
  131. LL_INLINE bool isLegacyRigValid() const { return mLegacyRigFlags == 0; }
  132. LL_INLINE void setLegacyRigValid(bool b)
  133. {
  134. mLegacyRigFlags = b ? LEGACY_RIG_FLAG_INVALID : 0;
  135. }
  136. LL_INLINE bool getLegacyRigFlags() const { return mLegacyRigFlags; }
  137. LL_INLINE void setLegacyRigFlags(U32 flags) { mLegacyRigFlags = flags; }
  138. LL_INLINE const LLSD& logOut() const { return mWarningsArray; }
  139. LL_INLINE void clearLog() { mWarningsArray.clear(); }
  140. LL_INLINE bool isNodeAJoint(const char* name)
  141. {
  142. return name && mJointMap.find(name) != mJointMap.end();
  143. }
  144. protected:
  145. // Determines if a rig is a legacy from the joint list
  146. U32 determineRigLegacyFlags(const std::vector<std::string>& joints);
  147. static bool isAlive(LLModelLoader* loader);
  148. public:
  149. S32 mLod;
  150. S32 mNumOfFetchingTextures;
  151. U32 mState;
  152. LLMatrix4 mTransform;
  153. LLVector3 mExtents[2];
  154. std::string mFilename;
  155. model_list mModelList;
  156. scene mScene;
  157. typedef std::queue<LLPointer<LLModel> > model_queue;
  158. // queue of models that need a physics rep
  159. model_queue mPhysicsQ;
  160. // map of avatar joints as named in COLLADA assets to internal joint
  161. // names
  162. JointMap mJointMap;
  163. JointTransformMap& mJointList;
  164. JointNameSet& mJointsFromNode;
  165. U32 mMaxJointsPerMesh;
  166. bool mFirstTransform;
  167. bool mTrySLM;
  168. // ignore cached SLM if it does not contain rig info and we want the latter
  169. bool mCacheOnlyHitIfRigged;
  170. protected:
  171. bool mNoNormalize;
  172. bool mNoOptimize;
  173. bool mRigValidJointUpload;
  174. U32 mLegacyRigFlags;
  175. // The model preview floater pulls logs from this
  176. LLSD mWarningsArray;
  177. LLModelLoader::load_callback_t mLoadCallback;
  178. LLModelLoader::joint_lookup_func_t mJointLookupFunc;
  179. LLModelLoader::texture_load_func_t mTextureLoadFunc;
  180. LLModelLoader::state_callback_t mStateCallback;
  181. void* mUserData;
  182. JointTransformMap mJointTransformMap;
  183. static std::list<LLModelLoader*> sActiveLoaderList;
  184. };
  185. void stretch_extents(LLModel* model, const LLMatrix4a& mat, LLVector4a& min,
  186. LLVector4a& max, bool& first_transform);
  187. void stretch_extents(LLModel* model, const LLMatrix4& mat, LLVector3& min,
  188. LLVector3& max, bool& first_transform);
  189. #endif // LL_LLMODELLOADER_H