Time for action — adding compositors

Having almost finished our application, we are going to add compositors to make the application more interesting.

  1. We are going to use compositors in our FrameListener, so we need a member variable containing the viewport:
    Ogre::Viewport* _viewport;
    
  2. We also are going to need to save which compositor is turned on; add three Booleans for this task:
    bool _comp1, _comp2, _comp3;
    
  3. We are going to use keyboard input to switch the compositors on and off. To be able to differentiate between key presses, we need to know the previous state of the key:
    bool _down1, _down2, _down3;
    
  4. Change the constructor of the FrameListener to take the viewport as a parameter:
    MyFrameListener(Ogre::RenderWindow* win,Ogre::Camera* cam,Ogre::Viewport* viewport)
    
  5. Assign the viewport pointer to the member and assign the Boolean value their starting value:
    _viewport = viewport;
    _comp1 = false;
    _comp2 = false;
    _comp3 = false;
    _down1 = false;
    _down2 = false;
    _down3 = false;
    
  6. If the key number 1 is pressed and it wasn't pressed before, change the state of the key to pressed, flip the state of the compositor, and use the flipped value to enable or disable the compositor. This code goes into the frameStarted function:
    if(_Keyboard->isKeyDown(OIS::KC_1) && ! _down1)
    {
    _down1 = true;
    _comp1 = !comp1;
    Ogre::CompositorManager::getSingleton().setCompositorEnabled(_viewport, "Compositor2", _comp1);
    }
    
  7. Do the same for the other two compositors we are going to have:
    if(_Keyboard->isKeyDown(OIS::KC_2) && ! _down2)
    {
    _down2 = true;
    _comp2 = !comp2;
    Ogre::CompositorManager::getSingleton().setCompositorEnabled(_viewport, "Compositor3", _comp2);
    }
    if(_Keyboard->isKeyDown(OIS::KC_3) && ! _down3)
    {
    _down3 = true;
    _comp3 = !comp3;
    Ogre::CompositorManager::getSingleton().setCompositorEnabled(_viewport, "Compositor7", _comp3);
    }
    
  8. If a key is no longer pressed, we need to change the state of the key:
    if(!_Keyboard->isKeyDown(OIS::KC_1))
    {
    _down1 = false;
    }
    if(!_Keyboard->isKeyDown(OIS::KC_2))
    {
    _down2 = false;
    }
    if(!_Keyboard->isKeyDown(OIS::KC_3))
    {
    _down3 = false;
    }
    
  9. In the startup() function, add the three compositors to the viewport to the end of the function:
    Ogre::CompositorManager::getSingleton().addCompositor(viewport, "Compositor2");
    Ogre::CompositorManager::getSingleton().addCompositor(viewport, "Compositor3");
    Ogre::CompositorManager::getSingleton().addCompositor(viewport, "Compositor7");
    
  10. Remember to change the instantiation of the FrameListener to add the viewport pointer as parameter:
    _listener = new MyFrameListener(window,camera,viewport);
    
  11. Compile and run the application. Using the 1, 2, 3 keys, you should be able to turn different compositors on and off. The 1 key is for making the image black and white, the 2 key inverts the image, and the 3 key makes the image look like it has a smaller resolution; you can combine all of the effect the way you like:
    Time for action — adding compositors

What just happened?

We added the compositors we wrote in the chapter about and made it possible to turn them on and off using the 1, 2, and 3 keys. To combine the compositors, we used the fact that Ogre 3D automatically chains compositors if more than one is enabled.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.157.106