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

OgreControllerManager.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://www.ogre3d.org/
00006 
00007 Copyright © 2000-2002 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 #include "OgreControllerManager.h"
00027 
00028 #include "OgreLogManager.h"
00029 #include "OgreTextureUnitState.h"
00030 
00031 
00032 namespace Ogre {
00033     //-----------------------------------------------------------------------
00034     template<> ControllerManager* Singleton<ControllerManager>::ms_Singleton = 0;
00035     ControllerManager* ControllerManager::getSingletonPtr(void)
00036     {
00037         return ms_Singleton;
00038     }
00039     ControllerManager& ControllerManager::getSingleton(void)
00040     {  
00041         assert( ms_Singleton );  return ( *ms_Singleton );  
00042     }
00043     //-----------------------------------------------------------------------
00044     ControllerManager::ControllerManager()
00045         : mFrameTimeController(new FrameTimeControllerValue())
00046     {
00047 
00048     }
00049     //-----------------------------------------------------------------------
00050     ControllerManager::~ControllerManager()
00051     {
00052         clearControllers();
00053     }
00054     //-----------------------------------------------------------------------
00055     Controller<Real>* ControllerManager::createController(
00056         SharedPtr< ControllerValue<Real> > src, SharedPtr< ControllerValue<Real> > dest, 
00057         SharedPtr< ControllerFunction<Real> > func)
00058     {
00059         Controller<Real>* c = new Controller<Real>(src, dest, func);
00060 
00061         mControllers.insert(c);
00062         return c;
00063     }
00064     //-----------------------------------------------------------------------
00065     void ControllerManager::updateAllControllers(void)
00066     {
00067         ControllerList::const_iterator ci;
00068         for (ci = mControllers.begin(); ci != mControllers.end(); ++ci)
00069         {
00070             (*ci)->update();
00071         }
00072     }
00073     //-----------------------------------------------------------------------
00074     void ControllerManager::clearControllers(void)
00075     {
00076         ControllerList::iterator ci;
00077         for (ci = mControllers.begin(); ci != mControllers.end(); ++ci)
00078         {
00079             delete *ci;
00080         }
00081         mControllers.clear();
00082     }
00083     //-----------------------------------------------------------------------
00084     SharedPtr< ControllerValue<Real> > ControllerManager::getFrameTimeSource(void) const
00085     {
00086         return mFrameTimeController;
00087     }
00088     //-----------------------------------------------------------------------
00089     Controller<Real>* ControllerManager::createTextureAnimator(TextureUnitState* layer, Real sequenceTime)
00090     {
00091         SharedPtr< ControllerValue<Real> > texVal(new TextureFrameControllerValue(layer));
00092         SharedPtr< ControllerFunction<Real> > animFunc(new AnimationControllerFunction(sequenceTime));
00093 
00094         return createController(mFrameTimeController, texVal, animFunc);
00095     }
00096     //-----------------------------------------------------------------------
00097     Controller<Real>* ControllerManager::createTextureScroller(TextureUnitState* layer, Real uSpeed, Real vSpeed)
00098     {
00099         Controller<Real>* ret = 0;
00100 
00101         // Set up 1 or 2 controllers to manage the scrolling texture
00102         if (uSpeed != 0)
00103         {
00104             SharedPtr< ControllerValue<Real> > uVal;
00105             SharedPtr< ControllerFunction<Real> > uFunc;
00106 
00107             if (uSpeed == vSpeed)
00108             {
00109                 // Cool, we can do both scrolls with a single controller
00110                 uVal.bind(new TexCoordModifierControllerValue(layer, true, true));
00111             }
00112             else
00113             {
00114                 // Just do u, v will take a second controller
00115                 uVal.bind(new TexCoordModifierControllerValue(layer, true));
00116             }
00117             // Create function: use -speed since we're altering texture coords so they have reverse effect
00118             uFunc.bind(new ScaleControllerFunction(-uSpeed, true));
00119             ret = createController(mFrameTimeController, uVal, uFunc);
00120         }
00121 
00122         if (vSpeed != 0 && (uSpeed == 0 || vSpeed != uSpeed))
00123         {
00124             SharedPtr< ControllerValue<Real> > vVal;
00125             SharedPtr< ControllerFunction<Real> > vFunc;
00126 
00127             // Set up a second controller for v scroll
00128             vVal.bind(new TexCoordModifierControllerValue(layer, false, true));
00129             // Create function: use -speed since we're altering texture coords so they have reverse effect
00130             vFunc.bind(new ScaleControllerFunction(-vSpeed, true));
00131             ret = createController(mFrameTimeController, vVal, vFunc);
00132         }
00133 
00134         return ret;
00135     }
00136     //-----------------------------------------------------------------------
00137     Controller<Real>* ControllerManager::createTextureRotater(TextureUnitState* layer, Real speed)
00138     {
00139         SharedPtr< ControllerValue<Real> > val;
00140         SharedPtr< ControllerFunction<Real> > func;
00141 
00142         // Target value is texture coord rotation
00143         val.bind(new TexCoordModifierControllerValue(layer, false, false, false, false, true));
00144         // Function is simple scale (seconds * speed)
00145         // Use -speed since altering texture coords has the reverse visible effect
00146         func.bind(new ScaleControllerFunction(-speed, true));
00147 
00148         return createController(mFrameTimeController, val, func);
00149 
00150     }
00151     //-----------------------------------------------------------------------
00152     Controller<Real>* ControllerManager::createTextureWaveTransformer(TextureUnitState* layer,
00153         TextureUnitState::TextureTransformType ttype, WaveformType waveType, Real base, Real frequency, Real phase, Real amplitude)
00154     {
00155         SharedPtr< ControllerValue<Real> > val;
00156         SharedPtr< ControllerFunction<Real> > func;
00157 
00158         switch (ttype)
00159         {
00160         case TextureUnitState::TT_TRANSLATE_U:
00161             // Target value is a u scroll
00162             val.bind(new TexCoordModifierControllerValue(layer, true));
00163             break;
00164         case TextureUnitState::TT_TRANSLATE_V:
00165             // Target value is a v scroll
00166             val.bind(new TexCoordModifierControllerValue(layer, false, true));
00167             break;
00168         case TextureUnitState::TT_SCALE_U:
00169             // Target value is a u scale
00170             val.bind(new TexCoordModifierControllerValue(layer, false, false, true));
00171             break;
00172         case TextureUnitState::TT_SCALE_V:
00173             // Target value is a v scale
00174             val.bind(new TexCoordModifierControllerValue(layer, false, false, false, true));
00175             break;
00176         case TextureUnitState::TT_ROTATE:
00177             // Target value is texture coord rotation
00178             val.bind(new TexCoordModifierControllerValue(layer, false, false, false, false, true));
00179             break;
00180         }
00181         // Create new wave function for alterations
00182         func.bind(new WaveformControllerFunction(waveType, base, frequency, phase, amplitude, true));
00183 
00184         return createController(mFrameTimeController, val, func);
00185     }
00186     //-----------------------------------------------------------------------
00187     Controller<Real>* ControllerManager::createGpuProgramTimerParam(
00188         GpuProgramParameters* params, size_t paramIndex, Real timeFactor)
00189     {
00190         SharedPtr< ControllerValue<Real> > val;
00191         SharedPtr< ControllerFunction<Real> > func;
00192 
00193         val.bind(new FloatGpuParameterControllerValue(params, paramIndex));
00194         func.bind(new ScaleControllerFunction(timeFactor, true));
00195 
00196         return createController(mFrameTimeController, val, func);
00197 
00198     }
00199     //-----------------------------------------------------------------------
00200     void ControllerManager::destroyController(Controller<Real>* controller)
00201     {
00202         ControllerList::iterator i = mControllers.find(controller);
00203         if (i != mControllers.end())
00204         {
00205             mControllers.erase(i);
00206             delete controller;
00207         }
00208     }
00209     //-----------------------------------------------------------------------
00210     Real ControllerManager::getTimeFactor(void) const {
00211         return static_cast<const FrameTimeControllerValue*>(mFrameTimeController.get())->getTimeFactor();
00212     }
00213     //-----------------------------------------------------------------------
00214     void ControllerManager::setTimeFactor(Real tf) {
00215         static_cast<FrameTimeControllerValue*>(mFrameTimeController.getPointer())->setTimeFactor(tf);
00216     }
00217 }

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:18 2004