Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/Samples/Compositor/src/Compositor.cpp @ 3

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

=update

File size: 17.9 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2006 Torus Knot Software Ltd
8Also see acknowledgements in Readme.html
9
10You may use this sample code for anything you like, it is not covered by the
11LGPL like the rest of the engine.
12-----------------------------------------------------------------------------
13*/
14
15/**
16    \file
17        Compositor.cpp
18    \brief
19        Shows OGRE's Compositor feature
20        \author
21                W.J. :wumpus: van der Laan
22                        Ogre composition framework
23                Manuel Bua
24                        Postfilter ideas and original out-of-core implementation
25        Jeff (nfz) Doyle
26            added gui framework to demo
27*/
28
29#include <Ogre.h>
30
31#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
32#define WIN32_LEAN_AND_MEAN
33#include "windows.h"
34#endif
35
36
37#include "Compositor.h"
38#include "CompositorDemo_FrameListener.h"
39
40/**********************************************************************
41OS X Specific Resource Location Finding
42**********************************************************************/
43#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
44
45Ogre::String bundlePath()
46{
47    char path[1024];
48    CFBundleRef mainBundle = CFBundleGetMainBundle();
49    assert(mainBundle);
50   
51    CFURLRef mainBundleURL = CFBundleCopyBundleURL( mainBundle);
52    assert(mainBundleURL);
53   
54    CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle);
55    assert(cfStringRef);
56   
57    CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII);
58   
59    CFRelease(mainBundleURL);
60    CFRelease(cfStringRef);
61   
62    return Ogre::String(path);
63}
64
65#endif
66
67/*************************************************************************
68                            CompositorDemo Methods
69*************************************************************************/
70    CompositorDemo::~CompositorDemo()
71    {
72        delete mGUISystem;
73        delete mGUIRenderer;
74        delete mFrameListener;
75
76        delete mRoot;
77    }
78
79//--------------------------------------------------------------------------
80    void CompositorDemo::go(void)
81    {
82        if (!setup())
83            return;
84
85        mRoot->startRendering();
86    }
87
88//--------------------------------------------------------------------------
89    bool CompositorDemo::setup(void)
90    {
91                #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
92            Ogre::String mResourcePath;
93            mResourcePath = bundlePath() + "/Contents/Resources/";
94            mRoot = new Ogre::Root(mResourcePath + "plugins.cfg", 
95                               mResourcePath + "ogre.cfg", mResourcePath + "Ogre.log");
96        #else
97               
98                        mRoot = new Ogre::Root();
99               
100                #endif
101
102        setupResources();
103        bool carryOn = configure();
104        if (!carryOn) return false;
105
106        chooseSceneManager();
107        createCamera();
108        createViewports();
109
110        // Set default mipmap level (NB some APIs ignore this)
111        Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
112        loadResources();
113
114        // Create the scene
115        createScene();
116
117        createFrameListener();
118
119        // load some GUI stuff for demo.
120        //loadAllMaterialControlFiles(mMaterialControlsContainer);
121
122        return true;
123
124    }
125
126//--------------------------------------------------------------------------
127    bool CompositorDemo::configure(void)
128    {
129        // Show the configuration dialog and initialise the system
130        // You can skip this and use root.restoreConfig() to load configuration
131        // settings if you were sure there are valid ones saved in ogre.cfg
132        if(mRoot->showConfigDialog())
133        {
134            // If returned true, user clicked OK so initialise
135            // Here we choose to let the system create a default rendering window by passing 'true'
136            mWindow = mRoot->initialise(true);
137            return true;
138        }
139        else
140        {
141            return false;
142        }
143    }
144
145//--------------------------------------------------------------------------
146    void CompositorDemo::chooseSceneManager(void)
147    {
148        // Get the SceneManager, in this case a generic one
149        mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC, "ExampleSMInstance");
150    }
151
152//--------------------------------------------------------------------------
153    void CompositorDemo::createCamera(void)
154    {
155        // Create the camera
156        mCamera = mSceneMgr->createCamera("PlayerCam");
157
158        // Position it at 500 in Z direction
159        mCamera->setPosition(Ogre::Vector3(0,0,0));
160        // Look back along -Z
161        mCamera->lookAt(Ogre::Vector3(0,0,-300));
162        mCamera->setNearClipDistance(1);
163
164    }
165
166//--------------------------------------------------------------------------
167void CompositorDemo::createViewports(void)
168{
169    // Create one viewport, entire window
170    Ogre::Viewport* vp = mWindow->addViewport(mCamera);
171    vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
172
173    // Alter the camera aspect ratio to match the viewport
174    mCamera->setAspectRatio(
175        Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
176}
177
178//--------------------------------------------------------------------------
179    void CompositorDemo::setupResources(void)
180    {
181        // Load resource paths from config file
182        Ogre::ConfigFile cf;
183               
184                #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
185                Ogre::String mResourcePath;
186                mResourcePath = bundlePath() + "/Contents/Resources/";
187                cf.load(mResourcePath + "resources.cfg");
188        #else
189               
190                        cf.load("resources.cfg");
191               
192                #endif
193
194        // Go through all sections & settings in the file
195        Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
196
197        Ogre::String secName, typeName, archName;
198        while (seci.hasMoreElements())
199        {
200            secName = seci.peekNextKey();
201            Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
202            Ogre::ConfigFile::SettingsMultiMap::iterator i;
203            for (i = settings->begin(); i != settings->end(); ++i)
204            {
205                typeName = i->first;
206                archName = i->second;
207#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
208                // OS X does not set the working directory relative to the app,
209                // In order to make things portable on OS X we need to provide
210                // the loading with it's own bundle path location
211                Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
212                    Ogre::String(bundlePath() + "/" + archName), typeName, secName);
213#else
214                Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
215                    archName, typeName, secName);
216#endif
217
218            }
219        }
220
221        Ogre::LogManager::getSingleton().logMessage( "Resource directories setup" );
222
223    }
224
225//-----------------------------------------------------------------------------------
226        void CompositorDemo::loadResources(void)
227        {
228                // Initialise, parse all scripts etc
229        Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
230
231        }
232
233//-----------------------------------------------------------------------------------
234    void CompositorDemo::createScene(void)
235    {
236                mSceneMgr->setShadowTechnique(Ogre::SHADOWTYPE_TEXTURE_MODULATIVE);
237                mSceneMgr->setShadowFarDistance(1000);
238        // setup GUI system
239        mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr);
240        // load scheme and set up defaults
241        mGUISystem = new CEGUI::System(mGUIRenderer, (CEGUI::ResourceProvider *)0, (CEGUI::XMLParser*)0,
242            (CEGUI::ScriptModule*)0, (CEGUI::utf8*)"CompositorDemoCegui.config");
243        CEGUI::System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
244
245                Ogre::MovableObject::setDefaultVisibilityFlags(0x00000001);
246
247                // Set ambient light
248                mSceneMgr->setAmbientLight(Ogre::ColourValue(0.3, 0.3, 0.2));
249
250                Ogre::Light* l = mSceneMgr->createLight("Light2");
251                Ogre::Vector3 dir(-1,-1,0);
252                dir.normalise();
253                l->setType(Ogre::Light::LT_DIRECTIONAL);
254                l->setDirection(dir);
255                l->setDiffuseColour(1, 1, 0.8);
256                l->setSpecularColour(1, 1, 1);
257
258
259                Ogre::Entity* pEnt;
260
261                // House
262                pEnt = mSceneMgr->createEntity( "1", "tudorhouse.mesh" );
263                Ogre::SceneNode* n1 = mSceneMgr->getRootSceneNode()->createChildSceneNode(Ogre::Vector3(350, 450, -200));
264                n1->attachObject( pEnt );
265
266                pEnt = mSceneMgr->createEntity( "2", "tudorhouse.mesh" );
267                Ogre::SceneNode* n2 = mSceneMgr->getRootSceneNode()->createChildSceneNode(Ogre::Vector3(-350, 450, -200));
268                n2->attachObject( pEnt );
269
270                pEnt = mSceneMgr->createEntity( "3", "knot.mesh" );
271                mSpinny = mSceneMgr->getRootSceneNode()->createChildSceneNode(Ogre::Vector3(0, 0, 300));
272                mSpinny->attachObject( pEnt );
273                pEnt->setMaterialName("Examples/MorningCubeMap");
274
275                mSceneMgr->setSkyBox(true, "Examples/MorningSkyBox");
276
277
278                Ogre::Plane plane;
279                plane.normal = Ogre::Vector3::UNIT_Y;
280                plane.d = 100;
281                Ogre::MeshManager::getSingleton().createPlane("Myplane",
282                        Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane,
283                        1500, 1500, 10, 10, true, 1, 5, 5, Ogre::Vector3::UNIT_Z);
284                Ogre::Entity* pPlaneEnt = mSceneMgr->createEntity( "plane", "Myplane" );
285                pPlaneEnt->setMaterialName("Examples/Rockwall");
286                pPlaneEnt->setCastShadows(false);
287                mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(pPlaneEnt);
288
289                mCamera->setPosition(-400, 50, 900);
290                mCamera->lookAt(0,80,0);
291
292        connectEventHandlers();
293                /// Create a couple of hard coded postfilter effects as an example of how to do it
294                /// but the preferred method is to use compositor scripts.
295                createEffects();
296    }
297//-----------------------------------------------------------------------------------
298    void CompositorDemo::createFrameListener(void)
299    {
300        mFrameListener = new CompositorDemo_FrameListener(this);
301                mFrameListener->setSpinningNode(mSpinny);
302
303    }
304//--------------------------------------------------------------------------
305    void CompositorDemo::connectEventHandlers(void)
306    {
307        CEGUI::WindowManager::getSingleton().getWindow("ExitDemoBtn")->
308            subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&CompositorDemo::handleQuit, this));
309    }
310
311//-----------------------------------------------------------------------------------
312        /// Create the hard coded postfilter effects
313        void CompositorDemo::createEffects(void)
314        {
315            // Bloom compositor is loaded from script but here is the hard coded equivalent
316//              CompositorPtr comp = CompositorManager::getSingleton().create(
317//                              "Bloom", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME
318//                      );
319//              {
320//                      CompositionTechnique *t = comp->createTechnique();
321//                      {
322//                              CompositionTechnique::TextureDefinition *def = t->createTextureDefinition("rt0");
323//                              def->width = 128;
324//                              def->height = 128;
325//                              def->format = PF_A8R8G8B8;
326//                      }
327//                      {
328//                              CompositionTechnique::TextureDefinition *def = t->createTextureDefinition("rt1");
329//                              def->width = 128;
330//                              def->height = 128;
331//                              def->format = PF_A8R8G8B8;
332//                      }
333//                      {
334//                              CompositionTargetPass *tp = t->createTargetPass();
335//                              tp->setInputMode(CompositionTargetPass::IM_PREVIOUS);
336//                              tp->setOutputName("rt1");
337//                      }
338//                      {
339//                              CompositionTargetPass *tp = t->createTargetPass();
340//                              tp->setInputMode(CompositionTargetPass::IM_NONE);
341//                              tp->setOutputName("rt0");
342//                              CompositionPass *pass = tp->createPass();
343//                              pass->setType(CompositionPass::PT_RENDERQUAD);
344//                              pass->setMaterialName("Ogre/Compositor/Blur0");
345//                              pass->setInput(0, "rt1");
346//                      }
347//                      {
348//                              CompositionTargetPass *tp = t->createTargetPass();
349//                              tp->setInputMode(CompositionTargetPass::IM_NONE);
350//                              tp->setOutputName("rt1");
351//                              CompositionPass *pass = tp->createPass();
352//                              pass->setType(CompositionPass::PT_RENDERQUAD);
353//                              pass->setMaterialName("Ogre/Compositor/Blur1");
354//                              pass->setInput(0, "rt0");
355//                      }
356//                      {
357//                              CompositionTargetPass *tp = t->getOutputTargetPass();
358//                              tp->setInputMode(CompositionTargetPass::IM_PREVIOUS);
359//                              { CompositionPass *pass = tp->createPass();
360//                              pass->setType(CompositionPass::PT_RENDERQUAD);
361//                              pass->setMaterialName("Ogre/Compositor/BloomBlend");
362//                              pass->setInput(0, "rt1");
363//                              }
364//                      }
365//              }
366            // Glass compositor is loaded from script but here is the hard coded equivalent
367                /// Glass effect
368//              CompositorPtr comp2 = CompositorManager::getSingleton().create(
369//                              "Glass", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME
370//                      );
371//              {
372//                      CompositionTechnique *t = comp2->createTechnique();
373//                      {
374//                              CompositionTechnique::TextureDefinition *def = t->createTextureDefinition("rt0");
375//                              def->width = 0;
376//                              def->height = 0;
377//                              def->format = PF_R8G8B8;
378//                      }
379//                      {
380//                              CompositionTargetPass *tp = t->createTargetPass();
381//                              tp->setInputMode(CompositionTargetPass::IM_PREVIOUS);
382//                              tp->setOutputName("rt0");
383//                      }
384//                      {
385//                              CompositionTargetPass *tp = t->getOutputTargetPass();
386//                              tp->setInputMode(CompositionTargetPass::IM_NONE);
387//                              { CompositionPass *pass = tp->createPass();
388//                              pass->setType(CompositionPass::PT_RENDERQUAD);
389//                              pass->setMaterialName("Ogre/Compositor/GlassPass");
390//                              pass->setInput(0, "rt0");
391//                              }
392//                      }
393//              }
394                /// Motion blur effect
395                Ogre::CompositorPtr comp3 = Ogre::CompositorManager::getSingleton().create(
396                                "Motion Blur", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME
397                        );
398                {
399                        Ogre::CompositionTechnique *t = comp3->createTechnique();
400                        {
401                                Ogre::CompositionTechnique::TextureDefinition *def = t->createTextureDefinition("scene");
402                                def->width = 0;
403                                def->height = 0;
404                                def->format = Ogre::PF_R8G8B8;
405                        }
406                        {
407                                Ogre::CompositionTechnique::TextureDefinition *def = t->createTextureDefinition("sum");
408                                def->width = 0;
409                                def->height = 0;
410                                def->format = Ogre::PF_R8G8B8;
411                        }
412                        {
413                                Ogre::CompositionTechnique::TextureDefinition *def = t->createTextureDefinition("temp");
414                                def->width = 0;
415                                def->height = 0;
416                                def->format = Ogre::PF_R8G8B8;
417                        }
418                        /// Render scene
419                        {
420                                Ogre::CompositionTargetPass *tp = t->createTargetPass();
421                                tp->setInputMode(Ogre::CompositionTargetPass::IM_PREVIOUS);
422                                tp->setOutputName("scene");
423                        }
424                        /// Initialisation pass for sum texture
425                        {
426                                Ogre::CompositionTargetPass *tp = t->createTargetPass();
427                                tp->setInputMode(Ogre::CompositionTargetPass::IM_PREVIOUS);
428                                tp->setOutputName("sum");
429                                tp->setOnlyInitial(true);
430                        }
431                        /// Do the motion blur
432                        {
433                                Ogre::CompositionTargetPass *tp = t->createTargetPass();
434                                tp->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
435                                tp->setOutputName("temp");
436                                { Ogre::CompositionPass *pass = tp->createPass();
437                                pass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
438                                pass->setMaterialName("Ogre/Compositor/Combine");
439                                pass->setInput(0, "scene");
440                                pass->setInput(1, "sum");
441                                }
442                        }
443                        /// Copy back sum texture
444                        {
445                                Ogre::CompositionTargetPass *tp = t->createTargetPass();
446                                tp->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
447                                tp->setOutputName("sum");
448                                { Ogre::CompositionPass *pass = tp->createPass();
449                                pass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
450                                pass->setMaterialName("Ogre/Compositor/Copyback");
451                                pass->setInput(0, "temp");
452                                }
453                        }
454                        /// Display result
455                        {
456                                Ogre::CompositionTargetPass *tp = t->getOutputTargetPass();
457                                tp->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
458                                { Ogre::CompositionPass *pass = tp->createPass();
459                                pass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
460                                pass->setMaterialName("Ogre/Compositor/MotionBlur");
461                                pass->setInput(0, "sum");
462                                }
463                        }
464                }
465                /// Heat vision effect
466                Ogre::CompositorPtr comp4 = Ogre::CompositorManager::getSingleton().create(
467                                "Heat Vision", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME
468                        );
469                {
470                        Ogre::CompositionTechnique *t = comp4->createTechnique();
471                        {
472                                Ogre::CompositionTechnique::TextureDefinition *def = t->createTextureDefinition("scene");
473                                def->width = 256;
474                                def->height = 256;
475                                def->format = Ogre::PF_R8G8B8;
476                        }
477                        {
478                                Ogre::CompositionTechnique::TextureDefinition *def = t->createTextureDefinition("temp");
479                                def->width = 256;
480                                def->height = 256;
481                                def->format = Ogre::PF_R8G8B8;
482                        }
483                        /// Render scene
484                        {
485                                Ogre::CompositionTargetPass *tp = t->createTargetPass();
486                                tp->setInputMode(Ogre::CompositionTargetPass::IM_PREVIOUS);
487                                tp->setOutputName("scene");
488                        }
489                        /// Light to heat pass
490                        {
491                                Ogre::CompositionTargetPass *tp = t->createTargetPass();
492                                tp->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
493                                tp->setOutputName("temp");
494                                {
495                                        Ogre::CompositionPass *pass = tp->createPass();
496                                        pass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
497                                        pass->setIdentifier(0xDEADBABE); /// Identify pass for use in listener
498                                        pass->setMaterialName("Fury/HeatVision/LightToHeat");
499                                        pass->setInput(0, "scene");
500                                }
501                        }
502                        /// Display result
503                        {
504                                Ogre::CompositionTargetPass *tp = t->getOutputTargetPass();
505                                tp->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
506                                {
507                                        Ogre::CompositionPass *pass = tp->createPass();
508                                        pass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
509                                        pass->setMaterialName("Fury/HeatVision/Blur");
510                                        pass->setInput(0, "temp");
511                                }
512                        }
513                }
514        }
515//--------------------------------------------------------------------------
516    bool CompositorDemo::handleQuit(const CEGUI::EventArgs& e)
517    {
518        mRoot->queueEndRendering();
519        return true;
520    }
521
522
523#ifdef __cplusplus
524extern "C" {
525#endif
526
527#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
528        INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
529#else
530        int main(int argc, char *argv[])
531#endif
532{
533   // Create application object
534    CompositorDemo app;
535
536    try {
537        app.go();
538    } catch( Ogre::Exception& e ) {
539#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
540        MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
541#else
542        std::cerr << "An exception has occured: " << e.getFullDescription();
543#endif
544    }
545
546
547    return 0;
548}
549
550#ifdef __cplusplus
551}
552#endif
Note: See TracBrowser for help on using the repository browser.