Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreTextureUnitState.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://ogre.sourceforge.net/
00006 
00007 Copyright © 2000-2003 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #include "OgreStableHeaders.h"
00026 
00027 #include "OgreTextureUnitState.h"
00028 #include "OgrePass.h"
00029 #include "OgreMaterialManager.h"
00030 #include "OgreControllerManager.h"
00031 #include "OgreLogManager.h"
00032 #include "OgreException.h"
00033 #include "OgreTextureManager.h"
00034 
00035 namespace Ogre {
00036 
00037     //-----------------------------------------------------------------------
00038     TextureUnitState::TextureUnitState(Pass* parent)
00039         : mParent(parent)
00040     {
00041         mIsBlank = true;
00042         colourBlendMode.blendType = LBT_COLOUR;
00043         setColourOperation(LBO_MODULATE);
00044         setTextureAddressingMode(TAM_WRAP);
00045 
00046         alphaBlendMode.operation = LBX_MODULATE;
00047         alphaBlendMode.blendType = LBT_ALPHA;
00048         alphaBlendMode.source1 = LBS_TEXTURE;
00049         alphaBlendMode.source2 = LBS_CURRENT;
00050         
00051         //default filtering
00052         mMinFilter = FO_LINEAR;
00053         mMagFilter = FO_LINEAR;
00054         mMipFilter = FO_POINT;
00055         mMaxAniso = MaterialManager::getSingleton().getDefaultAnisotropy();
00056         mIsDefaultAniso = true;
00057         mIsDefaultFiltering = true;
00058 
00059         mUMod = mVMod = 0;
00060         mUScale = mVScale = 1;
00061         mRotate = 0;
00062         mTexModMatrix = Matrix4::IDENTITY;
00063         mRecalcTexMatrix = false;
00064         mAlphaRejectFunc = CMPF_ALWAYS_PASS;
00065         mAlphaRejectVal = 0;
00066 
00067         mNumFrames = 0;
00068         mAnimDuration = 0;
00069         mAnimController = 0;
00070         mCubic = false;
00071         mTextureType = TEX_TYPE_2D;
00072         mTextureCoordSetIndex = 0;
00073 
00074         mFrames[0] = String::BLANK;
00075         mCurrentFrame = 0;
00076 
00077         mParent->_dirtyHash();
00078 
00079     }
00080 
00081     //-----------------------------------------------------------------------
00082     TextureUnitState::TextureUnitState(Pass* parent, const TextureUnitState& oth )
00083     {
00084         mParent = parent;
00085         *this = oth;
00086     }
00087 
00088     //-----------------------------------------------------------------------
00089     TextureUnitState::TextureUnitState( Pass* parent, const String& texName, unsigned int texCoordSet)
00090         :mParent(parent)
00091     {
00092         mIsBlank = true;
00093         colourBlendMode.blendType = LBT_COLOUR;
00094         setColourOperation(LBO_MODULATE);
00095         setTextureAddressingMode(TAM_WRAP);
00096 
00097         alphaBlendMode.operation = LBX_MODULATE;
00098         alphaBlendMode.blendType = LBT_ALPHA;
00099         alphaBlendMode.source1 = LBS_TEXTURE;
00100         alphaBlendMode.source2 = LBS_CURRENT;
00101 
00102         //default filtering && anisotropy
00103         mMinFilter = FO_LINEAR;
00104         mMagFilter = FO_LINEAR;
00105         mMipFilter = FO_POINT;
00106         mMaxAniso = MaterialManager::getSingleton().getDefaultAnisotropy();
00107         mIsDefaultAniso = true;
00108         mIsDefaultFiltering = true;
00109 
00110         mUMod = mVMod = 0;
00111         mUScale = mVScale = 1;
00112         mRotate = 0;
00113         mAnimDuration = 0;
00114         mAnimController = 0;
00115         mTexModMatrix = Matrix4::IDENTITY;
00116         mRecalcTexMatrix = false;
00117         mAlphaRejectFunc = CMPF_ALWAYS_PASS;
00118         mAlphaRejectVal = 0;
00119 
00120         mCubic = false;
00121         mTextureType = TEX_TYPE_2D;
00122         mTextureCoordSetIndex = 0;
00123 
00124         setTextureName(texName);
00125         setTextureCoordSet(texCoordSet);
00126     }
00127     //-----------------------------------------------------------------------
00128     TextureUnitState::~TextureUnitState()
00129     {
00130         // Destroy controllers
00131         if (mAnimController)
00132         {
00133             ControllerManager::getSingleton().destroyController(mAnimController);
00134         }
00135         // Destroy effect controllers
00136         for (EffectMap::iterator i = mEffects.begin(); i != mEffects.end(); ++i)
00137         {
00138             if (i->second.controller)
00139             {
00140                 ControllerManager::getSingleton().destroyController(i->second.controller);
00141             }
00142 
00143         }
00144         // Don't unload textures. may be used elsewhere
00145 
00146     }
00147     //-----------------------------------------------------------------------
00148     TextureUnitState & TextureUnitState::operator = ( 
00149         const TextureUnitState &oth )
00150     {
00151         // copy basic members (int's, real's)
00152         memcpy( this, &oth, (uchar *)(&oth.mFrames[0]) - (uchar *)(&oth) );
00153 
00154         // copy complex members
00155         for( ushort i = 0; i<mNumFrames; i++ )
00156             mFrames[i] = oth.mFrames[i];
00157 
00158         mEffects = oth.mEffects;
00159 
00160         mParent->_dirtyHash();
00161 
00162         return *this;
00163     }
00164     //-----------------------------------------------------------------------
00165     const String& TextureUnitState::getTextureName(void) const
00166     {
00167         // Return name of current frame
00168         return mFrames[mCurrentFrame];
00169     }
00170     //-----------------------------------------------------------------------
00171     void TextureUnitState::setTextureName( const String& name, TextureType texType)
00172     {
00173         if (texType == TEX_TYPE_CUBE_MAP)
00174         {
00175             // delegate to cubic texture implementation
00176             setCubicTextureName(name, true);
00177         }
00178         else
00179         {
00180             mFrames[0] = name;
00181             mNumFrames = 1;
00182             mCurrentFrame = 0;
00183             mCubic = false;
00184             mTextureType = texType;
00185 
00186             if (name == "")
00187             {
00188                 mIsBlank = true;
00189                 return;
00190             }
00191             
00192             // Load immediately ?
00193             if (isLoaded())
00194             {
00195                 _load(); // reload
00196                 // Tell parent to recalculate hash
00197                 mParent->_dirtyHash();
00198             }
00199         }
00200 
00201     }
00202     //-----------------------------------------------------------------------
00203     void TextureUnitState::setCubicTextureName( const String& name, bool forUVW)
00204     {
00205         if (forUVW)
00206         {
00207             setCubicTextureName(&name, forUVW);
00208         }
00209         else
00210         {
00211             String ext;
00212             String suffixes[6] = {"_fr", "_bk", "_lf", "_rt", "_up", "_dn"};
00213             String baseName;
00214             String fullNames[6];
00215 
00216 
00217             size_t pos = name.find_last_of(".");
00218             baseName = name.substr(0, pos);
00219             ext = name.substr(pos);
00220 
00221             for (int i = 0; i < 6; ++i)
00222             {
00223                 fullNames[i] = baseName + suffixes[i] + ext;
00224             }
00225 
00226             setCubicTextureName(fullNames, forUVW);
00227         }
00228     }
00229     //-----------------------------------------------------------------------
00230     void TextureUnitState::setCubicTextureName(const String* const names, bool forUVW)
00231     {
00232         mNumFrames = forUVW ? 1 : 6;
00233         mCurrentFrame = 0;
00234         mCubic = true;
00235         mTextureType = forUVW ? TEX_TYPE_CUBE_MAP : TEX_TYPE_2D;
00236 
00237         for (unsigned int i = 0; i < mNumFrames; ++i)
00238         {
00239             mFrames[i] = names[i];
00240         }
00241         // Tell parent we need recompiling, will cause reload too
00242         mParent->_notifyNeedsRecompile();
00243     }
00244     //-----------------------------------------------------------------------
00245     bool TextureUnitState::isCubic(void) const
00246     {
00247         return mCubic;
00248     }
00249     //-----------------------------------------------------------------------
00250     bool TextureUnitState::is3D(void) const
00251     {
00252         return mTextureType == TEX_TYPE_CUBE_MAP;
00253     }
00254     //-----------------------------------------------------------------------
00255     TextureType TextureUnitState::getTextureType(void) const
00256     {
00257         return mTextureType;
00258 
00259     }
00260     //-----------------------------------------------------------------------
00261     void TextureUnitState::setAnimatedTextureName( const String& name, unsigned int numFrames, Real duration)
00262     {
00263         String ext;
00264         String baseName;
00265 
00266         size_t pos = name.find_last_of(".");
00267         baseName = name.substr(0, pos);
00268         ext = name.substr(pos);
00269 
00270         if (numFrames > MAX_FRAMES)
00271         {
00272             char cmsg[128];
00273             sprintf(cmsg, "Maximum number of frames is %d.", MAX_FRAMES);
00274             Except(Exception::ERR_INVALIDPARAMS, cmsg, "TextureUnitState::setAnimatedTextureName");
00275         }
00276         mNumFrames = numFrames;
00277         mAnimDuration = duration;
00278         mCurrentFrame = 0;
00279         mCubic = false;
00280 
00281         for (unsigned int i = 0; i < mNumFrames; ++i)
00282         {
00283             char suffix[5];
00284             sprintf(suffix, "_%d", i);
00285 
00286             mFrames[i] = baseName + suffix + ext;
00287         }
00288 
00289         // Load immediately if Material loaded
00290         if (isLoaded())
00291         {
00292             _load();
00293             // Tell parent to recalculate hash
00294             mParent->_dirtyHash();
00295         }
00296 
00297     }
00298     //-----------------------------------------------------------------------
00299     void TextureUnitState::setAnimatedTextureName(const String* const names, unsigned int numFrames, Real duration)
00300     {
00301         if (numFrames > MAX_FRAMES)
00302         {
00303             char cmsg[128];
00304             sprintf(cmsg, "Maximum number of frames is %d.", MAX_FRAMES);
00305             Except(Exception::ERR_INVALIDPARAMS, cmsg, "TextureUnitState::setAnimatedTextureName");
00306         }
00307         mNumFrames = numFrames;
00308         mAnimDuration = duration;
00309         mCurrentFrame = 0;
00310         mCubic = false;
00311 
00312         for (unsigned int i = 0; i < mNumFrames; ++i)
00313         {
00314             mFrames[i] = names[i];
00315         }
00316 
00317         // Load immediately if Material loaded
00318         if (isLoaded())
00319         {
00320             _load();
00321             // Tell parent to recalculate hash
00322             mParent->_dirtyHash();
00323         }
00324     }
00325     //-----------------------------------------------------------------------
00326     std::pair< uint, uint > TextureUnitState::getTextureDimensions( unsigned int frame ) const
00327     {
00328         Texture *tex = (Texture *)TextureManager::getSingleton().getByName( mFrames[ frame ] );
00329         if (!tex)
00330             Except( Exception::ERR_ITEM_NOT_FOUND, "Could not find texture " + mFrames[ frame ],
00331                 "TextureUnitState::getTextureDimensions" );
00332         return std::pair< uint, uint >( tex->getWidth(), tex->getHeight() );
00333     }
00334     //-----------------------------------------------------------------------
00335     void TextureUnitState::setCurrentFrame(unsigned int frameNumber)
00336     {
00337         assert(frameNumber < mNumFrames);
00338         mCurrentFrame = frameNumber;
00339         // this will affect the hash
00340         mParent->_dirtyHash();
00341 
00342     }
00343     //-----------------------------------------------------------------------
00344     unsigned int TextureUnitState::getCurrentFrame(void) const
00345     {
00346         return mCurrentFrame;
00347     }
00348     //-----------------------------------------------------------------------
00349     unsigned int TextureUnitState::getNumFrames(void) const
00350     {
00351         return mNumFrames;
00352     }
00353     //-----------------------------------------------------------------------
00354     const String& TextureUnitState::getFrameTextureName(unsigned int frameNumber) const
00355     {
00356         assert(frameNumber < mNumFrames);
00357         return mFrames[frameNumber];
00358     }
00359     //-----------------------------------------------------------------------
00360     unsigned int TextureUnitState::getTextureCoordSet(void) const
00361     {
00362         return mTextureCoordSetIndex;
00363     }
00364     //-----------------------------------------------------------------------
00365     void TextureUnitState::setTextureCoordSet(unsigned int set)
00366     {
00367         mTextureCoordSetIndex = set;
00368     }
00369     //-----------------------------------------------------------------------
00370     void TextureUnitState::setColourOperationEx(LayerBlendOperationEx op,
00371         LayerBlendSource source1,
00372         LayerBlendSource source2,
00373         const ColourValue& arg1,
00374         const ColourValue& arg2,
00375         Real manualBlend)
00376     {
00377         colourBlendMode.operation = op;
00378         colourBlendMode.source1 = source1;
00379         colourBlendMode.source2 = source2;
00380         colourBlendMode.colourArg1 = arg1;
00381         colourBlendMode.colourArg2 = arg2;
00382         colourBlendMode.factor = manualBlend;
00383     }
00384     //-----------------------------------------------------------------------
00385     void TextureUnitState::setColourOperation(LayerBlendOperation op)
00386     {
00387         // Set up the multitexture and multipass blending operations
00388         switch (op)
00389         {
00390         case LBO_REPLACE:
00391             setColourOperationEx(LBX_SOURCE1, LBS_TEXTURE, LBS_CURRENT);
00392             setColourOpMultipassFallback(SBF_ONE, SBF_ZERO);
00393             break;
00394         case LBO_ADD:
00395             setColourOperationEx(LBX_ADD, LBS_TEXTURE, LBS_CURRENT);
00396             setColourOpMultipassFallback(SBF_ONE, SBF_ONE);
00397             break;
00398         case LBO_MODULATE:
00399             setColourOperationEx(LBX_MODULATE, LBS_TEXTURE, LBS_CURRENT);
00400             setColourOpMultipassFallback(SBF_DEST_COLOUR, SBF_ZERO);
00401             break;
00402         case LBO_ALPHA_BLEND:
00403             setColourOperationEx(LBX_BLEND_TEXTURE_ALPHA, LBS_TEXTURE, LBS_CURRENT);
00404             setColourOpMultipassFallback(SBF_SOURCE_ALPHA, SBF_ONE_MINUS_SOURCE_ALPHA);
00405             break;
00406         }
00407 
00408 
00409     }
00410     //-----------------------------------------------------------------------
00411     void TextureUnitState::setColourOpMultipassFallback(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor)
00412     {
00413         colourBlendFallbackSrc = sourceFactor;
00414         colourBlendFallbackDest = destFactor;
00415     }
00416     //-----------------------------------------------------------------------
00417     void TextureUnitState::setAlphaOperation(LayerBlendOperationEx op,
00418         LayerBlendSource source1,
00419         LayerBlendSource source2,
00420         Real arg1,
00421         Real arg2,
00422         Real manualBlend)
00423     {
00424         alphaBlendMode.operation = op;
00425         alphaBlendMode.source1 = source1;
00426         alphaBlendMode.source2 = source2;
00427         alphaBlendMode.alphaArg1 = arg1;
00428         alphaBlendMode.alphaArg2 = arg2;
00429         alphaBlendMode.factor = manualBlend;
00430     }
00431     //-----------------------------------------------------------------------
00432     void TextureUnitState::addEffect(TextureEffect& effect)
00433     {
00434         // Ensure controller pointer is null
00435         effect.controller = 0;
00436 
00437         if (effect.type == ET_ENVIRONMENT_MAP || effect.type == ET_SCROLL || effect.type == ET_ROTATE
00438             || effect.type == ET_PROJECTIVE_TEXTURE)
00439         {
00440             // Replace - must be unique
00441             // Search for existing effect of this type
00442             EffectMap::iterator i = mEffects.find(effect.type);
00443             if (i != mEffects.end())
00444             {
00445                 mEffects.erase(i);
00446             }
00447         }
00448 
00449         if (isLoaded())
00450         {
00451             // Create controller
00452             createEffectController(effect);
00453         }
00454 
00455         // Record new effect
00456         mEffects.insert(EffectMap::value_type(effect.type, effect));
00457 
00458     }
00459     //-----------------------------------------------------------------------
00460     void TextureUnitState::removeAllEffects(void)
00461     {
00462         mEffects.clear();
00463     }
00464 
00465     //-----------------------------------------------------------------------
00466     bool TextureUnitState::isBlank(void) const
00467     {
00468         return mIsBlank;
00469     }
00470 
00471     //-----------------------------------------------------------------------
00472     SceneBlendFactor TextureUnitState::getColourBlendFallbackSrc(void) const
00473     {
00474         return colourBlendFallbackSrc;
00475     }
00476     //-----------------------------------------------------------------------
00477     SceneBlendFactor TextureUnitState::getColourBlendFallbackDest(void) const
00478     {
00479         return colourBlendFallbackDest;
00480     }
00481     //-----------------------------------------------------------------------
00482     const LayerBlendModeEx& TextureUnitState::getColourBlendMode(void) const
00483     {
00484         return colourBlendMode;
00485     }
00486     //-----------------------------------------------------------------------
00487     const LayerBlendModeEx& TextureUnitState::getAlphaBlendMode(void) const
00488     {
00489         return alphaBlendMode;
00490     }
00491     //-----------------------------------------------------------------------
00492     TextureUnitState::TextureAddressingMode TextureUnitState::getTextureAddressingMode(void) const
00493     {
00494         return mAddressMode;
00495     }
00496     //-----------------------------------------------------------------------
00497     void TextureUnitState::setTextureAddressingMode(TextureUnitState::TextureAddressingMode tam)
00498     {
00499         mAddressMode = tam;
00500     }
00501     //-----------------------------------------------------------------------
00502     void TextureUnitState::setEnvironmentMap(bool enable, EnvMapType envMapType)
00503     {
00504         if (enable)
00505         {
00506             TextureEffect eff;
00507             eff.type = ET_ENVIRONMENT_MAP;
00508 
00509             eff.subtype = envMapType;
00510             addEffect(eff);
00511         }
00512         else
00513         {
00514             removeEffect(ET_ENVIRONMENT_MAP);
00515         }
00516     }
00517     //-----------------------------------------------------------------------
00518     void TextureUnitState::removeEffect(TextureEffectType type)
00519     {
00520       // EffectMap::iterator i = mEffects.find(type);
00521         std::pair< EffectMap::iterator, EffectMap::iterator > remPair = mEffects.equal_range( type );
00522         mEffects.erase( remPair.first, remPair.second );
00523         //EffectMap::iterator i = mEffects.find(type);
00524 /*        for (; i != mEffects.end() && i->first == type; ++i)
00525         {
00526             // Remove all instances of this type
00527 #ifdef __GNUC__
00528             assert(0);
00529             // Why do I get an error here???
00530             //i = mEffects.erase(i);
00531 #else
00532             i++;
00533             EffectMap::iterator j = i;
00534             i--;
00535             mEffects.erase( i );
00536             i = j;
00537 #endif
00538         }
00539 */
00540     }
00541     //-----------------------------------------------------------------------
00542     void TextureUnitState::setBlank(void)
00543     {
00544         mIsBlank = true;
00545     }
00546     //-----------------------------------------------------------------------
00547     void TextureUnitState::setTextureTransform(const Matrix4& xform)
00548     {
00549         mTexModMatrix = xform;
00550         mRecalcTexMatrix = false;
00551     }
00552     //-----------------------------------------------------------------------
00553     void TextureUnitState::setTextureScroll(Real u, Real v)
00554     {
00555         mUMod = u;
00556         mVMod = v;
00557         mRecalcTexMatrix = true;
00558     }
00559     //-----------------------------------------------------------------------
00560     void TextureUnitState::setTextureScale(Real uScale, Real vScale)
00561     {
00562         mUScale = uScale;
00563         mVScale = vScale;
00564         mRecalcTexMatrix = true;
00565     }
00566     //-----------------------------------------------------------------------
00567     void TextureUnitState::setTextureRotate(Real degrees)
00568     {
00569         mRotate = degrees;
00570         mRecalcTexMatrix = true;
00571     }
00572     //-----------------------------------------------------------------------
00573     const Matrix4& TextureUnitState::getTextureTransform()
00574     {
00575         if (mRecalcTexMatrix)
00576             recalcTextureMatrix();
00577         return mTexModMatrix;
00578 
00579     }
00580     //-----------------------------------------------------------------------
00581     void TextureUnitState::recalcTextureMatrix()
00582     {
00583         // Assumption: 2D texture coords
00584         Matrix3 xform, rot;
00585 
00586         xform = Matrix3::IDENTITY;
00587         if (mUScale || mVScale)
00588         {
00589             // Offset to center of texture
00590             xform[0][0] = 1/mUScale;
00591             xform[1][1] = 1/mVScale;
00592             // Skip matrix concat since first matrix update
00593             xform[0][2] = (-0.5 * xform[0][0]) + 0.5;
00594             xform[1][2] = (-0.5 * xform[1][1]) + 0.5;
00595 
00596         }
00597 
00598         if (mUMod || mVMod)
00599         {
00600             Matrix3 xlate = Matrix3::IDENTITY;
00601 
00602             xlate[0][2] = mUMod;
00603             xlate[1][2] = mVMod;
00604 
00605             xform = xlate * xform;
00606         }
00607 
00608         if (mRotate != 0)
00609         {
00610             rot = Matrix3::IDENTITY;
00611             Real theta = Math::AngleUnitsToRadians(mRotate);
00612             Real cosTheta = Math::Cos(theta);
00613             Real sinTheta = Math::Sin(theta);
00614 
00615             rot[0][0] = cosTheta;
00616             rot[0][1] = -sinTheta;
00617             rot[1][0] = sinTheta;
00618             rot[1][1] = cosTheta;
00619             // Offset center of rotation to center of texture
00620             rot[0][2] = 0.5 + ( (-0.5 * cosTheta) - (-0.5 * sinTheta) );
00621             rot[1][2] = 0.5 + ( (-0.5 * sinTheta) + (-0.5 * cosTheta) );
00622 
00623 
00624             xform = rot * xform;
00625         }
00626 
00627         mTexModMatrix = xform;
00628 
00629     }
00630     //-----------------------------------------------------------------------
00631     void TextureUnitState::setTextureUScroll(Real value)
00632     {
00633         mUMod = value;
00634         mRecalcTexMatrix = true;
00635     }
00636     //-----------------------------------------------------------------------
00637     void TextureUnitState::setTextureVScroll(Real value)
00638     {
00639         mVMod = value;
00640         mRecalcTexMatrix = true;
00641     }
00642     //-----------------------------------------------------------------------
00643     void TextureUnitState::setTextureUScale(Real value)
00644     {
00645         mUScale = value;
00646         mRecalcTexMatrix = true;
00647     }
00648     //-----------------------------------------------------------------------
00649     void TextureUnitState::setTextureVScale(Real value)
00650     {
00651         mVScale = value;
00652         mRecalcTexMatrix = true;
00653     }
00654     //-----------------------------------------------------------------------
00655     void TextureUnitState::setAlphaRejectSettings(CompareFunction func, unsigned char value)
00656     {
00657         mAlphaRejectFunc = func;
00658         mAlphaRejectVal = value;
00659     }
00660     //-----------------------------------------------------------------------
00661     CompareFunction TextureUnitState::getAlphaRejectFunction(void) const
00662     {
00663         return mAlphaRejectFunc;
00664     }
00665     //-----------------------------------------------------------------------
00666     unsigned char TextureUnitState::getAlphaRejectValue(void) const
00667     {
00668         return mAlphaRejectVal;
00669     }
00670     //-----------------------------------------------------------------------
00671     void TextureUnitState::setScrollAnimation(Real uSpeed, Real vSpeed)
00672     {
00673         TextureEffect eff;
00674         eff.type = ET_SCROLL;
00675         eff.arg1 = uSpeed;
00676         eff.arg2 = vSpeed;
00677         addEffect(eff);
00678     }
00679     //-----------------------------------------------------------------------
00680     void TextureUnitState::setRotateAnimation(Real speed)
00681     {
00682         TextureEffect eff;
00683         eff.type = ET_ROTATE;
00684         eff.arg1 = speed;
00685         addEffect(eff);
00686     }
00687     //-----------------------------------------------------------------------
00688     void TextureUnitState::setTransformAnimation(TextureTransformType ttype,
00689         WaveformType waveType, Real base, Real frequency, Real phase, Real amplitude)
00690     {
00691         TextureEffect eff;
00692         eff.type = ET_TRANSFORM;
00693         eff.subtype = ttype;
00694         eff.waveType = waveType;
00695         eff.base = base;
00696         eff.frequency = frequency;
00697         eff.phase = phase;
00698         eff.amplitude = amplitude;
00699         addEffect(eff);
00700     }
00701     //-----------------------------------------------------------------------
00702     void TextureUnitState::_load(void)
00703     {
00704         // Load textures
00705         for (unsigned int i = 0; i < mNumFrames; ++i)
00706         {
00707             if (mFrames[i] != "")
00708             {
00709                 // Ensure texture is loaded, default MipMaps and priority
00710                 try {
00711 
00712                     TextureManager::getSingleton().load(mFrames[i], mTextureType);
00713                     mIsBlank = false;
00714                 }
00715                 catch (...) {
00716                     String msg;
00717                     msg = msg + "Error loading texture " + mFrames[i]  + ". Texture layer will be blank.";
00718                     LogManager::getSingleton().logMessage(msg);
00719                     mIsBlank = true;
00720                 }
00721             }
00722         }
00723         // Animation controller
00724         if (mAnimDuration != 0)
00725         {
00726             createAnimController();
00727         }
00728         // Effect controllers
00729         for (EffectMap::iterator it = mEffects.begin(); it != mEffects.end(); ++it)
00730         {
00731             createEffectController(it->second);
00732         }
00733 
00734     }
00735     //-----------------------------------------------------------------------
00736     void TextureUnitState::createAnimController(void)
00737     {
00738         mAnimController = ControllerManager::getSingleton().createTextureAnimator(this, mAnimDuration);
00739 
00740     }
00741     //-----------------------------------------------------------------------
00742     void TextureUnitState::createEffectController(TextureEffect& effect)
00743     {
00744         ControllerManager& cMgr = ControllerManager::getSingleton();
00745         switch (effect.type)
00746         {
00747         case ET_SCROLL:
00748             effect.controller = cMgr.createTextureScroller(this, effect.arg1, effect.arg2);
00749             break;
00750         case ET_ROTATE:
00751             effect.controller = cMgr.createTextureRotater(this, effect.arg1);
00752             break;
00753         case ET_TRANSFORM:
00754             effect.controller = cMgr.createTextureWaveTransformer(this, (TextureUnitState::TextureTransformType)effect.subtype, effect.waveType, effect.base,
00755                 effect.frequency, effect.phase, effect.amplitude);
00756             break;
00757         case ET_ENVIRONMENT_MAP:
00758             break;
00759         default:
00760             break;
00761         }
00762     }
00763     //-----------------------------------------------------------------------
00764     Real TextureUnitState::getTextureUScroll(void) const
00765     {
00766         return mUMod;
00767     }
00768 
00769     //-----------------------------------------------------------------------
00770     Real TextureUnitState::getTextureVScroll(void) const
00771     {
00772         return mVMod;
00773     }
00774 
00775     //-----------------------------------------------------------------------
00776     Real TextureUnitState::getTextureUScale(void) const
00777     {
00778         return mUScale;
00779     }
00780 
00781     //-----------------------------------------------------------------------
00782     Real TextureUnitState::getTextureVScale(void) const
00783     {
00784         return mVScale;
00785     }
00786 
00787     //-----------------------------------------------------------------------
00788     Real TextureUnitState::getTextureRotate(void) const
00789     {
00790         return mRotate;
00791     }
00792     
00793     //-----------------------------------------------------------------------
00794     Real TextureUnitState::getAnimationDuration(void) const
00795     {
00796         return mAnimDuration;
00797     }
00798 
00799     //-----------------------------------------------------------------------
00800     std::multimap<TextureUnitState::TextureEffectType, TextureUnitState::TextureEffect> TextureUnitState::getEffects(void) const
00801     {
00802         return mEffects;
00803     }
00804 
00805     //-----------------------------------------------------------------------
00806     void TextureUnitState::setTextureFiltering(TextureFilterOptions filterType)
00807     {
00808         switch (filterType)
00809         {
00810         case TFO_NONE:
00811             setTextureFiltering(FO_POINT, FO_POINT, FO_NONE);
00812             break;
00813         case TFO_BILINEAR:
00814             setTextureFiltering(FO_LINEAR, FO_LINEAR, FO_POINT);
00815             break;
00816         case TFO_TRILINEAR:
00817             setTextureFiltering(FO_LINEAR, FO_LINEAR, FO_LINEAR);
00818             break;
00819         case TFO_ANISOTROPIC:
00820             setTextureFiltering(FO_ANISOTROPIC, FO_ANISOTROPIC, FO_LINEAR);
00821             break;
00822         }
00823         mIsDefaultFiltering = false;
00824     }
00825     //-----------------------------------------------------------------------
00826     void TextureUnitState::setTextureFiltering(FilterType ft, FilterOptions fo)
00827     {
00828         switch (ft)
00829         {
00830         case FT_MIN:
00831             mMinFilter = fo;
00832             break;
00833         case FT_MAG:
00834             mMagFilter = fo;
00835             break;
00836         case FT_MIP:
00837             mMagFilter = fo;
00838             break;
00839         }
00840         mIsDefaultFiltering = false;
00841     }
00842     //-----------------------------------------------------------------------
00843     void TextureUnitState::setTextureFiltering(FilterOptions minFilter, 
00844         FilterOptions magFilter, FilterOptions mipFilter)
00845     {
00846         mMinFilter = minFilter;
00847         mMagFilter = magFilter;
00848         mMipFilter = mipFilter;
00849         mIsDefaultFiltering = false;
00850     }
00851     //-----------------------------------------------------------------------
00852     FilterOptions TextureUnitState::getTextureFiltering(FilterType ft) const
00853     {
00854 
00855         switch (ft)
00856         {
00857         case FT_MIN:
00858             return mIsDefaultFiltering ? 
00859                 MaterialManager::getSingleton().getDefaultTextureFiltering(FT_MIN) : mMinFilter;
00860         case FT_MAG:
00861             return mIsDefaultFiltering ? 
00862                 MaterialManager::getSingleton().getDefaultTextureFiltering(FT_MAG) : mMagFilter;
00863         case FT_MIP:
00864             return mIsDefaultFiltering ? 
00865                 MaterialManager::getSingleton().getDefaultTextureFiltering(FT_MIP) : mMipFilter;
00866         }
00867         // to keep compiler happy
00868         return mMinFilter;
00869     }
00870 
00871     //-----------------------------------------------------------------------
00872     void TextureUnitState::setTextureAnisotropy(unsigned int maxAniso)
00873     {
00874         mMaxAniso = maxAniso;
00875         mIsDefaultAniso = false;
00876     }
00877     //-----------------------------------------------------------------------
00878     unsigned int TextureUnitState::getTextureAnisotropy() const
00879     {
00880         return mIsDefaultAniso? MaterialManager::getSingleton().getDefaultAnisotropy() : mMaxAniso;
00881     }
00882 
00883     //-----------------------------------------------------------------------
00884     void TextureUnitState::_unload(void)
00885     {
00886         // TODO
00887     }
00888     //-----------------------------------------------------------------------------
00889     bool TextureUnitState::isLoaded(void)
00890     {
00891         return mParent->isLoaded();
00892     }
00893     //-----------------------------------------------------------------------
00894     void TextureUnitState::_notifyNeedsRecompile(void)
00895     {
00896         mParent->_notifyNeedsRecompile();
00897     }
00898     //-----------------------------------------------------------------------
00899     bool TextureUnitState::hasViewRelativeTextureCoordinateGeneration(void)
00900     {
00901         // Right now this only returns true for reflection maps
00902 
00903         EffectMap::const_iterator i, iend;
00904         iend = mEffects.end();
00905         
00906         for(i = mEffects.find(ET_ENVIRONMENT_MAP); i != iend; ++i)
00907         {
00908             if (i->second.subtype == ENV_REFLECTION)
00909                 return true;
00910         }
00911         for(i = mEffects.find(ET_PROJECTIVE_TEXTURE); i != iend; ++i)
00912         {
00913             return true;
00914         }
00915 
00916         return false;
00917     }
00918     //-----------------------------------------------------------------------
00919     void TextureUnitState::setProjectiveTexturing(bool enable, 
00920         const Frustum* projectionSettings)
00921     {
00922         if (enable)
00923         {
00924             TextureEffect eff;
00925             eff.type = ET_PROJECTIVE_TEXTURE;
00926             eff.frustum = projectionSettings;
00927             addEffect(eff);
00928         }
00929         else
00930         {
00931             removeEffect(ET_PROJECTIVE_TEXTURE);
00932         }
00933 
00934     }
00935 
00936 }

Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:22:53 2004