Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/Samples/Instancing/include/Instancing.h @ 3

Last change on this file since 3 was 3, checked in by anonymous, 17 years ago

=update

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