Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/src/OgreCompositorManager.cpp @ 1

Last change on this file since 1 was 1, checked in by landauf, 17 years ago
File size: 8.4 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
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29#include "OgreStableHeaders.h"
30#include "OgreCompositorManager.h"
31#include "OgreCompositor.h"
32#include "OgreCompositorChain.h"
33#include "OgreCompositionPass.h"
34#include "OgreCompositionTargetPass.h"
35#include "OgreCompositionTechnique.h"
36#include "OgreRoot.h"
37
38namespace Ogre {
39
40template<> CompositorManager* Singleton<CompositorManager>::ms_Singleton = 0;
41CompositorManager* CompositorManager::getSingletonPtr(void)
42{
43        return ms_Singleton;
44}
45CompositorManager& CompositorManager::getSingleton(void)
46{ 
47        assert( ms_Singleton );  return ( *ms_Singleton ); 
48}//-----------------------------------------------------------------------
49CompositorManager::CompositorManager():
50        mRectangle(0)
51{
52        initialise();
53
54        // Loading order (just after materials)
55        mLoadOrder = 110.0f;
56        // Scripting is supported by this manager
57        mScriptPatterns.push_back("*.compositor");
58        ResourceGroupManager::getSingleton()._registerScriptLoader(this);
59
60        // Resource type
61        mResourceType = "Compositor";
62
63        // Create default thread serializer instance (also non-threaded)
64        OGRE_THREAD_POINTER_SET(mSerializer, new CompositorSerializer());
65
66        // Register with resource group manager
67        ResourceGroupManager::getSingleton()._registerResourceManager(mResourceType, this);
68
69}
70//-----------------------------------------------------------------------
71CompositorManager::~CompositorManager()
72{
73    freeChains();
74        delete mRectangle;
75
76        OGRE_THREAD_POINTER_DELETE(mSerializer);
77
78        // Resources cleared by superclass
79        // Unregister with resource group manager
80        ResourceGroupManager::getSingleton()._unregisterResourceManager(mResourceType);
81        ResourceGroupManager::getSingleton()._unregisterScriptLoader(this);
82}
83//-----------------------------------------------------------------------
84Resource* CompositorManager::createImpl(const String& name, ResourceHandle handle,
85    const String& group, bool isManual, ManualResourceLoader* loader,
86    const NameValuePairList* params)
87{
88    return new Compositor(this, name, handle, group, isManual, loader);
89}
90//-----------------------------------------------------------------------
91void CompositorManager::initialise(void)
92{
93    /// Create "default" compositor
94    /** Compositor that is used to implicitly represent the original
95        render in the chain. This is an identity compositor with only an output pass:
96    compositor Ogre/Scene
97    {
98        technique
99        {
100            target_output
101            {
102                                pass clear
103                                {
104                                        /// Clear frame
105                                }
106                pass render_scene
107                {
108                                        visibility_mask FFFFFFFF
109                                        render_queues SKIES_EARLY SKIES_LATE
110                }
111            }
112        }
113    };
114    */
115    CompositorPtr scene = create("Ogre/Scene", ResourceGroupManager::INTERNAL_RESOURCE_GROUP_NAME);
116    CompositionTechnique *t = scene->createTechnique();
117    CompositionTargetPass *tp = t->getOutputTargetPass();
118    tp->setVisibilityMask(0xFFFFFFFF);
119        {
120                CompositionPass *pass = tp->createPass();
121                pass->setType(CompositionPass::PT_CLEAR);
122        }
123        {
124                CompositionPass *pass = tp->createPass();
125                pass->setType(CompositionPass::PT_RENDERSCENE);
126                /// Render everything, including skies
127                pass->setFirstRenderQueue(RENDER_QUEUE_SKIES_EARLY);
128                pass->setLastRenderQueue(RENDER_QUEUE_SKIES_LATE);
129        }
130
131}
132//-----------------------------------------------------------------------
133void CompositorManager::parseScript(DataStreamPtr& stream, const String& groupName)
134{
135#if OGRE_THREAD_SUPPORT
136        // check we have an instance for this thread
137        if (!mSerializer.get())
138        {
139                // create a new instance for this thread - will get deleted when
140                // the thread dies
141                mSerializer.reset(new CompositorSerializer());
142        }
143#endif
144    mSerializer->parseScript(stream, groupName);
145}
146//-----------------------------------------------------------------------
147CompositorChain *CompositorManager::getCompositorChain(Viewport *vp)
148{
149    Chains::iterator i=mChains.find(vp);
150    if(i != mChains.end())
151    {
152        return i->second;
153    }
154    else
155    {
156        CompositorChain *chain = new CompositorChain(vp);
157        mChains[vp] = chain;
158        return chain;
159    }
160}
161//-----------------------------------------------------------------------
162bool CompositorManager::hasCompositorChain(Viewport *vp) const
163{
164    return mChains.find(vp) != mChains.end();
165}
166//-----------------------------------------------------------------------
167void CompositorManager::removeCompositorChain(Viewport *vp)
168{
169    Chains::iterator i = mChains.find(vp);
170    if (i != mChains.end())
171    {
172        delete i->second;
173        mChains.erase(i);
174    }
175}
176//-----------------------------------------------------------------------
177void CompositorManager::removeAll(void)
178{
179        freeChains();
180        ResourceManager::removeAll();
181}
182//-----------------------------------------------------------------------
183void CompositorManager::freeChains()
184{
185    Chains::iterator i, iend=mChains.end();
186    for(i=mChains.begin(); i!=iend;++i)
187    {
188        delete i->second;
189    }
190    mChains.clear();
191}
192//-----------------------------------------------------------------------
193Renderable *CompositorManager::_getTexturedRectangle2D()
194{
195        if(!mRectangle)
196        {
197                /// 2D rectangle, to use for render_quad passes
198                mRectangle = new Rectangle2D(true);
199        }
200        RenderSystem* rs = Root::getSingleton().getRenderSystem();
201        Viewport* vp = rs->_getViewport();
202        Real hOffset = rs->getHorizontalTexelOffset() / (0.5 * vp->getActualWidth());
203        Real vOffset = rs->getVerticalTexelOffset() / (0.5 * vp->getActualHeight());
204        mRectangle->setCorners(-1 + hOffset, 1 - vOffset, 1 + hOffset, -1 - vOffset);
205        return mRectangle;
206}
207//-----------------------------------------------------------------------
208CompositorInstance *CompositorManager::addCompositor(Viewport *vp, const String &compositor, int addPosition)
209{
210        CompositorPtr comp = getByName(compositor);
211        if(comp.isNull())
212                return 0;
213        CompositorChain *chain = getCompositorChain(vp);
214        return chain->addCompositor(comp, addPosition==-1 ? CompositorChain::LAST : (size_t)addPosition);
215}
216//-----------------------------------------------------------------------
217void CompositorManager::removeCompositor(Viewport *vp, const String &compositor)
218{
219        CompositorChain *chain = getCompositorChain(vp);
220        CompositorChain::InstanceIterator it = chain->getCompositors();
221        for(size_t pos=0; pos < chain->getNumCompositors(); ++pos)
222        {
223                CompositorInstance *instance = chain->getCompositor(pos);
224                if(instance->getCompositor()->getName() == compositor)
225                {
226                        chain->removeCompositor(pos);
227                        break;
228                }
229        }
230}
231//-----------------------------------------------------------------------
232void CompositorManager::setCompositorEnabled(Viewport *vp, const String &compositor, bool value)
233{
234        CompositorChain *chain = getCompositorChain(vp);
235        CompositorChain::InstanceIterator it = chain->getCompositors();
236        for(size_t pos=0; pos < chain->getNumCompositors(); ++pos)
237        {
238                CompositorInstance *instance = chain->getCompositor(pos);
239                if(instance->getCompositor()->getName() == compositor)
240                {
241                        chain->setCompositorEnabled(pos, value);
242                        break;
243                }
244        }
245}
246
247}
Note: See TracBrowser for help on using the repository browser.