结合使用Ogre 1.7.1 +CEGUI 0.7.1
这篇文章是对于Ogre3D中文网——基础教程六的补充,阅读前假定你已经对于Ogre和CEGUI有一定了解,并且阅读了《基础教程六》
如果你还不了解他们,你应该知道:
Ogre是一个3D图形渲染引擎;CEGUI是灵活的,可自由定义样式的GUI框架,他可以很好的支持Ogre,来绘制界面。
如果你对于Ogre和CEGUI感兴趣,以下将是很好的学习资源:
Ogre:http://www.ogre3d.org/
Ogre3D中文网:http://wiki.ogre3d.cn/wiki/
CEGUI:http://www.cegui.org.uk/
如果你和我一样也是从Ogre1.7开始接触这个食人魔,你也一定在学习的过程中遇到了很多麻烦,官方的入门教程还多数停留在Ogre 1.4的时代,面对这个更新如此频繁的怪物,很多守旧的东西早已不适用,如果你还在挖空心思想要了解这些经历了版本变迁过时的东西,如何能够应用到新的Ogre1.7+CEGUI0.7中,这篇文章或许对你会有帮助:D。另外,Ogre于4月28号刚刚更新到1.7.1,修正了1.7.0的诸多问题,继续学习前建议你更新到此版本。
结合使用Ogre1.7+CEGUI0.7,需要注意的问题:
1、你可能还在学习官方的Ogre1.4版本的教程,你会发现MouseMotionListener、MouseListener这些由Ogre自己实现的输入设备功能类早已被取消掉,原1.4版本的InputReader已由InputManager替代,键/鼠/手柄设备的输入功能可以由OIS来实现(这并非唯一途径,但,是官方极力推荐的),创建设备对象可以通过
1
mKeyboard
=
static_cast
<
OIS::Keyboard
*>
(mInputManager
->
createInputObject(OIS::OISKeyboard,
true
));
2 mMouse = static_cast < OIS::Mouse *> (mInputManager -> createInputObject(OIS::OISMouse, true ));
3 mJoy = static_cast < OIS::JoyStick *> (mInputManager -> createInputObject(OIS::OISJoyStick, true ));
2 mMouse = static_cast < OIS::Mouse *> (mInputManager -> createInputObject(OIS::OISMouse, true ));
3 mJoy = static_cast < OIS::JoyStick *> (mInputManager -> createInputObject(OIS::OISJoyStick, true ));
使用CEGUI时,还需通过System向CEGUI注入OIS事件对象,如:
1
CEGUI::System::getSingleton().injectMouseButtonDown(convertOgreButtonToCegui(id));
2、CEGUI0.7版本之前的Ogre渲染器CEGUI::OgreCEGUIRenderer已由CEGUI::OgreRender替代,并且创建Renderer和System的步骤已经被简化,原来的
1
mGUIRenderer
=
new
CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY,
false
,
3000
,mSceneMgr);
2 mGUISystem = new CEGUI::System(mGUIRenderer);
2 mGUISystem = new CEGUI::System(mGUIRenderer);
现在可以简化为:
1
mGUIRenderer
=&
CEGUI::OgreRenderer::bootstrapSystem();
下面对于基础教程六的代码进行新版改造:
下面的场景中创建了一个简单的天空穹,用CEGUI绘制了一个按钮,点击按钮OK后退出应用程序
注意:为了能让Ogre找到CEGUI的相关素材资源,你需要将Ogre的resource.cfg资源文件中的CEGUI资源文件路径做相关添加
全部代码如下:
1
#include
<
CEGUIImageset.h
>
2 #include < CEGUISystem.h >
3 #include < CEGUILogger.h >
4 #include < CEGUISchemeManager.h >
5 #include < CEGUIWindowManager.h >
6 #include < CEGUIWindow.h >
7 #include < elements / CEGUIPushButton.h >
8 #include < RendererModules / Ogre / CEGUIOgreRenderer.h >
9 #include < RendererModules / Ogre / CEGUIOgreResourceProvider.h >
10 #include < RendererModules / Ogre / CEGUIOgreGeometryBuffer.h >
11 // regular mem handler
12 #include " ExampleApplication.h "
13
14 CEGUI::MouseButton convertOgreButtonToCegui( int buttonID)
15 {
16 switch (buttonID)
17 {
18 case OIS::MB_Left:
19 return CEGUI::LeftButton;
20 case OIS::MB_Right:
21 return CEGUI::RightButton;
22 case OIS::MB_Middle:
23 return CEGUI::MiddleButton;
24 default:
25 return CEGUI::LeftButton;
26 }
27}
28
29 class GuiFrameListener: public ExampleFrameListener, public OIS::MouseListener, public OIS::KeyListener
30 {
31private:
32 CEGUI::Renderer *mGUIRenderer;
33 bool mShoutdownRequested;
34public:
35 GuiFrameListener(RenderWindow* win,Camera* cam,CEGUI::Renderer* renderer)
36 :ExampleFrameListener(win,cam,true,true),mGUIRenderer(renderer),mShoutdownRequested(false)
37 {
38 mMouse->setEventCallback(this);
39 mKeyboard->setEventCallback(this);
40
41 CEGUI::WindowManager* wmgr=CEGUI::WindowManager::getSingletonPtr();
42 CEGUI::Window* quitBtn=wmgr->getWindow((CEGUI::utf8*)"Quit");
43 quitBtn->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&GuiFrameListener::quit,this));
44 }
45
46 void requestShoutdown()
47 {
48 mShoutdownRequested=true;
49 }
50
51 bool quit(const CEGUI::EventArgs &evt)
52 {
53 mShoutdownRequested=true;
54 return true;
55 }
56
57 bool frameStarted(const FrameEvent& evt)
58 {
59 if(mMouse)
60 mMouse->capture();
61 if(mKeyboard)
62 mKeyboard->capture();
63
64 return ExampleFrameListener::frameStarted(evt);
65 }
66
67 bool frameEnded(const FrameEvent& evt)
68 {
69 if(mShoutdownRequested)
70 return false;
71 else
72 return ExampleFrameListener::frameEnded(evt);
73 }
74
75 bool mouseMoved(const OIS::MouseEvent &evt)
76 {
77 CEGUI::System::getSingleton().injectMouseMove(evt.state.X.rel,evt.state.Y.rel);
78 return true;
79 }
80
81 bool mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
82 {
83 CEGUI::System::getSingleton().injectMouseButtonDown(convertOgreButtonToCegui(id));
84 return true;
85 }
86
87 bool mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID id )
88 {
89 CEGUI::System::getSingleton().injectMouseButtonUp(convertOgreButtonToCegui(id));
90 return true;
91 }
92
93 bool keyPressed(const OIS::KeyEvent &evt )
94 {
95 if(evt.key==OIS::KC_ESCAPE)
96 {
97 mShoutdownRequested=true;
98 return true;
99 }
100
101 CEGUI::System::getSingleton().injectKeyDown(evt.key);
102 CEGUI::System::getSingleton().injectChar(evt.text);
103
104 return true;
105 }
106
107 bool keyReleased(const OIS::KeyEvent &evt )
108 {
109 CEGUI::System::getSingleton().injectKeyUp(evt.key);
110 return true;
111 }
112} ;
113
114 class TutorialApplication: public ExampleApplication
115 {
116public:
117 CEGUI::OgreRenderer* mGUIRenderer;
118 CEGUI::Window* mEditorGuiSheet;
119
120public:
121 TutorialApplication()
122 :mGUIRenderer(NULL),mEditorGuiSheet(NULL)
123 {
124 }
125
126 ~TutorialApplication()
127 {
128 /**//*if(mEditorGuiSheet)
129 {
130 CEGUI::WindowManager::getSingleton().destroyWindow(mEditorGuiSheet);
131 }*/
132
133
134 }
135
136protected:
137 void createScene(void)
138 {
139 mGUIRenderer=&CEGUI::OgreRenderer::bootstrapSystem();
140
141 //设置日志
142 //Ogre::Log::setLogDetail(Ogre::LoggingLevel::LL_NORMAL);
143 CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::LoggingLevel::Informative);
144
145 CEGUI::SchemeManager::getSingleton().create("TaharezLook.scheme");
146 CEGUI::System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
147 CEGUI::MouseCursor::getSingleton().setImage("TaharezLook","MouseMoveCursor");
148 CEGUI::System::getSingleton().setDefaultFont("DejaVuSans-10");
149
150 mEditorGuiSheet=(CEGUI::Window*)CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow","Sheet");
151 CEGUI::System::getSingleton().setGUISheet(mEditorGuiSheet);
152
153 CEGUI::PushButton* quitBtn=(CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().createWindow("TaharezLook/Button",(CEGUI::utf8*)"Quit");
154 quitBtn->setPosition(CEGUI::UVector2(cegui_reldim(.35f), cegui_reldim(.45f)));
155 quitBtn->setSize(CEGUI::UVector2(cegui_reldim(.15f), cegui_reldim(.05f)));
156 quitBtn->setText((CEGUI::utf8*)"OK");
157 mEditorGuiSheet->addChildWindow(quitBtn);
158
159
160 /**//*
161 mEditorGuiSheet = CEGUI::WindowManager::getSingleton().loadWindowLayout((CEGUI::utf8*)"Tutorial Gui.xml");
162 CEGUI::System::getSingleton().setGUISheet(mEditorGuiSheet);
163 CEGUI::PushButton* quitButton=(CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().getWindow((CEGUI::utf8*)"Quit");
164 */
165
166 mSceneMgr->setAmbientLight(ColourValue(1, 1, 1));
167 mSceneMgr->setSkyDome(true, "Examples/CloudySky", 5, 8);
168 }
169
170 void createFrameListener(void)
171 {
172 mFrameListener = new GuiFrameListener(mWindow, mCamera, mGUIRenderer);
173 mRoot->addFrameListener(mFrameListener);
174 }
175} ;
176
177 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
178 #define WIN32_LEAN_AND_MEAN
179 #include " windows.h "
180 INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
181 #else
182 int main( int argc, char ** argv)
183 #endif
184 {
185 // Create application object
186 TutorialApplication app;
187 try {
188 app.go();
189 } catch( Exception& e ) {
190#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
191 MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
192#else
193 fprintf(stderr, "An exception has occured: %s\n",e.getFullDescription().c_str());
194#endif
195 }
196
197 return 0;
198}
199
2 #include < CEGUISystem.h >
3 #include < CEGUILogger.h >
4 #include < CEGUISchemeManager.h >
5 #include < CEGUIWindowManager.h >
6 #include < CEGUIWindow.h >
7 #include < elements / CEGUIPushButton.h >
8 #include < RendererModules / Ogre / CEGUIOgreRenderer.h >
9 #include < RendererModules / Ogre / CEGUIOgreResourceProvider.h >
10 #include < RendererModules / Ogre / CEGUIOgreGeometryBuffer.h >
11 // regular mem handler
12 #include " ExampleApplication.h "
13
14 CEGUI::MouseButton convertOgreButtonToCegui( int buttonID)
15 {
16 switch (buttonID)
17 {
18 case OIS::MB_Left:
19 return CEGUI::LeftButton;
20 case OIS::MB_Right:
21 return CEGUI::RightButton;
22 case OIS::MB_Middle:
23 return CEGUI::MiddleButton;
24 default:
25 return CEGUI::LeftButton;
26 }
27}
28
29 class GuiFrameListener: public ExampleFrameListener, public OIS::MouseListener, public OIS::KeyListener
30 {
31private:
32 CEGUI::Renderer *mGUIRenderer;
33 bool mShoutdownRequested;
34public:
35 GuiFrameListener(RenderWindow* win,Camera* cam,CEGUI::Renderer* renderer)
36 :ExampleFrameListener(win,cam,true,true),mGUIRenderer(renderer),mShoutdownRequested(false)
37 {
38 mMouse->setEventCallback(this);
39 mKeyboard->setEventCallback(this);
40
41 CEGUI::WindowManager* wmgr=CEGUI::WindowManager::getSingletonPtr();
42 CEGUI::Window* quitBtn=wmgr->getWindow((CEGUI::utf8*)"Quit");
43 quitBtn->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&GuiFrameListener::quit,this));
44 }
45
46 void requestShoutdown()
47 {
48 mShoutdownRequested=true;
49 }
50
51 bool quit(const CEGUI::EventArgs &evt)
52 {
53 mShoutdownRequested=true;
54 return true;
55 }
56
57 bool frameStarted(const FrameEvent& evt)
58 {
59 if(mMouse)
60 mMouse->capture();
61 if(mKeyboard)
62 mKeyboard->capture();
63
64 return ExampleFrameListener::frameStarted(evt);
65 }
66
67 bool frameEnded(const FrameEvent& evt)
68 {
69 if(mShoutdownRequested)
70 return false;
71 else
72 return ExampleFrameListener::frameEnded(evt);
73 }
74
75 bool mouseMoved(const OIS::MouseEvent &evt)
76 {
77 CEGUI::System::getSingleton().injectMouseMove(evt.state.X.rel,evt.state.Y.rel);
78 return true;
79 }
80
81 bool mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
82 {
83 CEGUI::System::getSingleton().injectMouseButtonDown(convertOgreButtonToCegui(id));
84 return true;
85 }
86
87 bool mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID id )
88 {
89 CEGUI::System::getSingleton().injectMouseButtonUp(convertOgreButtonToCegui(id));
90 return true;
91 }
92
93 bool keyPressed(const OIS::KeyEvent &evt )
94 {
95 if(evt.key==OIS::KC_ESCAPE)
96 {
97 mShoutdownRequested=true;
98 return true;
99 }
100
101 CEGUI::System::getSingleton().injectKeyDown(evt.key);
102 CEGUI::System::getSingleton().injectChar(evt.text);
103
104 return true;
105 }
106
107 bool keyReleased(const OIS::KeyEvent &evt )
108 {
109 CEGUI::System::getSingleton().injectKeyUp(evt.key);
110 return true;
111 }
112} ;
113
114 class TutorialApplication: public ExampleApplication
115 {
116public:
117 CEGUI::OgreRenderer* mGUIRenderer;
118 CEGUI::Window* mEditorGuiSheet;
119
120public:
121 TutorialApplication()
122 :mGUIRenderer(NULL),mEditorGuiSheet(NULL)
123 {
124 }
125
126 ~TutorialApplication()
127 {
128 /**//*if(mEditorGuiSheet)
129 {
130 CEGUI::WindowManager::getSingleton().destroyWindow(mEditorGuiSheet);
131 }*/
132
133
134 }
135
136protected:
137 void createScene(void)
138 {
139 mGUIRenderer=&CEGUI::OgreRenderer::bootstrapSystem();
140
141 //设置日志
142 //Ogre::Log::setLogDetail(Ogre::LoggingLevel::LL_NORMAL);
143 CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::LoggingLevel::Informative);
144
145 CEGUI::SchemeManager::getSingleton().create("TaharezLook.scheme");
146 CEGUI::System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
147 CEGUI::MouseCursor::getSingleton().setImage("TaharezLook","MouseMoveCursor");
148 CEGUI::System::getSingleton().setDefaultFont("DejaVuSans-10");
149
150 mEditorGuiSheet=(CEGUI::Window*)CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow","Sheet");
151 CEGUI::System::getSingleton().setGUISheet(mEditorGuiSheet);
152
153 CEGUI::PushButton* quitBtn=(CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().createWindow("TaharezLook/Button",(CEGUI::utf8*)"Quit");
154 quitBtn->setPosition(CEGUI::UVector2(cegui_reldim(.35f), cegui_reldim(.45f)));
155 quitBtn->setSize(CEGUI::UVector2(cegui_reldim(.15f), cegui_reldim(.05f)));
156 quitBtn->setText((CEGUI::utf8*)"OK");
157 mEditorGuiSheet->addChildWindow(quitBtn);
158
159
160 /**//*
161 mEditorGuiSheet = CEGUI::WindowManager::getSingleton().loadWindowLayout((CEGUI::utf8*)"Tutorial Gui.xml");
162 CEGUI::System::getSingleton().setGUISheet(mEditorGuiSheet);
163 CEGUI::PushButton* quitButton=(CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().getWindow((CEGUI::utf8*)"Quit");
164 */
165
166 mSceneMgr->setAmbientLight(ColourValue(1, 1, 1));
167 mSceneMgr->setSkyDome(true, "Examples/CloudySky", 5, 8);
168 }
169
170 void createFrameListener(void)
171 {
172 mFrameListener = new GuiFrameListener(mWindow, mCamera, mGUIRenderer);
173 mRoot->addFrameListener(mFrameListener);
174 }
175} ;
176
177 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
178 #define WIN32_LEAN_AND_MEAN
179 #include " windows.h "
180 INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
181 #else
182 int main( int argc, char ** argv)
183 #endif
184 {
185 // Create application object
186 TutorialApplication app;
187 try {
188 app.go();
189 } catch( Exception& e ) {
190#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
191 MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
192#else
193 fprintf(stderr, "An exception has occured: %s\n",e.getFullDescription().c_str());
194#endif
195 }
196
197 return 0;
198}
199
运行效果图如下:
把这个例子跑通基础教程后面的部分就会很顺利了
最后,祝全体奋斗在IT基层行业的同行同胞们,五一愉快:P