OgreTextureUnitState.cpp 35 KB

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