lltextureentry.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /**
  2. * @file lltextureentry.cpp
  3. * @brief LLTextureEntry base 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 "linden_common.h"
  33. #include "lltextureentry.h"
  34. #include "imageids.h"
  35. #include "llcolor4.h"
  36. #include "llmediaentry.h"
  37. #include "llsdutil_math.h"
  38. constexpr U8 DEFAULT_BUMP_CODE = 0; // no bump or shininess
  39. const LLTextureEntry LLTextureEntry::null;
  40. // Some LLSD keys. Do not change these!
  41. #define OBJECT_ID_KEY_STR "object_id"
  42. #define TEXTURE_INDEX_KEY_STR "texture_index"
  43. #define OBJECT_MEDIA_VERSION_KEY_STR "object_media_version"
  44. #define OBJECT_MEDIA_DATA_KEY_STR "object_media_data"
  45. #define TEXTURE_MEDIA_DATA_KEY_STR "media_data"
  46. // Static member constants
  47. const char* LLTextureEntry::OBJECT_ID_KEY = OBJECT_ID_KEY_STR;
  48. const char* LLTextureEntry::OBJECT_MEDIA_DATA_KEY = OBJECT_MEDIA_DATA_KEY_STR;
  49. const char* LLTextureEntry::MEDIA_VERSION_KEY = OBJECT_MEDIA_VERSION_KEY_STR;
  50. const char* LLTextureEntry::TEXTURE_INDEX_KEY = TEXTURE_INDEX_KEY_STR;
  51. const char* LLTextureEntry::TEXTURE_MEDIA_DATA_KEY = TEXTURE_MEDIA_DATA_KEY_STR;
  52. static const std::string MEDIA_VERSION_STRING_PREFIX = "x-mv:";
  53. //static
  54. LLTextureEntry* LLTextureEntry::newTextureEntry()
  55. {
  56. return new LLTextureEntry();
  57. }
  58. LLTextureEntry::LLTextureEntry()
  59. {
  60. init(LLUUID::null, 1.f, 1.f, 0.f, 0.f, 0.f, DEFAULT_BUMP_CODE);
  61. }
  62. LLTextureEntry::LLTextureEntry(const LLUUID& tex_id)
  63. {
  64. init(tex_id, 1.f, 1.f, 0.f, 0.f, 0.f, DEFAULT_BUMP_CODE);
  65. }
  66. LLTextureEntry::LLTextureEntry(const LLTextureEntry& rhs)
  67. : mID(rhs.mID),
  68. mIsDefaultTexture(rhs.mIsDefaultTexture),
  69. mIsBlankTexture(rhs.mIsBlankTexture),
  70. mScaleS(rhs.mScaleS),
  71. mScaleT(rhs.mScaleT),
  72. mOffsetS(rhs.mOffsetS),
  73. mOffsetT(rhs.mOffsetT),
  74. mRotation(rhs.mRotation),
  75. mColor(rhs.mColor),
  76. mBump(rhs.mBump),
  77. mGlow(rhs.mGlow),
  78. mMaterialID(rhs.mMaterialID),
  79. mMaterial(rhs.mMaterial),
  80. mMaterialUpdatePending(rhs.mMaterialUpdatePending),
  81. mGLTFMaterial(rhs.mGLTFMaterial),
  82. mMediaFlags(rhs.mMediaFlags),
  83. mSelected(rhs.mSelected)
  84. {
  85. if (mGLTFMaterial.notNull())
  86. {
  87. mGLTFMaterial->addTextureEntry(this);
  88. }
  89. if (rhs.mGLTFMaterialOverrides.notNull())
  90. {
  91. mGLTFMaterialOverrides =
  92. new LLGLTFMaterial(*rhs.mGLTFMaterialOverrides);
  93. }
  94. if (rhs.mMediaEntry)
  95. {
  96. // Make a copy
  97. mMediaEntry = new LLMediaEntry(*rhs.mMediaEntry);
  98. }
  99. else
  100. {
  101. mMediaEntry = NULL;
  102. }
  103. }
  104. LLTextureEntry& LLTextureEntry::operator=(const LLTextureEntry& rhs)
  105. {
  106. if (this != &rhs)
  107. {
  108. mID = rhs.mID;
  109. mIsDefaultTexture = rhs.mIsDefaultTexture;
  110. mIsBlankTexture = rhs.mIsBlankTexture;
  111. mScaleS = rhs.mScaleS;
  112. mScaleT = rhs.mScaleT;
  113. mOffsetS = rhs.mOffsetS;
  114. mOffsetT = rhs.mOffsetT;
  115. mRotation = rhs.mRotation;
  116. mColor = rhs.mColor;
  117. mBump = rhs.mBump;
  118. mGlow = rhs.mGlow;
  119. mMediaFlags = rhs.mMediaFlags;
  120. mMaterialID = rhs.mMaterialID;
  121. mMaterial = rhs.mMaterial;
  122. if (mGLTFMaterial.notNull())
  123. {
  124. mGLTFMaterial->removeTextureEntry(this);
  125. }
  126. mGLTFMaterial = rhs.mGLTFMaterial;
  127. if (mGLTFMaterial.notNull())
  128. {
  129. mGLTFMaterial->addTextureEntry(this);
  130. }
  131. if (rhs.mGLTFMaterialOverrides.notNull())
  132. {
  133. mGLTFMaterialOverrides =
  134. new LLGLTFMaterial(*rhs.mGLTFMaterialOverrides);
  135. }
  136. else
  137. {
  138. mGLTFMaterialOverrides = NULL;
  139. }
  140. mSelected = rhs.mSelected;
  141. mMaterialUpdatePending = rhs.mMaterialUpdatePending;
  142. if (mMediaEntry)
  143. {
  144. delete mMediaEntry;
  145. }
  146. if (rhs.mMediaEntry)
  147. {
  148. // Make a copy
  149. mMediaEntry = new LLMediaEntry(*rhs.mMediaEntry);
  150. }
  151. else
  152. {
  153. mMediaEntry = NULL;
  154. }
  155. }
  156. return *this;
  157. }
  158. void LLTextureEntry::init(const LLUUID& tex_id, F32 scale_s, F32 scale_t,
  159. F32 offset_s, F32 offset_t, F32 rotation, U8 bump)
  160. {
  161. setID(tex_id);
  162. mScaleS = scale_s;
  163. mScaleT = scale_t;
  164. mOffsetS = offset_s;
  165. mOffsetT = offset_t;
  166. mRotation = rotation;
  167. mBump = bump;
  168. mGlow = 0;
  169. mMediaFlags = 0x0;
  170. mMediaEntry = NULL;
  171. mMaterialID.clear();
  172. mMaterialUpdatePending = false;
  173. mSelected = false;
  174. mColor.set(1.f, 1.f, 1.f, 1.f);
  175. }
  176. LLTextureEntry::~LLTextureEntry()
  177. {
  178. if (mMediaEntry)
  179. {
  180. delete mMediaEntry;
  181. mMediaEntry = NULL;
  182. }
  183. if (mGLTFMaterial.notNull())
  184. {
  185. mGLTFMaterial->removeTextureEntry(this);
  186. mGLTFMaterial = NULL;
  187. }
  188. }
  189. bool LLTextureEntry::operator!=(const LLTextureEntry& rhs) const
  190. {
  191. if (mID != rhs.mID) return true;
  192. if (mScaleS != rhs.mScaleS) return true;
  193. if (mScaleT != rhs.mScaleT) return true;
  194. if (mOffsetS != rhs.mOffsetS) return true;
  195. if (mOffsetT != rhs.mOffsetT) return true;
  196. if (mRotation != rhs.mRotation) return true;
  197. if (mColor != rhs.mColor) return true;
  198. if (mBump != rhs.mBump) return true;
  199. if (mMediaFlags != rhs.mMediaFlags) return true;
  200. if (mGlow != rhs.mGlow) return true;
  201. if (mMaterialID != rhs.mMaterialID) return true;
  202. return false;
  203. }
  204. bool LLTextureEntry::operator==(const LLTextureEntry& rhs) const
  205. {
  206. if (mID != rhs.mID) return false;
  207. if (mScaleS != rhs.mScaleS) return false;
  208. if (mScaleT != rhs.mScaleT) return false;
  209. if (mOffsetS != rhs.mOffsetS) return false;
  210. if (mOffsetT != rhs.mOffsetT) return false;
  211. if (mRotation != rhs.mRotation) return false;
  212. if (mColor != rhs.mColor) return false;
  213. if (mBump != rhs.mBump) return false;
  214. if (mMediaFlags != rhs.mMediaFlags) return false;
  215. if (mGlow != rhs.mGlow) return false;
  216. if (mMaterialID != rhs.mMaterialID) return false;
  217. return true;
  218. }
  219. LLSD LLTextureEntry::asLLSD() const
  220. {
  221. LLSD sd;
  222. asLLSD(sd);
  223. return sd;
  224. }
  225. void LLTextureEntry::asLLSD(LLSD& sd) const
  226. {
  227. sd["imageid"] = mID;
  228. sd["colors"] = ll_sd_from_color4(mColor);
  229. sd["scales"] = mScaleS;
  230. sd["scalet"] = mScaleT;
  231. sd["offsets"] = mOffsetS;
  232. sd["offsett"] = mOffsetT;
  233. sd["imagerot"] = mRotation;
  234. sd["bump"] = getBumpShiny();
  235. sd["fullbright"] = getFullbright();
  236. sd["media_flags"] = mMediaFlags;
  237. if (hasMedia())
  238. {
  239. LLSD mediaData;
  240. if (getMediaData())
  241. {
  242. getMediaData()->asLLSD(mediaData);
  243. }
  244. sd[TEXTURE_MEDIA_DATA_KEY] = mediaData;
  245. }
  246. sd["glow"] = mGlow;
  247. if (mGLTFMaterialOverrides.notNull())
  248. {
  249. sd["gltf_override"] = mGLTFMaterialOverrides->asJSON();
  250. }
  251. }
  252. bool LLTextureEntry::fromLLSD(const LLSD& sd)
  253. {
  254. const char *w, *x;
  255. w = "imageid";
  256. if (sd.has(w))
  257. {
  258. setID(sd[w]);
  259. }
  260. else
  261. {
  262. return false;
  263. }
  264. w = "colors";
  265. if (sd.has(w))
  266. {
  267. mColor = ll_color4_from_sd(sd["colors"]);
  268. }
  269. else
  270. {
  271. return false;
  272. }
  273. w = "scales";
  274. x = "scalet";
  275. if (sd.has(w) && sd.has(x))
  276. {
  277. setScale((F32)sd[w].asReal(), (F32)sd[x].asReal());
  278. }
  279. else
  280. {
  281. return false;
  282. }
  283. w = "offsets";
  284. x = "offsett";
  285. if (sd.has(w) && sd.has(x))
  286. {
  287. setOffset((F32)sd[w].asReal(), (F32)sd[x].asReal());
  288. }
  289. else
  290. {
  291. return false;
  292. }
  293. w = "imagerot";
  294. if (sd.has(w))
  295. {
  296. setRotation((F32)sd[w].asReal());
  297. }
  298. else
  299. {
  300. return false;
  301. }
  302. w = "bump";
  303. if (sd.has(w))
  304. {
  305. setBumpShiny(sd[w].asInteger());
  306. }
  307. else
  308. {
  309. return false;
  310. }
  311. w = "fullbright";
  312. if (sd.has(w))
  313. {
  314. setFullbright(sd[w].asInteger());
  315. }
  316. else
  317. {
  318. return false;
  319. }
  320. w = "media_flags";
  321. if (sd.has(w))
  322. {
  323. setMediaTexGen(sd[w].asInteger());
  324. }
  325. else
  326. {
  327. return false;
  328. }
  329. // If the "has media" flag doesn't match the fact that media data exists,
  330. // updateMediaData will "fix" it by either clearing or setting the flag.
  331. w = TEXTURE_MEDIA_DATA_KEY;
  332. if (hasMedia() != sd.has(w))
  333. {
  334. llwarns << "media_flags (" << hasMedia()
  335. << ") does not match presence of media_data (" << sd.has(w)
  336. << "). Fixing." << llendl;
  337. }
  338. updateMediaData(sd[w]);
  339. w = "glow";
  340. if (sd.has(w))
  341. {
  342. setGlow((F32)sd[w].asReal());
  343. }
  344. else
  345. {
  346. setGlow(0.f);
  347. }
  348. w = "gltf_override";
  349. if (sd.has(w))
  350. {
  351. if (mGLTFMaterialOverrides.isNull())
  352. {
  353. mGLTFMaterialOverrides = new LLGLTFMaterial();
  354. }
  355. std::string warn_msg, error_msg;
  356. if (!mGLTFMaterialOverrides->fromJSON(sd[w].asString(), warn_msg,
  357. error_msg))
  358. {
  359. llwarns << "Failed to parse GLTF json: "
  360. << (error_msg.empty() ? warn_msg : error_msg) << llendl;
  361. mGLTFMaterialOverrides = NULL;
  362. }
  363. }
  364. return true;
  365. }
  366. S32 LLTextureEntry::setID(const LLUUID& tex_id)
  367. {
  368. if (mID != tex_id)
  369. {
  370. mID = tex_id;
  371. // Cache the "blank" texture and "default texture" status for speed
  372. // during rendering. HB
  373. mIsBlankTexture = tex_id == IMG_BLANK;
  374. mIsDefaultTexture = mIsBlankTexture || tex_id == IMG_PLYWOOD;
  375. return TEM_CHANGE_TEXTURE;
  376. }
  377. return TEM_CHANGE_NONE;
  378. }
  379. S32 LLTextureEntry::setScale(F32 s, F32 t)
  380. {
  381. S32 retval = 0;
  382. if (mScaleS != s || mScaleT != t)
  383. {
  384. mScaleS = s;
  385. mScaleT = t;
  386. retval = TEM_CHANGE_TEXTURE;
  387. }
  388. return retval;
  389. }
  390. S32 LLTextureEntry::setScaleS(F32 s)
  391. {
  392. S32 retval = TEM_CHANGE_NONE;
  393. if (mScaleS != s)
  394. {
  395. mScaleS = s;
  396. retval = TEM_CHANGE_TEXTURE;
  397. }
  398. return retval;
  399. }
  400. S32 LLTextureEntry::setScaleT(F32 t)
  401. {
  402. S32 retval = TEM_CHANGE_NONE;
  403. if (mScaleT != t)
  404. {
  405. mScaleT = t;
  406. retval = TEM_CHANGE_TEXTURE;
  407. }
  408. return retval;
  409. }
  410. S32 LLTextureEntry::setColor(const LLColor4& color)
  411. {
  412. if (mColor != color)
  413. {
  414. mColor = color;
  415. return TEM_CHANGE_COLOR;
  416. }
  417. return TEM_CHANGE_NONE;
  418. }
  419. S32 LLTextureEntry::setColor(const LLColor3& color)
  420. {
  421. if (mColor != color)
  422. {
  423. // This preserves alpha.
  424. mColor.set(color);
  425. return TEM_CHANGE_COLOR;
  426. }
  427. return TEM_CHANGE_NONE;
  428. }
  429. S32 LLTextureEntry::setAlpha(F32 alpha)
  430. {
  431. if (mColor.mV[VW] != alpha)
  432. {
  433. mColor.mV[VW] = alpha;
  434. return TEM_CHANGE_COLOR;
  435. }
  436. return TEM_CHANGE_NONE;
  437. }
  438. S32 LLTextureEntry::setOffset(F32 s, F32 t)
  439. {
  440. S32 retval = 0;
  441. if (mOffsetS != s || mOffsetT != t)
  442. {
  443. mOffsetS = s;
  444. mOffsetT = t;
  445. retval = TEM_CHANGE_TEXTURE;
  446. }
  447. return retval;
  448. }
  449. S32 LLTextureEntry::setOffsetS(F32 s)
  450. {
  451. S32 retval = 0;
  452. if (mOffsetS != s)
  453. {
  454. mOffsetS = s;
  455. retval = TEM_CHANGE_TEXTURE;
  456. }
  457. return retval;
  458. }
  459. S32 LLTextureEntry::setOffsetT(F32 t)
  460. {
  461. S32 retval = 0;
  462. if (mOffsetT != t)
  463. {
  464. mOffsetT = t;
  465. retval = TEM_CHANGE_TEXTURE;
  466. }
  467. return retval;
  468. }
  469. S32 LLTextureEntry::setRotation(F32 theta)
  470. {
  471. if (mRotation != theta && llfinite(theta))
  472. {
  473. mRotation = theta;
  474. return TEM_CHANGE_TEXTURE;
  475. }
  476. return TEM_CHANGE_NONE;
  477. }
  478. S32 LLTextureEntry::setBumpShinyFullbright(U8 bump)
  479. {
  480. if (mBump != bump)
  481. {
  482. mBump = bump;
  483. return TEM_CHANGE_TEXTURE;
  484. }
  485. return TEM_CHANGE_NONE;
  486. }
  487. S32 LLTextureEntry::setMediaTexGen(U8 media)
  488. {
  489. S32 result = TEM_CHANGE_NONE;
  490. result |= setTexGen(media & TEM_TEX_GEN_MASK);
  491. result |= setMediaFlags(media & TEM_MEDIA_MASK);
  492. return result;
  493. }
  494. S32 LLTextureEntry::setBumpmap(U8 bump)
  495. {
  496. bump &= TEM_BUMP_MASK;
  497. if (getBumpmap() != bump)
  498. {
  499. mBump &= ~TEM_BUMP_MASK;
  500. mBump |= bump;
  501. return TEM_CHANGE_TEXTURE;
  502. }
  503. return TEM_CHANGE_NONE;
  504. }
  505. S32 LLTextureEntry::setFullbright(U8 fullbright)
  506. {
  507. fullbright &= TEM_FULLBRIGHT_MASK;
  508. if (getFullbright() != fullbright)
  509. {
  510. mBump &= ~(TEM_FULLBRIGHT_MASK<<TEM_FULLBRIGHT_SHIFT);
  511. mBump |= fullbright << TEM_FULLBRIGHT_SHIFT;
  512. return TEM_CHANGE_TEXTURE;
  513. }
  514. return TEM_CHANGE_NONE;
  515. }
  516. S32 LLTextureEntry::setShiny(U8 shiny)
  517. {
  518. shiny &= TEM_SHINY_MASK;
  519. if (getShiny() != shiny)
  520. {
  521. mBump &= ~(TEM_SHINY_MASK<<TEM_SHINY_SHIFT);
  522. mBump |= shiny << TEM_SHINY_SHIFT;
  523. return TEM_CHANGE_TEXTURE;
  524. }
  525. return TEM_CHANGE_NONE;
  526. }
  527. S32 LLTextureEntry::setBumpShiny(U8 bump_shiny)
  528. {
  529. bump_shiny &= TEM_BUMP_SHINY_MASK;
  530. if (getBumpShiny() != bump_shiny)
  531. {
  532. mBump &= ~TEM_BUMP_SHINY_MASK;
  533. mBump |= bump_shiny;
  534. return TEM_CHANGE_TEXTURE;
  535. }
  536. return TEM_CHANGE_NONE;
  537. }
  538. S32 LLTextureEntry::setMediaFlags(U8 media_flags)
  539. {
  540. media_flags &= TEM_MEDIA_MASK;
  541. if (getMediaFlags() != media_flags)
  542. {
  543. mMediaFlags &= ~TEM_MEDIA_MASK;
  544. mMediaFlags |= media_flags;
  545. // Special code for media handling
  546. if (hasMedia() && !mMediaEntry)
  547. {
  548. mMediaEntry = new LLMediaEntry;
  549. }
  550. else if (!hasMedia() && mMediaEntry)
  551. {
  552. delete mMediaEntry;
  553. mMediaEntry = NULL;
  554. }
  555. return TEM_CHANGE_MEDIA;
  556. }
  557. return TEM_CHANGE_NONE;
  558. }
  559. S32 LLTextureEntry::setTexGen(U8 tex_gen)
  560. {
  561. tex_gen &= TEM_TEX_GEN_MASK;
  562. if (getTexGen() != tex_gen)
  563. {
  564. mMediaFlags &= ~TEM_TEX_GEN_MASK;
  565. mMediaFlags |= tex_gen;
  566. return TEM_CHANGE_TEXTURE;
  567. }
  568. return TEM_CHANGE_NONE;
  569. }
  570. S32 LLTextureEntry::setGlow(F32 glow)
  571. {
  572. if (glow < 0.f)
  573. {
  574. glow = 0.f;
  575. }
  576. if (mGlow != glow)
  577. {
  578. mGlow = glow;
  579. return TEM_CHANGE_TEXTURE;
  580. }
  581. return TEM_CHANGE_NONE;
  582. }
  583. S32 LLTextureEntry::setMaterialID(const LLMaterialID& matidp)
  584. {
  585. if (mMaterialID != matidp || (mMaterialUpdatePending && !mSelected))
  586. {
  587. if (mSelected)
  588. {
  589. mMaterialUpdatePending = true;
  590. mMaterialID = matidp;
  591. return TEM_CHANGE_TEXTURE;
  592. }
  593. mMaterialUpdatePending = false;
  594. mMaterialID = matidp;
  595. return TEM_CHANGE_TEXTURE;
  596. }
  597. return TEM_CHANGE_NONE;
  598. }
  599. S32 LLTextureEntry::setMaterialParams(const LLMaterialPtr paramsp)
  600. {
  601. if (mSelected)
  602. {
  603. mMaterialUpdatePending = true;
  604. }
  605. mMaterial = paramsp;
  606. return TEM_CHANGE_TEXTURE;
  607. }
  608. void LLTextureEntry::setMediaData(const LLMediaEntry& media_entry)
  609. {
  610. mMediaFlags |= MF_HAS_MEDIA;
  611. if (mMediaEntry)
  612. {
  613. delete mMediaEntry;
  614. }
  615. mMediaEntry = new LLMediaEntry(media_entry);
  616. }
  617. bool LLTextureEntry::updateMediaData(const LLSD& media_data)
  618. {
  619. if (media_data.isUndefined())
  620. {
  621. // Clear the media data
  622. clearMediaData();
  623. return false;
  624. }
  625. mMediaFlags |= MF_HAS_MEDIA;
  626. if (!mMediaEntry)
  627. {
  628. mMediaEntry = new LLMediaEntry;
  629. }
  630. // *NOTE: this will *clobber* all of the fields in mMediaEntry with
  631. //whatever fields are present (or not present) in media_data !
  632. mMediaEntry->fromLLSD(media_data);
  633. return true;
  634. }
  635. void LLTextureEntry::clearMediaData()
  636. {
  637. mMediaFlags &= ~MF_HAS_MEDIA;
  638. if (mMediaEntry)
  639. {
  640. delete mMediaEntry;
  641. }
  642. mMediaEntry = NULL;
  643. }
  644. void LLTextureEntry::mergeIntoMediaData(const LLSD& media_fields)
  645. {
  646. mMediaFlags |= MF_HAS_MEDIA;
  647. if (!mMediaEntry)
  648. {
  649. mMediaEntry = new LLMediaEntry;
  650. }
  651. // *NOTE: this will *merge* the data in media_fields with the data in our
  652. // media entry
  653. mMediaEntry->mergeFromLLSD(media_fields);
  654. }
  655. //static
  656. std::string LLTextureEntry::touchMediaVersionString(const std::string& in_ver,
  657. const LLUUID& agent_id)
  658. {
  659. // *TODO: make media version string binary (base64-encoded ?)
  660. // Media "URL" is a representation of a version and the last-touched agent
  661. // x-mv:nnnnn/agent-id
  662. // where "nnnnn" is version number
  663. // *NOTE: not the most efficient code in the world...
  664. U32 current_version = getVersionFromMediaVersionString(in_ver) + 1;
  665. const size_t MAX_VERSION_LEN = 10; // 2^32 fits in 10 decimal digits
  666. char buf[MAX_VERSION_LEN + 1];
  667. // Note: added int cast to fix warning/breakage on mac:
  668. snprintf(buf, (int)MAX_VERSION_LEN + 1, "%0*u", (int)MAX_VERSION_LEN,
  669. current_version);
  670. return MEDIA_VERSION_STRING_PREFIX + buf + "/" + agent_id.asString();
  671. }
  672. //static
  673. U32 LLTextureEntry::getVersionFromMediaVersionString(const std::string& ver)
  674. {
  675. U32 version = 0;
  676. if (!ver.empty())
  677. {
  678. size_t found = ver.find(MEDIA_VERSION_STRING_PREFIX);
  679. if (found != std::string::npos)
  680. {
  681. found = ver.find_first_of("/", found);
  682. std::string v = ver.substr(MEDIA_VERSION_STRING_PREFIX.length(),
  683. found);
  684. version = strtoul(v.c_str(), NULL, 10);
  685. }
  686. }
  687. return version;
  688. }
  689. //static
  690. LLUUID LLTextureEntry::getAgentIDFromMediaVersionString(const std::string& ver)
  691. {
  692. LLUUID id;
  693. if (!ver.empty())
  694. {
  695. size_t found = ver.find(MEDIA_VERSION_STRING_PREFIX);
  696. if (found != std::string::npos)
  697. {
  698. found = ver.find_first_of("/", found);
  699. if (found != std::string::npos)
  700. {
  701. std::string v = ver.substr(found + 1);
  702. id.set(v);
  703. }
  704. }
  705. }
  706. return id;
  707. }
  708. //static
  709. bool LLTextureEntry::isMediaVersionString(const std::string& ver)
  710. {
  711. return std::string::npos != ver.find(MEDIA_VERSION_STRING_PREFIX);
  712. }
  713. // New methods for PBR support
  714. void LLTextureEntry::setGLTFMaterial(LLGLTFMaterial* matp, bool local_origin)
  715. {
  716. if (matp == mGLTFMaterial.get())
  717. {
  718. return;
  719. }
  720. if (local_origin && mGLTFMaterialOverrides.get() &&
  721. !mGLTFMaterialOverrides->isClearedForBaseMaterial())
  722. {
  723. // If this warning triggers, try to make sure calling code is using
  724. // LLViewerObject::setRenderMaterialID.
  725. llwarns << "isClearedForBaseMaterial() returned false for a local material"
  726. << llendl;
  727. llassert(false);
  728. }
  729. if (mGLTFMaterial.notNull())
  730. {
  731. // Local materials have to keep track due to update mechanics
  732. mGLTFMaterial->removeTextureEntry(this);
  733. }
  734. mGLTFMaterial = matp;
  735. if (mGLTFMaterial.notNull())
  736. {
  737. mGLTFMaterial->addTextureEntry(this);
  738. }
  739. else
  740. {
  741. setGLTFRenderMaterial(NULL);
  742. }
  743. }
  744. S32 LLTextureEntry::setGLTFMaterialOverride(LLGLTFMaterial* matp)
  745. {
  746. // If override is not NULL, base material must not be NULL
  747. llassert(!matp || mGLTFMaterial.notNull());
  748. if (matp == mGLTFMaterialOverrides.get())
  749. {
  750. return TEM_CHANGE_NONE;
  751. }
  752. mGLTFMaterialOverrides = matp;
  753. return TEM_CHANGE_TEXTURE;
  754. }
  755. S32 LLTextureEntry::setBaseMaterial()
  756. {
  757. S32 changed = TEM_CHANGE_NONE;
  758. if (mGLTFMaterialOverrides.notNull())
  759. {
  760. if (mGLTFMaterialOverrides->setBaseMaterial())
  761. {
  762. changed = TEM_CHANGE_TEXTURE;
  763. }
  764. if (LLGLTFMaterial::sDefault == *(mGLTFMaterialOverrides.get()))
  765. {
  766. mGLTFMaterialOverrides = NULL;
  767. changed = TEM_CHANGE_TEXTURE;
  768. }
  769. }
  770. return changed;
  771. }
  772. LLGLTFMaterial* LLTextureEntry::getGLTFRenderMaterial() const
  773. {
  774. LLGLTFMaterial* matp = mGLTFRenderMaterial.get();
  775. if (matp)
  776. {
  777. return matp;
  778. }
  779. llassert(mGLTFMaterialOverrides.isNull() ||
  780. mGLTFMaterialOverrides->isClearedForBaseMaterial());
  781. return mGLTFMaterial.get();
  782. }
  783. S32 LLTextureEntry::setGLTFRenderMaterial(LLGLTFMaterial* matp)
  784. {
  785. if (mGLTFRenderMaterial.get() == matp)
  786. {
  787. return TEM_CHANGE_NONE;
  788. }
  789. mGLTFRenderMaterial = matp;
  790. return TEM_CHANGE_TEXTURE;
  791. }