1 | #include <OgreNoMemoryMacros.h> |
---|
2 | |
---|
3 | |
---|
4 | |
---|
5 | #include "CEGUI/CEGUI.h" |
---|
6 | #include "OgreCEGUIRenderer.h" |
---|
7 | #include "OgreConfigFile.h" |
---|
8 | #include "OgreStringConverter.h" |
---|
9 | #include "OgreException.h" |
---|
10 | #include "OgreFrameListener.h" |
---|
11 | #include "ExampleApplication.h" |
---|
12 | #include "OgreInstancedGeometry.h" |
---|
13 | |
---|
14 | using namespace Ogre; |
---|
15 | |
---|
16 | #define maxObjectsPerBatch 4 |
---|
17 | const size_t numTypeMeshes = 6; |
---|
18 | #define FLT_MAX 3.402823466e+38F /* max value */ |
---|
19 | class CrowdApplication; |
---|
20 | |
---|
21 | enum CurrentGeomOpt{ |
---|
22 | INSTANCE_OPT, |
---|
23 | ENTITY_OPT |
---|
24 | }; |
---|
25 | |
---|
26 | CEGUI::MouseButton convertOISMouseButtonToCegui(int buttonID) |
---|
27 | { |
---|
28 | switch (buttonID) |
---|
29 | { |
---|
30 | case 0: return CEGUI::LeftButton; |
---|
31 | case 1: return CEGUI::RightButton; |
---|
32 | case 2: return CEGUI::MiddleButton; |
---|
33 | case 3: return CEGUI::X1Button; |
---|
34 | default: return CEGUI::LeftButton; |
---|
35 | } |
---|
36 | } |
---|
37 | //out of the listener class...if set as a member, the sample crashes when moving the mouse! |
---|
38 | |
---|
39 | // Event handler to add ability to alter subdivision |
---|
40 | class CrowdListener : public ExampleFrameListener, public OIS::KeyListener, public OIS::MouseListener |
---|
41 | { |
---|
42 | protected: |
---|
43 | CrowdApplication * mMain; |
---|
44 | bool mRequestShutDown; |
---|
45 | bool mLMBDown; |
---|
46 | bool mRMBDown; |
---|
47 | double mAvgFrameTime; |
---|
48 | size_t meshSelected; |
---|
49 | size_t numMesh; |
---|
50 | size_t objectCount; |
---|
51 | String mDebugText; |
---|
52 | |
---|
53 | CurrentGeomOpt currentGeomOpt; |
---|
54 | |
---|
55 | size_t numRender; |
---|
56 | |
---|
57 | Ogre::Timer*timer; |
---|
58 | double mLastTime,mBurnAmount; |
---|
59 | |
---|
60 | std::vector <InstancedGeometry *> renderInstance; |
---|
61 | std::vector <StaticGeometry *> renderStatic; |
---|
62 | std::vector <Entity *> renderEntity; |
---|
63 | std::vector <AnimationState*> animations; |
---|
64 | |
---|
65 | std::vector <SceneNode *> nodes; |
---|
66 | std::vector <Vector3 *> posMatrices; |
---|
67 | |
---|
68 | CEGUI::Renderer* mGUIRenderer; |
---|
69 | CEGUI::Window* mGuiAvg; |
---|
70 | CEGUI::Window* mGuiCurr; |
---|
71 | CEGUI::Window* mGuiBest; |
---|
72 | CEGUI::Window* mGuiWorst; |
---|
73 | CEGUI::Window* mGuiTris; |
---|
74 | CEGUI::Window* mGuiDbg; |
---|
75 | CEGUI::Window* mRoot; |
---|
76 | CEGUI::Point mLastMousePosition; |
---|
77 | public: |
---|
78 | |
---|
79 | //----------------------------------------------------------------------- |
---|
80 | CrowdListener(RenderWindow* win, Camera* cam,CEGUI::Renderer* renderer, CrowdApplication*main); |
---|
81 | //----------------------------------------------------------------------- |
---|
82 | ~CrowdListener(); |
---|
83 | //----------------------------------------------------------------------- |
---|
84 | bool frameStarted(const FrameEvent& evt); |
---|
85 | //----------------------------------------------------------------------- |
---|
86 | void burnCPU(void); |
---|
87 | //----------------------------------------------------------------------- |
---|
88 | void destroyCurrentGeomOpt(); |
---|
89 | //----------------------------------------------------------------------- |
---|
90 | void createCurrentGeomOpt(); |
---|
91 | //----------------------------------------------------------------------- |
---|
92 | void createInstanceGeom(); |
---|
93 | //----------------------------------------------------------------------- |
---|
94 | void destroyInstanceGeom(); |
---|
95 | //----------------------------------------------------------------------- |
---|
96 | void setupInstancedMaterialToEntity(Entity*); |
---|
97 | //----------------------------------------------------------------------- |
---|
98 | String buildCrowdMaterial(const String &,int); |
---|
99 | //----------------------------------------------------------------------- |
---|
100 | void createStaticGeom(); |
---|
101 | //----------------------------------------------------------------------- |
---|
102 | void destroyStaticGeom(); |
---|
103 | //----------------------------------------------------------------------- |
---|
104 | void createEntityGeom(); |
---|
105 | //----------------------------------------------------------------------- |
---|
106 | void destroyEntityGeom(); |
---|
107 | //----------------------------------------------------------------------- |
---|
108 | void setObjectCount(size_t val) |
---|
109 | { |
---|
110 | numMesh=val; |
---|
111 | }; |
---|
112 | //----------------------------------------------------------------------- |
---|
113 | void setBurnedTime(double timeBurned) |
---|
114 | { |
---|
115 | mBurnAmount=timeBurned; |
---|
116 | }; |
---|
117 | //----------------------------------------------------------------------- |
---|
118 | void changSelectedMesh(size_t number) |
---|
119 | { |
---|
120 | meshSelected=number; |
---|
121 | } |
---|
122 | bool mouseMoved( const OIS::MouseEvent &arg ); |
---|
123 | //----------------------------------------------------------------// |
---|
124 | bool mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id ); |
---|
125 | //----------------------------------------------------------------// |
---|
126 | bool mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id ); |
---|
127 | //----------------------------------------------------------------------- |
---|
128 | void mouseReleased (OIS::MouseEvent *e); |
---|
129 | void mouseClicked(OIS::MouseEvent* e); |
---|
130 | void mouseEntered(OIS::MouseEvent* e) ; |
---|
131 | void mouseExited(OIS::MouseEvent* e); |
---|
132 | void requestShutdown(void); |
---|
133 | void setCurrentGeometryOpt(CurrentGeomOpt opt); |
---|
134 | bool handleMouseMove(const CEGUI::EventArgs& e); |
---|
135 | //-------------------------------------------------------------------------- |
---|
136 | bool handleMouseButtonUp(const CEGUI::EventArgs& e); |
---|
137 | //-------------------------------------------------------------------------- |
---|
138 | bool handleMouseButtonDown(const CEGUI::EventArgs& e); |
---|
139 | //-------------------------------------------------------------------------- |
---|
140 | void updateStats(void); |
---|
141 | //----------------------------------------------------------------// |
---|
142 | bool keyPressed( const OIS::KeyEvent &arg ) |
---|
143 | { |
---|
144 | if( arg.key == OIS::KC_ESCAPE ) |
---|
145 | mRequestShutDown = true; |
---|
146 | return true; |
---|
147 | } |
---|
148 | |
---|
149 | //----------------------------------------------------------------// |
---|
150 | bool keyReleased( const OIS::KeyEvent &arg ) |
---|
151 | { |
---|
152 | return true; |
---|
153 | } |
---|
154 | |
---|
155 | }; |
---|
156 | |
---|
157 | //----------------------------------------------------------------------- |
---|
158 | //----------------------------------------------------------------------- |
---|
159 | class CrowdApplication : public ExampleApplication |
---|
160 | { |
---|
161 | |
---|
162 | public: |
---|
163 | //----------------------------------------------------------------------- |
---|
164 | CrowdApplication():mGUIRenderer(0), |
---|
165 | mGUISystem(0), |
---|
166 | mEditorGuiSheet(0) { } |
---|
167 | //----------------------------------------------------------------------- |
---|
168 | ~CrowdApplication() |
---|
169 | { |
---|
170 | if(mEditorGuiSheet) |
---|
171 | { |
---|
172 | CEGUI::WindowManager::getSingleton().destroyWindow(mEditorGuiSheet); |
---|
173 | } |
---|
174 | if(mGUISystem) |
---|
175 | { |
---|
176 | delete mGUISystem; |
---|
177 | mGUISystem = 0; |
---|
178 | } |
---|
179 | if(mGUIRenderer) |
---|
180 | { |
---|
181 | delete mGUIRenderer; |
---|
182 | mGUIRenderer = 0; |
---|
183 | } |
---|
184 | |
---|
185 | } |
---|
186 | RenderWindow*getRenderWindow(void) |
---|
187 | { |
---|
188 | return mWindow; |
---|
189 | } |
---|
190 | |
---|
191 | protected: |
---|
192 | CEGUI::OgreCEGUIRenderer* mGUIRenderer; |
---|
193 | CEGUI::System* mGUISystem; |
---|
194 | CEGUI::Window* mEditorGuiSheet; |
---|
195 | //----------------------------------------------------------------------- |
---|
196 | // Just override the mandatory scene manager choice method |
---|
197 | virtual void chooseSceneManager(void) |
---|
198 | { |
---|
199 | // Get the SceneManager, in this case a generic one |
---|
200 | mSceneMgr = mRoot->createSceneManager("TerrainSceneManager"); |
---|
201 | } |
---|
202 | //----------------------------------------------------------------------- |
---|
203 | // Just override the mandatory create scene method |
---|
204 | |
---|
205 | void createScene(void) |
---|
206 | { |
---|
207 | std::string terrain_cfg("terrain.cfg"); |
---|
208 | mSceneMgr -> setWorldGeometry( terrain_cfg ); |
---|
209 | // Set ambient light |
---|
210 | mSceneMgr->setAmbientLight(ColourValue(0.2, 0.2, 0.2)); |
---|
211 | |
---|
212 | Light* l = mSceneMgr->createLight("MainLight"); |
---|
213 | |
---|
214 | //add a skybox |
---|
215 | mSceneMgr->setSkyBox(true, "Examples/MorningSkyBox", 1000); |
---|
216 | |
---|
217 | //setup the light |
---|
218 | l->setType(Light::LT_DIRECTIONAL); |
---|
219 | Vector3 dir(-0.3, -1, 0.2); |
---|
220 | dir.normalise(); |
---|
221 | l->setDirection(dir); |
---|
222 | l->setDiffuseColour(1.0, 1.0, 0.8); |
---|
223 | |
---|
224 | mCamera->setPosition(700,100, 500); |
---|
225 | mCamera->lookAt(600,80,600); |
---|
226 | |
---|
227 | mSceneMgr->setShadowTextureSize(1024); |
---|
228 | setupGUI(); |
---|
229 | setupEventHandlers(); |
---|
230 | //set up the compositor |
---|
231 | Ogre::Viewport* pViewport = mWindow->getViewport(0); |
---|
232 | pViewport->setCamera(mCamera); |
---|
233 | CompositorManager::getSingleton().addCompositor(pViewport,"Bloom"); |
---|
234 | } |
---|
235 | //----------------------------------------------------------------------- |
---|
236 | void destroyScene(void) |
---|
237 | { |
---|
238 | } |
---|
239 | //----------------------------------------------------------------------- |
---|
240 | void createFrameListener(void) |
---|
241 | { |
---|
242 | // This is where we instantiate our own frame listener |
---|
243 | mFrameListener= new CrowdListener(mWindow, mCamera,mGUIRenderer,this); |
---|
244 | mRoot->addFrameListener(mFrameListener); |
---|
245 | } |
---|
246 | //----------------------------------------------------------------------- |
---|
247 | void setupResources(void) |
---|
248 | { |
---|
249 | ExampleApplication::setupResources(); |
---|
250 | |
---|
251 | /*ResourceGroupManager *rsm = ResourceGroupManager::getSingletonPtr (); |
---|
252 | StringVector groups = rsm->getResourceGroups (); |
---|
253 | if (std::find(groups.begin(), groups.end(), String("Crowd")) == groups.end()) |
---|
254 | { |
---|
255 | rsm->createResourceGroup("Crowd"); |
---|
256 | try |
---|
257 | { |
---|
258 | rsm->addResourceLocation("../../../../../ogreaddons/Crowd/Media/materials/programs","FileSystem", "Crowd"); |
---|
259 | rsm->addResourceLocation("../../../../../ogreaddons/Crowd/Media/materials/scripts","FileSystem", "Crowd"); |
---|
260 | rsm->addResourceLocation("../../../../../ogreaddons/Crowd/Media/models","FileSystem", "Crowd"); |
---|
261 | } |
---|
262 | catch (Ogre::Exception& e) |
---|
263 | { |
---|
264 | String error = e.getFullDescription(); |
---|
265 | rsm->addResourceLocation("../../../Crowd/Media/materials/programs","FileSystem", "Crowd"); |
---|
266 | rsm->addResourceLocation("../../../Crowd/Media/materials/scripts","FileSystem", "Crowd"); |
---|
267 | rsm->addResourceLocation("../../../Crowd/Media/models","FileSystem", "Crowd"); |
---|
268 | } |
---|
269 | }*/ |
---|
270 | } |
---|
271 | void setupGUI(void) |
---|
272 | { |
---|
273 | // Set up GUI system |
---|
274 | mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY, true, 3000, mSceneMgr); |
---|
275 | mGUISystem = new CEGUI::System(mGUIRenderer); |
---|
276 | CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::Informative); |
---|
277 | |
---|
278 | CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme"); |
---|
279 | mGUISystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow"); |
---|
280 | CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseMoveCursor"); |
---|
281 | mEditorGuiSheet= CEGUI::WindowManager::getSingleton().createWindow((CEGUI::utf8*)"DefaultWindow", (CEGUI::utf8*)"Sheet"); |
---|
282 | mGUISystem->setGUISheet(mEditorGuiSheet); |
---|
283 | |
---|
284 | |
---|
285 | |
---|
286 | mEditorGuiSheet = CEGUI::WindowManager::getSingleton().loadWindowLayout((CEGUI::utf8*)"CrowdDemo.layout"); |
---|
287 | mGUISystem->setGUISheet(mEditorGuiSheet); |
---|
288 | } |
---|
289 | void setupEventHandlers(void) |
---|
290 | { |
---|
291 | using namespace CEGUI; |
---|
292 | CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); |
---|
293 | wmgr.getWindow((CEGUI::utf8*)"ExitDemoBtn")->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&CrowdApplication::handleQuit, this)); |
---|
294 | |
---|
295 | wmgr.getWindow((CEGUI::utf8*)"tInstancing")->subscribeEvent(CEGUI::RadioButton::EventSelectStateChanged, CEGUI::Event::Subscriber(&CrowdApplication::handleTechniqueChanged, this)); |
---|
296 | wmgr.getWindow((CEGUI::utf8*)"tIndependantEntities")->subscribeEvent(CEGUI::RadioButton::EventSelectStateChanged, CEGUI::Event::Subscriber(&CrowdApplication::handleTechniqueChanged, this)); |
---|
297 | wmgr.getWindow((CEGUI::utf8*)"Object Count")->subscribeEvent(CEGUI::Scrollbar::EventScrollPositionChanged,CEGUI::Event::Subscriber(&CrowdApplication::handleObjectCountChanged, this)); |
---|
298 | wmgr.getWindow((CEGUI::utf8*)"Shadows")->subscribeEvent(CEGUI::Checkbox::EventCheckStateChanged,CEGUI::Event::Subscriber(&CrowdApplication::handleShadowsChanged, this)); |
---|
299 | |
---|
300 | wmgr.getWindow((CEGUI::utf8*)"Time Burner")->subscribeEvent(CEGUI::Scrollbar::EventScrollPositionChanged,CEGUI::Event::Subscriber(&CrowdApplication::handleTimeBurnerChanged, this)); |
---|
301 | wmgr.getWindow((CEGUI::utf8*)"PostEffect")->subscribeEvent(CEGUI::Checkbox::EventCheckStateChanged,CEGUI::Event::Subscriber(&CrowdApplication::handlePostEffectChanged, this)); |
---|
302 | |
---|
303 | |
---|
304 | CEGUI::Window* wndw = CEGUI::WindowManager::getSingleton().getWindow("root"); |
---|
305 | |
---|
306 | wndw->subscribeEvent(Window::EventMouseMove, CEGUI::Event::Subscriber(&CrowdApplication::handleMouseMove,this)); |
---|
307 | |
---|
308 | wndw->subscribeEvent(Window::EventMouseButtonUp, CEGUI::Event::Subscriber(&CrowdApplication::handleMouseButtonUp,this)); |
---|
309 | |
---|
310 | wndw->subscribeEvent(Window::EventMouseButtonDown, CEGUI::Event::Subscriber(&CrowdApplication::handleMouseButtonDown,this)); |
---|
311 | |
---|
312 | } |
---|
313 | //----------------------------------------------------------------------- |
---|
314 | bool handleQuit(const CEGUI::EventArgs& e) |
---|
315 | { |
---|
316 | static_cast<CrowdListener*>(mFrameListener)->requestShutdown(); |
---|
317 | return true; |
---|
318 | } |
---|
319 | //----------------------------------------------------------------------- |
---|
320 | bool handleMouseMove(const CEGUI::EventArgs& e) |
---|
321 | { |
---|
322 | static_cast<CrowdListener*>(mFrameListener)->handleMouseMove(e); |
---|
323 | return true; |
---|
324 | } |
---|
325 | //----------------------------------------------------------------------- |
---|
326 | bool handleMouseButtonDown(const CEGUI::EventArgs& e) |
---|
327 | { |
---|
328 | static_cast<CrowdListener*>(mFrameListener)->handleMouseButtonDown(e); |
---|
329 | return true; |
---|
330 | } |
---|
331 | //----------------------------------------------------------------------- |
---|
332 | bool handleMouseButtonUp(const CEGUI::EventArgs& e) |
---|
333 | { |
---|
334 | static_cast<CrowdListener*>(mFrameListener)->handleMouseButtonUp(e); |
---|
335 | return true; |
---|
336 | } |
---|
337 | //----------------------------------------------------------------------- |
---|
338 | bool handleTechniqueChanged(const CEGUI::EventArgs& e) |
---|
339 | { |
---|
340 | |
---|
341 | static_cast<CrowdListener*>(mFrameListener)->destroyCurrentGeomOpt(); |
---|
342 | |
---|
343 | CEGUI::uint id = ((CEGUI::RadioButton*)((const CEGUI::WindowEventArgs&)e).window)->getSelectedButtonInGroup()->getID(); |
---|
344 | if (id == 0) |
---|
345 | { |
---|
346 | static_cast<CrowdListener*>(mFrameListener)->setCurrentGeometryOpt(INSTANCE_OPT); |
---|
347 | } |
---|
348 | if (id == 1) |
---|
349 | { |
---|
350 | static_cast<CrowdListener*>(mFrameListener)->setCurrentGeometryOpt(ENTITY_OPT); |
---|
351 | } |
---|
352 | static_cast<CrowdListener*>(mFrameListener)->createCurrentGeomOpt(); |
---|
353 | return true; |
---|
354 | } |
---|
355 | //----------------------------------------------------------------------- |
---|
356 | bool handleObjectCountChanged(const CEGUI::EventArgs& e) |
---|
357 | { |
---|
358 | static_cast<CrowdListener*>(mFrameListener)->destroyCurrentGeomOpt(); |
---|
359 | float scrollval = ((CEGUI::Scrollbar*)((const CEGUI::WindowEventArgs&)e).window)->getScrollPosition(); |
---|
360 | int objectCount = 1000*scrollval; |
---|
361 | static_cast<CrowdListener*>(mFrameListener)->setObjectCount(objectCount ); |
---|
362 | static_cast<CrowdListener*>(mFrameListener)->createCurrentGeomOpt(); |
---|
363 | String value=StringConverter::toString(objectCount); |
---|
364 | CEGUI::WindowManager::getSingleton().getWindow((CEGUI::utf8*)"Object Count Number")->setText(CEGUI::String(value.c_str())); |
---|
365 | |
---|
366 | return true; |
---|
367 | } |
---|
368 | //----------------------------------------------------------------------- |
---|
369 | bool handleTimeBurnerChanged(const CEGUI::EventArgs& e) |
---|
370 | { |
---|
371 | |
---|
372 | float scrollval = ((CEGUI::Scrollbar*)((const CEGUI::WindowEventArgs&)e).window)->getScrollPosition(); |
---|
373 | double timeBurned = 0.0166f*scrollval; |
---|
374 | char* timeChar= new char[10]; |
---|
375 | sprintf(timeChar,"%0.1f ms",timeBurned*1000.0f); |
---|
376 | CEGUI::String timeText(timeChar); |
---|
377 | CEGUI::WindowManager::getSingleton().getWindow("Time Burner Value")->setText(timeText); |
---|
378 | |
---|
379 | static_cast<CrowdListener*>(mFrameListener)->setBurnedTime(timeBurned); |
---|
380 | delete[] timeChar; |
---|
381 | return true; |
---|
382 | } |
---|
383 | //----------------------------------------------------------------------- |
---|
384 | bool handlePostEffectChanged(const CEGUI::EventArgs& e) |
---|
385 | { |
---|
386 | Ogre::Viewport* pViewport = mWindow->getViewport(0); |
---|
387 | if(((CEGUI::Checkbox*)((const CEGUI::WindowEventArgs&)e).window)->isSelected()) |
---|
388 | { |
---|
389 | CompositorManager::getSingleton().setCompositorEnabled(pViewport,"Bloom",true); |
---|
390 | } |
---|
391 | else |
---|
392 | { |
---|
393 | CompositorManager::getSingleton().setCompositorEnabled(pViewport,"Bloom",false); |
---|
394 | } |
---|
395 | return true; |
---|
396 | } |
---|
397 | //----------------------------------------------------------------------- |
---|
398 | bool handleShadowsChanged(const CEGUI::EventArgs&e) |
---|
399 | { |
---|
400 | if(((CEGUI::Checkbox*)((const CEGUI::WindowEventArgs&)e).window)->isSelected()) |
---|
401 | { |
---|
402 | mSceneMgr->setShadowTechnique(SHADOWTYPE_TEXTURE_MODULATIVE); |
---|
403 | LiSPSMShadowCameraSetup* lispsm = new LiSPSMShadowCameraSetup(); |
---|
404 | lispsm->setOptimalAdjustFactor(0.3); |
---|
405 | mSceneMgr->setShadowCameraSetup(ShadowCameraSetupPtr(lispsm)); |
---|
406 | mSceneMgr->setShadowColour(ColourValue(0.5, 0.5, 0.5)); |
---|
407 | } |
---|
408 | else |
---|
409 | { |
---|
410 | mSceneMgr->setShadowTechnique(SHADOWTYPE_NONE); |
---|
411 | } |
---|
412 | return true; |
---|
413 | } |
---|
414 | |
---|
415 | |
---|
416 | |
---|
417 | }; |
---|