- First, create a new Ogre MFC application named
NumericValueAnimation
, by following the Creating an MFC Ogre application recipe
from Chapter 1. - Create a light in
CNumericValueAnimation::EngineSetup
namedAnimatedLight
.Ogre::NumericKeyFrame* kf; Ogre::Light *Light = SceneManager->createLight("AnimatedLight");
- Next, create an
AnimableValuePtr
for the named valuediffuseColour
, and set the initial value.Ogre::AnimableValuePtr animableValue = Light-> createAnimableValue("diffuseColour"); animableValue->setValue(lightInitialColor); animableValue->setCurrentStateAsBaseValue ();
- Next, create an animation named
AnimateValue
with a duration of5
seconds.Ogre::Animation *Animation = SceneManager-> createAnimation("AnimateValue", 5);
- Create a
NumericAnimationTrack
, and associate it withAnimableValue
that we created.Ogre::NumericAnimationTrack *track = Animation-> createNumericTrack(0, animableValue);
- Next, create key frames for each time index, and set the values for each key frame to a varying color of red.
float colourValue = 0.0; for(int i = 1; i <= 1000; i++) { kf = track->createNumericKeyFrame(i * 0.025); colourValue = colourValue + 0.01; kf->setValue( Ogre::AnyNumeric(Ogre::ColourValue (colourValue, 0.0, 0.0, 1.0)) ); }
- Start a timer, and add a timer event handler named
OnTimer
. In the timer function, get the value from the nextNumericKeyFrame
,
and set the background color of the viewport to the same color value as the light.Ogre::Animation *Animation = SceneManager-> getAnimation("AnimateValue"); Ogre::NumericAnimationTrack *track = Animation-> getNumericTrack(0); Ogre::NumericKeyFrame *frame = track-> getNumericKeyFrame(m_TimeIndex++); Ogre::AnyNumeric value = frame->getValue(); Ogre::ColourValue color = Ogre::any_cast <Ogre::ColourValue>(value); Viewport->setBackgroundColour(color); Root->renderOneFrame();
The attribute we change in this recipe is the light diffuse color. When we create the key frames, we store a new color in each key frame. The key frame setValue()
function only accepts a value of type AnyNumeric
,
so we must cast our color value to this type first. Then, when the timer function is run, we get the value for the current key frame, and set the background color with that value. The starting background color starts out black and ends bright red.