source: OGRE/trunk/ogre_changes/Ogre1.2/OgreMain/src/OgrePredefinedControllers.cpp @ 2129

Revision 2129, 12.3 KB checked in by szirmay, 17 years ago (diff)
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#include "OgreStableHeaders.h"
26#include "OgrePredefinedControllers.h"
27
28#include "OgreRoot.h"
29#include "OgreMath.h"
30#include "OgreLogManager.h"
31#include "OgreTextureUnitState.h"
32
33namespace Ogre
34{
35    //-----------------------------------------------------------------------
36    // FrameTimeControllerValue
37    //-----------------------------------------------------------------------
38    FrameTimeControllerValue::FrameTimeControllerValue()
39    {
40        // Register self
41                #ifdef GAMETOOLS_ILLUMINATION_MODULE
42                        this->setPriority(0);
43                #endif
44        Root::getSingleton().addFrameListener(this);
45        mFrameTime = 0;
46                mTimeFactor = 1;
47                mFrameDelay = 0;
48        mElapsedTime = 0;
49
50    }
51    //-----------------------------------------------------------------------
52    bool FrameTimeControllerValue::frameStarted(const FrameEvent &evt)
53    {
54                if(mFrameDelay)
55                {
56                        // Fixed frame time
57                        mFrameTime = mFrameDelay;
58                        mTimeFactor =  mFrameDelay / evt.timeSinceLastFrame;
59                }
60                else
61                {
62                        // Save the time value after applying time factor
63                        mFrameTime = mTimeFactor * evt.timeSinceLastFrame;
64                }
65        // Accumulate the elapsed time
66        mElapsedTime += mFrameTime;
67        return true;
68    }
69    //-----------------------------------------------------------------------
70    bool FrameTimeControllerValue::frameEnded(const FrameEvent &evt)
71    {
72        return true;
73    }
74    //-----------------------------------------------------------------------
75    Real FrameTimeControllerValue::getValue() const
76    {
77        return mFrameTime;
78    }
79    //-----------------------------------------------------------------------
80    void FrameTimeControllerValue::setValue(Real value)
81    {
82        // Do nothing - value is set from frame listener
83    }
84        //-----------------------------------------------------------------------
85        Real FrameTimeControllerValue::getTimeFactor(void) const {
86                return mTimeFactor;
87        }
88        //-----------------------------------------------------------------------
89        void FrameTimeControllerValue::setTimeFactor(Real tf) {
90                if(tf >= 0)
91                {
92                        mTimeFactor = tf;
93                        mFrameDelay = 0;
94                }
95        }
96        //-----------------------------------------------------------------------
97        Real FrameTimeControllerValue::getFrameDelay(void) const {
98                return mFrameDelay;
99        }
100        //-----------------------------------------------------------------------
101        void FrameTimeControllerValue::setFrameDelay(Real fd) {
102                mTimeFactor = 0;
103                mFrameDelay = fd;
104        }
105    //-----------------------------------------------------------------------
106    Real FrameTimeControllerValue::getElapsedTime(void) const
107    {
108        return mElapsedTime;
109    }
110    //-----------------------------------------------------------------------
111    void FrameTimeControllerValue::setElapsedTime(Real elapsedTime)
112    {
113        mElapsedTime = elapsedTime;
114    }
115    //-----------------------------------------------------------------------
116    // TextureFrameControllerValue
117    //-----------------------------------------------------------------------
118    TextureFrameControllerValue::TextureFrameControllerValue(TextureUnitState* t)
119    {
120        mTextureLayer = t;
121    }
122    //-----------------------------------------------------------------------
123    Real TextureFrameControllerValue::getValue(void) const
124    {
125        int numFrames = mTextureLayer->getNumFrames();
126        return (mTextureLayer->getCurrentFrame() / numFrames);
127    }
128    //-----------------------------------------------------------------------
129    void TextureFrameControllerValue::setValue(Real value)
130    {
131        int numFrames = mTextureLayer->getNumFrames();
132        mTextureLayer->setCurrentFrame((int)(value * numFrames) % numFrames);
133    }
134    //-----------------------------------------------------------------------
135    // TexCoordModifierControllerValue
136    //-----------------------------------------------------------------------
137    TexCoordModifierControllerValue::TexCoordModifierControllerValue(TextureUnitState* t,
138        bool translateU, bool translateV, bool scaleU, bool scaleV, bool rotate )
139    {
140        mTextureLayer = t;
141        mTransU = translateU;
142        mTransV = translateV;
143        mScaleU = scaleU;
144        mScaleV = scaleV;
145        mRotate = rotate;
146    }
147    //-----------------------------------------------------------------------
148    Real TexCoordModifierControllerValue::getValue() const
149    {
150        const Matrix4& pMat = mTextureLayer->getTextureTransform();
151        if (mTransU)
152        {
153            return pMat[0][3];
154        }
155        else if (mTransV)
156        {
157            return pMat[1][3];
158        }
159        else if (mScaleU)
160        {
161            return pMat[0][0];
162        }
163        else if (mScaleV)
164        {
165            return pMat[1][1];
166        }
167        // Shouldn't get here
168        return 0;
169    }
170    //-----------------------------------------------------------------------
171    void TexCoordModifierControllerValue::setValue(Real value)
172    {
173        if (mTransU)
174        {
175            mTextureLayer->setTextureUScroll(value);
176        }
177        if (mTransV)
178        {
179            mTextureLayer->setTextureVScroll(value);
180        }
181        if (mScaleU)
182        {
183            if (value >= 0)
184            {
185                // Add 1 to scale (+ve scales up)
186                mTextureLayer->setTextureUScale(1 + value);
187            }
188            else
189            {
190                // (-ve scales down)
191                mTextureLayer->setTextureUScale(1 / -value);
192            }
193        }
194        if (mScaleV)
195        {
196            if (value >= 0)
197            {
198                // Add 1 to scale (+ve scales up)
199                mTextureLayer->setTextureVScale(1 + value);
200            }
201            else
202            {
203                // (-ve scales down)
204                mTextureLayer->setTextureVScale(1 / -value);
205            }
206        }
207        if (mRotate)
208        {
209            mTextureLayer->setTextureRotate(Radian(value * Math::TWO_PI));
210        }
211    }
212    //-----------------------------------------------------------------------
213    //-----------------------------------------------------------------------
214        FloatGpuParameterControllerValue::FloatGpuParameterControllerValue(
215                        GpuProgramParameters* params, size_t index) :
216                mParams(params), mParamIndex(index)
217        {
218        }
219    //-----------------------------------------------------------------------
220        Real FloatGpuParameterControllerValue::getValue(void) const
221        {
222                // do nothing, reading from a set of params not supported
223                return 0.0f;
224        }
225    //-----------------------------------------------------------------------
226        void FloatGpuParameterControllerValue::setValue(Real val)
227        {
228                static Vector4 v4 = Vector4(0,0,0,0);
229                v4.x = val;
230                mParams->setConstant(mParamIndex, v4);
231        }
232        //-----------------------------------------------------------------------
233        // PassthroughControllerFunction
234        //-----------------------------------------------------------------------
235        PassthroughControllerFunction::PassthroughControllerFunction(bool delta)
236                : ControllerFunction<Real>(delta)
237        {
238        }
239        //-----------------------------------------------------------------------
240        Real PassthroughControllerFunction::calculate(Real source)
241        {
242                return getAdjustedInput(source);
243
244        }
245    //-----------------------------------------------------------------------
246    // AnimationControllerFunction
247    //-----------------------------------------------------------------------
248    AnimationControllerFunction::AnimationControllerFunction(Real sequenceTime, Real timeOffset)
249                : ControllerFunction<Real>(false)
250    {
251        mSeqTime = sequenceTime;
252        mTime = timeOffset;
253    }
254    //-----------------------------------------------------------------------
255    Real AnimationControllerFunction::calculate(Real source)
256    {
257        // Assume source is time since last update
258        mTime += source;
259        // Wrap
260        while (mTime >= mSeqTime) mTime -= mSeqTime;
261        while (mTime < 0) mTime += mSeqTime;
262
263        // Return parametric
264        return mTime / mSeqTime;
265    }
266    //-----------------------------------------------------------------------
267    // ScaleControllerFunction
268    //-----------------------------------------------------------------------
269    ScaleControllerFunction::ScaleControllerFunction(Real factor, bool delta) : ControllerFunction<Real>(delta)
270    {
271        mScale = factor;
272    }
273    //-----------------------------------------------------------------------
274    Real ScaleControllerFunction::calculate(Real source)
275    {
276        return getAdjustedInput(source * mScale);
277
278    }
279    //-----------------------------------------------------------------------
280    // WaveformControllerFunction
281    //-----------------------------------------------------------------------
282    WaveformControllerFunction::WaveformControllerFunction(WaveformType wType, Real base,  Real frequency, Real phase, Real amplitude, bool delta, Real dutyCycle)
283        :ControllerFunction<Real>(delta)
284    {
285        mWaveType = wType;
286        mBase = base;
287        mFrequency = frequency;
288        mPhase = phase;
289        mAmplitude = amplitude;
290        mDeltaCount = phase;
291                mDutyCycle = dutyCycle;
292    }
293    //-----------------------------------------------------------------------
294    Real WaveformControllerFunction::getAdjustedInput(Real input)
295    {
296        Real adjusted = ControllerFunction<Real>::getAdjustedInput(input);
297
298        // If not delta, adjust by phase here
299        // (delta inputs have it adjusted at initialisation)
300        if (!mDeltaInput)
301        {
302            adjusted += mPhase;
303        }
304
305        return adjusted;
306    }
307    //-----------------------------------------------------------------------
308    Real WaveformControllerFunction::calculate(Real source)
309    {
310        Real input = getAdjustedInput(source * mFrequency);
311        Real output;
312        // For simplicity, factor input down to {0,1)
313        // Use looped subtract rather than divide / round
314        while (input >= 1.0)
315            input -= 1.0;
316        while (input < 0.0)
317            input += 1.0;
318
319        // Calculate output in -1..1 range
320        switch (mWaveType)
321        {
322        case WFT_SINE:
323            output = Math::Sin(Radian(input * Math::TWO_PI));
324            break;
325        case WFT_TRIANGLE:
326            if (input < 0.25)
327                output = input * 4;
328            else if (input >= 0.25 && input < 0.75)
329                output = 1.0 - ((input - 0.25) * 4);
330            else
331                output = ((input - 0.75) * 4) - 1.0;
332
333            break;
334        case WFT_SQUARE:
335            if (input <= 0.5)
336                output = 1.0;
337            else
338                output = -1.0;
339            break;
340        case WFT_SAWTOOTH:
341            output = (input * 2) - 1;
342            break;
343        case WFT_INVERSE_SAWTOOTH:
344            output = -((input * 2) - 1);
345            break;
346                case WFT_PWM:
347                        if( input <= mDutyCycle )
348                                output = 1.0;
349                        else
350                                output = -1.0;
351                        break;
352        }
353
354        // Scale output into 0..1 range and then by base + amplitude
355        return mBase + ((output + 1.0) * 0.5 * mAmplitude);
356
357
358    }
359}
360
Note: See TracBrowser for help on using the repository browser.