經(jīng)過前兩兩篇博文的講解,我們已經(jīng)完成了渲染工作,但只是渲染而沒有交互性,本篇博文我們就來加上事件的處理方法。 首先我們需要為項(xiàng)目添加一個幀監(jiān)聽類:CMyFrameListener,為了直觀,在這直接貼上代碼 頭文件 - #pragma once
- #include "ogre.h"
- #include "OgreConfigFile.h"
- #include "OgreFrameListener.h"
- #include "OgreStringConverter.h"
- #include "OIS.h"
-
- #include "MyOgreApp.h"
- #include <OISEvents.h>
- #include <OISInputManager.h>
- #include <OISKeyboard.h>
- #include <OISMouse.h>
-
- #include <SdkTrays.h>
- #include <SdkCameraMan.h>
- using namespace Ogre;
- class CMyFrameListener: public FrameListener, public OIS::MouseListener,public Ogre::WindowEventListener, public OIS::KeyListener
- {
- public:
- CMyFrameListener(RenderWindow* win, Camera* cam, SceneManager *sceneMgr, Light* light);
- ~CMyFrameListener(void);
- bool frameRenderingQueued(const Ogre::FrameEvent& evt);
- bool mouseMoved(const OIS::MouseEvent &e);
- bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id);
- bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id);
- bool keyPressed(const OIS::KeyEvent &e);
- bool keyReleased(const OIS::KeyEvent &e);
-
- protected:
-
- Ogre::SceneManager *mSceneMgr;
- Ogre::SceneNode* mCamNode;
- Ogre::Camera* mCamera;
- Ogre::RenderWindow* mWindow;
- Ogre::Light* light;
-
- OIS::Keyboard* mKeyboard;
- OIS::Mouse* mMouse;
- OIS::InputManager* mInputManager;
-
-
- OgreBites::SdkCameraMan* mCameraMan;
- bool mRBtdown;
- };
源文件- #include "StdAfx.h"
- #include "MyFrameListener.h"
-
- CMyFrameListener::CMyFrameListener(RenderWindow* win, Camera* cam, SceneManager *sceneMgr, Light *l): mMouse(0),
- mKeyboard(0), mInputManager(0), mWindow(win), mCamera(cam), light(l)
- {
- mRBtdown = false;
- mCamNode=cam->getParentSceneNode();
- mSceneMgr = sceneMgr;
-
-
- size_t windowHnd = 0;
- std::ostringstream windowHndStr;
- OIS::ParamList pl;
-
-
- mCameraMan = new OgreBites::SdkCameraMan(mCamera);
-
-
- windowHnd = (size_t )AfxGetMainWnd()->GetSafeHwnd();
- windowHndStr << windowHnd;
-
- pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
-
-
-
- pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND" )));
- pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE")));
-
- pl.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_FOREGROUND")));
- pl.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_NONEXCLUSIVE")));
- mInputManager = OIS::InputManager::createInputSystem(pl);
-
-
- mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, true ));
- mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject( OIS::OISMouse, true ));
-
-
- mMouse->setEventCallback(this);
- mKeyboard->setEventCallback(this);
- }
-
-
-
-
- CMyFrameListener::~CMyFrameListener(void)
- {
- mInputManager->destroyInputObject(mMouse);
- mInputManager->destroyInputObject(mKeyboard);
- OIS::InputManager::destroyInputSystem(mInputManager);
- mInputManager = 0;
- }
-
-
-
-
- bool CMyFrameListener::frameRenderingQueued(const Ogre::FrameEvent& e){
-
-
- mMouse->capture();
- mKeyboard->capture();
- mCameraMan->frameRenderingQueued(e);
- return true;
- }
-
-
- bool CMyFrameListener::mouseMoved(const OIS::MouseEvent &e)
- {
- if (mRBtdown)
- {
- mCameraMan->injectMouseMove(e);
- }
-
- return true;
- }
-
-
- bool CMyFrameListener::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
- {
-
- if (id == OIS::MB_Right)
- {
- mRBtdown = true;
- }
-
- return true;
- }
-
-
- bool CMyFrameListener::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
- {
-
-
-
- if (id == OIS::MB_Right)
- {
- mRBtdown = false;
- }
- return true;
- }
-
- bool CMyFrameListener::keyPressed(const OIS::KeyEvent &e)
- {
-
- mCameraMan->injectKeyDown(e);
-
-
- return true;
- }
-
-
- bool CMyFrameListener::keyReleased(const OIS::KeyEvent &e)
- {
- mCameraMan->injectKeyUp(e);
- return true;
- }
此類實(shí)現(xiàn)了對于鼠標(biāo)和鍵盤事件的監(jiān)聽與響應(yīng)。按住鼠標(biāo)右鍵拖動可旋轉(zhuǎn)攝像機(jī)角度,WASD和方向鍵可移動攝像機(jī)位置。里面的代碼都很簡單,我就不再贅述了。下面修改CMyOgreApp類其能夠響應(yīng)鼠標(biāo)和鍵盤的事件。 1、首先在MyOgreApp.h中添加監(jiān)聽類的引用 - #include "MyFrameListener.h"
2、聲明一個CMyFrameListener類的變量- class CMyFrameListener* mListener;
3、聲明一個函數(shù) - void createFrameListener(void);
4、在MyOgreApp.cpp文件中定義createFrameListener()- void CMyOgreApp::createFrameListener(void)
- {
- mListener= new CMyFrameListener(mWindow, mCamera, mSceneMgr, light);
- mRoot->addFrameListener(mListener);
- }
5、在go() 函數(shù)中調(diào)用createFrameListener()- bool CMyOgreApp::go(CRect rt, HWND hWnd)
- {
- createRoot();
- setupResources();
- setupRenderSystem();
- createRenderWindow(hWnd, rt.Width(), rt.Height());
- chooseSceneManager();
- createCamera();
- createViewport();
- initializeResourceGroups();
- createScene();
- createFrameListener();
- return true;
- }
生成并運(yùn)行,使用鼠標(biāo)和鍵盤就可以控制攝像機(jī)運(yùn)動了。至此,整個工作就完成了,希望這幾篇文章能幫到還在受這個問題困擾的朋友。 PS:整個項(xiàng)目都在我上傳的資源中,配置好環(huán)境變量,再按照里面的說明簡單配置下就能編譯運(yùn)行 Demo地址:http://download.csdn.net/detail/guoyk1990/7360731
原文:http://blog.csdn.net/guoyk1990/article/details/26060353
|