小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Ogre嵌入MFC傻瓜完全教程(三)

 藍(lán)色飄零 2015-05-05

經(jīng)過(guò)前兩兩篇博文的講解,我們已經(jīng)完成了渲染工作,但只是渲染而沒(méi)有交互性,本篇博文我們就來(lái)加上事件的處理方法。

首先我們需要為項(xiàng)目添加一個(gè)幀監(jiān)聽(tīng)類(lèi):CMyFrameListener,為了直觀,在這直接貼上代碼

頭文件

  1. #pragma once  
  2. #include "ogre.h"  
  3. #include "OgreConfigFile.h"  
  4. #include "OgreFrameListener.h"  
  5. #include "OgreStringConverter.h"  
  6. #include "OIS.h"  
  7.   
  8. #include "MyOgreApp.h"  
  9. #include <OISEvents.h>  
  10. #include <OISInputManager.h>  
  11. #include <OISKeyboard.h>  
  12. #include <OISMouse.h>  
  13.   
  14. #include <SdkTrays.h>  
  15. #include <SdkCameraMan.h>  
  16. using namespace Ogre;  
  17. class CMyFrameListener: public FrameListener, public OIS::MouseListener,public Ogre::WindowEventListener, public OIS::KeyListener  
  18. {  
  19. public:  
  20.     CMyFrameListener(RenderWindow* win, Camera* cam, SceneManager *sceneMgr, Light* light);  
  21.     ~CMyFrameListener(void);  
  22.     bool frameRenderingQueued(const Ogre::FrameEvent& evt);  
  23.     bool mouseMoved(const OIS::MouseEvent &e);  
  24.     bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id);   
  25.     bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id);  
  26.     bool keyPressed(const OIS::KeyEvent &e);  
  27.     bool keyReleased(const OIS::KeyEvent &e);  
  28.   
  29. protected:  
  30.   
  31.     Ogre::SceneManager *mSceneMgr;//場(chǎng)景管理器  
  32.     Ogre::SceneNode* mCamNode;//攝像機(jī)節(jié)點(diǎn)  
  33.     Ogre::Camera* mCamera;//攝像機(jī)  
  34.     Ogre::RenderWindow* mWindow;//渲染窗口  
  35.     Ogre::Light* light;//燈光  
  36.   
  37.     OIS::Keyboard* mKeyboard;//鍵盤(pán)           
  38.     OIS::Mouse* mMouse;      //鼠標(biāo)      
  39.     OIS::InputManager* mInputManager;//輸入管理器   
  40.   
  41. //  OgreBites::SdkTrayManager* mTrayMgr;  
  42.     OgreBites::SdkCameraMan* mCameraMan;     // basic camera controller  
  43.     bool mRBtdown;  
  44. };  
源文件

  1. #include "StdAfx.h"  
  2. #include "MyFrameListener.h"  
  3.   
  4. CMyFrameListener::CMyFrameListener(RenderWindow* win, Camera* cam, SceneManager *sceneMgr, Light *l): mMouse(0),   
  5.     mKeyboard(0), mInputManager(0), mWindow(win), mCamera(cam), light(l)  
  6. {  
  7.     mRBtdown = false;  
  8.     mCamNode=cam->getParentSceneNode();  
  9.     mSceneMgr = sceneMgr;  
  10.   
  11.   
  12.     size_t windowHnd = 0;  
  13.     std::ostringstream windowHndStr;  
  14.     OIS::ParamList pl;  
  15.   
  16.   
  17.     mCameraMan = new OgreBites::SdkCameraMan(mCamera);    
  18.   
  19.   
  20.     windowHnd = (size_t )AfxGetMainWnd()->GetSafeHwnd(); // 這里這個(gè)窗口句柄就是傳入的MFC主窗口  
  21.     windowHndStr << windowHnd;  
  22.     // OIS的窗口必須要頂層窗口,所以只有傳MFC的主窗口給他,傳view就不行  
  23.     pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));  
  24.   
  25.   
  26.     // 設(shè)置鼠標(biāo)顯示和非游戲獨(dú)占,這樣鼠標(biāo)可以顯示在屏幕上并可以移動(dòng)到窗口外  
  27.     pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND" )));  
  28.     pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE")));  
  29.     // 鍵盤(pán)非游戲獨(dú)占  
  30.     pl.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_FOREGROUND")));  
  31.     pl.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_NONEXCLUSIVE")));  
  32.     mInputManager = OIS::InputManager::createInputSystem(pl);  
  33.   
  34.   
  35.     mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, true ));  
  36.     mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject( OIS::OISMouse, true ));  
  37.   
  38.   
  39.     mMouse->setEventCallback(this);  
  40.     mKeyboard->setEventCallback(this);  
  41. }  
  42.   
  43.   
  44.   
  45.   
  46. CMyFrameListener::~CMyFrameListener(void)  
  47. {  
  48.     mInputManager->destroyInputObject(mMouse);  
  49.     mInputManager->destroyInputObject(mKeyboard);  
  50.     OIS::InputManager::destroyInputSystem(mInputManager);  
  51.     mInputManager = 0;  
  52. }  
  53.   
  54.   
  55.   
  56.   
  57. bool CMyFrameListener::frameRenderingQueued(const Ogre::FrameEvent& e){  
  58.   
  59.   
  60.     mMouse->capture();  
  61.     mKeyboard->capture();  
  62.     mCameraMan->frameRenderingQueued(e);   
  63.     return true;  
  64. }  
  65.   
  66.   
  67. bool CMyFrameListener::mouseMoved(const OIS::MouseEvent &e)  
  68. {  
  69.     if (mRBtdown)  
  70.     {  
  71.         mCameraMan->injectMouseMove(e);  
  72.     }  
  73.       
  74.     return true;  
  75. }  
  76.   
  77.   
  78. bool CMyFrameListener::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)  
  79. {  
  80. //  mCameraMan->injectMouseDown(e, id);  
  81.     if (id == OIS::MB_Right)  
  82.     {  
  83.         mRBtdown = true;  
  84.     }  
  85.       
  86.     return true;  
  87. }  
  88.   
  89.   
  90. bool CMyFrameListener::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)  
  91. {  
  92.   
  93.   
  94. //  mCameraMan->injectMouseUp(e, id);  
  95.     if (id == OIS::MB_Right)  
  96.     {  
  97.         mRBtdown = false;  
  98.     }  
  99.     return true;  
  100. }  
  101. //鍵盤(pán)響應(yīng)  
  102. bool CMyFrameListener::keyPressed(const OIS::KeyEvent &e)  
  103. {  
  104.       
  105.     mCameraMan->injectKeyDown(e);  
  106.   
  107.   
  108.     return true;  
  109. }  
  110.   
  111.   
  112. bool CMyFrameListener::keyReleased(const OIS::KeyEvent &e)  
  113. {  
  114.      mCameraMan->injectKeyUp(e);  
  115.     return true;  
  116. }  

此類(lèi)實(shí)現(xiàn)了對(duì)于鼠標(biāo)和鍵盤(pán)事件的監(jiān)聽(tīng)與響應(yīng)。按住鼠標(biāo)右鍵拖動(dòng)可旋轉(zhuǎn)攝像機(jī)角度,WASD和方向鍵可移動(dòng)攝像機(jī)位置。里面的代碼都很簡(jiǎn)單,我就不再贅述了。

下面修改CMyOgreApp類(lèi)其能夠響應(yīng)鼠標(biāo)和鍵盤(pán)的事件。

1、首先在MyOgreApp.h中添加監(jiān)聽(tīng)類(lèi)的引用

  1. #include "MyFrameListener.h"  


2、聲明一個(gè)CMyFrameListener類(lèi)的變量

  1. class CMyFrameListener* mListener;  

3、聲明一個(gè)函數(shù) 

  1. void createFrameListener(void);  

4、在MyOgreApp.cpp文件中定義createFrameListener()

  1. void CMyOgreApp::createFrameListener(void)  
  2. {  
  3.     mListener= new CMyFrameListener(mWindow, mCamera, mSceneMgr, light);  
  4.     mRoot->addFrameListener(mListener);  
  5. }  
5、在go() 函數(shù)中調(diào)用createFrameListener()

  1. bool CMyOgreApp::go(CRect rt, HWND hWnd)  
  2. {  
  3.     createRoot();  
  4.     setupResources();  
  5.     setupRenderSystem();  
  6.     createRenderWindow(hWnd, rt.Width(), rt.Height());  
  7.     chooseSceneManager();  
  8.     createCamera();  
  9.     createViewport();  
  10.     initializeResourceGroups();  
  11.     createScene();  
  12.     createFrameListener();//創(chuàng)建偵聽(tīng)  
  13.     return true;  
  14. }  
生成并運(yùn)行,使用鼠標(biāo)和鍵盤(pán)就可以控制攝像機(jī)運(yùn)動(dòng)了。

至此,整個(gè)工作就完成了,希望這幾篇文章能幫到還在受這個(gè)問(wèn)題困擾的朋友。
 PS:整個(gè)項(xiàng)目都在我上傳的資源中,配置好環(huán)境變量,再按照里面的說(shuō)明簡(jiǎn)單配置下就能編譯運(yùn)行

Demo地址:http://download.csdn.net/detail/guoyk1990/7360731

原文:http://blog.csdn.net/guoyk1990/article/details/26060353

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多