Coverage Report

Created: 2026-05-24 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ogre/OgreMain/src/OgrePredefinedControllers.cpp
Line
Count
Source
1
/*
2
-----------------------------------------------------------------------------
3
This source file is part of OGRE
4
    (Object-oriented Graphics Rendering Engine)
5
For the latest info, see http://www.ogre3d.org/
6
7
Copyright (c) 2000-2014 Torus Knot Software Ltd
8
9
Permission is hereby granted, free of charge, to any person obtaining a copy
10
of this software and associated documentation files (the "Software"), to deal
11
in the Software without restriction, including without limitation the rights
12
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
copies of the Software, and to permit persons to whom the Software is
14
furnished to do so, subject to the following conditions:
15
16
The above copyright notice and this permission notice shall be included in
17
all copies or substantial portions of the Software.
18
19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
THE SOFTWARE.
26
-----------------------------------------------------------------------------
27
*/
28
#include "OgreStableHeaders.h"
29
#include "OgrePredefinedControllers.h"
30
#include "OgreTextureUnitState.h"
31
32
namespace Ogre
33
{
34
    //-----------------------------------------------------------------------
35
    // FrameTimeControllerValue
36
    //-----------------------------------------------------------------------
37
    FrameTimeControllerValue::FrameTimeControllerValue()
38
0
    {
39
        // Register self
40
0
        Root::getSingleton().addFrameListener(this);
41
0
        mFrameTime = 0;
42
0
        mTimeFactor = 1;
43
0
        mFrameDelay = 0;
44
0
        mElapsedTime = 0;
45
46
0
    }
47
    //-----------------------------------------------------------------------
48
    bool FrameTimeControllerValue::frameStarted(const FrameEvent &evt)
49
0
    {
50
0
        if(mFrameDelay) 
51
0
        {
52
            // Fixed frame time
53
0
            mFrameTime = mFrameDelay;
54
0
            mTimeFactor =  mFrameDelay / evt.timeSinceLastFrame;
55
0
        }
56
0
        else 
57
0
        {
58
            // Save the time value after applying time factor
59
0
            mFrameTime = mTimeFactor * evt.timeSinceLastFrame;
60
0
        }
61
        // Accumulate the elapsed time
62
0
        mElapsedTime += mFrameTime;
63
0
        return true;
64
0
    }
65
    //-----------------------------------------------------------------------
66
0
    void FrameTimeControllerValue::setTimeFactor(Real tf) {
67
0
        if(tf >= 0) 
68
0
        {
69
0
            mTimeFactor = tf;
70
0
            mFrameDelay = 0;
71
0
        }
72
0
    }
73
    //-----------------------------------------------------------------------
74
0
    void FrameTimeControllerValue::setFrameDelay(Real fd) {
75
0
        mTimeFactor = 0;
76
0
        mFrameDelay = fd;
77
0
    }
78
    //-----------------------------------------------------------------------
79
    // TextureFrameControllerValue
80
    //-----------------------------------------------------------------------
81
    TextureFrameControllerValue::TextureFrameControllerValue(TextureUnitState* t)
82
0
    {
83
0
        mTextureLayer = t;
84
0
    }
85
    //-----------------------------------------------------------------------
86
    float TextureFrameControllerValue::getValue(void) const
87
0
    {
88
0
        int numFrames = mTextureLayer->getNumFrames();
89
0
        return ((float)mTextureLayer->getCurrentFrame() / (float)numFrames);
90
0
    }
91
    //-----------------------------------------------------------------------
92
    void TextureFrameControllerValue::setValue(float value)
93
0
    {
94
0
        int numFrames = mTextureLayer->getNumFrames();
95
0
        mTextureLayer->setCurrentFrame(numFrames ? (int)(value * numFrames) % numFrames : 0);
96
0
    }
97
    //-----------------------------------------------------------------------
98
    // TexCoordModifierControllerValue
99
    //-----------------------------------------------------------------------
100
    TexCoordModifierControllerValue::TexCoordModifierControllerValue(TextureUnitState* t,
101
        bool translateU, bool translateV, bool scaleU, bool scaleV, bool rotate )
102
0
    {
103
0
        mTextureLayer = t;
104
0
        mTransU = translateU;
105
0
        mTransV = translateV;
106
0
        mScaleU = scaleU;
107
0
        mScaleV = scaleV;
108
0
        mRotate = rotate;
109
0
    }
110
    //-----------------------------------------------------------------------
111
    float TexCoordModifierControllerValue::getValue() const
112
0
    {
113
0
        const Matrix4& pMat = mTextureLayer->getTextureTransform();
114
0
        if (mTransU)
115
0
        {
116
0
            return pMat[0][3];
117
0
        }
118
0
        else if (mTransV)
119
0
        {
120
0
            return pMat[1][3];
121
0
        }
122
0
        else if (mScaleU)
123
0
        {
124
0
            return pMat[0][0];
125
0
        }
126
0
        else if (mScaleV)
127
0
        {
128
0
            return pMat[1][1];
129
0
        }
130
        // Shouldn't get here
131
0
        return 0;
132
0
    }
133
    //-----------------------------------------------------------------------
134
    void TexCoordModifierControllerValue::setValue(float value)
135
0
    {
136
0
        if (mTransU)
137
0
        {
138
0
            mTextureLayer->setTextureUScroll(value);
139
0
        }
140
0
        if (mTransV)
141
0
        {
142
0
            mTextureLayer->setTextureVScroll(value);
143
0
        }
144
0
        if (mScaleU)
145
0
        {
146
0
            mTextureLayer->setTextureUScale(value);
147
0
        }
148
0
        if (mScaleV)
149
0
        {
150
0
            mTextureLayer->setTextureVScale(value);
151
0
        }
152
0
        if (mRotate)
153
0
        {
154
0
            mTextureLayer->setTextureRotate(Radian(value * Math::TWO_PI));
155
0
        }
156
0
    }
157
    //-----------------------------------------------------------------------
158
    //-----------------------------------------------------------------------
159
    FloatGpuParameterControllerValue::FloatGpuParameterControllerValue(
160
            const GpuProgramParametersSharedPtr& params, size_t index) :
161
0
        mParams(params), mParamIndex(index)
162
0
    {
163
0
    }
164
    //-----------------------------------------------------------------------
165
    float FloatGpuParameterControllerValue::getValue(void) const
166
0
    {
167
        // do nothing, reading from a set of params not supported
168
0
        return 0.0f;
169
0
    }
170
    //-----------------------------------------------------------------------
171
    void FloatGpuParameterControllerValue::setValue(float val)
172
0
    {
173
0
        Vector4 v4 = Vector4(0,0,0,0);
174
0
        v4.x = val;
175
0
        mParams->setConstant(mParamIndex, v4);
176
0
    }
177
    //-----------------------------------------------------------------------
178
    // PassthroughControllerFunction
179
    //-----------------------------------------------------------------------
180
    PassthroughControllerFunction::PassthroughControllerFunction(bool delta) 
181
0
        : ControllerFunction<float>(delta)
182
0
    {
183
0
    }
184
    //-----------------------------------------------------------------------
185
    float PassthroughControllerFunction::calculate(float source)
186
0
    {
187
0
        return getAdjustedInput(source);
188
189
0
    }
190
    //-----------------------------------------------------------------------
191
    // AnimationControllerFunction
192
    //-----------------------------------------------------------------------
193
    AnimationControllerFunction::AnimationControllerFunction(Real sequenceTime, Real timeOffset) 
194
0
        : ControllerFunction<float>(false)
195
0
    {
196
0
        mSeqTime = sequenceTime;
197
0
        mTime = timeOffset;
198
0
    }
199
    //-----------------------------------------------------------------------
200
    float AnimationControllerFunction::calculate(float source)
201
0
    {
202
        // Assume source is time since last update
203
0
        mTime += source;
204
        // Wrap
205
0
        while (mTime >= mSeqTime) mTime -= mSeqTime;
206
0
        while (mTime < 0) mTime += mSeqTime;
207
208
        // Return parametric
209
0
        return mTime / mSeqTime;
210
0
    }
211
    //-----------------------------------------------------------------------
212
    void AnimationControllerFunction::setTime(Real timeVal)
213
0
    {
214
0
        mTime = timeVal;
215
0
    }
216
    //-----------------------------------------------------------------------
217
    void AnimationControllerFunction::setSequenceTime(Real seqVal)
218
0
    {
219
0
        mSeqTime = seqVal;
220
0
    }
221
    //-----------------------------------------------------------------------
222
    // ScaleControllerFunction
223
    //-----------------------------------------------------------------------
224
0
    ScaleControllerFunction::ScaleControllerFunction(Real factor, bool delta) : ControllerFunction<float>(delta)
225
0
    {
226
0
        mScale = factor;
227
0
    }
228
    //-----------------------------------------------------------------------
229
    float ScaleControllerFunction::calculate(float source)
230
0
    {
231
0
        return getAdjustedInput(source * mScale);
232
233
0
    }
234
    //-----------------------------------------------------------------------
235
    // WaveformControllerFunction
236
    //-----------------------------------------------------------------------
237
    WaveformControllerFunction::WaveformControllerFunction(WaveformType wType, Real base,  Real frequency, Real phase, Real amplitude, bool delta, Real dutyCycle)
238
0
        :ControllerFunction<float>(delta)
239
0
    {
240
0
        mWaveType = wType;
241
0
        mBase = base;
242
0
        mFrequency = frequency;
243
0
        mPhase = phase;
244
0
        mAmplitude = amplitude;
245
0
        mDeltaCount = phase;
246
0
        mDutyCycle = dutyCycle;
247
0
    }
248
    //-----------------------------------------------------------------------
249
    Real WaveformControllerFunction::getAdjustedInput(Real input)
250
0
    {
251
0
        Real adjusted = ControllerFunction<float>::getAdjustedInput(input);
252
253
        // If not delta, adjust by phase here
254
        // (delta inputs have it adjusted at initialisation)
255
0
        if (!mDeltaInput)
256
0
        {
257
0
            adjusted += mPhase;
258
0
        }
259
260
0
        return adjusted;
261
0
    }
262
    //-----------------------------------------------------------------------
263
    float WaveformControllerFunction::calculate(float source)
264
0
    {
265
0
        Real input = getAdjustedInput(source * mFrequency);
266
0
        Real output = 0;
267
        // For simplicity, factor input down to {0,1)
268
0
        input = std::fmod(input, Real(1));
269
0
        if(input < 0) input += 1;
270
271
        // Calculate output in -1..1 range
272
0
        switch (mWaveType)
273
0
        {
274
0
        case WFT_SINE:
275
0
            output = Math::Sin(Radian(input * Math::TWO_PI));
276
0
            break;
277
0
        case WFT_TRIANGLE:
278
0
            if (input < 0.25)
279
0
                output = input * 4;
280
0
            else if (input >= 0.25 && input < 0.75)
281
0
                output = 1.0f - ((input - 0.25f) * 4.0f);
282
0
            else
283
0
                output = ((input - 0.75f) * 4.0f) - 1.0f;
284
285
0
            break;
286
0
        case WFT_SQUARE:
287
0
            if (input <= 0.5f)
288
0
                output = 1.0f;
289
0
            else
290
0
                output = -1.0f;
291
0
            break;
292
0
        case WFT_SAWTOOTH:
293
0
            output = (input * 2.0f) - 1.0f;
294
0
            break;
295
0
        case WFT_INVERSE_SAWTOOTH:
296
0
            output = -((input * 2.0f) - 1.0f);
297
0
            break;
298
0
        case WFT_PWM:
299
0
            if( input <= mDutyCycle )
300
0
                output = 1.0f;
301
0
            else
302
0
                output = -1.0f;
303
0
            break;
304
0
        }
305
306
        // Scale output into 0..1 range and then by base + amplitude
307
0
        return mBase + ((output + 1.0f) * 0.5f * mAmplitude);
308
309
310
0
    }
311
    //-----------------------------------------------------------------------
312
    // LinearControllerFunction
313
    //-----------------------------------------------------------------------
314
    LinearControllerFunction::LinearControllerFunction(const std::vector<Real>& keys, const std::vector<Real>& values, Real frequency, bool deltaInput) :
315
0
            ControllerFunction<float>(deltaInput), mFrequency(frequency), mKeys(keys), mValues(values) {
316
0
        assert(mKeys.size() == mValues.size());
317
0
    }
318
    //-----------------------------------------------------------------------
319
0
    float LinearControllerFunction::calculate(float source) {
320
0
        Real input = getAdjustedInput(source*mFrequency);
321
322
0
        std::vector<Real>::iterator ifirst = std::lower_bound(mKeys.begin(), mKeys.end(), input);
323
0
        size_t idx = ifirst - mKeys.begin() - 1;
324
325
0
        assert(ifirst != mKeys.end());
326
327
0
        Real alpha = Math::inverseLerp(mKeys[idx], mKeys[idx + 1], input);
328
0
        return Math::lerp(mValues[idx], mValues[idx + 1], alpha);
329
0
    }
330
}
331