Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Feb 26, 2011, 8:50:06 PM (14 years ago)
Author:
landauf
Message:

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
Location:
code/branches/usability/src/libraries/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/usability/src/libraries/tools/Shader.cc

    r7966 r7972  
    5353
    5454        this->scenemanager_ = scenemanager;
    55         this->compositorInstance_ = 0;
    5655        this->bVisible_ = true;
    5756        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         }
     57
     58        static bool hasCgProgramManager = Shader::hasCgProgramManager();
     59        Shader::bLoadedCgPlugin_s = hasCgProgramManager;
    7360
    7461        this->bLoadCompositor_ &= Shader::bLoadedCgPlugin_s;
     
    7764    Shader::~Shader()
    7865    {
    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;
     66        if (this->compositorInstance_ && GraphicsManager::getInstance().getViewport())
     67            Ogre::CompositorManager::getSingleton().removeCompositor(GraphicsManager::getInstance().getViewport(), this->compositorName_);
    9268    }
    9369
    9470    void Shader::cameraChanged(Ogre::Viewport* viewport, Ogre::Camera* oldCamera)
    9571    {
    96         if (!this->scenemanager_ || (viewport != this->scenemanager_->getCurrentViewport() && this->scenemanager_->getCurrentViewport() != NULL))
     72        if (!this->bLoadCompositor_ || !this->scenemanager_)
    9773            return;
     74
     75        // load the compositor if not already done
     76        if (!this->compositorName_.empty() && !this->compositorInstance_)
     77            this->changedCompositorName(viewport);
    9878
    9979        // update compositor in viewport (shader should only be active if the current camera is in the same scene as the shader)
     
    10888        // 1.7.2
    10989
    110 
    11190        if (oldCamera && this->scenemanager_ == oldCamera->getSceneManager())
    112             Ogre::CompositorManager::getSingleton().setCompositorEnabled(viewport, this->compositor_, false);
     91            Ogre::CompositorManager::getSingleton().setCompositorEnabled(viewport, this->compositorName_, false);
    11392
    11493        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()
     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)
    130106    {
    131107        if (this->bLoadCompositor_)
    132108        {
    133             Ogre::Viewport* viewport = GraphicsManager::getInstance().getViewport();
    134109            assert(viewport);
    135             if (!this->oldcompositor_.empty())
    136             {
    137                 Ogre::CompositorManager::getSingleton().removeCompositor(viewport, this->oldcompositor_);
     110            if (!this->oldcompositorName_.empty())
     111            {
     112                Ogre::CompositorManager::getSingleton().removeCompositor(viewport, this->oldcompositorName_);
    138113                this->compositorInstance_ = 0;
    139114            }
    140             if (!this->compositor_.empty())
    141             {
    142                 this->compositorInstance_ = Ogre::CompositorManager::getSingleton().addCompositor(viewport, this->compositor_);
     115            if (!this->compositorName_.empty())
     116            {
     117                this->compositorInstance_ = Ogre::CompositorManager::getSingleton().addCompositor(viewport, this->compositorName_);
    143118                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_;
     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_;
    148124        }
    149125    }
     
    151127    void Shader::updateVisibility()
    152128    {
    153         if (this->compositorInstance_ && this->scenemanager_)
    154             this->compositorInstance_->setEnabled(this->scenemanager_->getCurrentViewport() && this->isVisible());
     129        if (this->compositorInstance_)
     130            Ogre::CompositorManager::getSingleton().setCompositorEnabled(GraphicsManager::getInstance().getViewport(), this->compositorName_, this->isVisible());
    155131    }
    156132
     
    159135        if (Shader::_setParameter(material, technique, pass, parameter, value))
    160136        {
    161             if (this->bViewportInitialized_ && this->compositorInstance_ && this->isVisible())
    162             {
    163                 this->compositorInstance_->setEnabled(false);
    164                 this->compositorInstance_->setEnabled(true);
     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);
    165141            }
    166142        }
     
    171147        if (Shader::_setParameter(material, technique, pass, parameter, value))
    172148        {
    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)
     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)
    182158    {
    183159        ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter);
     
    204180    }
    205181
    206     bool Shader::_setParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter, int value)
     182    /* static */ bool Shader::_setParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter, int value)
    207183    {
    208184        ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter);
     
    229205    }
    230206
    231     float Shader::getParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter)
     207    /* static */ float Shader::getParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter)
    232208    {
    233209        ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter);
     
    243219    }
    244220
    245     bool Shader::getParameterIsFloat(const std::string& material, size_t technique, size_t pass, const std::string& parameter)
     221    /* static */ bool Shader::getParameterIsFloat(const std::string& material, size_t technique, size_t pass, const std::string& parameter)
    246222    {
    247223        ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter);
     
    252228    }
    253229
    254     bool Shader::getParameterIsInt(const std::string& material, size_t technique, size_t pass, const std::string& parameter)
     230    /* static */ bool Shader::getParameterIsInt(const std::string& material, size_t technique, size_t pass, const std::string& parameter)
    255231    {
    256232        ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter);
     
    261237    }
    262238
    263     Shader::ParameterPointer* Shader::getParameterPointer(const std::string& material, size_t technique, size_t pass, const std::string& parameter)
     239    /* static */ Shader::ParameterPointer* Shader::getParameterPointer(const std::string& material, size_t technique, size_t pass, const std::string& parameter)
    264240    {
    265241        if (!GameMode::showsGraphics() || !Shader::bLoadedCgPlugin_s)
     
    351327        return 0;
    352328    }
     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    }
    353341}
  • code/branches/usability/src/libraries/tools/Shader.h

    r7966 r7972  
    6565            void updateVisibility();
    6666
    67             inline void setCompositor(const std::string& compositor)
     67            inline void setCompositorName(const std::string& name)
    6868            {
    69                 if (this->compositor_ != compositor)
     69                if (this->compositorName_ != name)
    7070                {
    71                     this->compositor_ = compositor;
    72                     this->changedCompositor();
     71                    this->compositorName_ = name;
     72                    this->changedCompositorName();
    7373                }
    7474            }
    75             inline const std::string& getCompositor() const
    76                 { return this->compositor_; }
    77             void changedCompositor();
     75            inline const std::string& getCompositorName() const
     76                { return this->compositorName_; }
     77            void changedCompositorName();
     78            void changedCompositorName(Ogre::Viewport* viewport);
    7879
    79             void setSceneManager(Ogre::SceneManager* scenemanager);
     80            inline void setSceneManager(Ogre::SceneManager* scenemanager)
     81                { this->scenemanager_ = scenemanager; }
    8082            inline Ogre::SceneManager* getSceneManager() const
    8183                { return this->scenemanager_; }
     
    9496
    9597        private:
     98            static bool hasCgProgramManager();
     99
    96100            Ogre::SceneManager* scenemanager_;
    97101            Ogre::CompositorInstance* compositorInstance_;
    98102            bool bVisible_;
    99103            bool bLoadCompositor_;
    100             bool bViewportInitialized_;
    101             std::string compositor_;
    102             std::string oldcompositor_;
     104            std::string compositorName_;
     105            std::string oldcompositorName_;
    103106
    104107            static MaterialMap parameters_s;
Note: See TracChangeset for help on using the changeset viewer.