Ogr + wxWidgets

///——————Ogre + wxWidgets——————///

 

///MyWxOgre.h

 

#ifndef _My_WXOGRE_H
#define _My_WXOGRE_H
#include “MyCommon.h”
#include <wx/mdi.h>

class MyBasicListener;

class MyWxOgre : public wxControl, public Ogre::Singleton<MyWxOgre>
{
public:

 static MyWxOgre& getSingleton();

 static MyWxOgre* getSingletonPtr();

 MyWxOgre (wxFrame *pParent);

 ~MyWxOgre();

 /*
 更新渲染
 **/
 void update();

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

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

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

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

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

 /*
 得到屏幕的高
 **/
 unsigned int getHeight(void);
protected:
 DECLARE_CLASS(MyWxOgre)

 DECLARE_EVENT_TABLE()
private:
 /*
 创建世界
 **/
 bool createWorld(void);

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

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

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

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

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

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

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

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

 /*
 **/
 void toggleTimerRendering(void);

 /*
 鼠标事件
 **/
 void OnMouse(wxMouseEvent& event);

 /*
 **/
 void OnSize(wxSizeEvent& event);

 /*
 **/
 void OnPaint(wxPaintEvent& event);

 /*
 **/
 void OnEraseBackground( wxEraseEvent& event);

 /*
 **/
 void OnRenderTimer(wxTimerEvent& event);

public:
 ///监听器对象指针
 MyBasicListener* m_pBasicListener;
 ///渲染窗口指针
 Ogre::RenderWindow* m_pRenderWindow;
 ///场景管理器对象指针
 Ogre::SceneManager* m_pSceneMgr;
 ///摄像机对象指针
 Ogre::Camera*       m_pCamera;
 ///光照
 Ogre::Light* m_pLight;
private:
 ///
 wxTimer          mTimer;
 ///Ogre入口对象指针
 Ogre::Root*         m_pRoot;
 ///视口对象指针
 Ogre::Viewport*     m_pViewPort;
 ///wxWidgets中鼠标的位置
 float m_fMouseX;
 float m_fMouseY;
 ///screen width and height
 unsigned int m_uiWidth;
 unsigned int m_uiHeight;
};

#endif // _My_WXOGRE_H

 

 

///MyWxOgre.cpp

#include “MyCommon.h”
#include “MywxOgre.h”
#include “MyBasicListener.h”

const long ID_RENDERTIMER = wxNewId();

IMPLEMENT_CLASS(MyWxOgre, wxControl)

BEGIN_EVENT_TABLE(MyWxOgre,wxControl)
EVT_SIZE(MyWxOgre::OnSize)
EVT_PAINT(MyWxOgre::OnPaint)
EVT_ERASE_BACKGROUND( MyWxOgre::OnEraseBackground )
EVT_TIMER( ID_RENDERTIMER, MyWxOgre::OnRenderTimer )
EVT_MOUSE_EVENTS(MyWxOgre::OnMouse)
END_EVENT_TABLE()

MyWxOgre::MyWxOgre(wxFrame *pParent) :
wxControl(pParent, -1),
// mTimer(this, ID_RENDERTIMER),
m_pRoot(0),
m_pViewPort(0),
m_pCamera(0),
m_pSceneMgr(0),
m_pRenderWindow(0),
m_pLight(0),
m_uiWidth(0),
m_uiHeight(0),
m_fMouseX(0),
m_fMouseY(0)
{
 createWorld();
}

MyWxOgre::~MyWxOgre()
{

 SafeDelete(m_pBasicListener);
 SafeDelete(m_pRoot);
}
///
bool MyWxOgre::createWorld(void)
{
 ///读取所需资源
 if(!setupResources())
  return false;

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

 Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(0);

 ///创建摄像机
 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;
}

void MyWxOgre::toggleTimerRendering(void)
{
 //if (mTimer.IsRunning())
 // mTimer.Stop();
 //mTimer.Start(10);
}

void MyWxOgre::update(void)
{
 Ogre::Root::getSingletonPtr()->renderOneFrame();
}

bool MyWxOgre::createScene()
{  
 return true;
}

bool MyWxOgre::setupResources(void)
{
 ///创建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);

 ///
 m_pRoot->initialise(false);

 ///创建窗口
 Ogre::NameValuePairList params;
 Ogre::String handle;
 handle = Ogre::StringConverter::toString((size_t)((HWND)(GetHandle())));
 params[“externalWindowHandle”] = handle;
 int iWidth = 0;
 int iHeight = 0;

 GetSize(&iWidth, &iHeight);
 m_pRenderWindow = m_pRoot->createRenderWindow(“OgreRenderWindow”, iWidth, iHeight, false, &params);
 assert(NULL != m_pRenderWindow);
 if(NULL == m_pRenderWindow)
  return false;

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

 return true;
}

bool MyWxOgre::chooseSceneManager(void)
{
 m_pSceneMgr = m_pRoot->createSceneManager(Ogre::ST_GENERIC, “ExampleSMInstance”);
 assert(NULL != m_pSceneMgr);
 if(NULL == m_pSceneMgr)
  return false;

 return true;
}

bool MyWxOgre::createCamera(void)
{
 // 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,50));
 m_pCamera->lookAt(Ogre::Vector3(0,0,0));
 m_pCamera->setNearClipDistance(5);

 return true;
}

bool MyWxOgre::createViewports(void)
{
 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 MyWxOgre::loadResources(void)
{
 Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();

 return true;
}

bool MyWxOgre::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 MyWxOgre::createFrameListener()
{
 m_pBasicListener = new MyBasicListener(this);
 m_pBasicListener->initialise();
 assert(NULL != m_pBasicListener);
 if(NULL == m_pBasicListener)
  return false;

 m_pRoot->addFrameListener(m_pBasicListener);

 return true;
}

//ON EVENTS
void MyWxOgre::OnSize(wxSizeEvent& event)
{
 int iWidth = 0;
 int iHeight = 0;
 GetSize(&iWidth, &iHeight);
 m_pRenderWindow->resize( iWidth, iHeight );
 m_pRenderWindow->windowMovedOrResized();
 if (m_pCamera)
  m_pCamera->setAspectRatio(Ogre::Real(iWidth) / Ogre::Real(iHeight));
}

void MyWxOgre::OnMouse(wxMouseEvent& event)

}

void MyWxOgre::OnPaint(wxPaintEvent& event)
{
 update();
}

void MyWxOgre::OnEraseBackground( wxEraseEvent& event)
{
 //update();
}

void MyWxOgre::OnRenderTimer(wxTimerEvent& event)
{
 //wxPaintEvent pe;
 //::wxPostEvent(this, pe);
 //update();
}

//ON TEMPLATES
template<> MyWxOgre* Ogre::Singleton<MyWxOgre>::ms_Singleton = 0;
MyWxOgre& MyWxOgre::getSingleton()
{
 assert( ms_Singleton );  return ( *ms_Singleton ); 
}

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

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

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

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

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

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

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

 

 

///MyBasicListener.h

 

#ifndef _My_BASIC_LISTENER_H
#define _My_BASIC_LISTENER_H

//#include “MyCommon.h”
#include “MyCommon.h”
class MyWxOgre;

class MyBasicListener : public Ogre::FrameListener, public Ogre::WindowEventListener
{
public:
 MyBasicListener(MyWxOgre* pOgre);

 ~MyBasicListener(void);

 void initialise(void);

 void destroy(void);

private:
 /*
 开始帧监听
 **/
 bool frameStarted (const Ogre::FrameEvent &evt);

 /*
 结束帧监听
 **/
 bool frameEnded (const Ogre::FrameEvent &evt); 

private:
 ///wxOgre对象指针
 MyWxOgre*  m_pWxOgre;
};
#endif //_My_BASIC_LISTENER_H

 

///MyBasicListener.cpp

 

#include “MyCommon.h”

#include “MyBasicListener.h”
#include “MyWxOgre.h”

MyBasicListener::MyBasicListener(MyWxOgre *pOgre): m_pWxOgre(pOgre)

}

MyBasicListener::~MyBasicListener()
{
 destroy();
}

void MyBasicListener::initialise(void)
{
 Ogre::WindowEventUtilities::addWindowEventListener(m_pWxOgre->getRenderWindow(), this);
}

void MyBasicListener::destroy(void)
{
 Ogre::WindowEventUtilities::removeWindowEventListener(m_pWxOgre->getRenderWindow(), this);
}

bool MyBasicListener::frameStarted(const Ogre::FrameEvent &evt)
{
 if (m_pWxOgre->getRenderWindow()->isClosed())
 {
  return false;
 }

 return true;
}

bool MyBasicListener::frameEnded(const Ogre::FrameEvent &evt)
{
 return true;

 

///MyMainFrame.h

 

#ifndef _My_MAIN_FRAME_H
#define _My_MAIN_FRAME_H

#include “MyCommon.h”
#include <wx/treectrl.h>

class MyWxOgre;
class MySkinEditFrame;

class MyMainFrame : public wxFrame
{
public:
 MyMainFrame(const wxString& title, const wxPoint& pos, const wxSize& size);

 ~MyMainFrame();

 /*
 更新Ogre渲染
 **/
 void UpdateOgre();
 /*
 初始化各菜单项
 **/
 void InitMenu();

 /*
 初始化各pane
 **/
 void InitPane();

 /*
 初始化各对象
 */
 void InitObjects();
private:
 /*
 屏幕大小改变
 **/
 void OnSize(wxSizeEvent& event);

 /*
 关闭窗口
 **/
 void OnClose(wxCloseEvent &event);

 /*
 菜单项消息处理 
 **/
 void OnToggleMenu(wxCommandEvent& event);

 /*
 树结构的消息处理
 **/
 void OnTreeSelect(wxTreeEvent &event);

 /*
 创建树结构
 **/
 wxTreeCtrl* CreateTreeCtrl(void);
public:
 ///FrameManager对象
 wxFrameManager m_mgr;
private:
 ///Ogre渲染对象
 MyWxOgre* m_pWxOgre;
 ///皮肤编辑窗口
 MySkinEditFrame* m_pSkinEditFrame;
 ///树结构
 wxTreeCtrl *m_pTreeCtrl;
private: 
 DECLARE_EVENT_TABLE()
};

///树事件消息的传递处理
class MyTreeItemData : public wxTreeItemData
{
public:

 MyTreeItemData(const wxString& desc) : m_desc(desc)
 {
 }

 const wxString& GetDesc() const
 {
  return m_desc;
 }
private:
 const  wxString m_desc;
};

#endif //_My_MAIN_FRAME_H

 

///MyMainFrame.cpp

 

#include “MyCommon.h”
#include “MyMainFrame.h”
#include “MyWxOgre.h”
#include “MySkinEditFrame.h”

 

BEGIN_EVENT_TABLE(MyMainFrame, wxFrame)
EVT_SIZE(MyMainFrame::OnSize)
EVT_CLOSE(MyMainFrame::OnClose)
EVT_MENU(Minimal_Exit,  MyMainFrame::OnToggleMenu)
EVT_TREE_SEL_CHANGED(ID_TREECONTROL, MyMainFrame::OnTreeSelect)
END_EVENT_TABLE()

//—————————The process of MyMainFrame———————————————-

MyMainFrame::MyMainFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
:wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size),
m_pWxOgre(0),
m_pTreeCtrl(0),
m_pSkinEditFrame(0)
{
}

MyMainFrame::~MyMainFrame()
{
 m_mgr.UnInit();

 SafeDelete(m_pSkinEditFrame);
 SafeDelete(m_pWxOgre);
 SafeDelete(m_pTreeCtrl);
}

void MyMainFrame::UpdateOgre()
{
 m_pWxOgre->update();
}

void MyMainFrame::OnClose(wxCloseEvent &event)
{
 Destroy();
 //Close(true);
}

void MyMainFrame::OnSize(wxSizeEvent& event)
{
 Update();
 Refresh();
}

void MyMainFrame::InitMenu()
{
 // 创建文件菜单
 wxMenu * menuFile = new wxMenu;
 menuFile->AppendSeparator();
 menuFile->Append(Minimal_Exit, _(“退出/tEsc”));

 //创建菜单条,并把文件和功能菜单添加到菜单条上
 wxMenuBar *menuBar = new wxMenuBar;

 menuBar->Append(menuFile, _(“文件”));

 SetMenuBar(menuBar);
}

void MyMainFrame::InitObjects()
{
 m_pTreeCtrl = new wxTreeCtrl(this, ID_TREECONTROL,wxPoint(0,0), wxSize(160,250)/*,wxTR_HIDE_ROOT|wxTR_HAS_BUTTONS|wxTR_LINES_AT_ROOT*/);
 assert(NULL != m_pTreeCtrl);
 if(NULL == m_pTreeCtrl)
  return ;

 m_pWxOgre = new MyWxOgre(this);
 assert(NULL != m_pWxOgre);
 if(NULL == m_pWxOgre)
  return ;

 m_pSkinEditFrame = new MySkinEditFrame(this, ID_PARTICLE_TEMPLATE); 
 assert(NULL != m_pSkinEditFrame);
 if(NULL == m_pSkinEditFrame)
  return ;
}

void MyMainFrame::InitPane()
{
 m_mgr.SetFrame(this);

 
 m_mgr.AddPane(CreateTreeCtrl(), wxPaneInfo().
  Name(wxT(“tree”)).Caption(wxT(“文件表”)).
  BestSize(wxSize(160,400)).Left().Layer(1));

 m_mgr.AddPane(m_pSkinEditFrame, wxPaneInfo().
  Name(wxT(“par”)).Caption(_(“Default1”)).
  BestSize(wxSize(260,450)).Float().Show(false));

 ///注意:将m_pWxOgre挂在pane上,就无法正常退出程序,如果再加一个普通的窗口到pane上,就可以正常退出程序!
 m_mgr.AddPane(m_pWxOgre, wxCENTER, wxT(“Ogre Pane”));
}

void MyMainFrame::OnToggleMenu(wxCommandEvent& event)
{
 int id = event.GetId();
 if(id == Minimal_Exit )
 {
  Close(true);
 }

 m_mgr.Update();
}

void MyMainFrame::OnTreeSelect(wxTreeEvent &event)
{

}

wxTreeCtrl* MyMainFrame::CreateTreeCtrl()
{
 
 //wxTreeItemId rootId = tree->AddRoot(wxT(“Root”),0, 0, new MyTreeItemData(wxT(“Root item”)));
 return m_pTreeCtrl;
}

 

//MyCommon.h

 

#ifndef  _HY_COMMON_H
#define  _HY_COMMON_H

#include  <windows.h>
#include  <ogre.h>
#include  <vector>
#include  <map>
#include  <string>
#include  <iostream>

#include  “HyEnums.h”

#include  <wx/wxprec.h>
#include  <wxaui_manager.h>
#include  <wx/stattext.h>
#include  <wx/sizer.h>
#include  <wx/menuitem.h>
#include  <wx/checklst.h>
#include  <wx/listbox.h>
#include  <wx/radiobox.h>
#include  <wx/dialog.h>
#include  <wx/filesys.h>
#include  <wx/filefn.h>
#include  <wx/listctrl.h>

#define SafeRelease(pObject) if(pObject!=NULL){pObject->Release();pObject=NULL;}
#define SafeDelete(object) if(object!=NULL){delete object;object=NULL;}

#endif ///_My_COMMON_H

 

///MyApp.h

 

#ifndef  _My_APP_H
#define  _My_APP_H
#include “MyCommon.h”
#include “MyMainFrame.h”

class MyApp : public wxApp
{
private:
 ///程序的主入口
 virtual bool OnInit();
 virtual int OnExit();
 
private:
 ///主窗口对象
 MyMainFrame *m_pMainFrame;
};
#endif  //_My_APP_H

 

///MyApp.cpp

 

#include “MyCommon.h”
#include “MyApp.h”

 

IMPLEMENT_APP(MyApp)
//—————————-The process of MyApp————————————————
bool MyApp::OnInit()
{
 m_pMainFrame = new MyMainFrame(_(“wxOgre”),wxDefaultPosition, wxSize(800, 600));
 assert(NULL != m_pMainFrame);
 if(NULL == m_pMainFrame)
  return false;

 m_pMainFrame->InitMenu();
 m_pMainFrame->InitObjects();
 m_pMainFrame->InitPane();
 m_pMainFrame->m_mgr.Update();
 m_pMainFrame->UpdateOgre();
 m_pMainFrame->Refresh();
 m_pMainFrame->Update();

 m_pMainFrame->Show(true);
 SetTopWindow(m_pMainFrame);

 return true;
}

int MyApp::OnExit()
{
 CleanUp();

 return 0;
}

 

 

 

转载自:https://blog.csdn.net/StoneChang/article/details/4128715

You may also like...