Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/usability/src/libraries/tools/Shader.cc @ 7972

Last change on this file since 7972 was 7972, checked in by landauf, 13 years ago

slight rework of the Shader class:

  • correctly loads and initializes active and inactive shaders in different scenes
  • uses the fast Ogre::CompositorManager::getSingleton().setCompositorEnabled() instead of the slow compositorInstance_→setEnabled() to change the visibility of a shader
  • uses consistently GraphicsManager::getInstance().getViewport() to get the viewport - this is not optimal, scenemanager→getCurrentViewport() would be better, however this returns NULL when displaying a new scene for the first time
  • Property svn:eol-style set to native
File size: 14.4 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "Shader.h"
30
31#include <OgreCompositorManager.h>
32#include <OgreCompositorInstance.h>
33#include <OgreSceneManager.h>
34#include <OgreRoot.h>
35#include <OgrePlugin.h>
36#include <OgreMaterial.h>
37#include <OgreTechnique.h>
38#include <OgrePass.h>
39#include <OgreMaterialManager.h>
40
41#include "core/CoreIncludes.h"
42#include "core/GameMode.h"
43#include "core/GraphicsManager.h"
44
45namespace orxonox
46{
47    bool Shader::bLoadedCgPlugin_s = false;
48    Shader::MaterialMap Shader::parameters_s;
49
50    Shader::Shader(Ogre::SceneManager* scenemanager) : compositorInstance_(0)
51    {
52        RegisterObject(Shader);
53
54        this->scenemanager_ = scenemanager;
55        this->bVisible_ = true;
56        this->bLoadCompositor_ = GameMode::showsGraphics();
57
58        static bool hasCgProgramManager = Shader::hasCgProgramManager();
59        Shader::bLoadedCgPlugin_s = hasCgProgramManager;
60
61        this->bLoadCompositor_ &= Shader::bLoadedCgPlugin_s;
62    }
63
64    Shader::~Shader()
65    {
66        if (this->compositorInstance_ && GraphicsManager::getInstance().getViewport())
67            Ogre::CompositorManager::getSingleton().removeCompositor(GraphicsManager::getInstance().getViewport(), this->compositorName_);
68    }
69
70    void Shader::cameraChanged(Ogre::Viewport* viewport, Ogre::Camera* oldCamera)
71    {
72        if (!this->bLoadCompositor_ || !this->scenemanager_)
73            return;
74
75        // load the compositor if not already done
76        if (!this->compositorName_.empty() && !this->compositorInstance_)
77            this->changedCompositorName(viewport);
78
79        // update compositor in viewport (shader should only be active if the current camera is in the same scene as the shader)
80
81        // Note:
82        // The shader needs also to be switched off and on after changing the camera in the
83        // same scene to avoid weird behaviour with active compositors while switching the
84        // camera (like freezing the image)
85        //
86        // Last known Ogre version needing this workaround:
87        // 1.4.8
88        // 1.7.2
89
90        if (oldCamera && this->scenemanager_ == oldCamera->getSceneManager())
91            Ogre::CompositorManager::getSingleton().setCompositorEnabled(viewport, this->compositorName_, false);
92
93        if (viewport->getCamera() && this->scenemanager_ == viewport->getCamera()->getSceneManager())
94            Ogre::CompositorManager::getSingleton().setCompositorEnabled(viewport, this->compositorName_, this->isVisible());
95    }
96
97    void Shader::changedCompositorName()
98    {
99        // For the moment, we get the viewport always from the graphics manager
100        // TODO: Try to support multiple viewports - note however that scenemanager_->getCurrentViewport() returns NULL
101        //       after switching to a camera in a different scene (only for the first time this scene is displayed though)
102        this->changedCompositorName(GraphicsManager::getInstance().getViewport());
103    }
104
105    void Shader::changedCompositorName(Ogre::Viewport* viewport)
106    {
107        if (this->bLoadCompositor_)
108        {
109            assert(viewport);
110            if (!this->oldcompositorName_.empty())
111            {
112                Ogre::CompositorManager::getSingleton().removeCompositor(viewport, this->oldcompositorName_);
113                this->compositorInstance_ = 0;
114            }
115            if (!this->compositorName_.empty())
116            {
117                this->compositorInstance_ = Ogre::CompositorManager::getSingleton().addCompositor(viewport, this->compositorName_);
118                if (!this->compositorInstance_)
119                    COUT(2) << "Warning: Couldn't load compositor with name \"" << this->compositorName_ << "\"." << std::endl;
120                else if (viewport->getCamera())
121                    Ogre::CompositorManager::getSingleton().setCompositorEnabled(viewport, this->compositorName_, this->isVisible() && viewport->getCamera() && this->scenemanager_ == viewport->getCamera()->getSceneManager());
122            }
123            this->oldcompositorName_ = this->compositorName_;
124        }
125    }
126
127    void Shader::updateVisibility()
128    {
129        if (this->compositorInstance_)
130            Ogre::CompositorManager::getSingleton().setCompositorEnabled(GraphicsManager::getInstance().getViewport(), this->compositorName_, this->isVisible());
131    }
132
133    void Shader::setParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter, float value)
134    {
135        if (Shader::_setParameter(material, technique, pass, parameter, value))
136        {
137            if (this->compositorInstance_ && this->isVisible())
138            {
139                Ogre::CompositorManager::getSingleton().setCompositorEnabled(GraphicsManager::getInstance().getViewport(), this->compositorName_, false);
140                Ogre::CompositorManager::getSingleton().setCompositorEnabled(GraphicsManager::getInstance().getViewport(), this->compositorName_, true);
141            }
142        }
143    }
144
145    void Shader::setParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter, int value)
146    {
147        if (Shader::_setParameter(material, technique, pass, parameter, value))
148        {
149            if (this->compositorInstance_ && this->isVisible())
150            {
151                Ogre::CompositorManager::getSingleton().setCompositorEnabled(GraphicsManager::getInstance().getViewport(), this->compositorName_, false);
152                Ogre::CompositorManager::getSingleton().setCompositorEnabled(GraphicsManager::getInstance().getViewport(), this->compositorName_, true);
153            }
154        }
155    }
156
157    /* static */ bool Shader::_setParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter, float value)
158    {
159        ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter);
160        if (pointer)
161        {
162            if (pointer->first)
163            {
164                if ((*static_cast<float*>(pointer->second)) != value)
165                {
166                    (*static_cast<float*>(pointer->second)) = value;
167                    return true;
168                }
169            }
170            else
171            {
172                if ((*static_cast<int*>(pointer->second)) != static_cast<int>(value))
173                {
174                    (*static_cast<int*>(pointer->second)) = static_cast<int>(value);
175                    return true;
176                }
177            }
178        }
179        return false;
180    }
181
182    /* static */ bool Shader::_setParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter, int value)
183    {
184        ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter);
185        if (pointer)
186        {
187            if (pointer->first)
188            {
189                if ((*static_cast<float*>(pointer->second)) != static_cast<float>(value))
190                {
191                    (*static_cast<float*>(pointer->second)) = static_cast<float>(value);
192                    return true;
193                }
194            }
195            else
196            {
197                if ((*static_cast<int*>(pointer->second)) != value)
198                {
199                    (*static_cast<int*>(pointer->second)) = value;
200                    return true;
201                }
202            }
203        }
204        return false;
205    }
206
207    /* static */ float Shader::getParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter)
208    {
209        ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter);
210        if (pointer)
211        {
212            if (pointer->first)
213                return (*static_cast<float*>(pointer->second));
214            else
215                return static_cast<float>(*static_cast<int*>(pointer->second));
216        }
217        else
218            return 0;
219    }
220
221    /* static */ bool Shader::getParameterIsFloat(const std::string& material, size_t technique, size_t pass, const std::string& parameter)
222    {
223        ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter);
224        if (pointer)
225            return pointer->first;
226        else
227            return false;
228    }
229
230    /* static */ bool Shader::getParameterIsInt(const std::string& material, size_t technique, size_t pass, const std::string& parameter)
231    {
232        ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter);
233        if (pointer)
234            return (!pointer->first);
235        else
236            return false;
237    }
238
239    /* static */ Shader::ParameterPointer* Shader::getParameterPointer(const std::string& material, size_t technique, size_t pass, const std::string& parameter)
240    {
241        if (!GameMode::showsGraphics() || !Shader::bLoadedCgPlugin_s)
242            return 0;
243
244        MaterialMap::iterator material_iterator = Shader::parameters_s.find(material);
245        if (material_iterator != Shader::parameters_s.end())
246        {
247            TechniqueVector& technique_vector = material_iterator->second;
248            if (technique < technique_vector.size())
249            {
250                PassVector& pass_vector = technique_vector[technique];
251                if (pass < pass_vector.size())
252                {
253                    ParameterMap& parameter_map = pass_vector[pass];
254                    ParameterMap::iterator parameter_iterator = parameter_map.find(parameter);
255
256                    if (parameter_iterator != parameter_map.end())
257                        return (&parameter_iterator->second);
258                    else
259                        COUT(2) << "Warning: No shader parameter \"" << parameter << "\" in pass " << pass << " in technique " << technique << " in material \"" << material << "\"." << std::endl;
260                }
261                else
262                    COUT(2) << "Warning: No pass " << pass << " in technique " << technique << " in material \"" << material << "\" or pass has no shader." << std::endl;
263            }
264            else
265                COUT(2) << "Warning: No technique " << technique << " in material \"" << material << "\" or technique has no pass with shader." << std::endl;
266        }
267        else
268        {
269            bool foundAtLeastOneShaderParameter = false;
270            Ogre::MaterialManager::ResourceMapIterator iterator = Ogre::MaterialManager::getSingleton().getResourceIterator();
271            Ogre::Material* material_pointer = 0;
272
273            while (iterator.hasMoreElements())
274            {
275                Ogre::Resource* resource = iterator.getNext().get();
276                if (resource->getName() == material)
277                    material_pointer = (Ogre::Material*)resource;
278            }
279
280            if (!material_pointer)
281            {
282                COUT(2) << "Warning: No material with name \"" << material << "\" found." << std::endl;
283                return 0;
284            }
285
286            for (unsigned int t = 0; t < material_pointer->getNumTechniques(); ++t)
287            {
288                Ogre::Technique* technique_pointer = material_pointer->getTechnique(t);
289                if (!technique_pointer)
290                    continue;
291
292                for (unsigned int p = 0; p < technique_pointer->getNumPasses(); ++p)
293                {
294                    Ogre::Pass* pass_pointer = technique_pointer->getPass(p);
295                    if (!pass_pointer)
296                        continue;
297
298                    if (!pass_pointer->getFragmentProgramName().empty())
299                    {
300                        Ogre::GpuProgramParameters* parameter_pointer = pass_pointer->getFragmentProgramParameters().get();
301                        if (!parameter_pointer)
302                            continue;
303
304                        const Ogre::GpuConstantDefinitionMap& constant_definitions = parameter_pointer->getConstantDefinitions().map;
305                        for (Ogre::GpuConstantDefinitionMap::const_iterator definition_iterator = constant_definitions.begin(); definition_iterator != constant_definitions.end(); ++definition_iterator)
306                        {
307                            void* temp = (definition_iterator->second.isFloat())
308                                            ? static_cast<void*>(parameter_pointer->getFloatPointer(definition_iterator->second.physicalIndex))
309                                            : static_cast<void*>(parameter_pointer->getIntPointer(definition_iterator->second.physicalIndex));
310                            ParameterPointer parameter_pointer = ParameterPointer(definition_iterator->second.isFloat(), temp);
311
312                            TechniqueVector& technique_vector = Shader::parameters_s[material];
313                            technique_vector.resize(technique + 1);
314                            PassVector& pass_vector = technique_vector[technique];
315                            pass_vector.resize(pass + 1);
316                            pass_vector[pass][definition_iterator->first] = parameter_pointer;
317                            foundAtLeastOneShaderParameter = true;
318                        }
319                    }
320                }
321            }
322
323            // recursive call if the material was added to the map
324            if (foundAtLeastOneShaderParameter)
325                return Shader::getParameterPointer(material, technique, pass, parameter);
326        }
327        return 0;
328    }
329
330    /* static */ bool Shader::hasCgProgramManager()
331    {
332        if (Ogre::Root::getSingletonPtr())
333        {
334            const Ogre::Root::PluginInstanceList& plugins = Ogre::Root::getSingleton().getInstalledPlugins();
335            for (size_t i = 0; i < plugins.size(); ++i)
336                if (plugins[i]->getName() == "Cg Program Manager")
337                    return true;
338        }
339        return false;
340    }
341}
Note: See TracBrowser for help on using the repository browser.