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

Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:30 2004