YourPaste - For your paste! Archive - Tools - Login

Posted by unknown on Mon 22 Feb 2010 0:46 187 views - Syntax: C++ - Expires: never - Report - IMG - Download -

  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-2006 Torus Knot Software Ltd
  8. Also see acknowledgements in Readme.html
  9.  
  10. You may use this sample code for anything you like, it is not covered by the
  11. LGPL like the rest of the engine.
  12. -----------------------------------------------------------------------------
  13. */
  14. /*
  15. -----------------------------------------------------------------------------
  16. Filename:    ExampleFrameListener.h
  17. Description: Defines an example frame listener which responds to frame events.
  18. This frame listener just moves a specified camera around based on
  19. keyboard and mouse movements.
  20. Mouse:    Freelook
  21. W or Up:  Forward
  22. S or Down:Backward
  23. A:        Step left
  24. D:        Step right
  25.              PgUp:     Move upwards
  26.              PgDown:   Move downwards
  27.              F:        Toggle frame rate stats on/off
  28.                          R:        Render mode
  29.              T:        Cycle texture filtering
  30.                        Bilinear, Trilinear, Anisotropic(8)
  31.              P:        Toggle on/off display of camera position / orientation
  32. -----------------------------------------------------------------------------
  33. */
  34.  
  35. #ifndef __ExampleFrameListener_H__
  36. #define __ExampleFrameListener_H__
  37.  
  38. #include "Ogre.h"
  39. #include "OgreStringConverter.h"
  40. #include "OgreException.h"
  41.  
  42. //Use this define to signify OIS will be used as a DLL
  43. //(so that dll import/export macros are in effect)
  44. #define OIS_DYNAMIC_LIB
  45. #include <OIS/OIS.h>
  46.  
  47. using namespace Ogre;
  48.  
  49. class ExampleFrameListener: public FrameListener, public WindowEventListener
  50. {
  51. protected:
  52.         virtual void updateStats(void)
  53.         {
  54.                 static String currFps = "Current FPS: ";
  55.                 static String avgFps = "Average FPS: ";
  56.                 static String bestFps = "Best FPS: ";
  57.                 static String worstFps = "Worst FPS: ";
  58.                 static String tris = "Triangle Count: ";
  59.                 static String batches = "Batch Count: ";
  60.  
  61.                 // update stats when necessary
  62.                 try {
  63.                         OverlayElement* guiAvg = OverlayManager::getSingleton().getOverlayElement("Core/AverageFps");
  64.                         OverlayElement* guiCurr = OverlayManager::getSingleton().getOverlayElement("Core/CurrFps");
  65.                         OverlayElement* guiBest = OverlayManager::getSingleton().getOverlayElement("Core/BestFps");
  66.                         OverlayElement* guiWorst = OverlayManager::getSingleton().getOverlayElement("Core/WorstFps");
  67.  
  68.                         const RenderTarget::FrameStats& stats = mWindow->getStatistics();
  69.                         guiAvg->setCaption(avgFps + StringConverter::toString(stats.avgFPS));
  70.                         guiCurr->setCaption(currFps + StringConverter::toString(stats.lastFPS));
  71.                         guiBest->setCaption(bestFps + StringConverter::toString(stats.bestFPS)
  72.                                 +" "+StringConverter::toString(stats.bestFrameTime)+" ms");
  73.                         guiWorst->setCaption(worstFps + StringConverter::toString(stats.worstFPS)
  74.                                 +" "+StringConverter::toString(stats.worstFrameTime)+" ms");
  75.  
  76.                         OverlayElement* guiTris = OverlayManager::getSingleton().getOverlayElement("Core/NumTris");
  77.                         guiTris->setCaption(tris + StringConverter::toString(stats.triangleCount));
  78.  
  79.                         OverlayElement* guiBatches = OverlayManager::getSingleton().getOverlayElement("Core/NumBatches");
  80.                         guiBatches->setCaption(batches + StringConverter::toString(stats.batchCount));
  81.  
  82.                         OverlayElement* guiDbg = OverlayManager::getSingleton().getOverlayElement("Core/DebugText");
  83.                         guiDbg->setCaption(mDebugText);
  84.                 }
  85.                 catch(...) { /* ignore */ }
  86.         }
  87.  
  88. public:
  89.         // Constructor takes a RenderWindow because it uses that to determine input context
  90.         ExampleFrameListener(RenderWindow* win, Camera* cam, bool bufferedKeys = false, bool bufferedMouse = false,
  91.                              bool bufferedJoy = false ) :
  92.                 mCamera(cam), mTranslateVector(Vector3::ZERO), mCurrentSpeed(0), mWindow(win), mStatsOn(true), mNumScreenShots(0),
  93.                 mMoveScale(0.0f), mRotScale(0.0f), mTimeUntilNextToggle(0), mFiltering(TFO_BILINEAR),
  94.                 mAniso(1), mSceneDetailIndex(0), mMoveSpeed(100), mRotateSpeed(36), mDebugOverlay(0),
  95.                 mInputManager(0), mMouse(0), mKeyboard(0), mJoy(0)
  96.         {
  97.  
  98.                 mDebugOverlay = OverlayManager::getSingleton().getByName("Core/DebugOverlay");
  99.  
  100.                 LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
  101.                 OIS::ParamList pl;
  102.                 size_t windowHnd = 0;
  103.                 std::ostringstream windowHndStr;
  104.  
  105.                 win->getCustomAttribute("WINDOW", &windowHnd);
  106.                 windowHndStr << windowHnd;
  107.                 pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
  108.  
  109.                 mInputManager = OIS::InputManager::createInputSystem( pl );
  110.  
  111.                 //Create all devices (We only catch joystick exceptions here, as, most people have Key/Mouse)
  112.                 mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, bufferedKeys ));
  113.                 mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject( OIS::OISMouse, bufferedMouse ));
  114.                 try {
  115.                         mJoy = static_cast<OIS::JoyStick*>(mInputManager->createInputObject( OIS::OISJoyStick, bufferedJoy ));
  116.                 }
  117.                 catch(...) {
  118.                         mJoy = 0;
  119.                 }
  120.  
  121.                 //Set initial mouse clipping size
  122.                 windowResized(mWindow);
  123.  
  124.                 showDebugOverlay(true);
  125.  
  126.                 //Register as a Window listener
  127.                 WindowEventUtilities::addWindowEventListener(mWindow, this);
  128.         }
  129.  
  130.         //Adjust mouse clipping area
  131.         virtual void windowResized(RenderWindow* rw)
  132.         {
  133.                 unsigned int width, height, depth;
  134.                 int left, top;
  135.                 rw->getMetrics(width, height, depth, left, top);
  136.  
  137.                 const OIS::MouseState &ms = mMouse->getMouseState();
  138.                 ms.width = width;
  139.                 ms.height = height;
  140.         }
  141.  
  142.         //Unattach OIS before window shutdown (very important under Linux)
  143.         virtual void windowClosed(RenderWindow* rw)
  144.         {
  145.                 //Only close for window that created OIS (the main window in these demos)
  146.                 if( rw == mWindow )
  147.                 {
  148.                         if( mInputManager )
  149.                         {
  150.                                 mInputManager->destroyInputObject( mMouse );
  151.                                 mInputManager->destroyInputObject( mKeyboard );
  152.                                 mInputManager->destroyInputObject( mJoy );
  153.  
  154.                                 OIS::InputManager::destroyInputSystem(mInputManager);
  155.                                 mInputManager = 0;
  156.                         }
  157.                 }
  158.         }
  159.  
  160.         virtual ~ExampleFrameListener()
  161.         {
  162.                 //Remove ourself as a Window listener
  163.                 WindowEventUtilities::removeWindowEventListener(mWindow, this);
  164.                 windowClosed(mWindow);
  165.         }
  166.  
  167.         virtual bool processUnbufferedKeyInput(const FrameEvent& evt)
  168.         {
  169.  
  170.                 if(mKeyboard->isKeyDown(OIS::KC_A))
  171.                         mTranslateVector.x = -mMoveScale;       // Move camera left
  172.  
  173.                 if(mKeyboard->isKeyDown(OIS::KC_D))
  174.                         mTranslateVector.x = mMoveScale;        // Move camera RIGHT
  175.  
  176.                 if(mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W) )
  177.                         mTranslateVector.z = -mMoveScale;       // Move camera forward
  178.  
  179.                 if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S) )
  180.                         mTranslateVector.z = mMoveScale;        // Move camera backward
  181.  
  182.                 if(mKeyboard->isKeyDown(OIS::KC_PGUP))
  183.                         mTranslateVector.y = mMoveScale;        // Move camera up
  184.  
  185.                 if(mKeyboard->isKeyDown(OIS::KC_PGDOWN))
  186.                         mTranslateVector.y = -mMoveScale;       // Move camera down
  187.  
  188.                 if(mKeyboard->isKeyDown(OIS::KC_RIGHT))
  189.                         mCamera->yaw(-mRotScale);
  190.  
  191.                 if(mKeyboard->isKeyDown(OIS::KC_LEFT))
  192.                         mCamera->yaw(mRotScale);
  193.  
  194.                 if( mKeyboard->isKeyDown(OIS::KC_ESCAPE) || mKeyboard->isKeyDown(OIS::KC_Q) )
  195.                         return false;
  196.  
  197.         if( mKeyboard->isKeyDown(OIS::KC_F) && mTimeUntilNextToggle <= 0 )
  198.                 {
  199.                         mStatsOn = !mStatsOn;
  200.                         showDebugOverlay(mStatsOn);
  201.                         mTimeUntilNextToggle = 1;
  202.                 }
  203.  
  204.                 if( mKeyboard->isKeyDown(OIS::KC_T) && mTimeUntilNextToggle <= 0 )
  205.                 {
  206.                         switch(mFiltering)
  207.                         {
  208.                         case TFO_BILINEAR:
  209.                                 mFiltering = TFO_TRILINEAR;
  210.                                 mAniso = 1;
  211.                                 break;
  212.                         case TFO_TRILINEAR:
  213.                                 mFiltering = TFO_ANISOTROPIC;
  214.                                 mAniso = 8;
  215.                                 break;
  216.                         case TFO_ANISOTROPIC:
  217.                                 mFiltering = TFO_BILINEAR;
  218.                                 mAniso = 1;
  219.                                 break;
  220.                         default: break;
  221.                         }
  222.                         MaterialManager::getSingleton().setDefaultTextureFiltering(mFiltering);
  223.                         MaterialManager::getSingleton().setDefaultAnisotropy(mAniso);
  224.  
  225.                         showDebugOverlay(mStatsOn);
  226.                         mTimeUntilNextToggle = 1;
  227.                 }
  228.  
  229.                 if(mKeyboard->isKeyDown(OIS::KC_SYSRQ) && mTimeUntilNextToggle <= 0)
  230.                 {
  231.                         std::ostringstream ss;
  232.                         ss << "screenshot_" << ++mNumScreenShots << ".png";
  233.                         mWindow->writeContentsToFile(ss.str());
  234.                         mTimeUntilNextToggle = 0.5;
  235.                         mDebugText = "Saved: " + ss.str();
  236.                 }
  237.  
  238.                 if(mKeyboard->isKeyDown(OIS::KC_R) && mTimeUntilNextToggle <=0)
  239.                 {
  240.                         mSceneDetailIndex = (mSceneDetailIndex+1)%3 ;
  241.                         switch(mSceneDetailIndex) {
  242.                                 case 0 : mCamera->setPolygonMode(PM_SOLID); break;
  243.                                 case 1 : mCamera->setPolygonMode(PM_WIREFRAME); break;
  244.                                 case 2 : mCamera->setPolygonMode(PM_POINTS); break;
  245.                         }
  246.                         mTimeUntilNextToggle = 0.5;
  247.                 }
  248.  
  249.                 static bool displayCameraDetails = false;
  250.                 if(mKeyboard->isKeyDown(OIS::KC_P) && mTimeUntilNextToggle <= 0)
  251.                 {
  252.                         displayCameraDetails = !displayCameraDetails;
  253.                         mTimeUntilNextToggle = 0.5;
  254.                         if (!displayCameraDetails)
  255.                                 mDebugText = "";
  256.                 }
  257.  
  258.                 // Print camera details
  259.                 if(displayCameraDetails)
  260.                         mDebugText = "P: " + StringConverter::toString(mCamera->getDerivedPosition()) +
  261.                                                  " " + "O: " + StringConverter::toString(mCamera->getDerivedOrientation());
  262.  
  263.                 // Return true to continue rendering
  264.                 return true;
  265.         }
  266.  
  267.         virtual bool processUnbufferedMouseInput(const FrameEvent& evt)
  268.         {
  269.  
  270.                 // Rotation factors, may not be used if the second mouse button is pressed
  271.                 // 2nd mouse button - slide, otherwise rotate
  272.                 const OIS::MouseState &ms = mMouse->getMouseState();
  273.                 if( ms.buttonDown( OIS::MB_Right ) )
  274.                 {
  275.                         mTranslateVector.x += ms.X.rel * 0.13;
  276.                         mTranslateVector.y -= ms.Y.rel * 0.13;
  277.                 }
  278.                 else
  279.                 {
  280.                         mRotX = Degree(-ms.X.rel * 0.13);
  281.                         mRotY = Degree(-ms.Y.rel * 0.13);
  282.                 }
  283.  
  284.                 return true;
  285.         }
  286.  
  287.         virtual void moveCamera()
  288.         {
  289.                 // Make all the changes to the camera
  290.                 // Note that YAW direction is around a fixed axis (freelook style) rather than a natural YAW
  291.                 //(e.g. airplane)
  292.                 mCamera->yaw(mRotX);
  293.                 mCamera->pitch(mRotY);
  294.                 mCamera->moveRelative(mTranslateVector);
  295.         }
  296.  
  297.         virtual void showDebugOverlay(bool show)
  298.         {
  299.                 if (mDebugOverlay)
  300.                 {
  301.                         if (show)
  302.                                 mDebugOverlay->show();
  303.                         else
  304.                                 mDebugOverlay->hide();
  305.                 }
  306.         }
  307.  
  308.         // Override frameRenderingQueued event to process that (don't care about frameEnded)
  309.         bool frameRenderingQueued(const FrameEvent& evt)
  310.         {
  311.  
  312.                 if(mWindow->isClosed()) return false;
  313.  
  314.                 mSpeedLimit = mMoveScale * evt.timeSinceLastFrame;
  315.  
  316.                 //Need to capture/update each device
  317.                 mKeyboard->capture();
  318.                 mMouse->capture();
  319.                 if( mJoy ) mJoy->capture();
  320.  
  321.                 bool buffJ = (mJoy) ? mJoy->buffered() : true;
  322.  
  323.         Ogre::Vector3 lastMotion = mTranslateVector;
  324.  
  325.                 //Check if one of the devices is not buffered
  326.                 if( !mMouse->buffered() || !mKeyboard->buffered() || !buffJ )
  327.                 {
  328.                         // one of the input modes is immediate, so setup what is needed for immediate movement
  329.                         if (mTimeUntilNextToggle >= 0)
  330.                                 mTimeUntilNextToggle -= evt.timeSinceLastFrame;
  331.  
  332.                         // Move about 100 units per second
  333.                         mMoveScale = mMoveSpeed * evt.timeSinceLastFrame;
  334.                         // Take about 10 seconds for full rotation
  335.                         mRotScale = mRotateSpeed * evt.timeSinceLastFrame;
  336.  
  337.                         mRotX = 0;
  338.                         mRotY = 0;
  339.                         mTranslateVector = Ogre::Vector3::ZERO;
  340.  
  341.                 }
  342.  
  343.                 //Check to see which device is not buffered, and handle it
  344.                 if( !mKeyboard->buffered() )
  345.                         if( processUnbufferedKeyInput(evt) == false )
  346.                                 return false;
  347.                 if( !mMouse->buffered() )
  348.                         if( processUnbufferedMouseInput(evt) == false )
  349.                                 return false;
  350.  
  351.                 // ramp up / ramp down speed
  352.         if (mTranslateVector == Ogre::Vector3::ZERO)
  353.                 {
  354.                         // decay (one third speed)
  355.                         mCurrentSpeed -= evt.timeSinceLastFrame * 0.3;
  356.                         mTranslateVector = lastMotion;
  357.                 }
  358.                 else
  359.                 {
  360.                         // ramp up
  361.                         mCurrentSpeed += evt.timeSinceLastFrame;
  362.  
  363.                 }
  364.                 // Limit motion speed
  365.                 if (mCurrentSpeed > 1.0)
  366.                         mCurrentSpeed = 1.0;
  367.                 if (mCurrentSpeed < 0.0)
  368.                         mCurrentSpeed = 0.0;
  369.  
  370.                 mTranslateVector *= mCurrentSpeed;
  371.  
  372.  
  373.                 if( !mMouse->buffered() || !mKeyboard->buffered() || !buffJ )
  374.                         moveCamera();
  375.  
  376.                 return true;
  377.         }
  378.  
  379.         bool frameEnded(const FrameEvent& evt)
  380.         {
  381.                 updateStats();
  382.                 return true;
  383.         }
  384.  
  385. protected:
  386.         Camera* mCamera;
  387.  
  388.         Vector3 mTranslateVector;
  389.         Real mCurrentSpeed;
  390.         RenderWindow* mWindow;
  391.         bool mStatsOn;
  392.  
  393.         std::string mDebugText;
  394.  
  395.         unsigned int mNumScreenShots;
  396.         float mMoveScale;
  397.         float mSpeedLimit;
  398.         Degree mRotScale;
  399.         // just to stop toggles flipping too fast
  400.         Real mTimeUntilNextToggle ;
  401.         Radian mRotX, mRotY;
  402.         TextureFilterOptions mFiltering;
  403.         int mAniso;
  404.  
  405.         int mSceneDetailIndex ;
  406.         Real mMoveSpeed;
  407.         Degree mRotateSpeed;
  408.         Overlay* mDebugOverlay;
  409.  
  410.         //OIS Input devices
  411.         OIS::InputManager* mInputManager;
  412.         OIS::Mouse*    mMouse;
  413.         OIS::Keyboard* mKeyboard;
  414.         OIS::JoyStick* mJoy;
  415. };
  416.  
  417. #endif
  418.  

Comments


Name:
Comment:

© 2010 YourPaste.net - Disclaimer