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