Ogre + MFC + OIS

来源:互联网 发布:异构系统数据迁移 编辑:程序博客网 时间:2024/05/01 09:43

//////////MFC 中的 view 类中加入

view.h

 

 

 ///渲染
 afx_msg BOOL OnEraseBkgnd(CDC* pDC);
 afx_msg void OnTimer(UINT nIDEvent);
 afx_msg void OnSize(UINT nType, int cx, int cy);
 virtual void OnDraw(CDC* pDC);  // overridden to draw this view

 

view.cpp

 

  //ON_WM_PAINT()
 ON_WM_ERASEBKGND()
 ON_WM_TIMER()
 ON_WM_SIZE()

 

void CUIEditorView::OnDraw(CDC* /*pDC*/)
{
 CUIEditorDoc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 if (!pDoc)
  return;

 if(m_firstDraw)
 {
  m_firstDraw = false;
  //SetupOgre();
  m_pWorld = new BasicWorld();
  assert(NULL != m_pWorld);
  if(NULL == m_pWorld)
   return;
  
  m_pWorld->createWorld(AfxGetMainWnd()->GetSafeHwnd());

  //(unsigned long);

  SetTimer(1, 30, NULL);
 }

 m_pWorld->renderOneFrame();


 // TODO: add draw code for native data here
}

BOOL CUIEditorView::OnEraseBkgnd(CDC* pDC)
{
 return TRUE;
}

void CUIEditorView::OnTimer(UINT nIDEvent)
{
 Invalidate();

 CWnd::OnTimer(nIDEvent);
}

 

 

 

 

 

下面是Ogre的两个类

//////////BascisWorld.h

 

 

#ifndef _FRAME_WORLD_H__
#define _FRAME_WORLD_H__

#include "common.h"
#include "atltypes.h"

class Listener;

class BasicWorld : public Ogre::Singleton<BasicWorld>
{  
public:
 static BasicWorld & getSingleton(void);
 static BasicWorld * getSingletonPtr(void);
 BasicWorld();
 ~BasicWorld();


 /*
 得到场景管理器
 **/
 Ogre::SceneManager* getSceneManager(void);

 /*
 得到窗口
 **/
 Ogre::RenderWindow* getRenderWindow(void);

 /*
 获得摄像机
 **/
 Ogre::Camera* getCamera();

 /*
 设置摄像机
 **/
 void setCamera(Ogre::Camera* pCamera);

 /*
 得到屏幕的宽
 **/
 unsigned int getWidth(void);

 /*
 得到屏幕的高
 **/
 unsigned int getHeight(void);

 /*
 创建世界
 **/
 bool createWorld( HWND wnd);

 /*
 渲染
 **/
 void renderOneFrame(void);

protected:

 /*
 读取所有资源
 **/
 bool setupResources(HWND wnd);

 /*
 选择场景管理器
 **/
 bool chooseSceneManager(void);

 /*
 创建摄像机
 **/
 bool createCamera(void);

 /*
 创建视口
 **/
 bool createViewports(void);

 /*
 载入资源
 **/
 bool loadResources(void);

 /*
 设置灯光
 **/
 bool setLights(void);

 /*
 创建场景渲染
 **/
 bool createScene(void);

 /*
 创建监听器
 **/
 bool createFrameListener(void);

protected:

 ///Ogre入口对象指针
 Ogre::Root*         m_pRoot;
 ///视口对象指针
 Ogre::Viewport*     m_pViewPort;
 ///监听器对象指针
 Listener* m_pBasicListener;
 ///渲染窗口指针
 Ogre::RenderWindow* m_pRenderWindow;
 ///场景管理器对象指针
 Ogre::SceneManager* m_pSceneMgr;
 ///摄像机对象指针
 Ogre::Camera*       m_pCamera;
 ///光照
 Ogre::Light* m_pLight;

 ///screen width and height
 unsigned int m_uiWidth;
 unsigned int m_uiHeight;
};

#define g_pBasicWorld BasicWorld::getSingletonPtr()

#endif  //_FRAME_WORLD_H__

 

/////////////////BasicWorld.cpp

 

 

 

#include "common.h"
#include "BasicWorld.h"
#include "BasicListener.h"

 

BasicWorld::BasicWorld():
 m_pRoot(0)
,m_pViewPort(0)
,m_pBasicListener(0)
,m_pRenderWindow(0)
,m_pSceneMgr(0)
,m_pCamera(0)
,m_pLight(0)
,m_uiWidth(0)
,m_uiHeight(0)
{

}

BasicWorld::~BasicWorld()
{
 SafeDelete(m_pBasicListener);
 SafeDelete(m_pRoot);
}

bool BasicWorld::createWorld(HWND wnd)
{
 ///读取所需资源
 if(!setupResources(wnd))
  return false;

 ///创建场景管理器
 if(!chooseSceneManager())
  return false;

 ///创建摄像机
 if(!createCamera())
  return false;

 ///创建视口
 if(!createViewports())
  return false;

 ///载入资源
 if(!loadResources())
  return false;

 ///创建场景
 if(!createScene())
  return false;

 ///创建灯光
 if(!setLights())
  return false;

 ///创建监听器
 if(!createFrameListener())
  return false;

 return true;
}

bool BasicWorld::setupResources(HWND wnd)
{
 ///创建Ogre root
 m_pRoot = new Ogre::Root("", "", "ogre.log");
 assert(NULL != m_pRoot);
 if(NULL == m_pRoot)
  return false;

 ///载入插件
#ifdef _DEBUG
 m_pRoot->loadPlugin("RenderSystem_Direct3D9_d");
 //m_pRoot->loadPlugin("Plugin_ParticleFX_d");
#else
 m_pRoot->loadPlugin("RenderSystem_Direct3D9");
 //m_pRoot->loadPlugin("Plugin_ParticleFX");
#endif

 ///解析资源
 Ogre::ConfigFile cf;
 cf.load("resources.cfg");

 Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
 Ogre::String secName, typeName, archName;
 while (seci.hasMoreElements())
 {
  secName = seci.peekNextKey();
  Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
  Ogre::ConfigFile::SettingsMultiMap::iterator i;
  for (i = settings->begin(); i != settings->end(); ++i)
  {
   typeName = i->first;
   archName = i->second;

   Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
    archName, typeName, secName);
  }
 }

 ///设置显示设备
 Ogre::RenderSystemList * rList = m_pRoot->getAvailableRenderers();
 Ogre::RenderSystemList::iterator it = rList->begin();
 Ogre::RenderSystem *rSys = 0;

 while(it != rList->end())
 {
  rSys = * (it++);
 }

 Ogre::String str = rSys->getName();
 m_pRoot->setRenderSystem(rSys);

 ///不让ogre自动创建窗口
 m_pRoot->initialise(false);


 ///创建窗口
 Ogre::NameValuePairList params;
 Ogre::String handle;
 handle = Ogre::StringConverter::toString((size_t)(wnd));
 params["externalWindowHandle"] = handle;
 

 CRect rect;
 GetWindowRect(wnd, &rect);
 m_pRenderWindow = m_pRoot->createRenderWindow("OgreRenderWindow", rect.Width(), rect.Height(), false, &params);
 assert(NULL != m_pRenderWindow);
 if(NULL == m_pRenderWindow)
  return false;

 ///得到屏幕宽高
 m_uiWidth = m_pRenderWindow->getWidth();
 m_uiHeight = m_pRenderWindow->getHeight();

 // Make window active and post an update
 m_pRenderWindow->setActive(true);
 m_pRenderWindow->update();


 return true;
}


bool BasicWorld::chooseSceneManager()
{
 m_pSceneMgr = m_pRoot->createSceneManager(Ogre::ST_GENERIC, "ExampleSMInstance");
 assert(NULL != m_pSceneMgr);
 if(NULL == m_pSceneMgr)
  return false;

 return true;
}

bool BasicWorld::createCamera()
{
 // Initialize the camera
 m_pCamera = m_pSceneMgr->createCamera("mainCamera");
 assert(NULL != m_pCamera);
 if(NULL == m_pCamera)
  return false;

 m_pCamera->setPosition(Ogre::Vector3(0,0,500));
 m_pCamera->lookAt(Ogre::Vector3(0,0,0));
 m_pCamera->setNearClipDistance(5);
 return true;
}

bool BasicWorld::setLights(void)
{
 //set ambient light
 m_pSceneMgr->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));

 // Blue light
 m_pLight = m_pSceneMgr->createLight("BlueLight");
 assert(NULL != m_pLight);
 if(NULL == m_pLight)
  return false;

 m_pLight->setType(Ogre::Light::LT_SPOTLIGHT);
 m_pLight->setPosition(0,3000,0);
 Ogre::Vector3 dir(-m_pLight->getPosition());
 dir.normalise();
 m_pLight->setDirection(dir);
 m_pLight->setDiffuseColour(1.0, 1.0, 1.0);
 m_pLight->setVisible(false);

 return true;
}

bool BasicWorld::createViewports()
{
 m_pViewPort = m_pRenderWindow->addViewport(m_pCamera);
 assert(NULL != m_pViewPort);
 if(NULL == m_pViewPort)
  return false;
 // Alter the camera aspect ratio to match the viewport
 //mCamera->setAspectRatio(Ogre::Real(mWidth) / Ogre::Real(mHeight));
 m_pViewPort->setBackgroundColour(Ogre::ColourValue(0,0,0));
 return true;
}

bool BasicWorld::createFrameListener()
{
 m_pBasicListener = new Listener(this);
 m_pBasicListener->initialise();
 assert(NULL != m_pBasicListener);
 if(NULL == m_pBasicListener)
  return false;

 m_pRoot->addFrameListener(m_pBasicListener);
 return true;
}

bool  BasicWorld::loadResources()
{
 Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();

 return true;
}

bool BasicWorld::createScene()
{
 Ogre::Entity * ogreHead = m_pSceneMgr->createEntity("ogreHead","ogrehead.mesh");
 Ogre::SceneNode * ogreNode = m_pSceneMgr->getRootSceneNode()->createChildSceneNode("ogreNode");
 ogreNode->attachObject(ogreHead);

 return true;
}

void BasicWorld::renderOneFrame()
{
 m_pRoot->renderOneFrame();
}

Ogre::SceneManager* BasicWorld::getSceneManager(void)
{
 return m_pSceneMgr;
}

Ogre::RenderWindow* BasicWorld::getRenderWindow(void)
{
 return m_pRenderWindow;
}

Ogre::Camera* BasicWorld::getCamera(void)
{
 return m_pCamera;
}

void BasicWorld::setCamera(Ogre::Camera* pCamera)
{
 m_pCamera = pCamera;
}

unsigned int BasicWorld::getWidth(void)
{
 return m_uiWidth;
}

unsigned int BasicWorld::getHeight(void)
{
 return m_uiHeight;
}

 

template<>  BasicWorld* Ogre::Singleton<BasicWorld>::ms_Singleton = 0; //修改
BasicWorld& BasicWorld::getSingleton()
{
 assert( ms_Singleton );  return ( *ms_Singleton ); 
}

BasicWorld* BasicWorld::getSingletonPtr()
{
 return ms_Singleton;
}

 

////////////listener.h

 

#ifndef _FRAME_LISTENER_H
#define _FRAME_LISTENER_H
#include "common.h"


class BasicWorld;

class Listener : public Ogre::FrameListener, public OIS::KeyListener, OIS::MouseListener
{

public:
 Listener(BasicWorld* pWorld);
 ~Listener();

 void initialise(void);
private:
 virtual bool mouseMoved ( const OIS::MouseEvent &arg );
 virtual bool mousePressed ( const OIS::MouseEvent &arg, OIS::MouseButtonID id );
 virtual bool mouseReleased ( const OIS::MouseEvent &arg, OIS::MouseButtonID id );

 virtual bool keyPressed ( const OIS::KeyEvent &arg );
 virtual bool keyReleased ( const OIS::KeyEvent &arg );

 bool frameStarted (const Ogre::FrameEvent &evt);
 bool frameEnded (const Ogre::FrameEvent &evt );   
 bool processKeyInput(const Ogre::FrameEvent& evt);
 bool processMouseInput(const Ogre::FrameEvent& evt);
private:
 BasicWorld       *m_pWorld;

 //OIS Input devices
 OIS::InputManager    *m_pInputManager;
 OIS::Mouse      *m_pMouse;
 OIS::Keyboard     *m_pKeyboard;

 Ogre::Radian   m_RotX, m_RotY;
};


#endif //_FRAME_LISTENER_H

 

 

////////listener.cpp

 

#include "common.h"
#include "BasicListener.h"
#include "BasicWorld.h"

Listener::Listener(BasicWorld* pWorld)
: m_pWorld(pWorld),
m_pInputManager(0),
m_pMouse(0),
m_pKeyboard(0)
{


}


Listener::~Listener()
{
 if (m_pWorld->getRenderWindow())
 {
  m_pInputManager->destroyInputObject(m_pMouse);
  m_pInputManager->destroyInputObject(m_pKeyboard);

  OIS::InputManager::destroyInputSystem(m_pInputManager);
  m_pInputManager = 0;
 }
}

void Listener::initialise(void)
{

 OIS::ParamList pl;
 size_t windowHnd = 0;
 std::ostringstream windowHndStr;

 m_pWorld->getRenderWindow()->getCustomAttribute("WINDOW", &windowHnd);
 windowHndStr << windowHnd;
 pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
 pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE")));
 pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND")));
 pl.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_NONEXCLUSIVE")));
 pl.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_FOREGROUND")));


 m_pInputManager = OIS::InputManager::createInputSystem( pl );

 // Create all devices (We only catch joystick exceptions here, as, most people have Key/Mouse)
 m_pKeyboard = static_cast<OIS::Keyboard*>(m_pInputManager->createInputObject( OIS::OISKeyboard, true ));
 m_pMouse = static_cast<OIS::Mouse*>(m_pInputManager->createInputObject( OIS::OISMouse, true ));

 unsigned int width, height, depth;
 int left, top;
 m_pWorld->getRenderWindow()->getMetrics(width, height, depth, left, top);

 const OIS::MouseState &ms = m_pMouse->getMouseState();
 ms.width = width;
 ms.height = height;

 m_pMouse->setEventCallback(this);
 m_pKeyboard->setEventCallback(this);
}

bool Listener::frameStarted(const Ogre::FrameEvent &evt)
{

 if (m_pWorld->getRenderWindow()->isClosed())
  return false;

 m_RotX = m_RotY = 0.0f;

 //Need to capture/update each device
 m_pKeyboard->capture();
 m_pMouse->capture();

 //Check to see which device is not buffered, and handle it
 if(!processKeyInput(evt))
  return false;

 if (!processMouseInput(evt))
  return false;

 //if(!m_pMouse->buffered())
 //moveCamera(); 

 return true;
}


bool Listener::frameEnded(const Ogre::FrameEvent &evt)
{
 if(m_pKeyboard->isKeyDown(OIS::KC_SYSRQ))
 {
  std::ostringstream ss;

  time_t ltime;
  time(&ltime);
  struct tm* now = localtime(&ltime);
  char szDate[128];
  strftime(szDate, 128, "%Y_%m_%d_%H_%M_%S/0", now);
  ss << "screenshot_" << szDate << ".png";
  m_pWorld->getRenderWindow()->writeContentsToFile(ss.str());
 }

 return true;


bool Listener::processKeyInput(const Ogre::FrameEvent& evt)
{
 if (m_pKeyboard->isKeyDown(OIS::KC_ESCAPE))///关闭窗口
  return false;


 if (m_pKeyboard->isKeyDown(OIS::KC_F12))///最大化窗口
 {
  static bool s_bFullScreen = false;
  s_bFullScreen = !s_bFullScreen;
  m_pWorld->getRenderWindow()->setFullscreen(s_bFullScreen, 800, 600);
 }

 return true;
}


bool Listener::processMouseInput(const Ogre::FrameEvent& evt)
{
 const float factor = -0.5f;
 const OIS::MouseState &ms = m_pMouse->getMouseState();
 if (ms.buttonDown(OIS::MB_Left))
 {
  m_RotX = Ogre::Degree(-ms.X.rel * factor);
  m_RotY = Ogre::Degree(-ms.Y.rel * factor);
 }

 if(ms.Z.rel)///camera Zoom in/Zoom out
  m_pWorld->getCamera()->moveRelative(Ogre::Vector3(0, 0,ms.Z.rel * -0.5f));

 return true;
}

bool Listener::mouseMoved (const OIS::MouseEvent &arg)
{
 return true;
}

bool Listener::mousePressed ( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
 return true;
}

bool Listener::mouseReleased ( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
 return true;
}

bool Listener::keyPressed ( const OIS::KeyEvent &arg )
{
 return true;
}

bool Listener::keyReleased ( const OIS::KeyEvent &arg )
{
 return true;
}

 

原创粉丝点击