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

Revision 657, 11.0 KB checked in by mattausch, 19 years ago (diff)

added ogre dependencies and patched ogre sources

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