source: OGRE/trunk/ogrenew/OgreMain/src/OgrePredefinedControllers.cpp @ 692

Revision 692, 12.2 KB checked in by mattausch, 18 years ago (diff)

adding ogre 1.2 and dependencies

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        Root::getSingleton().addFrameListener(this);
42        mFrameTime = 0;
43                mTimeFactor = 1;
44                mFrameDelay = 0;
45        mElapsedTime = 0;
46
47    }
48    //-----------------------------------------------------------------------
49    bool FrameTimeControllerValue::frameStarted(const FrameEvent &evt)
50    {
51                if(mFrameDelay)
52                {
53                        // Fixed frame time
54                        mFrameTime = mFrameDelay;
55                        mTimeFactor =  mFrameDelay / evt.timeSinceLastFrame;
56                }
57                else
58                {
59                        // Save the time value after applying time factor
60                        mFrameTime = mTimeFactor * evt.timeSinceLastFrame;
61                }
62        // Accumulate the elapsed time
63        mElapsedTime += mFrameTime;
64        return true;
65    }
66    //-----------------------------------------------------------------------
67    bool FrameTimeControllerValue::frameEnded(const FrameEvent &evt)
68    {
69        return true;
70    }
71    //-----------------------------------------------------------------------
72    Real FrameTimeControllerValue::getValue() const
73    {
74        return mFrameTime;
75    }
76    //-----------------------------------------------------------------------
77    void FrameTimeControllerValue::setValue(Real value)
78    {
79        // Do nothing - value is set from frame listener
80    }
81        //-----------------------------------------------------------------------
82        Real FrameTimeControllerValue::getTimeFactor(void) const {
83                return mTimeFactor;
84        }
85        //-----------------------------------------------------------------------
86        void FrameTimeControllerValue::setTimeFactor(Real tf) {
87                if(tf >= 0)
88                {
89                        mTimeFactor = tf;
90                        mFrameDelay = 0;
91                }
92        }
93        //-----------------------------------------------------------------------
94        Real FrameTimeControllerValue::getFrameDelay(void) const {
95                return mFrameDelay;
96        }
97        //-----------------------------------------------------------------------
98        void FrameTimeControllerValue::setFrameDelay(Real fd) {
99                mTimeFactor = 0;
100                mFrameDelay = fd;
101        }
102    //-----------------------------------------------------------------------
103    Real FrameTimeControllerValue::getElapsedTime(void) const
104    {
105        return mElapsedTime;
106    }
107    //-----------------------------------------------------------------------
108    void FrameTimeControllerValue::setElapsedTime(Real elapsedTime)
109    {
110        mElapsedTime = elapsedTime;
111    }
112    //-----------------------------------------------------------------------
113    // TextureFrameControllerValue
114    //-----------------------------------------------------------------------
115    TextureFrameControllerValue::TextureFrameControllerValue(TextureUnitState* t)
116    {
117        mTextureLayer = t;
118    }
119    //-----------------------------------------------------------------------
120    Real TextureFrameControllerValue::getValue(void) const
121    {
122        int numFrames = mTextureLayer->getNumFrames();
123        return (mTextureLayer->getCurrentFrame() / numFrames);
124    }
125    //-----------------------------------------------------------------------
126    void TextureFrameControllerValue::setValue(Real value)
127    {
128        int numFrames = mTextureLayer->getNumFrames();
129        mTextureLayer->setCurrentFrame((int)(value * numFrames) % numFrames);
130    }
131    //-----------------------------------------------------------------------
132    // TexCoordModifierControllerValue
133    //-----------------------------------------------------------------------
134    TexCoordModifierControllerValue::TexCoordModifierControllerValue(TextureUnitState* t,
135        bool translateU, bool translateV, bool scaleU, bool scaleV, bool rotate )
136    {
137        mTextureLayer = t;
138        mTransU = translateU;
139        mTransV = translateV;
140        mScaleU = scaleU;
141        mScaleV = scaleV;
142        mRotate = rotate;
143    }
144    //-----------------------------------------------------------------------
145    Real TexCoordModifierControllerValue::getValue() const
146    {
147        const Matrix4& pMat = mTextureLayer->getTextureTransform();
148        if (mTransU)
149        {
150            return pMat[0][3];
151        }
152        else if (mTransV)
153        {
154            return pMat[1][3];
155        }
156        else if (mScaleU)
157        {
158            return pMat[0][0];
159        }
160        else if (mScaleV)
161        {
162            return pMat[1][1];
163        }
164        // Shouldn't get here
165        return 0;
166    }
167    //-----------------------------------------------------------------------
168    void TexCoordModifierControllerValue::setValue(Real value)
169    {
170        if (mTransU)
171        {
172            mTextureLayer->setTextureUScroll(value);
173        }
174        if (mTransV)
175        {
176            mTextureLayer->setTextureVScroll(value);
177        }
178        if (mScaleU)
179        {
180            if (value >= 0)
181            {
182                // Add 1 to scale (+ve scales up)
183                mTextureLayer->setTextureUScale(1 + value);
184            }
185            else
186            {
187                // (-ve scales down)
188                mTextureLayer->setTextureUScale(1 / -value);
189            }
190        }
191        if (mScaleV)
192        {
193            if (value >= 0)
194            {
195                // Add 1 to scale (+ve scales up)
196                mTextureLayer->setTextureVScale(1 + value);
197            }
198            else
199            {
200                // (-ve scales down)
201                mTextureLayer->setTextureVScale(1 / -value);
202            }
203        }
204        if (mRotate)
205        {
206            mTextureLayer->setTextureRotate(Radian(value * Math::TWO_PI));
207        }
208    }
209    //-----------------------------------------------------------------------
210    //-----------------------------------------------------------------------
211        FloatGpuParameterControllerValue::FloatGpuParameterControllerValue(
212                        GpuProgramParameters* params, size_t index) :
213                mParams(params), mParamIndex(index)
214        {
215        }
216    //-----------------------------------------------------------------------
217        Real FloatGpuParameterControllerValue::getValue(void) const
218        {
219                // do nothing, reading from a set of params not supported
220                return 0.0f;
221        }
222    //-----------------------------------------------------------------------
223        void FloatGpuParameterControllerValue::setValue(Real val)
224        {
225                static Vector4 v4 = Vector4(0,0,0,0);
226                v4.x = val;
227                mParams->setConstant(mParamIndex, v4);
228        }
229        //-----------------------------------------------------------------------
230        // PassthroughControllerFunction
231        //-----------------------------------------------------------------------
232        PassthroughControllerFunction::PassthroughControllerFunction(bool delta)
233                : ControllerFunction<Real>(delta)
234        {
235        }
236        //-----------------------------------------------------------------------
237        Real PassthroughControllerFunction::calculate(Real source)
238        {
239                return getAdjustedInput(source);
240
241        }
242    //-----------------------------------------------------------------------
243    // AnimationControllerFunction
244    //-----------------------------------------------------------------------
245    AnimationControllerFunction::AnimationControllerFunction(Real sequenceTime, Real timeOffset)
246                : ControllerFunction<Real>(false)
247    {
248        mSeqTime = sequenceTime;
249        mTime = timeOffset;
250    }
251    //-----------------------------------------------------------------------
252    Real AnimationControllerFunction::calculate(Real source)
253    {
254        // Assume source is time since last update
255        mTime += source;
256        // Wrap
257        while (mTime >= mSeqTime) mTime -= mSeqTime;
258        while (mTime < 0) mTime += mSeqTime;
259
260        // Return parametric
261        return mTime / mSeqTime;
262    }
263    //-----------------------------------------------------------------------
264    // ScaleControllerFunction
265    //-----------------------------------------------------------------------
266    ScaleControllerFunction::ScaleControllerFunction(Real factor, bool delta) : ControllerFunction<Real>(delta)
267    {
268        mScale = factor;
269    }
270    //-----------------------------------------------------------------------
271    Real ScaleControllerFunction::calculate(Real source)
272    {
273        return getAdjustedInput(source * mScale);
274
275    }
276    //-----------------------------------------------------------------------
277    // WaveformControllerFunction
278    //-----------------------------------------------------------------------
279    WaveformControllerFunction::WaveformControllerFunction(WaveformType wType, Real base,  Real frequency, Real phase, Real amplitude, bool delta, Real dutyCycle)
280        :ControllerFunction<Real>(delta)
281    {
282        mWaveType = wType;
283        mBase = base;
284        mFrequency = frequency;
285        mPhase = phase;
286        mAmplitude = amplitude;
287        mDeltaCount = phase;
288                mDutyCycle = dutyCycle;
289    }
290    //-----------------------------------------------------------------------
291    Real WaveformControllerFunction::getAdjustedInput(Real input)
292    {
293        Real adjusted = ControllerFunction<Real>::getAdjustedInput(input);
294
295        // If not delta, adjust by phase here
296        // (delta inputs have it adjusted at initialisation)
297        if (!mDeltaInput)
298        {
299            adjusted += mPhase;
300        }
301
302        return adjusted;
303    }
304    //-----------------------------------------------------------------------
305    Real WaveformControllerFunction::calculate(Real source)
306    {
307        Real input = getAdjustedInput(source * mFrequency);
308        Real output;
309        // For simplicity, factor input down to {0,1)
310        // Use looped subtract rather than divide / round
311        while (input >= 1.0)
312            input -= 1.0;
313        while (input < 0.0)
314            input += 1.0;
315
316        // Calculate output in -1..1 range
317        switch (mWaveType)
318        {
319        case WFT_SINE:
320            output = Math::Sin(Radian(input * Math::TWO_PI));
321            break;
322        case WFT_TRIANGLE:
323            if (input < 0.25)
324                output = input * 4;
325            else if (input >= 0.25 && input < 0.75)
326                output = 1.0 - ((input - 0.25) * 4);
327            else
328                output = ((input - 0.75) * 4) - 1.0;
329
330            break;
331        case WFT_SQUARE:
332            if (input <= 0.5)
333                output = 1.0;
334            else
335                output = -1.0;
336            break;
337        case WFT_SAWTOOTH:
338            output = (input * 2) - 1;
339            break;
340        case WFT_INVERSE_SAWTOOTH:
341            output = -((input * 2) - 1);
342            break;
343                case WFT_PWM:
344                        if( input <= mDutyCycle )
345                                output = 1.0;
346                        else
347                                output = -1.0;
348                        break;
349        }
350
351        // Scale output into 0..1 range and then by base + amplitude
352        return mBase + ((output + 1.0) * 0.5 * mAmplitude);
353
354
355    }
356}
357
Note: See TracBrowser for help on using the repository browser.