llmodelloader.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /**
  2. * @file llmodelloader.cpp
  3. * @brief LLModelLoader class implementation
  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. #include "linden_common.h"
  33. #include "llmodelloader.h"
  34. #include "llapp.h"
  35. #include "llcallbacklist.h"
  36. #include "llmatrix4a.h"
  37. #include "lljoint.h"
  38. #include "llsdserialize.h"
  39. #include "llstring.h"
  40. #include "lltimer.h" // For ms_sleep()
  41. std::list<LLModelLoader*> LLModelLoader::sActiveLoaderList;
  42. void stretch_extents(LLModel* model, const LLMatrix4a& mat, LLVector4a& min,
  43. LLVector4a& max, bool& first_transform)
  44. {
  45. static const LLVector4a box[] =
  46. {
  47. LLVector4a(-1.f, 1.f, -1.f),
  48. LLVector4a(-1.f, 1.f, 1.f),
  49. LLVector4a(-1.f, -1.f, -1.f),
  50. LLVector4a(-1.f, -1.f, 1.f),
  51. LLVector4a(1.f, 1.f, -1.f),
  52. LLVector4a(1.f, 1.f, 1.f),
  53. LLVector4a(1.f, -1.f, -1.f),
  54. LLVector4a(1.f, -1.f, 1.f),
  55. };
  56. for (S32 j = 0; j < model->getNumVolumeFaces(); ++j)
  57. {
  58. const LLVolumeFace& face = model->getVolumeFace(j);
  59. LLVector4a center;
  60. center.setAdd(face.mExtents[0], face.mExtents[1]);
  61. center.mul(0.5f);
  62. LLVector4a size;
  63. size.setSub(face.mExtents[1],face.mExtents[0]);
  64. size.mul(0.5f);
  65. for (U32 i = 0; i < 8; ++i)
  66. {
  67. LLVector4a t;
  68. t.setMul(size, box[i]);
  69. t.add(center);
  70. LLVector4a v;
  71. mat.affineTransform(t, v);
  72. if (first_transform)
  73. {
  74. first_transform = false;
  75. min = max = v;
  76. }
  77. else
  78. {
  79. update_min_max(min, max, v);
  80. }
  81. }
  82. }
  83. }
  84. void stretch_extents(LLModel* model, const LLMatrix4& mat, LLVector3& min,
  85. LLVector3& max, bool& first_transform)
  86. {
  87. LLVector4a mina, maxa;
  88. LLMatrix4a mata;
  89. mata.loadu(mat);
  90. mina.load3(min.mV);
  91. maxa.load3(max.mV);
  92. stretch_extents(model, mata, mina, maxa, first_transform);
  93. min.set(mina.getF32ptr());
  94. max.set(maxa.getF32ptr());
  95. }
  96. LLModelLoader::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,
  101. void* userdata,
  102. JointTransformMap& joint_transform_map,
  103. JointNameSet& joints_from_nodes,
  104. JointMap& legal_joint_names,
  105. U32 max_joints_per_mesh)
  106. : mJointList(joint_transform_map),
  107. mJointsFromNode(joints_from_nodes),
  108. LLThread("Model Loader"),
  109. mFilename(filename),
  110. mLod(lod),
  111. mFirstTransform(true),
  112. mNumOfFetchingTextures(0),
  113. mLoadCallback(load_cb),
  114. mJointLookupFunc(joint_lookup_func),
  115. mTextureLoadFunc(texture_load_func),
  116. mStateCallback(state_cb),
  117. mUserData(userdata),
  118. mNoNormalize(false),
  119. mNoOptimize(false),
  120. mCacheOnlyHitIfRigged(false),
  121. mTrySLM(false),
  122. mRigValidJointUpload(true),
  123. mLegacyRigFlags(0),
  124. mWarningsArray(LLSD::emptyArray()),
  125. mMaxJointsPerMesh(max_joints_per_mesh),
  126. mJointMap(legal_joint_names)
  127. {
  128. LLStringUtil::replaceString(mFilename, "#", "%23");
  129. assert_main_thread();
  130. sActiveLoaderList.push_back(this);
  131. }
  132. LLModelLoader::~LLModelLoader()
  133. {
  134. assert_main_thread();
  135. sActiveLoaderList.remove(this);
  136. }
  137. void LLModelLoader::run()
  138. {
  139. mWarningsArray.clear();
  140. doLoadModel();
  141. doOnIdleOneTime(boost::bind(&LLModelLoader::loadModelCallback, this));
  142. }
  143. //static
  144. bool LLModelLoader::getSLMFilename(const std::string& model_filename,
  145. std::string& slm_filename)
  146. {
  147. slm_filename = model_filename;
  148. size_t i = model_filename.rfind(".");
  149. if (i != std::string::npos && i > 0)
  150. {
  151. slm_filename.resize(i, '\0');
  152. slm_filename.append(".slm");
  153. return true;
  154. }
  155. return false;
  156. }
  157. bool LLModelLoader::doLoadModel()
  158. {
  159. if (mTrySLM)
  160. {
  161. // First, look for a .slm file of the same name that was modified later
  162. // than the specified model file
  163. std::string slm_filename;
  164. if (getSLMFilename(mFilename, slm_filename))
  165. {
  166. time_t slm_time = LLFile::lastModidied(slm_filename);
  167. if (slm_time) // If .slm file exists
  168. {
  169. time_t model_time = LLFile::lastModidied(mFilename);
  170. if (!model_time || model_time < slm_time)
  171. {
  172. // If this fails, fall through and try loading from the
  173. // model file
  174. if (loadFromSLM(slm_filename))
  175. {
  176. // Successfully loading from an slm implicitly sets all
  177. // LoDs
  178. mLod = -1;
  179. return true;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. return openFile(mFilename);
  186. }
  187. void LLModelLoader::setLoadState(U32 state)
  188. {
  189. if (mStateCallback)
  190. {
  191. mStateCallback(state, mUserData);
  192. }
  193. }
  194. bool LLModelLoader::loadFromSLM(const std::string& filename)
  195. {
  196. // Only need to populate mScene with data from slm
  197. llstat stat;
  198. if (LLFile::stat(filename, &stat))
  199. {
  200. // File does not exist
  201. return false;
  202. }
  203. S32 file_size = (S32)stat.st_size;
  204. llifstream stream(filename.c_str(),
  205. std::ifstream::in | std::ifstream::binary);
  206. if (!stream.is_open())
  207. {
  208. llwarns << "Could not open file '" << filename << "' for reading."
  209. << llendl;
  210. return false;
  211. }
  212. LLSD data;
  213. LLSDSerialize::fromBinary(data, stream, file_size);
  214. stream.close();
  215. // Build model list for each LoD
  216. model_list model[LLModel::NUM_LODS];
  217. if (data["version"].asInteger() != SLM_SUPPORTED_VERSION)
  218. {
  219. // unsupported version
  220. return false;
  221. }
  222. LLSD& mesh = data["mesh"];
  223. LLVolumeParams volume_params;
  224. volume_params.setType(LL_PCODE_PROFILE_SQUARE, LL_PCODE_PATH_LINE);
  225. for (S32 lod = 0; lod < LLModel::NUM_LODS; ++lod)
  226. {
  227. for (U32 i = 0, count = mesh.size(); i < count; ++i)
  228. {
  229. std::stringstream str(mesh[i].asString());
  230. LLPointer<LLModel> loaded_model = new LLModel(volume_params,
  231. (F32)lod);
  232. if (loaded_model->loadModel(str))
  233. {
  234. loaded_model->mLocalID = i;
  235. model[lod].emplace_back(loaded_model);
  236. if (lod == LLModel::LOD_HIGH)
  237. {
  238. if (!loaded_model->mSkinInfo.mJointNames.empty())
  239. {
  240. // Check to see if rig is valid
  241. critiqueRigForUploadApplicability(loaded_model->mSkinInfo.mJointNames);
  242. }
  243. else if (mCacheOnlyHitIfRigged)
  244. {
  245. return false;
  246. }
  247. }
  248. }
  249. }
  250. }
  251. if (model[LLModel::LOD_HIGH].empty())
  252. {
  253. // Failed to load high lod
  254. return false;
  255. }
  256. // Set name.
  257. std::string name = data["name"];
  258. if (!name.empty())
  259. {
  260. model[LLModel::LOD_HIGH][0]->mLabel = name;
  261. }
  262. // Load instance list
  263. model_instance_list_t instances;
  264. LLSD& instance = data["instance"];
  265. for (U32 i = 0, count = instance.size(); i < count; ++i)
  266. {
  267. // Deserialize instance list
  268. instances.emplace_back(instance[i]);
  269. // Match up model instance pointers
  270. S32 idx = instances[i].mLocalMeshID;
  271. std::string instance_label = instances[i].mLabel;
  272. for (U32 lod = 0; lod < LLModel::NUM_LODS; ++lod)
  273. {
  274. if (!model[lod].empty())
  275. {
  276. S32 lod_size = model[lod].size();
  277. if (idx >= lod_size)
  278. {
  279. if (lod_size)
  280. {
  281. instances[i].mLOD[lod] = model[lod][0];
  282. }
  283. else
  284. {
  285. instances[i].mLOD[lod] = NULL;
  286. }
  287. continue;
  288. }
  289. if (model[lod][idx] && model[lod][idx]->mLabel.empty() &&
  290. !instance_label.empty())
  291. {
  292. // restore model names
  293. std::string name = instance_label;
  294. switch (lod)
  295. {
  296. case LLModel::LOD_IMPOSTOR: name += "_LOD0"; break;
  297. case LLModel::LOD_LOW: name += "_LOD1"; break;
  298. case LLModel::LOD_MEDIUM: name += "_LOD2"; break;
  299. case LLModel::LOD_PHYSICS: name += "_PHYS"; break;
  300. case LLModel::LOD_HIGH: break;
  301. }
  302. model[lod][idx]->mLabel = name;
  303. }
  304. instances[i].mLOD[lod] = model[lod][idx];
  305. }
  306. }
  307. if (!instances[i].mModel)
  308. {
  309. instances[i].mModel = model[LLModel::LOD_HIGH][idx];
  310. }
  311. }
  312. // Convert instances to mScene
  313. mFirstTransform = true;
  314. for (U32 i = 0, count = instances.size(); i < count; ++i)
  315. {
  316. LLModelInstance& cur_instance = instances[i];
  317. mScene[cur_instance.mTransform].emplace_back(cur_instance);
  318. stretch_extents(cur_instance.mModel, cur_instance.mTransform,
  319. mExtents[0], mExtents[1], mFirstTransform);
  320. }
  321. setLoadState(DONE);
  322. return true;
  323. }
  324. //static
  325. bool LLModelLoader::isAlive(LLModelLoader* loader)
  326. {
  327. if (!loader)
  328. {
  329. return false;
  330. }
  331. std::list<LLModelLoader*>::iterator iter = sActiveLoaderList.begin();
  332. std::list<LLModelLoader*>::iterator end = sActiveLoaderList.end();
  333. for ( ; iter != end && *iter != loader; ++iter) ;
  334. return *iter == loader;
  335. }
  336. void LLModelLoader::loadModelCallback()
  337. {
  338. if (!LLApp::isExiting() && mLoadCallback)
  339. {
  340. mLoadCallback(mScene, mModelList ,mLod, mUserData);
  341. }
  342. // Wait until this thread is stopped before deleting self
  343. while (!isStopped())
  344. {
  345. ms_sleep(10);
  346. }
  347. // Double check if "this" is valid before deleting it, in case it is
  348. // aborted while running.
  349. if (!isAlive(this))
  350. {
  351. return;
  352. }
  353. delete this;
  354. }
  355. void LLModelLoader::critiqueRigForUploadApplicability(const std::vector<std::string>& joints)
  356. {
  357. // Determines the following use cases for a rig:
  358. // 1. It is suitable for upload with skin weights & joint positions, or
  359. // 2. It is suitable for upload as standard av with just skin weights
  360. // It's OK that both could end up being true. Both start out as true and
  361. // are forced to false if any mesh in the model file is not vald by that
  362. // criterion. Note that a file can contain multiple meshes.
  363. mLegacyRigFlags |= determineRigLegacyFlags(joints);
  364. }
  365. U32 LLModelLoader::determineRigLegacyFlags(const std::vector<std::string>& joints)
  366. {
  367. U32 count = joints.size();
  368. if (count == 0)
  369. {
  370. // No joints in asset
  371. LLSD args;
  372. args["Message"] = "NoJoint";
  373. mWarningsArray.append(args);
  374. return LEGACY_RIG_FLAG_NO_JOINT;
  375. }
  376. if (count > mMaxJointsPerMesh)
  377. {
  378. // Too many joints in asset
  379. llwarns << "Rigged to " << count << " joints, while maximum is "
  380. << mMaxJointsPerMesh << ". Skinning disabled." << llendl;
  381. LLSD args;
  382. args["Message"] = "TooManyJoint";
  383. args["JOINTS"] = LLSD::Integer(count);
  384. args["MAX"] = LLSD::Integer(mMaxJointsPerMesh);
  385. mWarningsArray.append(args);
  386. return LEGACY_RIG_FLAG_TOO_MANY_JOINTS;
  387. }
  388. U32 unknown_joint_count = 0;
  389. for (U32 i = 0; i < count; ++i)
  390. {
  391. const std::string& name = joints[i];
  392. if (!mJointMap.count(name))
  393. {
  394. llwarns << "Rigged to unrecognized joint name: " << name << llendl;
  395. LLSD args;
  396. args["Message"] = "UnrecognizedJoint";
  397. args["NAME"] = name;
  398. mWarningsArray.append(args);
  399. ++unknown_joint_count;
  400. }
  401. }
  402. if (unknown_joint_count)
  403. {
  404. llwarns << "Skinning disabled due to unknown joints." << llendl;
  405. LLSD args;
  406. args["Message"] = "UnknownJoints";
  407. args["COUNT"] = LLSD::Integer(unknown_joint_count);
  408. mWarningsArray.append(args);
  409. return LEGACY_RIG_FLAG_UNKNOWN_JOINT;
  410. }
  411. return 0; // All OK !
  412. }
  413. // Called in the main thread
  414. void LLModelLoader::loadTextures()
  415. {
  416. if (!mTextureLoadFunc) return;
  417. bool is_paused = isPaused();
  418. pause(); //pause the loader
  419. for (scene::iterator iter = mScene.begin(), end = mScene.end();
  420. iter != end; ++iter)
  421. {
  422. for (U32 i = 0, count = iter->second.size(); i < count; ++i)
  423. {
  424. for (std::map<std::string, LLImportMaterial>::iterator
  425. j = iter->second[i].mMaterial.begin(),
  426. end2 = iter->second[i].mMaterial.end();
  427. j != end2; ++j)
  428. {
  429. LLImportMaterial& material = j->second;
  430. if (!material.mDiffuseMapFilename.empty())
  431. {
  432. mNumOfFetchingTextures += mTextureLoadFunc(material,
  433. mUserData);
  434. }
  435. }
  436. }
  437. }
  438. if (!is_paused)
  439. {
  440. unpause();
  441. }
  442. }