經(jīng)過前兩兩篇博文的講解,我們已經(jīng)完成了渲染工作,但只是渲染而沒有交互性,本篇博文我們就來加上事件的處理方法。
首先我們需要為項(xiàng)目添加一個(gè)幀監(jiān)聽類:CMyFrameListener,為了直觀,在這直接貼上代碼
頭文件
[cpp]
view plaincopy#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;//場(chǎng)景管理器
Ogre::SceneNode* mCamNode;//攝像機(jī)節(jié)點(diǎn)
Ogre::Camera* mCamera;//攝像機(jī)
Ogre::RenderWindow* mWindow;//渲染窗口
Ogre::Light* light;//燈光
OIS::Keyboard* mKeyboard;//鍵盤
OIS::Mouse* mMouse; //鼠標(biāo)
OIS::InputManager* mInputManager;//輸入管理器
// OgreBites::SdkTrayManager* mTrayMgr;
OgreBites::SdkCameraMan* mCameraMan; // basic camera controller
bool mRBtdown;
};
源文件[cpp]
view plaincopy#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(); // 這里這個(gè)窗口句柄就是傳入的MFC主窗口
windowHndStr << windowHnd;
// OIS的窗口必須要頂層窗口,所以只有傳MFC的主窗口給他,傳view就不行
pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
// 設(shè)置鼠標(biāo)顯示和非游戲獨(dú)占,這樣鼠標(biāo)可以顯示在屏幕上并可以移動(dòng)到窗口外
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")));
// 鍵盤非游戲獨(dú)占
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)
{
// mCameraMan->injectMouseDown(e, id);
if (id == OIS::MB_Right)
{
mRBtdown = true;
}
return true;
}
bool CMyFrameListener::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
{
// mCameraMan->injectMouseUp(e, id);
if (id == OIS::MB_Right)
{
mRBtdown = false;
}
return true;
}
//鍵盤響應(yīng)
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)了對(duì)于鼠標(biāo)和鍵盤事件的監(jiān)聽與響應(yīng)。按住鼠標(biāo)右鍵拖動(dòng)可旋轉(zhuǎn)攝像機(jī)角度,WASD和方向鍵可移動(dòng)攝像機(jī)位置。里面的代碼都很簡(jiǎn)單,我就不再贅述了。下面修改CMyOgreApp類其能夠響應(yīng)鼠標(biāo)和鍵盤的事件。
1、首先在MyOgreApp.h中添加監(jiān)聽類的引用
[cpp]
view plaincopy#include "MyFrameListener.h"
2、聲明一個(gè)CMyFrameListener類的變量[cpp]
view plaincopyclass CMyFrameListener* mListener;
3、聲明一個(gè)函數(shù) [cpp]
view plaincopyvoid createFrameListener(void);
4、在MyOgreApp.cpp文件中定義createFrameListener()[cpp]
view plaincopyvoid CMyOgreApp::createFrameListener(void)
{
mListener= new CMyFrameListener(mWindow, mCamera, mSceneMgr, light);
mRoot->addFrameListener(mListener);
}
5、在go() 函數(shù)中調(diào)用createFrameListener()[cpp]
view plaincopybool CMyOgreApp::go(CRect rt, HWND hWnd)
{
createRoot();
setupResources();
setupRenderSystem();
createRenderWindow(hWnd, rt.Width(), rt.Height());
chooseSceneManager();
createCamera();
createViewport();
initializeResourceGroups();
createScene();
createFrameListener();//創(chuàng)建偵聽
return true;
}
生成并運(yùn)行,使用鼠標(biāo)和鍵盤就可以控制攝像機(jī)運(yùn)動(dòng)了。至此,整個(gè)工作就完成了,希望這幾篇文章能幫到還在受這個(gè)問題困擾的朋友。
PS:整個(gè)項(xiàng)目都在我上傳的資源中,配置好環(huán)境變量,再按照里面的說明簡(jiǎn)單配置下就能編譯運(yùn)行
Demo地址:http://download.csdn.net/detail/guoyk1990/7360731
原文:http://blog.csdn.net/guoyk1990/article/details/26060353