OgreTextureUnitState.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "OgreTextureUnitState.h"
  25. #include "OgreException.h"
  26. namespace Ogre {
  27. //-----------------------------------------------------------------------
  28. TextureUnitState::TextureUnitState(Pass* parent)
  29. : mCurrentFrame(0)
  30. , mAnimDuration(0)
  31. , mCubic(false)
  32. , mTextureType(TEX_TYPE_2D)
  33. , mDesiredFormat(PF_UNKNOWN)
  34. , mTextureSrcMipmaps(MIP_DEFAULT)
  35. , mTextureCoordSetIndex(0)
  36. , mBorderColour(ColourValue::Black)
  37. , mTextureLoadFailed(false)
  38. , mIsAlpha(false)
  39. , mHwGamma(false)
  40. , mRecalcTexMatrix(false)
  41. , mUMod(0)
  42. , mVMod(0)
  43. , mUScale(1)
  44. , mVScale(1)
  45. , mRotate(0)
  46. , mTexModMatrix(Matrix4::IDENTITY)
  47. , mMinFilter(FO_LINEAR)
  48. , mMagFilter(FO_LINEAR)
  49. , mMipFilter(FO_POINT)
  50. , mMaxAniso(0)
  51. , mMipmapBias(0)
  52. , mIsDefaultAniso(true)
  53. , mIsDefaultFiltering(true)
  54. , mBindingType(BT_FRAGMENT)
  55. , mContentType(CONTENT_NAMED)
  56. , mParent(parent)
  57. {
  58. mColourBlendMode.blendType = LBT_COLOUR;
  59. mAlphaBlendMode.operation = LBX_MODULATE;
  60. mAlphaBlendMode.blendType = LBT_ALPHA;
  61. mAlphaBlendMode.source1 = LBS_TEXTURE;
  62. mAlphaBlendMode.source2 = LBS_CURRENT;
  63. setColourOperation(LBO_MODULATE);
  64. setTextureAddressingMode(TAM_WRAP);
  65. }
  66. //-----------------------------------------------------------------------
  67. TextureUnitState::TextureUnitState(Pass* parent, const TextureUnitState& oth )
  68. {
  69. mParent = parent;
  70. *this = oth;
  71. }
  72. //-----------------------------------------------------------------------
  73. TextureUnitState::TextureUnitState( Pass* parent, const String& texName, unsigned int texCoordSet)
  74. : mCurrentFrame(0)
  75. , mAnimDuration(0)
  76. , mCubic(false)
  77. , mTextureType(TEX_TYPE_2D)
  78. , mDesiredFormat(PF_UNKNOWN)
  79. , mTextureSrcMipmaps(MIP_DEFAULT)
  80. , mTextureCoordSetIndex(0)
  81. , mBorderColour(ColourValue::Black)
  82. , mTextureLoadFailed(false)
  83. , mIsAlpha(false)
  84. , mHwGamma(false)
  85. , mRecalcTexMatrix(false)
  86. , mUMod(0)
  87. , mVMod(0)
  88. , mUScale(1)
  89. , mVScale(1)
  90. , mRotate(0)
  91. , mTexModMatrix(Matrix4::IDENTITY)
  92. , mMinFilter(FO_LINEAR)
  93. , mMagFilter(FO_LINEAR)
  94. , mMipFilter(FO_POINT)
  95. , mMaxAniso(0)
  96. , mMipmapBias(0)
  97. , mIsDefaultAniso(true)
  98. , mIsDefaultFiltering(true)
  99. , mBindingType(BT_FRAGMENT)
  100. , mContentType(CONTENT_NAMED)
  101. , mParent(parent)
  102. {
  103. mColourBlendMode.blendType = LBT_COLOUR;
  104. mAlphaBlendMode.operation = LBX_MODULATE;
  105. mAlphaBlendMode.blendType = LBT_ALPHA;
  106. mAlphaBlendMode.source1 = LBS_TEXTURE;
  107. mAlphaBlendMode.source2 = LBS_CURRENT;
  108. setColourOperation(LBO_MODULATE);
  109. setTextureAddressingMode(TAM_WRAP);
  110. setTextureName(texName);
  111. setTextureCoordSet(texCoordSet);
  112. }
  113. //-----------------------------------------------------------------------
  114. TextureUnitState::~TextureUnitState()
  115. {
  116. // Unload ensure all controllers destroyed
  117. _unload();
  118. }
  119. //-----------------------------------------------------------------------
  120. TextureUnitState & TextureUnitState::operator = (
  121. const TextureUnitState &oth )
  122. {
  123. assert(mEffects.empty());
  124. // copy basic members (int's, real's)
  125. memcpy( this, &oth, (uchar *)(&oth.mFrames) - (uchar *)(&oth) );
  126. // copy complex members
  127. mFrames = oth.mFrames;
  128. mFramePtrs = oth.mFramePtrs;
  129. mName = oth.mName;
  130. mEffects = oth.mEffects;
  131. mTextureNameAlias = oth.mTextureNameAlias;
  132. mCompositorRefName = oth.mCompositorRefName;
  133. mCompositorRefTexName = oth.mCompositorRefTexName;
  134. // Load immediately if Material loaded
  135. if (isLoaded())
  136. {
  137. _load();
  138. }
  139. return *this;
  140. }
  141. //-----------------------------------------------------------------------
  142. const String& TextureUnitState::getTextureName(void) const
  143. {
  144. // Return name of current frame
  145. if (mCurrentFrame < mFrames.size())
  146. return mFrames[mCurrentFrame];
  147. else
  148. return StringUtil::BLANK;
  149. }
  150. //-----------------------------------------------------------------------
  151. void TextureUnitState::setTextureName( const String& name, TextureType texType)
  152. {
  153. setContentType(CONTENT_NAMED);
  154. mTextureLoadFailed = false;
  155. if (texType == TEX_TYPE_CUBE_MAP)
  156. {
  157. // delegate to cubic texture implementation
  158. setCubicTextureName(name, true);
  159. }
  160. else
  161. {
  162. mFrames.resize(1);
  163. mFramePtrs.resize(1);
  164. mFrames[0] = name;
  165. mFramePtrs[0] = nullptr;
  166. // defer load until used, so don't grab pointer yet
  167. mCurrentFrame = 0;
  168. mCubic = false;
  169. mTextureType = texType;
  170. if (name.empty())
  171. {
  172. return;
  173. }
  174. // Load immediately ?
  175. if (isLoaded())
  176. {
  177. _load(); // reload
  178. }
  179. }
  180. }
  181. //-----------------------------------------------------------------------
  182. void TextureUnitState::setBindingType(TextureUnitState::BindingType bt)
  183. {
  184. mBindingType = bt;
  185. }
  186. //-----------------------------------------------------------------------
  187. TextureUnitState::BindingType TextureUnitState::getBindingType(void) const
  188. {
  189. return mBindingType;
  190. }
  191. //-----------------------------------------------------------------------
  192. void TextureUnitState::setContentType(TextureUnitState::ContentType ct)
  193. {
  194. mContentType = ct;
  195. if (ct == CONTENT_SHADOW || ct == CONTENT_COMPOSITOR)
  196. {
  197. // Clear out texture frames, not applicable
  198. mFrames.clear();
  199. // One reference space, set manually through _setTexturePtr
  200. mFramePtrs.resize(1);
  201. mFramePtrs[0] = nullptr;
  202. }
  203. }
  204. //-----------------------------------------------------------------------
  205. TextureUnitState::ContentType TextureUnitState::getContentType(void) const
  206. {
  207. return mContentType;
  208. }
  209. //-----------------------------------------------------------------------
  210. void TextureUnitState::setCubicTextureName( const String& name, bool forUVW)
  211. {
  212. if (forUVW)
  213. {
  214. setCubicTextureName(&name, forUVW);
  215. }
  216. else
  217. {
  218. setContentType(CONTENT_NAMED);
  219. mTextureLoadFailed = false;
  220. String ext;
  221. String suffixes[6] = {"_fr", "_bk", "_lf", "_rt", "_up", "_dn"};
  222. String baseName;
  223. String fullNames[6];
  224. size_t pos = name.find_last_of(".");
  225. if( pos != String::npos )
  226. {
  227. baseName = name.substr(0, pos);
  228. ext = name.substr(pos);
  229. }
  230. else
  231. baseName = name;
  232. for (int i = 0; i < 6; ++i)
  233. {
  234. fullNames[i] = baseName + suffixes[i] + ext;
  235. }
  236. setCubicTextureName(fullNames, forUVW);
  237. }
  238. }
  239. //-----------------------------------------------------------------------
  240. void TextureUnitState::setCubicTextureName(const String* const names, bool forUVW)
  241. {
  242. setContentType(CONTENT_NAMED);
  243. mTextureLoadFailed = false;
  244. mFrames.resize(forUVW ? 1 : 6);
  245. // resize pointers, but don't populate until asked for
  246. mFramePtrs.resize(forUVW ? 1 : 6);
  247. mAnimDuration = 0;
  248. mCurrentFrame = 0;
  249. mCubic = true;
  250. mTextureType = forUVW ? TEX_TYPE_CUBE_MAP : TEX_TYPE_2D;
  251. for (unsigned int i = 0; i < mFrames.size(); ++i)
  252. {
  253. mFrames[i] = names[i];
  254. mFramePtrs[i] = nullptr;
  255. }
  256. }
  257. //-----------------------------------------------------------------------
  258. bool TextureUnitState::isCubic(void) const
  259. {
  260. return mCubic;
  261. }
  262. //-----------------------------------------------------------------------
  263. bool TextureUnitState::is3D(void) const
  264. {
  265. return mTextureType == TEX_TYPE_CUBE_MAP;
  266. }
  267. //-----------------------------------------------------------------------
  268. TextureType TextureUnitState::getTextureType(void) const
  269. {
  270. return mTextureType;
  271. }
  272. //-----------------------------------------------------------------------
  273. void TextureUnitState::setFrameTextureName(const String& name, unsigned int frameNumber)
  274. {
  275. mTextureLoadFailed = false;
  276. if (frameNumber < mFrames.size())
  277. {
  278. mFrames[frameNumber] = name;
  279. // reset pointer (don't populate until requested)
  280. mFramePtrs[frameNumber] = nullptr;
  281. if (isLoaded())
  282. {
  283. _load(); // reload
  284. }
  285. }
  286. else // raise exception for frameNumber out of bounds
  287. {
  288. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "frameNumber paramter value exceeds number of stored frames.",
  289. "TextureUnitState::setFrameTextureName");
  290. }
  291. }
  292. //-----------------------------------------------------------------------
  293. void TextureUnitState::addFrameTextureName(const String& name)
  294. {
  295. setContentType(CONTENT_NAMED);
  296. mTextureLoadFailed = false;
  297. mFrames.push_back(name);
  298. // Add blank pointer, load on demand
  299. mFramePtrs.push_back(TexturePtr());
  300. // Load immediately if Material loaded
  301. if (isLoaded())
  302. {
  303. _load();
  304. }
  305. }
  306. //-----------------------------------------------------------------------
  307. void TextureUnitState::deleteFrameTextureName(const size_t frameNumber)
  308. {
  309. mTextureLoadFailed = false;
  310. if (frameNumber < mFrames.size())
  311. {
  312. mFrames.erase(mFrames.begin() + frameNumber);
  313. mFramePtrs.erase(mFramePtrs.begin() + frameNumber);
  314. if (isLoaded())
  315. {
  316. _load();
  317. }
  318. }
  319. else
  320. {
  321. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "frameNumber paramter value exceeds number of stored frames.",
  322. "TextureUnitState::deleteFrameTextureName");
  323. }
  324. }
  325. //-----------------------------------------------------------------------
  326. void TextureUnitState::setAnimatedTextureName( const String& name, unsigned int numFrames, Real duration)
  327. {
  328. setContentType(CONTENT_NAMED);
  329. mTextureLoadFailed = false;
  330. String ext;
  331. String baseName;
  332. size_t pos = name.find_last_of(".");
  333. baseName = name.substr(0, pos);
  334. ext = name.substr(pos);
  335. mFrames.resize(numFrames);
  336. // resize pointers, but don't populate until needed
  337. mFramePtrs.resize(numFrames);
  338. mAnimDuration = duration;
  339. mCurrentFrame = 0;
  340. mCubic = false;
  341. for (unsigned int i = 0; i < mFrames.size(); ++i)
  342. {
  343. StringUtil::StrStreamType str;
  344. str << baseName << "_" << i << ext;
  345. mFrames[i] = str.str();
  346. mFramePtrs[i] = nullptr;
  347. }
  348. // Load immediately if Material loaded
  349. if (isLoaded())
  350. {
  351. _load();
  352. }
  353. }
  354. //-----------------------------------------------------------------------
  355. void TextureUnitState::setAnimatedTextureName(const String* const names, unsigned int numFrames, Real duration)
  356. {
  357. setContentType(CONTENT_NAMED);
  358. mTextureLoadFailed = false;
  359. mFrames.resize(numFrames);
  360. // resize pointers, but don't populate until needed
  361. mFramePtrs.resize(numFrames);
  362. mAnimDuration = duration;
  363. mCurrentFrame = 0;
  364. mCubic = false;
  365. for (unsigned int i = 0; i < mFrames.size(); ++i)
  366. {
  367. mFrames[i] = names[i];
  368. mFramePtrs[i] = nullptr;
  369. }
  370. // Load immediately if Material loaded
  371. if (isLoaded())
  372. {
  373. _load();
  374. }
  375. }
  376. //-----------------------------------------------------------------------
  377. std::pair< size_t, size_t > TextureUnitState::getTextureDimensions( unsigned int frame ) const
  378. {
  379. TexturePtr tex = _getTexturePtr(frame);
  380. if (tex == nullptr)
  381. OGRE_EXCEPT( Exception::ERR_ITEM_NOT_FOUND, "Could not find texture " + mFrames[ frame ],
  382. "TextureUnitState::getTextureDimensions" );
  383. return std::pair< size_t, size_t >( tex->getWidth(), tex->getHeight() );
  384. }
  385. //-----------------------------------------------------------------------
  386. void TextureUnitState::setCurrentFrame(unsigned int frameNumber)
  387. {
  388. if (frameNumber < mFrames.size())
  389. {
  390. mCurrentFrame = frameNumber;
  391. }
  392. else
  393. {
  394. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "frameNumber paramter value exceeds number of stored frames.",
  395. "TextureUnitState::setCurrentFrame");
  396. }
  397. }
  398. //-----------------------------------------------------------------------
  399. unsigned int TextureUnitState::getCurrentFrame(void) const
  400. {
  401. return mCurrentFrame;
  402. }
  403. //-----------------------------------------------------------------------
  404. unsigned int TextureUnitState::getNumFrames(void) const
  405. {
  406. return (unsigned int)mFrames.size();
  407. }
  408. //-----------------------------------------------------------------------
  409. const String& TextureUnitState::getFrameTextureName(unsigned int frameNumber) const
  410. {
  411. if (frameNumber >= mFrames.size())
  412. {
  413. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "frameNumber paramter value exceeds number of stored frames.",
  414. "TextureUnitState::getFrameTextureName");
  415. }
  416. return mFrames[frameNumber];
  417. }
  418. //-----------------------------------------------------------------------
  419. void TextureUnitState::setDesiredFormat(PixelFormat desiredFormat)
  420. {
  421. mDesiredFormat = desiredFormat;
  422. }
  423. //-----------------------------------------------------------------------
  424. PixelFormat TextureUnitState::getDesiredFormat(void) const
  425. {
  426. return mDesiredFormat;
  427. }
  428. //-----------------------------------------------------------------------
  429. void TextureUnitState::setNumMipmaps(int numMipmaps)
  430. {
  431. mTextureSrcMipmaps = numMipmaps;
  432. }
  433. //-----------------------------------------------------------------------
  434. int TextureUnitState::getNumMipmaps(void) const
  435. {
  436. return mTextureSrcMipmaps;
  437. }
  438. //-----------------------------------------------------------------------
  439. void TextureUnitState::setIsAlpha(bool isAlpha)
  440. {
  441. mIsAlpha = isAlpha;
  442. }
  443. //-----------------------------------------------------------------------
  444. bool TextureUnitState::getIsAlpha(void) const
  445. {
  446. return mIsAlpha;
  447. }
  448. //-----------------------------------------------------------------------
  449. void TextureUnitState::setHardwareGammaEnabled(bool g)
  450. {
  451. mHwGamma = g;
  452. }
  453. //-----------------------------------------------------------------------
  454. bool TextureUnitState::isHardwareGammaEnabled() const
  455. {
  456. return mHwGamma;
  457. }
  458. //-----------------------------------------------------------------------
  459. unsigned int TextureUnitState::getTextureCoordSet(void) const
  460. {
  461. return mTextureCoordSetIndex;
  462. }
  463. //-----------------------------------------------------------------------
  464. void TextureUnitState::setTextureCoordSet(unsigned int set)
  465. {
  466. mTextureCoordSetIndex = set;
  467. }
  468. //-----------------------------------------------------------------------
  469. void TextureUnitState::setColourOperationEx(LayerBlendOperationEx op,
  470. LayerBlendSource source1,
  471. LayerBlendSource source2,
  472. const ColourValue& arg1,
  473. const ColourValue& arg2,
  474. Real manualBlend)
  475. {
  476. mColourBlendMode.operation = op;
  477. mColourBlendMode.source1 = source1;
  478. mColourBlendMode.source2 = source2;
  479. mColourBlendMode.colourArg1 = arg1;
  480. mColourBlendMode.colourArg2 = arg2;
  481. mColourBlendMode.factor = manualBlend;
  482. }
  483. //-----------------------------------------------------------------------
  484. void TextureUnitState::setColourOperation(LayerBlendOperation op)
  485. {
  486. // Set up the multitexture and multipass blending operations
  487. switch (op)
  488. {
  489. case LBO_REPLACE:
  490. setColourOperationEx(LBX_SOURCE1, LBS_TEXTURE, LBS_CURRENT);
  491. setColourOpMultipassFallback(SBF_ONE, SBF_ZERO);
  492. break;
  493. case LBO_ADD:
  494. setColourOperationEx(LBX_ADD, LBS_TEXTURE, LBS_CURRENT);
  495. setColourOpMultipassFallback(SBF_ONE, SBF_ONE);
  496. break;
  497. case LBO_MODULATE:
  498. setColourOperationEx(LBX_MODULATE, LBS_TEXTURE, LBS_CURRENT);
  499. setColourOpMultipassFallback(SBF_DEST_COLOUR, SBF_ZERO);
  500. break;
  501. case LBO_ALPHA_BLEND:
  502. setColourOperationEx(LBX_BLEND_TEXTURE_ALPHA, LBS_TEXTURE, LBS_CURRENT);
  503. setColourOpMultipassFallback(SBF_SOURCE_ALPHA, SBF_ONE_MINUS_SOURCE_ALPHA);
  504. break;
  505. }
  506. }
  507. //-----------------------------------------------------------------------
  508. void TextureUnitState::setColourOpMultipassFallback(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor)
  509. {
  510. mColourBlendFallbackSrc = sourceFactor;
  511. mColourBlendFallbackDest = destFactor;
  512. }
  513. //-----------------------------------------------------------------------
  514. void TextureUnitState::setAlphaOperation(LayerBlendOperationEx op,
  515. LayerBlendSource source1,
  516. LayerBlendSource source2,
  517. Real arg1,
  518. Real arg2,
  519. Real manualBlend)
  520. {
  521. mAlphaBlendMode.operation = op;
  522. mAlphaBlendMode.source1 = source1;
  523. mAlphaBlendMode.source2 = source2;
  524. mAlphaBlendMode.alphaArg1 = arg1;
  525. mAlphaBlendMode.alphaArg2 = arg2;
  526. mAlphaBlendMode.factor = manualBlend;
  527. }
  528. //-----------------------------------------------------------------------
  529. bool TextureUnitState::isBlank(void) const
  530. {
  531. if (mFrames.empty())
  532. return true;
  533. else
  534. return mFrames[0].empty() || mTextureLoadFailed;
  535. }
  536. //-----------------------------------------------------------------------
  537. SceneBlendFactor TextureUnitState::getColourBlendFallbackSrc(void) const
  538. {
  539. return mColourBlendFallbackSrc;
  540. }
  541. //-----------------------------------------------------------------------
  542. SceneBlendFactor TextureUnitState::getColourBlendFallbackDest(void) const
  543. {
  544. return mColourBlendFallbackDest;
  545. }
  546. //-----------------------------------------------------------------------
  547. const LayerBlendModeEx& TextureUnitState::getColourBlendMode(void) const
  548. {
  549. return mColourBlendMode;
  550. }
  551. //-----------------------------------------------------------------------
  552. const LayerBlendModeEx& TextureUnitState::getAlphaBlendMode(void) const
  553. {
  554. return mAlphaBlendMode;
  555. }
  556. //-----------------------------------------------------------------------
  557. const TextureUnitState::UVWAddressingMode&
  558. TextureUnitState::getTextureAddressingMode(void) const
  559. {
  560. return mAddressMode;
  561. }
  562. //-----------------------------------------------------------------------
  563. void TextureUnitState::setTextureAddressingMode(
  564. TextureUnitState::TextureAddressingMode tam)
  565. {
  566. mAddressMode.u = tam;
  567. mAddressMode.v = tam;
  568. mAddressMode.w = tam;
  569. }
  570. //-----------------------------------------------------------------------
  571. void TextureUnitState::setTextureAddressingMode(
  572. TextureUnitState::TextureAddressingMode u,
  573. TextureUnitState::TextureAddressingMode v,
  574. TextureUnitState::TextureAddressingMode w)
  575. {
  576. mAddressMode.u = u;
  577. mAddressMode.v = v;
  578. mAddressMode.w = w;
  579. }
  580. //-----------------------------------------------------------------------
  581. void TextureUnitState::setTextureAddressingMode(
  582. const TextureUnitState::UVWAddressingMode& uvw)
  583. {
  584. mAddressMode = uvw;
  585. }
  586. //-----------------------------------------------------------------------
  587. void TextureUnitState::setTextureBorderColour(const ColourValue& colour)
  588. {
  589. mBorderColour = colour;
  590. }
  591. //-----------------------------------------------------------------------
  592. const ColourValue& TextureUnitState::getTextureBorderColour(void) const
  593. {
  594. return mBorderColour;
  595. }
  596. //-----------------------------------------------------------------------
  597. void TextureUnitState::setBlank(void)
  598. {
  599. setTextureName(StringUtil::BLANK);
  600. }
  601. //-----------------------------------------------------------------------
  602. void TextureUnitState::setTextureTransform(const Matrix4& xform)
  603. {
  604. mTexModMatrix = xform;
  605. mRecalcTexMatrix = false;
  606. }
  607. //-----------------------------------------------------------------------
  608. void TextureUnitState::setTextureScroll(Real u, Real v)
  609. {
  610. mUMod = u;
  611. mVMod = v;
  612. mRecalcTexMatrix = true;
  613. }
  614. //-----------------------------------------------------------------------
  615. void TextureUnitState::setTextureScale(Real uScale, Real vScale)
  616. {
  617. mUScale = uScale;
  618. mVScale = vScale;
  619. mRecalcTexMatrix = true;
  620. }
  621. //-----------------------------------------------------------------------
  622. void TextureUnitState::setTextureRotate(const Radian& angle)
  623. {
  624. mRotate = angle;
  625. mRecalcTexMatrix = true;
  626. }
  627. //-----------------------------------------------------------------------
  628. const Matrix4& TextureUnitState::getTextureTransform() const
  629. {
  630. if (mRecalcTexMatrix)
  631. recalcTextureMatrix();
  632. return mTexModMatrix;
  633. }
  634. //-----------------------------------------------------------------------
  635. void TextureUnitState::recalcTextureMatrix() const
  636. {
  637. // Assumption: 2D texture coords
  638. Matrix4 xform;
  639. xform = Matrix4::IDENTITY;
  640. if (mUScale != 1 || mVScale != 1)
  641. {
  642. // Offset to center of texture
  643. xform[0][0] = 1/mUScale;
  644. xform[1][1] = 1/mVScale;
  645. // Skip matrix concat since first matrix update
  646. xform[0][3] = (-0.5f * xform[0][0]) + 0.5f;
  647. xform[1][3] = (-0.5f * xform[1][1]) + 0.5f;
  648. }
  649. if (mUMod || mVMod)
  650. {
  651. Matrix4 xlate = Matrix4::IDENTITY;
  652. xlate[0][3] = mUMod;
  653. xlate[1][3] = mVMod;
  654. xform = xlate * xform;
  655. }
  656. if (mRotate != Radian(0))
  657. {
  658. Matrix4 rot = Matrix4::IDENTITY;
  659. Radian theta ( mRotate );
  660. Real cosTheta = Math::Cos(theta);
  661. Real sinTheta = Math::Sin(theta);
  662. rot[0][0] = cosTheta;
  663. rot[0][1] = -sinTheta;
  664. rot[1][0] = sinTheta;
  665. rot[1][1] = cosTheta;
  666. // Offset center of rotation to center of texture
  667. rot[0][3] = 0.5f + ( (-0.5f * cosTheta) - (-0.5f * sinTheta) );
  668. rot[1][3] = 0.5f + ( (-0.5f * sinTheta) + (-0.5f * cosTheta) );
  669. xform = rot * xform;
  670. }
  671. mTexModMatrix = xform;
  672. mRecalcTexMatrix = false;
  673. }
  674. //-----------------------------------------------------------------------
  675. void TextureUnitState::setTextureUScroll(Real value)
  676. {
  677. mUMod = value;
  678. mRecalcTexMatrix = true;
  679. }
  680. //-----------------------------------------------------------------------
  681. void TextureUnitState::setTextureVScroll(Real value)
  682. {
  683. mVMod = value;
  684. mRecalcTexMatrix = true;
  685. }
  686. //-----------------------------------------------------------------------
  687. void TextureUnitState::setTextureUScale(Real value)
  688. {
  689. mUScale = value;
  690. mRecalcTexMatrix = true;
  691. }
  692. //-----------------------------------------------------------------------
  693. void TextureUnitState::setTextureVScale(Real value)
  694. {
  695. mVScale = value;
  696. mRecalcTexMatrix = true;
  697. }
  698. //-----------------------------------------------------------------------
  699. void TextureUnitState::_prepare(void)
  700. {
  701. // Unload first
  702. //_unload();
  703. // Load textures
  704. for (unsigned int i = 0; i < mFrames.size(); ++i)
  705. {
  706. ensurePrepared(i);
  707. }
  708. }
  709. //-----------------------------------------------------------------------
  710. void TextureUnitState::_load(void)
  711. {
  712. // Load textures
  713. for (unsigned int i = 0; i < mFrames.size(); ++i)
  714. {
  715. ensureLoaded(i);
  716. }
  717. }
  718. //-----------------------------------------------------------------------
  719. const TexturePtr& TextureUnitState::_getTexturePtr(void) const
  720. {
  721. return _getTexturePtr(mCurrentFrame);
  722. }
  723. //-----------------------------------------------------------------------
  724. const TexturePtr& TextureUnitState::_getTexturePtr(size_t frame) const
  725. {
  726. if (mContentType == CONTENT_NAMED)
  727. {
  728. if (frame < mFrames.size() && !mTextureLoadFailed)
  729. {
  730. ensureLoaded(frame);
  731. return mFramePtrs[frame];
  732. }
  733. else
  734. {
  735. // Silent fail with empty texture for internal method
  736. static TexturePtr nullTexPtr;
  737. return nullTexPtr;
  738. }
  739. }
  740. else
  741. {
  742. // Manually bound texture, no name or loading
  743. assert(frame < mFramePtrs.size());
  744. return mFramePtrs[frame];
  745. }
  746. }
  747. //-----------------------------------------------------------------------
  748. void TextureUnitState::_setTexturePtr(const TexturePtr& texptr)
  749. {
  750. _setTexturePtr(texptr, mCurrentFrame);
  751. }
  752. //-----------------------------------------------------------------------
  753. void TextureUnitState::_setTexturePtr(const TexturePtr& texptr, size_t frame)
  754. {
  755. assert(frame < mFramePtrs.size());
  756. mFramePtrs[frame] = texptr;
  757. }
  758. //-----------------------------------------------------------------------
  759. void TextureUnitState::ensurePrepared(size_t frame) const
  760. {
  761. }
  762. //-----------------------------------------------------------------------
  763. void TextureUnitState::ensureLoaded(size_t frame) const
  764. {
  765. }
  766. //-----------------------------------------------------------------------
  767. Real TextureUnitState::getTextureUScroll(void) const
  768. {
  769. return mUMod;
  770. }
  771. //-----------------------------------------------------------------------
  772. Real TextureUnitState::getTextureVScroll(void) const
  773. {
  774. return mVMod;
  775. }
  776. //-----------------------------------------------------------------------
  777. Real TextureUnitState::getTextureUScale(void) const
  778. {
  779. return mUScale;
  780. }
  781. //-----------------------------------------------------------------------
  782. Real TextureUnitState::getTextureVScale(void) const
  783. {
  784. return mVScale;
  785. }
  786. //-----------------------------------------------------------------------
  787. const Radian& TextureUnitState::getTextureRotate(void) const
  788. {
  789. return mRotate;
  790. }
  791. //-----------------------------------------------------------------------
  792. Real TextureUnitState::getAnimationDuration(void) const
  793. {
  794. return mAnimDuration;
  795. }
  796. //-----------------------------------------------------------------------
  797. const TextureUnitState::EffectMap& TextureUnitState::getEffects(void) const
  798. {
  799. return mEffects;
  800. }
  801. //-----------------------------------------------------------------------
  802. void TextureUnitState::setTextureFiltering(TextureFilterOptions filterType)
  803. {
  804. switch (filterType)
  805. {
  806. case TFO_NONE:
  807. setTextureFiltering(FO_POINT, FO_POINT, FO_NONE);
  808. break;
  809. case TFO_BILINEAR:
  810. setTextureFiltering(FO_LINEAR, FO_LINEAR, FO_POINT);
  811. break;
  812. case TFO_TRILINEAR:
  813. setTextureFiltering(FO_LINEAR, FO_LINEAR, FO_LINEAR);
  814. break;
  815. case TFO_ANISOTROPIC:
  816. setTextureFiltering(FO_ANISOTROPIC, FO_ANISOTROPIC, FO_LINEAR);
  817. break;
  818. }
  819. mIsDefaultFiltering = false;
  820. }
  821. //-----------------------------------------------------------------------
  822. void TextureUnitState::setTextureFiltering(FilterType ft, FilterOptions fo)
  823. {
  824. switch (ft)
  825. {
  826. case FT_MIN:
  827. mMinFilter = fo;
  828. break;
  829. case FT_MAG:
  830. mMagFilter = fo;
  831. break;
  832. case FT_MIP:
  833. mMipFilter = fo;
  834. break;
  835. }
  836. mIsDefaultFiltering = false;
  837. }
  838. //-----------------------------------------------------------------------
  839. void TextureUnitState::setTextureFiltering(FilterOptions minFilter,
  840. FilterOptions magFilter, FilterOptions mipFilter)
  841. {
  842. mMinFilter = minFilter;
  843. mMagFilter = magFilter;
  844. mMipFilter = mipFilter;
  845. mIsDefaultFiltering = false;
  846. }
  847. //-----------------------------------------------------------------------
  848. FilterOptions TextureUnitState::getTextureFiltering(FilterType ft) const
  849. {
  850. switch (ft)
  851. {
  852. case FT_MIN:
  853. return mMinFilter;
  854. case FT_MAG:
  855. return mMagFilter;
  856. case FT_MIP:
  857. return mMipFilter;
  858. }
  859. // to keep compiler happy
  860. return mMinFilter;
  861. }
  862. //-----------------------------------------------------------------------
  863. void TextureUnitState::setTextureAnisotropy(unsigned int maxAniso)
  864. {
  865. mMaxAniso = maxAniso;
  866. mIsDefaultAniso = false;
  867. }
  868. //-----------------------------------------------------------------------
  869. unsigned int TextureUnitState::getTextureAnisotropy() const
  870. {
  871. return mMaxAniso;
  872. }
  873. //-----------------------------------------------------------------------
  874. void TextureUnitState::_unprepare(void)
  875. {
  876. // Unreference textures
  877. vector<TexturePtr>::type::iterator ti, tiend;
  878. tiend = mFramePtrs.end();
  879. for (ti = mFramePtrs.begin(); ti != tiend; ++ti)
  880. {
  881. (*ti) = nullptr;
  882. }
  883. }
  884. //-----------------------------------------------------------------------
  885. void TextureUnitState::_unload(void)
  886. {
  887. // Unreference but don't unload textures. may be used elsewhere
  888. vector<TexturePtr>::type::iterator ti, tiend;
  889. tiend = mFramePtrs.end();
  890. for (ti = mFramePtrs.begin(); ti != tiend; ++ti)
  891. {
  892. (*ti) = nullptr;
  893. }
  894. }
  895. //-----------------------------------------------------------------------------
  896. bool TextureUnitState::isLoaded(void) const
  897. {
  898. return true;
  899. }
  900. //-----------------------------------------------------------------------
  901. void TextureUnitState::_notifyNeedsRecompile(void)
  902. {
  903. }
  904. //-----------------------------------------------------------------------
  905. bool TextureUnitState::hasViewRelativeTextureCoordinateGeneration(void) const
  906. {
  907. // Right now this only returns true for reflection maps
  908. EffectMap::const_iterator i, iend;
  909. iend = mEffects.end();
  910. for(i = mEffects.find(ET_ENVIRONMENT_MAP); i != iend; ++i)
  911. {
  912. if (i->second.subtype == ENV_REFLECTION)
  913. return true;
  914. }
  915. for(i = mEffects.find(ET_PROJECTIVE_TEXTURE); i != iend; ++i)
  916. {
  917. return true;
  918. }
  919. return false;
  920. }
  921. //-----------------------------------------------------------------------
  922. void TextureUnitState::setName(const String& name)
  923. {
  924. mName = name;
  925. if (mTextureNameAlias.empty())
  926. mTextureNameAlias = mName;
  927. }
  928. //-----------------------------------------------------------------------
  929. void TextureUnitState::setTextureNameAlias(const String& name)
  930. {
  931. mTextureNameAlias = name;
  932. }
  933. //-----------------------------------------------------------------------
  934. bool TextureUnitState::applyTextureAliases(const AliasTextureNamePairList& aliasList, const bool apply)
  935. {
  936. bool testResult = false;
  937. // if TUS has an alias see if its in the alias container
  938. if (!mTextureNameAlias.empty())
  939. {
  940. AliasTextureNamePairList::const_iterator aliasEntry =
  941. aliasList.find(mTextureNameAlias);
  942. if (aliasEntry != aliasList.end())
  943. {
  944. // match was found so change the texture name in mFrames
  945. testResult = true;
  946. if (apply)
  947. {
  948. // currently assumes animated frames are sequentially numbered
  949. // cubic, 1d, 2d, and 3d textures are determined from current TUS state
  950. // if cubic or 3D
  951. if (mCubic)
  952. {
  953. setCubicTextureName(aliasEntry->second, mTextureType == TEX_TYPE_CUBE_MAP);
  954. }
  955. else
  956. {
  957. // if more than one frame then assume animated frames
  958. if (mFrames.size() > 1)
  959. setAnimatedTextureName(aliasEntry->second,
  960. static_cast<unsigned int>(mFrames.size()), mAnimDuration);
  961. else
  962. setTextureName(aliasEntry->second, mTextureType);
  963. }
  964. }
  965. }
  966. }
  967. return testResult;
  968. }
  969. //-----------------------------------------------------------------------------
  970. void TextureUnitState::_notifyParent(Pass* parent)
  971. {
  972. mParent = parent;
  973. }
  974. //-----------------------------------------------------------------------------
  975. void TextureUnitState::setCompositorReference(const String& compositorName, const String& textureName, size_t mrtIndex)
  976. {
  977. mCompositorRefName = compositorName;
  978. mCompositorRefTexName = textureName;
  979. mCompositorRefMrtIndex = mrtIndex;
  980. }
  981. }