-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathOgreTextureUnitState.cpp
More file actions
1230 lines (1134 loc) · 41.7 KB
/
OgreTextureUnitState.cpp
File metadata and controls
1230 lines (1134 loc) · 41.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org
Copyright (c) 2000-2014 Torus Knot Software Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#include "OgreStableHeaders.h"
#include "OgreTextureUnitState.h"
#include "OgreControllerManager.h"
#include "OgreTextureManager.h"
#include "OgreHardwarePixelBuffer.h"
namespace Ogre {
// allow operation without hardware support
static SamplerPtr DUMMY_SAMPLER = std::make_shared<Sampler>();
Sampler::Sampler()
: mBorderColour(ColourValue::Black)
, mMaxAniso(1)
, mMipmapBias(0)
, mMinFilter(FO_LINEAR)
, mMagFilter(FO_LINEAR)
, mMipFilter(FO_POINT)
, mCompareFunc(CMPF_GREATER_EQUAL)
, mCompareEnabled(false)
, mDirty(true)
{
setAddressingMode(TAM_WRAP);
}
Sampler::~Sampler() {}
//-----------------------------------------------------------------------
void Sampler::setAddressingMode(const UVWAddressingMode& uvw)
{
mAddressMode = uvw;
mDirty = true;
}
//-----------------------------------------------------------------------
void Sampler::setFiltering(TextureFilterOptions filterType)
{
switch (filterType)
{
case TFO_NONE:
setFiltering(FO_POINT, FO_POINT, FO_NONE);
break;
case TFO_BILINEAR:
setFiltering(FO_LINEAR, FO_LINEAR, FO_POINT);
break;
case TFO_TRILINEAR:
setFiltering(FO_LINEAR, FO_LINEAR, FO_LINEAR);
break;
case TFO_ANISOTROPIC:
setFiltering(FO_ANISOTROPIC, FO_ANISOTROPIC, FO_LINEAR);
break;
}
}
//-----------------------------------------------------------------------
void Sampler::setFiltering(FilterType ft, FilterOptions fo)
{
switch (ft)
{
case FT_MIN:
mMinFilter = fo;
break;
case FT_MAG:
mMagFilter = fo;
break;
case FT_MIP:
mMipFilter = fo;
break;
}
mDirty = true;
}
//-----------------------------------------------------------------------
void Sampler::setFiltering(FilterOptions minFilter, FilterOptions magFilter, FilterOptions mipFilter)
{
mMinFilter = minFilter;
mMagFilter = magFilter;
mMipFilter = mipFilter;
mDirty = true;
}
//-----------------------------------------------------------------------
FilterOptions Sampler::getFiltering(FilterType ft) const
{
switch (ft)
{
case FT_MIN:
return mMinFilter;
case FT_MAG:
return mMagFilter;
case FT_MIP:
return mMipFilter;
}
// to keep compiler happy
return mMinFilter;
}
//-----------------------------------------------------------------------
TextureUnitState::TextureUnitState(Pass* parent)
: mCurrentFrame(0)
, mAnimDuration(0)
, mUnorderedAccessMipLevel(-1)
, mGamma(1)
, mUMod(0)
, mVMod(0)
, mUScale(1)
, mVScale(1)
, mRotate(0)
, mTexModMatrix(Matrix4::IDENTITY)
, mContentType(CONTENT_NAMED)
, mTextureLoadFailed(false)
, mRecalcTexMatrix(false)
, mTextureCoordSetIndex(0)
, mProjectiveTexturingFrustum(0)
, mTexCoordCalcMethod(TEXCALC_NONE)
, mFramePtrs(1)
, mSampler(TextureManager::getSingletonPtr() ? TextureManager::getSingleton().getDefaultSampler() : DUMMY_SAMPLER)
, mParent(parent)
, mAnimController(0)
{
mColourBlendMode.blendType = LBT_COLOUR;
mAlphaBlendMode.operation = LBX_MODULATE;
mAlphaBlendMode.blendType = LBT_ALPHA;
mAlphaBlendMode.source1 = LBS_TEXTURE;
mAlphaBlendMode.source2 = LBS_CURRENT;
setColourOperation(LBO_MODULATE);
if( Pass::getHashFunction() == Pass::getBuiltinHashFunction( Pass::MIN_TEXTURE_CHANGE ) )
{
mParent->_dirtyHash();
}
}
//-----------------------------------------------------------------------
TextureUnitState::TextureUnitState(Pass* parent, const TextureUnitState& oth )
{
mParent = parent;
mAnimController = 0;
*this = oth;
}
//-----------------------------------------------------------------------
TextureUnitState::TextureUnitState( Pass* parent, const String& texName, uint8 texCoordSet)
: TextureUnitState(parent)
{
setTextureName(texName);
setTextureCoordSet(texCoordSet);
}
//-----------------------------------------------------------------------
TextureUnitState::~TextureUnitState()
{
// Unload ensure all controllers destroyed
_unload();
}
//-----------------------------------------------------------------------
TextureUnitState & TextureUnitState::operator = (
const TextureUnitState &oth )
{
if (this == &oth)
return *this;
assert(mAnimController == 0);
removeAllEffects();
// copy basic members (int's, real's)
memcpy( (uchar*)this, &oth, (const uchar *)(&oth.mFramePtrs) - (const uchar *)(&oth) );
// copy complex members
mFramePtrs = oth.mFramePtrs;
mSampler = oth.mSampler;
mName = oth.mName;
mEffects = oth.mEffects;
mCompositorRefName = oth.mCompositorRefName;
mCompositorRefTexName = oth.mCompositorRefTexName;
// Can't sharing controllers with other TUS, reset to null to avoid potential bug.
for (auto & e : mEffects)
{
e.second.controller = 0;
}
// Load immediately if Material loaded
if (isLoaded())
{
_load();
}
// Tell parent to recalculate hash
if( Pass::getHashFunction() == Pass::getBuiltinHashFunction( Pass::MIN_TEXTURE_CHANGE ) )
{
mParent->_dirtyHash();
}
return *this;
}
//-----------------------------------------------------------------------
const String& TextureUnitState::getTextureName(void) const
{
// Return name of current frame
if (mCurrentFrame < mFramePtrs.size() && mFramePtrs[mCurrentFrame])
return mFramePtrs[mCurrentFrame]->getName();
else
return BLANKSTRING;
}
//-----------------------------------------------------------------------
void TextureUnitState::setTextureName( const String& name)
{
if(TexturePtr tex = retrieveTexture(name))
setTexture(tex);
}
void TextureUnitState::setTextureName( const String& name, TextureType texType)
{
TexturePtr tex = retrieveTexture(name);
if(!tex)
return;
tex->setTextureType(texType);
setTexture(tex);
}
//-----------------------------------------------------------------------
void TextureUnitState::setTexture( const TexturePtr& texPtr)
{
if (!texPtr)
{
OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
"Texture Pointer is empty.",
"TextureUnitState::setTexture");
}
setContentType(CONTENT_NAMED);
mTextureLoadFailed = false;
if (texPtr->getTextureType() == TEX_TYPE_EXTERNAL_OES)
{
setTextureAddressingMode( TAM_CLAMP );
setTextureFiltering(FT_MIP, FO_NONE);
}
mFramePtrs.resize(1);
mFramePtrs[0] = texPtr;
mCurrentFrame = 0;
// Load immediately ?
if (isLoaded())
{
_load(); // reload
}
// Tell parent to recalculate hash
if( Pass::getHashFunction() == Pass::getBuiltinHashFunction( Pass::MIN_TEXTURE_CHANGE ) )
{
mParent->_dirtyHash();
}
}
//-----------------------------------------------------------------------
void TextureUnitState::setContentType(TextureUnitState::ContentType ct)
{
mContentType = ct;
}
//-----------------------------------------------------------------------
TextureUnitState::ContentType TextureUnitState::getContentType(void) const
{
return mContentType;
}
//-----------------------------------------------------------------------
TextureType TextureUnitState::getTextureType(void) const
{
return !mFramePtrs[0] ? TEX_TYPE_2D : mFramePtrs[0]->getTextureType();
}
//-----------------------------------------------------------------------
void TextureUnitState::setFrameTextureName(const String& name, unsigned int frameNumber)
{
mTextureLoadFailed = false;
OgreAssert(frameNumber < mFramePtrs.size(), "out of range");
mFramePtrs[frameNumber] = retrieveTexture(name);
if (isLoaded())
{
_load(); // reload
}
// Tell parent to recalculate hash
if( Pass::getHashFunction() == Pass::getBuiltinHashFunction( Pass::MIN_TEXTURE_CHANGE ) )
{
mParent->_dirtyHash();
}
}
//-----------------------------------------------------------------------
void TextureUnitState::addFrameTextureName(const String& name)
{
setContentType(CONTENT_NAMED);
mTextureLoadFailed = false;
mFramePtrs.push_back(retrieveTexture(name));
// Load immediately if Material loaded
if (isLoaded())
{
_load();
}
// Tell parent to recalculate hash
if( Pass::getHashFunction() == Pass::getBuiltinHashFunction( Pass::MIN_TEXTURE_CHANGE ) )
{
mParent->_dirtyHash();
}
}
//-----------------------------------------------------------------------
void TextureUnitState::deleteFrameTextureName(const size_t frameNumber)
{
mTextureLoadFailed = false;
OgreAssert(frameNumber < mFramePtrs.size(), "out of range");
mFramePtrs.erase(mFramePtrs.begin() + frameNumber);
if (isLoaded())
{
_load();
}
// Tell parent to recalculate hash
if( Pass::getHashFunction() == Pass::getBuiltinHashFunction( Pass::MIN_TEXTURE_CHANGE ) )
{
mParent->_dirtyHash();
}
}
void TextureUnitState::setCubicTextureName(const String* const names, bool forUVW)
{
setLayerArrayNames(TEX_TYPE_CUBE_MAP, std::vector<String>(names, names + 6));
}
//-----------------------------------------------------------------------
void TextureUnitState::setAnimatedTextureName( const String& name, size_t numFrames, Real duration)
{
String baseName, ext;
StringUtil::splitBaseFilename(name, baseName, ext);
std::vector<String> names(numFrames);
for (uint32 i = 0; i < names.size(); ++i)
{
names[i] = StringUtil::format("%s_%u.%s", baseName.c_str(), i, ext.c_str());
}
setAnimatedTextureName(names, duration);
}
//-----------------------------------------------------------------------
void TextureUnitState::setAnimatedTextureName(const String* const names, size_t numFrames, Real duration)
{
setContentType(CONTENT_NAMED);
mTextureLoadFailed = false;
// resize pointers, but don't populate until needed
mFramePtrs.resize(numFrames);
mAnimDuration = duration;
mCurrentFrame = 0;
for (unsigned int i = 0; i < mFramePtrs.size(); ++i)
{
mFramePtrs[i] = retrieveTexture(names[i]);
}
// Load immediately if Material loaded
if (isLoaded())
{
_load();
}
// Tell parent to recalculate hash
if( Pass::getHashFunction() == Pass::getBuiltinHashFunction( Pass::MIN_TEXTURE_CHANGE ) )
{
mParent->_dirtyHash();
}
}
void TextureUnitState::setLayerArrayNames(TextureType type, const std::vector<String>& names)
{
OgreAssert(!names.empty(), "array layers empty");
const char* typeName;
switch(type)
{
case TEX_TYPE_CUBE_MAP:
typeName = "Cube";
break;
case TEX_TYPE_2D_ARRAY:
typeName = "Array";
break;
case TEX_TYPE_3D:
typeName = "Volume";
break;
default:
OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "arrays not possible for this texture type");
return;
}
// use hash to auto-name the texture
uint32 hash = 0;
for(const String& name : names)
hash = FastHash(name.data(), name.size(), hash);
auto tex = retrieveTexture(StringUtil::format("%sTex_%x", typeName, hash));
tex->setTextureType(type);
tex->setLayerNames(names);
setTexture(tex);
}
//-----------------------------------------------------------------------
std::pair<uint32, uint32> TextureUnitState::getTextureDimensions(unsigned int frame) const
{
TexturePtr tex = _getTexturePtr(frame);
if (!tex)
OGRE_EXCEPT( Exception::ERR_ITEM_NOT_FOUND, "Could not find texture " + StringConverter::toString(frame),
"TextureUnitState::getTextureDimensions" );
return {tex->getWidth(), tex->getHeight()};
}
//-----------------------------------------------------------------------
void TextureUnitState::setCurrentFrame(unsigned int frameNumber)
{
OgreAssert(frameNumber < mFramePtrs.size(), "out of range");
mCurrentFrame = frameNumber;
// this will affect the hash
if( Pass::getHashFunction() == Pass::getBuiltinHashFunction( Pass::MIN_TEXTURE_CHANGE ) )
{
mParent->_dirtyHash();
}
}
//-----------------------------------------------------------------------
unsigned int TextureUnitState::getCurrentFrame(void) const
{
return mCurrentFrame;
}
//-----------------------------------------------------------------------
unsigned int TextureUnitState::getNumFrames(void) const
{
return (unsigned int)mFramePtrs.size();
}
//-----------------------------------------------------------------------
const String& TextureUnitState::getFrameTextureName(unsigned int frameNumber) const
{
OgreAssert(frameNumber < mFramePtrs.size(), "out of range");
return mFramePtrs[0] ? mFramePtrs[frameNumber]->getName() : BLANKSTRING;
}
//-----------------------------------------------------------------------
void TextureUnitState::setDesiredFormat(PixelFormat desiredFormat)
{
OgreAssert(mFramePtrs[0], "frame must not be blank");
for(auto& frame : mFramePtrs)
frame->setFormat(desiredFormat);
}
//-----------------------------------------------------------------------
PixelFormat TextureUnitState::getDesiredFormat(void) const
{
return !mFramePtrs[0] ? PF_UNKNOWN : mFramePtrs[0]->getDesiredFormat();
}
//-----------------------------------------------------------------------
void TextureUnitState::setNumMipmaps(int numMipmaps)
{
OgreAssert(mFramePtrs[0], "frame must not be blank");
for (auto& frame : mFramePtrs)
frame->setNumMipmaps(numMipmaps == MIP_DEFAULT
? TextureManager::getSingleton().getDefaultNumMipmaps()
: numMipmaps);
}
//-----------------------------------------------------------------------
int TextureUnitState::getNumMipmaps(void) const
{
return !mFramePtrs[0] ? int(MIP_DEFAULT) : mFramePtrs[0]->getNumMipmaps();
}
//-----------------------------------------------------------------------
void TextureUnitState::setIsAlpha(bool isAlpha)
{
OgreAssert(mFramePtrs[0], "frame must not be blank");
OGRE_IGNORE_DEPRECATED_BEGIN
for(auto& frame : mFramePtrs)
frame->setTreatLuminanceAsAlpha(isAlpha);
OGRE_IGNORE_DEPRECATED_END
}
float TextureUnitState::getGamma() const
{
return !mFramePtrs[0] ? 1.0f : mFramePtrs[0]->getGamma();
}
void TextureUnitState::setGamma(float gamma)
{
OgreAssert(mFramePtrs[0], "frame must not be blank");
for(auto& frame : mFramePtrs)
frame->setGamma(gamma);
}
//-----------------------------------------------------------------------
void TextureUnitState::setHardwareGammaEnabled(bool g)
{
OgreAssert(mFramePtrs[0], "frame must not be blank");
for(auto& frame : mFramePtrs)
frame->setHardwareGammaEnabled(g);
}
//-----------------------------------------------------------------------
bool TextureUnitState::isHardwareGammaEnabled() const
{
return mFramePtrs[0] && mFramePtrs[0]->isHardwareGammaEnabled();
}
//-----------------------------------------------------------------------
uint8 TextureUnitState::getTextureCoordSet(void) const
{
return mTextureCoordSetIndex;
}
//-----------------------------------------------------------------------
void TextureUnitState::setTextureCoordSet(uint8 set)
{
mTextureCoordSetIndex = set;
}
//-----------------------------------------------------------------------
void TextureUnitState::setColourOperationEx(LayerBlendOperationEx op,
LayerBlendSource source1,
LayerBlendSource source2,
const ColourValue& arg1,
const ColourValue& arg2,
Real manualBlend)
{
mColourBlendMode.operation = op;
mColourBlendMode.source1 = source1;
mColourBlendMode.source2 = source2;
mColourBlendMode.colourArg1 = arg1;
mColourBlendMode.colourArg2 = arg2;
mColourBlendMode.factor = manualBlend;
}
//-----------------------------------------------------------------------
void TextureUnitState::setColourOperation(LayerBlendOperation op)
{
// Set up the multitexture and multipass blending operations
switch (op)
{
case LBO_REPLACE:
setColourOperationEx(LBX_SOURCE1, LBS_TEXTURE, LBS_CURRENT);
setColourOpMultipassFallback(SBF_ONE, SBF_ZERO);
break;
case LBO_ADD:
setColourOperationEx(LBX_ADD, LBS_TEXTURE, LBS_CURRENT);
setColourOpMultipassFallback(SBF_ONE, SBF_ONE);
break;
case LBO_MODULATE:
setColourOperationEx(LBX_MODULATE, LBS_TEXTURE, LBS_CURRENT);
setColourOpMultipassFallback(SBF_DEST_COLOUR, SBF_ZERO);
break;
case LBO_ALPHA_BLEND:
setColourOperationEx(LBX_BLEND_TEXTURE_ALPHA, LBS_TEXTURE, LBS_CURRENT);
setColourOpMultipassFallback(SBF_SOURCE_ALPHA, SBF_ONE_MINUS_SOURCE_ALPHA);
break;
}
}
//-----------------------------------------------------------------------
void TextureUnitState::setColourOpMultipassFallback(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor)
{
mColourBlendFallbackSrc = sourceFactor;
mColourBlendFallbackDest = destFactor;
}
//-----------------------------------------------------------------------
void TextureUnitState::setAlphaOperation(LayerBlendOperationEx op,
LayerBlendSource source1,
LayerBlendSource source2,
Real arg1,
Real arg2,
Real manualBlend)
{
mAlphaBlendMode.operation = op;
mAlphaBlendMode.source1 = source1;
mAlphaBlendMode.source2 = source2;
mAlphaBlendMode.alphaArg1 = arg1;
mAlphaBlendMode.alphaArg2 = arg2;
mAlphaBlendMode.factor = manualBlend;
}
//-----------------------------------------------------------------------
void TextureUnitState::addEffect(TextureEffect effect)
{
// Ensure controller pointer is null
effect.controller = 0;
if (effect.type != ET_TRANSFORM)
{
// Replace - must be unique
// Search for existing effect of this type
EffectMap::iterator i = mEffects.find(effect.type);
if (i != mEffects.end())
{
// Destroy old effect controller if exist
if (i->second.controller)
{
ControllerManager::getSingleton().destroyController(i->second.controller);
}
mEffects.erase(i);
}
}
if (isLoaded())
{
// Create controller
createEffectController(effect);
}
// Record new effect
mEffects.emplace(effect.type, effect);
}
//-----------------------------------------------------------------------
void TextureUnitState::removeAllEffects(void)
{
// Iterate over effects to remove controllers
EffectMap::iterator i, iend;
iend = mEffects.end();
for (i = mEffects.begin(); i != iend; ++i)
{
if (i->second.controller)
{
ControllerManager::getSingleton().destroyController(i->second.controller);
}
}
mEffects.clear();
}
//-----------------------------------------------------------------------
bool TextureUnitState::isBlank(void) const
{
return !mFramePtrs[0] || mTextureLoadFailed;
}
//-----------------------------------------------------------------------
SceneBlendFactor TextureUnitState::getColourBlendFallbackSrc(void) const
{
return mColourBlendFallbackSrc;
}
//-----------------------------------------------------------------------
SceneBlendFactor TextureUnitState::getColourBlendFallbackDest(void) const
{
return mColourBlendFallbackDest;
}
//-----------------------------------------------------------------------
const LayerBlendModeEx& TextureUnitState::getColourBlendMode(void) const
{
return mColourBlendMode;
}
//-----------------------------------------------------------------------
const LayerBlendModeEx& TextureUnitState::getAlphaBlendMode(void) const
{
return mAlphaBlendMode;
}
//-----------------------------------------------------------------------
void TextureUnitState::setEnvironmentMap(bool enable, int envMapType)
{
if (enable)
{
switch (envMapType)
{
case ENV_CURVED:
mTexCoordCalcMethod = TEXCALC_ENVIRONMENT_MAP;
break;
case ENV_PLANAR:
mTexCoordCalcMethod = TEXCALC_ENVIRONMENT_MAP_PLANAR;
break;
case ENV_REFLECTION:
mTexCoordCalcMethod = TEXCALC_ENVIRONMENT_MAP_REFLECTION;
break;
case ENV_NORMAL:
mTexCoordCalcMethod = TEXCALC_ENVIRONMENT_MAP_NORMAL;
break;
}
}
else
{
mTexCoordCalcMethod = TEXCALC_NONE;
}
}
//-----------------------------------------------------------------------
void TextureUnitState::removeEffect(TextureEffectType type)
{
// Get range of items matching this effect
std::pair< EffectMap::iterator, EffectMap::iterator > remPair =
mEffects.equal_range( type );
// Remove controllers
for (EffectMap::iterator i = remPair.first; i != remPair.second; ++i)
{
if (i->second.controller)
{
ControllerManager::getSingleton().destroyController(i->second.controller);
}
}
// Erase
mEffects.erase( remPair.first, remPair.second );
}
//-----------------------------------------------------------------------
void TextureUnitState::setBlank(void)
{
mFramePtrs.clear();
mFramePtrs.push_back(TexturePtr()); // insert nullptr to show warning tex
}
//-----------------------------------------------------------------------
void TextureUnitState::setTextureTransform(const Matrix4& xform)
{
mTexModMatrix = xform;
mRecalcTexMatrix = false;
}
//-----------------------------------------------------------------------
void TextureUnitState::setTextureScroll(float u, float v)
{
mUMod = u;
mVMod = v;
mRecalcTexMatrix = true;
}
//-----------------------------------------------------------------------
void TextureUnitState::setTextureScale(float uScale, float vScale)
{
mUScale = uScale;
mVScale = vScale;
mRecalcTexMatrix = true;
}
//-----------------------------------------------------------------------
void TextureUnitState::setTextureRotate(const Radian& angle)
{
mRotate = angle;
mRecalcTexMatrix = true;
}
//-----------------------------------------------------------------------
const Matrix4& TextureUnitState::getTextureTransform() const
{
if (mRecalcTexMatrix)
recalcTextureMatrix();
return mTexModMatrix;
}
//-----------------------------------------------------------------------
void TextureUnitState::recalcTextureMatrix() const
{
// Assumption: 2D texture coords
// that would make this Affine2(Matrix3), but we lack such a class
// Matrix3 is horribly unoptimized ATM
Affine3 xform = Affine3::IDENTITY;
if (mUScale != 1 || mVScale != 1)
{
// Offset to center of texture
xform[0][0] = 1/mUScale;
xform[1][1] = 1/mVScale;
// Skip matrix concat since first matrix update
xform[0][3] = (-0.5f * xform[0][0]) + 0.5f;
xform[1][3] = (-0.5f * xform[1][1]) + 0.5f;
}
if (mUMod || mVMod)
{
xform = Affine3::getTrans(mUMod, mVMod, 0) * xform;
}
if (mRotate != Radian(0))
{
Affine3 rot = Affine3::IDENTITY;
Radian theta ( mRotate );
Real cosTheta = Math::Cos(theta);
Real sinTheta = Math::Sin(theta);
rot[0][0] = cosTheta;
rot[0][1] = -sinTheta;
rot[1][0] = sinTheta;
rot[1][1] = cosTheta;
// Offset center of rotation to center of texture
rot[0][3] = 0.5f + ( (-0.5f * cosTheta) - (-0.5f * sinTheta) );
rot[1][3] = 0.5f + ( (-0.5f * sinTheta) + (-0.5f * cosTheta) );
xform = rot * xform;
}
mTexModMatrix = xform;
mRecalcTexMatrix = false;
}
//-----------------------------------------------------------------------
void TextureUnitState::setTextureUScroll(float value)
{
mUMod = value;
mRecalcTexMatrix = true;
}
//-----------------------------------------------------------------------
void TextureUnitState::setTextureVScroll(float value)
{
mVMod = value;
mRecalcTexMatrix = true;
}
//-----------------------------------------------------------------------
void TextureUnitState::setTextureUScale(float value)
{
mUScale = value;
mRecalcTexMatrix = true;
}
//-----------------------------------------------------------------------
void TextureUnitState::setTextureVScale(float value)
{
mVScale = value;
mRecalcTexMatrix = true;
}
//-----------------------------------------------------------------------
void TextureUnitState::setScrollAnimation(float uSpeed, float vSpeed)
{
// Remove existing effects
removeEffect(ET_UVSCROLL);
removeEffect(ET_USCROLL);
removeEffect(ET_VSCROLL);
// don't create an effect if the speeds are both 0
if (uSpeed == 0.0f && vSpeed == 0.0f)
{
return;
}
// Create new effect
if (uSpeed == vSpeed)
{
addEffect({ET_UVSCROLL, uSpeed});
}
else
{
if (uSpeed)
{
addEffect({ET_USCROLL, uSpeed});
}
if (vSpeed)
{
addEffect({ET_VSCROLL, vSpeed});
}
}
}
//-----------------------------------------------------------------------
void TextureUnitState::setRotateAnimation(float speed)
{
// Remove existing effect
removeEffect(ET_ROTATE);
// don't create an effect if the speed is 0
if(speed == 0.0f)
{
return;
}
// Create new effect
addEffect({ET_ROTATE, speed});
}
//-----------------------------------------------------------------------
void TextureUnitState::setTransformAnimation(TextureTransformType ttype,
WaveformType waveType, float base, float frequency, float phase, float amplitude)
{
// Remove existing effect
// note, only remove for subtype, not entire ET_TRANSFORM
// otherwise we won't be able to combine subtypes
// Get range of items matching this effect
for (EffectMap::iterator i = mEffects.begin(); i != mEffects.end(); ++i)
{
if (i->second.type == ET_TRANSFORM && i->second.subtype == ttype)
{
if (i->second.controller)
{
ControllerManager::getSingleton().destroyController(i->second.controller);
}
mEffects.erase(i);
// should only be one, so jump out
break;
}
}
// don't create an effect if the given values are all 0
if(base == 0.0f && phase == 0.0f && frequency == 0.0f && amplitude == 0.0f)
{
return;
}
// Create new effect
TextureEffect eff = {ET_TRANSFORM};
eff.subtype = ttype;
eff.waveType = waveType;
eff.base = base;
eff.frequency = frequency;
eff.phase = phase;
eff.amplitude = amplitude;
addEffect(eff);
}
//-----------------------------------------------------------------------
void TextureUnitState::_prepare(void)
{
// Unload first
//_unload();
// Load textures
for (unsigned int i = 0; i < mFramePtrs.size(); ++i)
{
ensurePrepared(i);
}
}
//-----------------------------------------------------------------------
void TextureUnitState::_load(void)
{
// Load textures
for (unsigned int i = 0; i < mFramePtrs.size(); ++i)
{
ensureLoaded(i);
}
// Animation controller
if (mAnimDuration != 0)
{
createAnimController();
}
// Effect controllers
for (auto & e : mEffects)
{
createEffectController(e.second);
}
}
//-----------------------------------------------------------------------
const TexturePtr& TextureUnitState::_getTexturePtr(void) const
{
return _getTexturePtr(mCurrentFrame);
}
//-----------------------------------------------------------------------
const TexturePtr& TextureUnitState::_getTexturePtr(size_t frame) const
{
if (frame < mFramePtrs.size())
{
if (mContentType == CONTENT_NAMED)
{
ensureLoaded(frame);
}
return mFramePtrs[frame];
}
// Silent fail with empty texture for internal method
static TexturePtr nullTexPtr;
return nullTexPtr;
}
//-----------------------------------------------------------------------
void TextureUnitState::_setTexturePtr(const TexturePtr& texptr)
{
_setTexturePtr(texptr, mCurrentFrame);
}
//-----------------------------------------------------------------------
void TextureUnitState::_setTexturePtr(const TexturePtr& texptr, size_t frame)
{
assert(frame < mFramePtrs.size());
mFramePtrs[frame] = texptr;
}
//-----------------------------------------------------------------------
TexturePtr TextureUnitState::retrieveTexture(const String& name) {
TextureManager::ResourceCreateOrRetrieveResult res;
res = TextureManager::getSingleton().createOrRetrieve(name, mParent->getResourceGroup());
return static_pointer_cast<Texture>(res.first);
}
//-----------------------------------------------------------------------
bool TextureUnitState::checkTexCalcSettings(const TexturePtr& tex) const
{
if(mContentType != TextureUnitState::CONTENT_NAMED)
return true; // can only check normal textures
String err;
auto texCalc = _deriveTexCoordCalcMethod();
if ((texCalc == TEXCALC_ENVIRONMENT_MAP_PLANAR || texCalc == TEXCALC_ENVIRONMENT_MAP) &&
getTextureType() != TEX_TYPE_2D)
{
err = "env_map setting requires a 2d texture";
}
else if ((texCalc == TEXCALC_ENVIRONMENT_MAP_NORMAL || texCalc == TEXCALC_ENVIRONMENT_MAP_REFLECTION) &&
getTextureType() != TEX_TYPE_CUBE_MAP)
{
err = "env_map setting requires a cubic texture";
}
if(err.empty())
return true;
String msg = err+", but '"+tex->getName()+"' is not. Texture layer will be blank";