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

OgrePredefinedControllers.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 "OgrePredefinedControllers.h"
00027 
00028 #include "OgreRoot.h"
00029 #include "OgreMath.h"
00030 #include "OgreLogManager.h"
00031 #include "OgreTextureUnitState.h"
00032 
00033 namespace Ogre
00034 {
00035     //-----------------------------------------------------------------------
00036     // FrameTimeControllerValue
00037     //-----------------------------------------------------------------------
00038     FrameTimeControllerValue::FrameTimeControllerValue()
00039     {
00040         // Register self
00041         Root::getSingleton().addFrameListener(this);
00042         mFrameTime = 0;
00043         mTimeFactor = 1;
00044 
00045     }
00046     //-----------------------------------------------------------------------
00047     bool FrameTimeControllerValue::frameStarted(const FrameEvent &evt)
00048     {
00049         // Save the time value after applying time factor
00050         mFrameTime = mTimeFactor * evt.timeSinceLastFrame;
00051         return true;
00052     }
00053     //-----------------------------------------------------------------------
00054     bool FrameTimeControllerValue::frameEnded(const FrameEvent &evt)
00055     {
00056         return true;
00057     }
00058     //-----------------------------------------------------------------------
00059     Real FrameTimeControllerValue::getValue() const
00060     {
00061         return mFrameTime;
00062     }
00063     //-----------------------------------------------------------------------
00064     void FrameTimeControllerValue::setValue(Real value)
00065     {
00066         // Do nothing - value is set from frame listener
00067     }
00068     //-----------------------------------------------------------------------
00069     Real FrameTimeControllerValue::getTimeFactor(void) const {
00070         return mTimeFactor;
00071     }
00072     //-----------------------------------------------------------------------
00073     void FrameTimeControllerValue::setTimeFactor(Real tf) {
00074         if(tf >= 0) mTimeFactor = tf;
00075     }
00076     //-----------------------------------------------------------------------
00077     // TextureFrameControllerValue
00078     //-----------------------------------------------------------------------
00079     TextureFrameControllerValue::TextureFrameControllerValue(TextureUnitState* t)
00080     {
00081         mTextureLayer = t;
00082     }
00083     //-----------------------------------------------------------------------
00084     Real TextureFrameControllerValue::getValue(void) const
00085     {
00086         int numFrames = mTextureLayer->getNumFrames();
00087         return (mTextureLayer->getCurrentFrame() / numFrames);
00088     }
00089     //-----------------------------------------------------------------------
00090     void TextureFrameControllerValue::setValue(Real value)
00091     {
00092         int numFrames = mTextureLayer->getNumFrames();
00093         mTextureLayer->setCurrentFrame((int)(value * numFrames));
00094     }
00095     //-----------------------------------------------------------------------
00096     // TexCoordModifierControllerValue
00097     //-----------------------------------------------------------------------
00098     TexCoordModifierControllerValue::TexCoordModifierControllerValue(TextureUnitState* t,
00099         bool translateU, bool translateV, bool scaleU, bool scaleV, bool rotate )
00100     {
00101         mTextureLayer = t;
00102         mTransU = translateU;
00103         mTransV = translateV;
00104         mScaleU = scaleU;
00105         mScaleV = scaleV;
00106         mRotate = rotate;
00107     }
00108     //-----------------------------------------------------------------------
00109     Real TexCoordModifierControllerValue::getValue() const
00110     {
00111         const Matrix4& pMat = mTextureLayer->getTextureTransform();
00112         if (mTransU)
00113         {
00114             return pMat[0][3];
00115         }
00116         else if (mTransV)
00117         {
00118             return pMat[1][3];
00119         }
00120         else if (mScaleU)
00121         {
00122             return pMat[0][0];
00123         }
00124         else if (mScaleV)
00125         {
00126             return pMat[1][1];
00127         }
00128         // Shouldn't get here
00129         return 0;
00130     }
00131     //-----------------------------------------------------------------------
00132     void TexCoordModifierControllerValue::setValue(Real value)
00133     {
00134         if (mTransU)
00135         {
00136             mTextureLayer->setTextureUScroll(value);
00137         }
00138         if (mTransV)
00139         {
00140             mTextureLayer->setTextureVScroll(value);
00141         }
00142         if (mScaleU)
00143         {
00144             if (value >= 0)
00145             {
00146                 // Add 1 to scale (+ve scales up)
00147                 mTextureLayer->setTextureUScale(1 + value);
00148             }
00149             else
00150             {
00151                 // (-ve scales down)
00152                 mTextureLayer->setTextureUScale(1 / -value);
00153             }
00154         }
00155         if (mScaleV)
00156         {
00157             if (value >= 0)
00158             {
00159                 // Add 1 to scale (+ve scales up)
00160                 mTextureLayer->setTextureVScale(1 + value);
00161             }
00162             else
00163             {
00164                 // (-ve scales down)
00165                 mTextureLayer->setTextureVScale(1 / -value);
00166             }
00167         }
00168         if (mRotate)
00169         {
00170             mTextureLayer->setTextureRotate(value * 360);
00171         }
00172     }
00173     //-----------------------------------------------------------------------
00174     //-----------------------------------------------------------------------
00175     FloatGpuParameterControllerValue::FloatGpuParameterControllerValue(
00176             GpuProgramParametersSharedPtr params, size_t index) :
00177         mParams(params), mParamIndex(index)
00178     {
00179     }
00180     //-----------------------------------------------------------------------
00181     Real FloatGpuParameterControllerValue::getValue(void) const
00182     {
00183         // do nothing, reading from a set of params not supported
00184         return 0.0f;
00185     }
00186     //-----------------------------------------------------------------------
00187     void FloatGpuParameterControllerValue::setValue(Real val)
00188     {
00189         static Vector4 v4 = Vector4(0,0,0,0);
00190         v4.x = val;
00191         mParams->setConstant(mParamIndex, v4);
00192     }
00193     //-----------------------------------------------------------------------
00194     // AnimationControllerFunction
00195     //-----------------------------------------------------------------------
00196     AnimationControllerFunction::AnimationControllerFunction(Real sequenceTime, Real timeOffset) 
00197         : ControllerFunction<Real>(false)
00198     {
00199         mSeqTime = sequenceTime;
00200         mTime = timeOffset;
00201     }
00202     //-----------------------------------------------------------------------
00203     Real AnimationControllerFunction::calculate(Real source)
00204     {
00205         // Assume source is time since last update
00206         mTime += source;
00207         // Wrap
00208         while (mTime >= mSeqTime) mTime -= mSeqTime;
00209 
00210         // Return parametric
00211         return mTime / mSeqTime;
00212     }
00213     //-----------------------------------------------------------------------
00214     // ScaleControllerFunction
00215     //-----------------------------------------------------------------------
00216     ScaleControllerFunction::ScaleControllerFunction(Real factor, bool delta) : ControllerFunction<Real>(delta)
00217     {
00218         mScale = factor;
00219     }
00220     //-----------------------------------------------------------------------
00221     Real ScaleControllerFunction::calculate(Real source)
00222     {
00223         return getAdjustedInput(source * mScale);
00224 
00225     }
00226     //-----------------------------------------------------------------------
00227     // WaveformControllerFunction
00228     //-----------------------------------------------------------------------
00229     WaveformControllerFunction::WaveformControllerFunction(WaveformType wType, Real base,  Real frequency, Real phase, Real amplitude, bool delta)
00230         :ControllerFunction<Real>(delta)
00231     {
00232         mWaveType = wType;
00233         mBase = base;
00234         mFrequency = frequency;
00235         mPhase = phase;
00236         mAmplitude = amplitude;
00237         mDeltaCount = phase;
00238 
00239     }
00240     //-----------------------------------------------------------------------
00241     Real WaveformControllerFunction::getAdjustedInput(Real input)
00242     {
00243         Real adjusted = ControllerFunction<Real>::getAdjustedInput(input);
00244 
00245         // If not delta, adjust by phase here
00246         // (delta inputs have it adjusted at initialisation)
00247         if (!mDeltaInput)
00248         {
00249             adjusted += mPhase;
00250         }
00251 
00252         return adjusted;
00253     }
00254     //-----------------------------------------------------------------------
00255     Real WaveformControllerFunction::calculate(Real source)
00256     {
00257         Real input = getAdjustedInput(source * mFrequency);
00258         Real output;
00259         // For simplicity, factor input down to {0,1)
00260         // Use looped subtract rather than divide / round
00261         while (input >= 1.0)
00262             input -= 1.0;
00263 
00264         // Calculate output in -1..1 range
00265         switch (mWaveType)
00266         {
00267         case WFT_SINE:
00268             output = Math::Sin(input * Math::TWO_PI);
00269             break;
00270         case WFT_TRIANGLE:
00271             if (input < 0.25)
00272                 output = input * 4;
00273             else if (input >= 0.25 && input < 0.75)
00274                 output = 1.0 - ((input - 0.25) * 4);
00275             else
00276                 output = ((input - 0.75) * 4) - 1.0;
00277 
00278             break;
00279         case WFT_SQUARE:
00280             if (input <= 0.5)
00281                 output = 1.0;
00282             else
00283                 output = -1.0;
00284             break;
00285         case WFT_SAWTOOTH:
00286             output = (input * 2) - 1;
00287             break;
00288         case WFT_INVERSE_SAWTOOTH:
00289             output = -((input * 2) - 1);
00290             break;
00291         }
00292 
00293         // Scale output into 0..1 range and then by base + amplitude
00294         return mBase + ((output + 1.0) * 0.5 * mAmplitude);
00295 
00296 
00297     }
00298 }
00299 

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