Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some changes related to camera switching:

  • added ViewportEventListener (currently only listens to camera changes in a viewport)
  • Shader now correctly updates its visibility if the camera changes the scene
  • (the same lines of code also fix the weird Ogre behavior which was originally fixed in CameraManager)
  • GraphicsManager also updates the GUIManager's camera
  • if all cameras are destroyed, CameraManager now officially switches to NULL camera
  • Property svn:eol-style set to native
File size: 14.0 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->compositorInstance_ = 0;
56        this->bVisible_ = true;
57        this->bLoadCompositor_ = GameMode::showsGraphics();
58        this->bViewportInitialized_ = true;
59
60        if (this->bLoadCompositor_ && Ogre::Root::getSingletonPtr())
61        {
62            Shader::bLoadedCgPlugin_s = false;
63            const Ogre::Root::PluginInstanceList& plugins = Ogre::Root::getSingleton().getInstalledPlugins();
64            for (size_t i = 0; i < plugins.size(); ++i)
65            {
66                if (plugins[i]->getName() == "Cg Program Manager")
67                {
68                    Shader::bLoadedCgPlugin_s = true;
69                    break;
70                }
71            }
72        }
73
74        this->bLoadCompositor_ &= Shader::bLoadedCgPlugin_s;
75    }
76
77    Shader::~Shader()
78    {
79        if (this->compositorInstance_ && this->bLoadCompositor_)
80        {
81            Ogre::Viewport* viewport = GraphicsManager::getInstance().getViewport();
82            assert(viewport);
83            Ogre::CompositorManager::getSingleton().removeCompositor(viewport, this->compositor_);
84        }
85
86    }
87
88    void Shader::setSceneManager(Ogre::SceneManager* scenemanager)
89    {
90        this->scenemanager_ = scenemanager;
91//        this->bViewportInitialized_ = false;
92    }
93
94    void Shader::cameraChanged(Ogre::Viewport* viewport, Ogre::Camera* oldCamera)
95    {
96        if (!this->scenemanager_ || (viewport != this->scenemanager_->getCurrentViewport() && this->scenemanager_->getCurrentViewport() != NULL))
97            return;
98
99        // update compositor in viewport (shader should only be active if the current camera is in the same scene as the shader)
100
101        // Note:
102        // The shader needs also to be switched off and on after changing the camera in the
103        // same scene to avoid weird behaviour with active compositors while switching the
104        // camera (like freezing the image)
105        //
106        // Last known Ogre version needing this workaround:
107        // 1.4.8
108        // 1.7.2
109
110
111        if (oldCamera && this->scenemanager_ == oldCamera->getSceneManager())
112            Ogre::CompositorManager::getSingleton().setCompositorEnabled(viewport, this->compositor_, false);
113
114        if (viewport->getCamera() && this->scenemanager_ == viewport->getCamera()->getSceneManager())
115            Ogre::CompositorManager::getSingleton().setCompositorEnabled(viewport, this->compositor_, this->isVisible());
116    }
117/*
118    void Shader::tick(float dt)
119    {
120        SUPER(Shader, tick, dt);
121
122        if (this->bLoadCompositor_ && !this->bViewportInitialized_ && this->scenemanager_ && this->scenemanager_->getCurrentViewport())
123        {
124            this->bViewportInitialized_ = true;
125            this->updateVisibility();
126        }
127    }
128*/
129    void Shader::changedCompositor()
130    {
131        if (this->bLoadCompositor_)
132        {
133            Ogre::Viewport* viewport = GraphicsManager::getInstance().getViewport();
134            assert(viewport);
135            if (!this->oldcompositor_.empty())
136            {
137                Ogre::CompositorManager::getSingleton().removeCompositor(viewport, this->oldcompositor_);
138                this->compositorInstance_ = 0;
139            }
140            if (!this->compositor_.empty())
141            {
142                this->compositorInstance_ = Ogre::CompositorManager::getSingleton().addCompositor(viewport, this->compositor_);
143                if (!this->compositorInstance_)
144                    COUT(2) << "Warning: Couldn't load compositor with name \"" << this->compositor_ << "\"." << std::endl;
145                //Ogre::CompositorManager::getSingleton().setCompositorEnabled(viewport, this->compositor_, this->bViewportInitialized_ && this->isVisible());
146            }
147            this->oldcompositor_ = this->compositor_;
148        }
149    }
150
151    void Shader::updateVisibility()
152    {
153        if (this->compositorInstance_ && this->scenemanager_)
154            this->compositorInstance_->setEnabled(this->scenemanager_->getCurrentViewport() && this->isVisible());
155    }
156
157    void Shader::setParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter, float value)
158    {
159        if (Shader::_setParameter(material, technique, pass, parameter, value))
160        {
161            if (this->bViewportInitialized_ && this->compositorInstance_ && this->isVisible())
162            {
163                this->compositorInstance_->setEnabled(false);
164                this->compositorInstance_->setEnabled(true);
165            }
166        }
167    }
168
169    void Shader::setParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter, int value)
170    {
171        if (Shader::_setParameter(material, technique, pass, parameter, value))
172        {
173            if (this->bViewportInitialized_ && this->compositorInstance_ && this->isVisible())
174            {
175                this->compositorInstance_->setEnabled(false);
176                this->compositorInstance_->setEnabled(true);
177            }
178        }
179    }
180
181    bool Shader::_setParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter, float value)
182    {
183        ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter);
184        if (pointer)
185        {
186            if (pointer->first)
187            {
188                if ((*static_cast<float*>(pointer->second)) != value)
189                {
190                    (*static_cast<float*>(pointer->second)) = value;
191                    return true;
192                }
193            }
194            else
195            {
196                if ((*static_cast<int*>(pointer->second)) != static_cast<int>(value))
197                {
198                    (*static_cast<int*>(pointer->second)) = static_cast<int>(value);
199                    return true;
200                }
201            }
202        }
203        return false;
204    }
205
206    bool Shader::_setParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter, int value)
207    {
208        ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter);
209        if (pointer)
210        {
211            if (pointer->first)
212            {
213                if ((*static_cast<float*>(pointer->second)) != static_cast<float>(value))
214                {
215                    (*static_cast<float*>(pointer->second)) = static_cast<float>(value);
216                    return true;
217                }
218            }
219            else
220            {
221                if ((*static_cast<int*>(pointer->second)) != value)
222                {
223                    (*static_cast<int*>(pointer->second)) = value;
224                    return true;
225                }
226            }
227        }
228        return false;
229    }
230
231    float Shader::getParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter)
232    {
233        ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter);
234        if (pointer)
235        {
236            if (pointer->first)
237                return (*static_cast<float*>(pointer->second));
238            else
239                return static_cast<float>(*static_cast<int*>(pointer->second));
240        }
241        else
242            return 0;
243    }
244
245    bool Shader::getParameterIsFloat(const std::string& material, size_t technique, size_t pass, const std::string& parameter)
246    {
247        ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter);
248        if (pointer)
249            return pointer->first;
250        else
251            return false;
252    }
253
254    bool Shader::getParameterIsInt(const std::string& material, size_t technique, size_t pass, const std::string& parameter)
255    {
256        ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter);
257        if (pointer)
258            return (!pointer->first);
259        else
260            return false;
261    }
262
263    Shader::ParameterPointer* Shader::getParameterPointer(const std::string& material, size_t technique, size_t pass, const std::string& parameter)
264    {
265        if (!GameMode::showsGraphics() || !Shader::bLoadedCgPlugin_s)
266            return 0;
267
268        MaterialMap::iterator material_iterator = Shader::parameters_s.find(material);
269        if (material_iterator != Shader::parameters_s.end())
270        {
271            TechniqueVector& technique_vector = material_iterator->second;
272            if (technique < technique_vector.size())
273            {
274                PassVector& pass_vector = technique_vector[technique];
275                if (pass < pass_vector.size())
276                {
277                    ParameterMap& parameter_map = pass_vector[pass];
278                    ParameterMap::iterator parameter_iterator = parameter_map.find(parameter);
279
280                    if (parameter_iterator != parameter_map.end())
281                        return (&parameter_iterator->second);
282                    else
283                        COUT(2) << "Warning: No shader parameter \"" << parameter << "\" in pass " << pass << " in technique " << technique << " in material \"" << material << "\"." << std::endl;
284                }
285                else
286                    COUT(2) << "Warning: No pass " << pass << " in technique " << technique << " in material \"" << material << "\" or pass has no shader." << std::endl;
287            }
288            else
289                COUT(2) << "Warning: No technique " << technique << " in material \"" << material << "\" or technique has no pass with shader." << std::endl;
290        }
291        else
292        {
293            bool foundAtLeastOneShaderParameter = false;
294            Ogre::MaterialManager::ResourceMapIterator iterator = Ogre::MaterialManager::getSingleton().getResourceIterator();
295            Ogre::Material* material_pointer = 0;
296
297            while (iterator.hasMoreElements())
298            {
299                Ogre::Resource* resource = iterator.getNext().get();
300                if (resource->getName() == material)
301                    material_pointer = (Ogre::Material*)resource;
302            }
303
304            if (!material_pointer)
305            {
306                COUT(2) << "Warning: No material with name \"" << material << "\" found." << std::endl;
307                return 0;
308            }
309
310            for (unsigned int t = 0; t < material_pointer->getNumTechniques(); ++t)
311            {
312                Ogre::Technique* technique_pointer = material_pointer->getTechnique(t);
313                if (!technique_pointer)
314                    continue;
315
316                for (unsigned int p = 0; p < technique_pointer->getNumPasses(); ++p)
317                {
318                    Ogre::Pass* pass_pointer = technique_pointer->getPass(p);
319                    if (!pass_pointer)
320                        continue;
321
322                    if (!pass_pointer->getFragmentProgramName().empty())
323                    {
324                        Ogre::GpuProgramParameters* parameter_pointer = pass_pointer->getFragmentProgramParameters().get();
325                        if (!parameter_pointer)
326                            continue;
327
328                        const Ogre::GpuConstantDefinitionMap& constant_definitions = parameter_pointer->getConstantDefinitions().map;
329                        for (Ogre::GpuConstantDefinitionMap::const_iterator definition_iterator = constant_definitions.begin(); definition_iterator != constant_definitions.end(); ++definition_iterator)
330                        {
331                            void* temp = (definition_iterator->second.isFloat())
332                                            ? static_cast<void*>(parameter_pointer->getFloatPointer(definition_iterator->second.physicalIndex))
333                                            : static_cast<void*>(parameter_pointer->getIntPointer(definition_iterator->second.physicalIndex));
334                            ParameterPointer parameter_pointer = ParameterPointer(definition_iterator->second.isFloat(), temp);
335
336                            TechniqueVector& technique_vector = Shader::parameters_s[material];
337                            technique_vector.resize(technique + 1);
338                            PassVector& pass_vector = technique_vector[technique];
339                            pass_vector.resize(pass + 1);
340                            pass_vector[pass][definition_iterator->first] = parameter_pointer;
341                            foundAtLeastOneShaderParameter = true;
342                        }
343                    }
344                }
345            }
346
347            // recursive call if the material was added to the map
348            if (foundAtLeastOneShaderParameter)
349                return Shader::getParameterPointer(material, technique, pass, parameter);
350        }
351        return 0;
352    }
353}
Note: See TracBrowser for help on using the repository browser.