项目下载地址:http://download.csdn.net/detail/wxg694175346/6340347
头文件ShrewMouseApplication.h:
#ifndef __ShrewMouseApplication_H__ #define __ShrewMouseApplication_H__ #include "ExampleApplication.h" class ShrewMouseManager; class ShrewMouseApplication : public ExampleApplication { public: ShrewMouseApplication(void); ~ShrewMouseApplication(void); protected: //用于系统回调来创建场景 void createScene(void); //用于提供框架所需要的FrameListener类型。 virtual void createFrameListener(void); private: //这里提供一个管理器,用于协调所有的地鼠。 ShrewMouseManager * _miceManager; }; #endif
头文件ShrewMouseFrameListener.h:
#ifndef __ShrewMouseFrameListener_H__ #define __ShrewMouseFrameListener_H__ #include "ExampleFrameListener.h" #include <sstream> class ShrewMouseManager; class ShrewMouseFrameListener: public ExampleFrameListener { public: // Constructor takes a RenderWindow because it uses that to determine input context ShrewMouseFrameListener(ShrewMouseManager * miceManager, RenderWindow* win, Camera* cam, bool bufferedKeys = false, bool bufferedMouse = false, bool bufferedJoy = false ); virtual bool processUnbufferedKeyInput(const FrameEvent& evt) { if(mKeyboard->isKeyDown(OIS::KC_SPACE)) { std::stringstream ss; ss<<this->mCamera->getPosition()<<this->mCamera->getOrientation()<<std::endl; OutputDebugStringA(ss.str().c_str()); return true; } return ExampleFrameListener::processUnbufferedKeyInput(evt); } private: ShrewMouseManager * _miceManager; }; #endif
头文件ShrewMouseManager.h:
#ifndef __ShrewMouseManager_H__ #define __ShrewMouseManager_H__ #include <Ogre.h> class ShrewMouseManager { public: ShrewMouseManager(Ogre::SceneManager *sm); ~ShrewMouseManager(void); protected: //这里需要一个Ogre3D场景管理器的指针,用于调度游戏空间 Ogre::SceneManager * _sm; }; #endif
源代码main.cpp:
#include "ShrewMouseApplication.h" #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 #define WIN32_LEAN_AND_MEAN #include "windows.h" #endif #ifdef __cplusplus extern "C" { #endif #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT ) #else int main(int argc, char **argv) #endif { // 创建应用实例 ShrewMouseApplication app; try { app.go(); } catch( Exception& e ) { #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL); #else std::cerr << "An exception has occured: " << e.getFullDescription(); #endif } return 0; } #ifdef __cplusplus } #endif
源代码ShrewMouseApplication.cpp:
#include "ShrewMouseApplication.h" #include "ShrewMouseFrameListener.h" #include "ShrewMouseManager.h" using namespace Ogre; ShrewMouseApplication::ShrewMouseApplication(void):_miceManager(NULL) { } ShrewMouseApplication::~ShrewMouseApplication(void) { //最后释放地鼠管理器 if(_miceManager) delete _miceManager; } void ShrewMouseApplication::createScene(void) { //设置场景环境光 mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5)); //创建一个灯光 Light* l = mSceneMgr->createLight("MainLight"); //设置灯光位置 l->setPosition(20,80,50); //设置摄像机位置 mCamera->setPosition(100.0f, 100.0f, 300.0f); //设置摄像机角度 mCamera->setOrientation(Ogre::Quaternion(Degree(-30), Vector3::UNIT_X)); //创建一个地鼠管理器,并把Ogre3D引擎中场景管理器作为参数提供。 _miceManager = new ShrewMouseManager(mSceneMgr); } void ShrewMouseApplication::createFrameListener(void) { //把我们自己的FrameListener派生类型提供给Ogre3D图形引擎 mFrameListener= new ShrewMouseFrameListener(_miceManager, mWindow, mCamera); mRoot->addFrameListener(mFrameListener); }
源代码ShrewMouseFrameListener.cpp:
#include "ShrewMouseFrameListener.h" #include "ShrewMouseManager.h" ShrewMouseFrameListener::ShrewMouseFrameListener(ShrewMouseManager * miceManager, RenderWindow* win, Camera* cam, bool bufferedKeys, bool bufferedMouse, bool bufferedJoy):ExampleFrameListener(win, cam, bufferedKeys, bufferedMouse, bufferedJoy),_miceManager(miceManager) { }
源代码ShrewMouseManager.cpp:
#include "ShrewMouseManager.h" ShrewMouseManager::ShrewMouseManager(Ogre::SceneManager * sm):_sm(sm) { using namespace Ogre; for(int i=0; i<3; ++i) { for(int j =0; j<3; ++j) { //采用模型“ogrehead.mesh”,通过场景管理器创建一个实体。 Entity *ent = _sm->createEntity("head"+Ogre::StringConverter::toString(i*10+j*1), "ogrehead.mesh"); //在场景管理器中,找到场景最根部节点的指针。 SceneNode * rsn = _sm->getRootSceneNode(); //在这个节点下创建一个子节点,位置是Ogre::Vector3(100 *i, 0, 100 *j)。 SceneNode * node = rsn->createChildSceneNode(Ogre::Vector3(100 *i, 0, 100 *j)); //把之前建立的实体挂接到这个节点上。 node->attachObject(ent); } } } ShrewMouseManager::~ShrewMouseManager(void) { }
项目运行截图: