| [2350] | 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 <OgreRoot.h> | 
|---|
 | 33 | #include <OgrePlugin.h> | 
|---|
 | 34 |  | 
|---|
| [3196] | 35 | #include "core/CoreIncludes.h" | 
|---|
 | 36 | #include "core/GameMode.h" | 
|---|
| [3370] | 37 | #include "core/GraphicsManager.h" | 
|---|
| [3196] | 38 |  | 
|---|
| [2350] | 39 | namespace orxonox | 
|---|
 | 40 | { | 
|---|
| [9667] | 41 |     RegisterClassNoArgs(Shader); | 
|---|
 | 42 |  | 
|---|
| [8079] | 43 |     /** | 
|---|
 | 44 |         @brief Initializes the values and sets the scene manager. | 
|---|
 | 45 |     */ | 
|---|
| [11071] | 46 |     Shader::Shader(Ogre::SceneManager* scenemanager) : compositorInstance_(nullptr) | 
|---|
| [2350] | 47 |     { | 
|---|
 | 48 |         RegisterObject(Shader); | 
|---|
 | 49 |  | 
|---|
 | 50 |         this->scenemanager_ = scenemanager; | 
|---|
 | 51 |         this->bVisible_ = true; | 
|---|
| [2896] | 52 |         this->bLoadCompositor_ = GameMode::showsGraphics(); | 
|---|
| [8079] | 53 |         this->registeredAsListener_ = false; | 
|---|
| [2350] | 54 |  | 
|---|
| [8079] | 55 |         static bool hasCgProgramManager = Shader::hasCgProgramManager(); | 
|---|
| [2350] | 56 |  | 
|---|
| [8079] | 57 |         this->bLoadCompositor_ &= hasCgProgramManager; | 
|---|
| [2350] | 58 |     } | 
|---|
 | 59 |  | 
|---|
| [8079] | 60 |     /** | 
|---|
 | 61 |         @brief Removes the compositor and frees the resources. | 
|---|
 | 62 |     */ | 
|---|
| [2350] | 63 |     Shader::~Shader() | 
|---|
 | 64 |     { | 
|---|
| [8079] | 65 |         if (this->compositorInstance_ && GraphicsManager::getInstance().getViewport()) | 
|---|
 | 66 |             Ogre::CompositorManager::getSingleton().removeCompositor(GraphicsManager::getInstance().getViewport(), this->compositorName_); | 
|---|
| [2350] | 67 |     } | 
|---|
 | 68 |  | 
|---|
| [8079] | 69 |     /** | 
|---|
 | 70 |         @brief Inherited from ViewportEventListener - called if the camera changes. | 
|---|
 | 71 |  | 
|---|
 | 72 |         Since the new camera could be in a different scene, the shader has to make sure | 
|---|
 | 73 |         it deactivates or activates itself accordingly. | 
|---|
 | 74 |  | 
|---|
 | 75 |         Additionally the shader has to be turned off and on even if the camera stays in | 
|---|
 | 76 |         the same scene to fix a weird behavior of Ogre. | 
|---|
 | 77 |     */ | 
|---|
 | 78 |     void Shader::cameraChanged(Ogre::Viewport* viewport, Ogre::Camera* oldCamera) | 
|---|
| [2350] | 79 |     { | 
|---|
| [8079] | 80 |         if (!this->bLoadCompositor_ || !this->scenemanager_) | 
|---|
 | 81 |             return; | 
|---|
 | 82 |  | 
|---|
 | 83 |         // load the compositor if not already done | 
|---|
 | 84 |         if (!this->compositorName_.empty() && !this->compositorInstance_) | 
|---|
 | 85 |             this->changedCompositorName(viewport); | 
|---|
 | 86 |  | 
|---|
 | 87 |         // update compositor in viewport (shader should only be active if the current camera is in the same scene as the shader) | 
|---|
 | 88 |  | 
|---|
 | 89 |         // Note: | 
|---|
 | 90 |         // The shader needs also to be switched off and on after changing the camera in the | 
|---|
 | 91 |         // same scene to avoid weird behaviour with active compositors while switching the | 
|---|
 | 92 |         // camera (like freezing the image) | 
|---|
 | 93 |         // | 
|---|
 | 94 |         // Last known Ogre version needing this workaround: | 
|---|
 | 95 |         // 1.4.8 | 
|---|
 | 96 |         // 1.7.2 | 
|---|
 | 97 |  | 
|---|
 | 98 |         if (oldCamera && this->scenemanager_ == oldCamera->getSceneManager()) | 
|---|
 | 99 |             Ogre::CompositorManager::getSingleton().setCompositorEnabled(viewport, this->compositorName_, false); | 
|---|
 | 100 |  | 
|---|
 | 101 |         if (viewport->getCamera() && this->scenemanager_ == viewport->getCamera()->getSceneManager()) | 
|---|
 | 102 |             Ogre::CompositorManager::getSingleton().setCompositorEnabled(viewport, this->compositorName_, this->isVisible()); | 
|---|
| [2350] | 103 |     } | 
|---|
 | 104 |  | 
|---|
| [8079] | 105 |     /** | 
|---|
 | 106 |         @brief Changes the compositor - default viewport. | 
|---|
 | 107 |     */ | 
|---|
 | 108 |     void Shader::changedCompositorName() | 
|---|
| [2350] | 109 |     { | 
|---|
| [8079] | 110 |         // For the moment, we get the viewport always from the graphics manager | 
|---|
| [11071] | 111 |         // TODO: Try to support multiple viewports - note however that scenemanager_->getCurrentViewport() returns nullptr | 
|---|
| [8079] | 112 |         //       after switching to a camera in a different scene (only for the first time this scene is displayed though) | 
|---|
 | 113 |         this->changedCompositorName(GraphicsManager::getInstance().getViewport()); | 
|---|
| [2350] | 114 |     } | 
|---|
 | 115 |  | 
|---|
| [8079] | 116 |     /** | 
|---|
 | 117 |         @brief Changes the compositor. | 
|---|
 | 118 |     */ | 
|---|
 | 119 |     void Shader::changedCompositorName(Ogre::Viewport* viewport) | 
|---|
| [2350] | 120 |     { | 
|---|
 | 121 |         if (this->bLoadCompositor_) | 
|---|
 | 122 |         { | 
|---|
 | 123 |             assert(viewport); | 
|---|
| [8079] | 124 |             if (this->compositorInstance_) | 
|---|
| [2350] | 125 |             { | 
|---|
| [8079] | 126 |                 // remove the old compositor, remove the listener | 
|---|
 | 127 |                 Ogre::CompositorManager::getSingleton().removeCompositor(viewport, this->oldcompositorName_); | 
|---|
 | 128 |                 this->compositorInstance_->removeListener(this); | 
|---|
| [11071] | 129 |                 this->compositorInstance_ = nullptr; | 
|---|
| [2350] | 130 |             } | 
|---|
| [8079] | 131 |             if (!this->compositorName_.empty()) | 
|---|
| [2350] | 132 |             { | 
|---|
| [8079] | 133 |                 // add the new compositor | 
|---|
 | 134 |                 this->compositorInstance_ = Ogre::CompositorManager::getSingleton().addCompositor(viewport, this->compositorName_); | 
|---|
 | 135 |                 if (this->compositorInstance_) | 
|---|
 | 136 |                 { | 
|---|
 | 137 |                     // register as listener if required | 
|---|
 | 138 |                     if (this->registeredAsListener_) | 
|---|
 | 139 |                         this->compositorInstance_->addListener(this); | 
|---|
 | 140 |                     // set visibility according to the isVisible() and the camera/viewport | 
|---|
 | 141 |                     if (viewport->getCamera()) | 
|---|
 | 142 |                         Ogre::CompositorManager::getSingleton().setCompositorEnabled(viewport, this->compositorName_, this->isVisible() && viewport->getCamera() && this->scenemanager_ == viewport->getCamera()->getSceneManager()); | 
|---|
 | 143 |                 } | 
|---|
 | 144 |                 else | 
|---|
| [8858] | 145 |                     orxout(internal_warning) << "Couldn't load compositor with name \"" << this->compositorName_ << "\"." << endl; | 
|---|
| [2350] | 146 |             } | 
|---|
| [8079] | 147 |             this->oldcompositorName_ = this->compositorName_; | 
|---|
| [2350] | 148 |         } | 
|---|
 | 149 |     } | 
|---|
 | 150 |  | 
|---|
| [8079] | 151 |     /** | 
|---|
 | 152 |         @brief Changes the visibility of the shader. Doesn't free any resources if set to invisible. | 
|---|
 | 153 |     */ | 
|---|
| [2350] | 154 |     void Shader::updateVisibility() | 
|---|
 | 155 |     { | 
|---|
| [8079] | 156 |         if (this->compositorInstance_) | 
|---|
 | 157 |             Ogre::CompositorManager::getSingleton().setCompositorEnabled(GraphicsManager::getInstance().getViewport(), this->compositorName_, this->isVisible()); | 
|---|
| [2350] | 158 |     } | 
|---|
 | 159 |  | 
|---|
| [8079] | 160 |     /** | 
|---|
 | 161 |         @brief Defines a new integer value for a given parameter. The parameter will be updated if the compositor is rendered the next time. | 
|---|
 | 162 |     */ | 
|---|
| [10727] | 163 |     void Shader::setParameter(unsigned short technique, unsigned short pass, const std::string& parameter, int value) | 
|---|
| [2350] | 164 |     { | 
|---|
| [9550] | 165 |         ParameterContainer container = {technique, pass, parameter, value}; | 
|---|
| [8079] | 166 |         this->parameters_.push_back(container); | 
|---|
 | 167 |         this->addAsListener(); | 
|---|
| [2350] | 168 |     } | 
|---|
 | 169 |  | 
|---|
| [8079] | 170 |     /** | 
|---|
 | 171 |         @brief Defines a new float value for a given parameter. The parameter will be updated if the compositor is rendered the next time. | 
|---|
 | 172 |     */ | 
|---|
| [10727] | 173 |     void Shader::setParameter(unsigned short technique, unsigned short pass, const std::string& parameter, float value) | 
|---|
| [2350] | 174 |     { | 
|---|
| [9550] | 175 |         ParameterContainer container = {technique, pass, parameter, value}; | 
|---|
| [8079] | 176 |         this->parameters_.push_back(container); | 
|---|
 | 177 |         this->addAsListener(); | 
|---|
| [2350] | 178 |     } | 
|---|
 | 179 |  | 
|---|
| [8079] | 180 |     /** | 
|---|
 | 181 |         @brief Registers the shader as CompositorInstance::Listener at the compositor. Used to change parameters. | 
|---|
 | 182 |     */ | 
|---|
 | 183 |     void Shader::addAsListener() | 
|---|
| [2350] | 184 |     { | 
|---|
| [8079] | 185 |         if (!this->registeredAsListener_) | 
|---|
| [2350] | 186 |         { | 
|---|
| [8079] | 187 |             this->registeredAsListener_ = true; | 
|---|
 | 188 |             if (this->compositorInstance_) | 
|---|
 | 189 |                 this->compositorInstance_->addListener(this); | 
|---|
| [2350] | 190 |         } | 
|---|
 | 191 |     } | 
|---|
 | 192 |  | 
|---|
| [8079] | 193 |     /** | 
|---|
 | 194 |         @brief Inherited by Ogre::CompositorInstance::Listener, called whenever the material is rendered. Used to change parameters. | 
|---|
 | 195 |     */ | 
|---|
 | 196 |     void Shader::notifyMaterialRender(Ogre::uint32 pass_id, Ogre::MaterialPtr& materialPtr) | 
|---|
| [2350] | 197 |     { | 
|---|
| [8079] | 198 |         // iterate through the list of parameters | 
|---|
| [11071] | 199 |         for (const ParameterContainer& parameter : this->parameters_) | 
|---|
| [2350] | 200 |         { | 
|---|
| [11071] | 201 |             Ogre::Technique* techniquePtr = materialPtr->getTechnique(parameter.technique_); | 
|---|
| [8079] | 202 |             if (techniquePtr) | 
|---|
| [2350] | 203 |             { | 
|---|
| [11071] | 204 |                 Ogre::Pass* passPtr = techniquePtr->getPass(parameter.pass_); | 
|---|
| [8079] | 205 |                 if (passPtr) | 
|---|
| [2350] | 206 |                 { | 
|---|
| [8079] | 207 |                     // change the value of the parameter depending on its type | 
|---|
| [11071] | 208 |                     if (parameter.value_.isType<int>()) | 
|---|
 | 209 |                         passPtr->getFragmentProgramParameters()->setNamedConstant(parameter.parameter_, parameter.value_.get<int>()); | 
|---|
 | 210 |                     else if (parameter.value_.isType<float>()) | 
|---|
 | 211 |                         passPtr->getFragmentProgramParameters()->setNamedConstant(parameter.parameter_, parameter.value_.get<float>()); | 
|---|
| [2350] | 212 |                 } | 
|---|
| [8079] | 213 |                 else | 
|---|
| [11071] | 214 |                     orxout(internal_warning) << "No pass " << parameter.pass_ << " in technique " << parameter.technique_ << " in compositor \"" << this->compositorName_ << "\" or pass has no shader." << endl; | 
|---|
| [2350] | 215 |             } | 
|---|
 | 216 |             else | 
|---|
| [11071] | 217 |                 orxout(internal_warning) << "No technique " << parameter.technique_ << " in compositor \"" << this->compositorName_ << "\" or technique has no pass with shader." << endl; | 
|---|
| [2350] | 218 |         } | 
|---|
| [8079] | 219 |         this->parameters_.clear(); | 
|---|
| [2350] | 220 |     } | 
|---|
 | 221 |  | 
|---|
| [8079] | 222 |     /** | 
|---|
 | 223 |         @brief Detects if the Cg program manager plugin is active. | 
|---|
 | 224 |     */ | 
|---|
 | 225 |     /* static */ bool Shader::hasCgProgramManager() | 
|---|
| [2350] | 226 |     { | 
|---|
| [8079] | 227 |         if (Ogre::Root::getSingletonPtr()) | 
|---|
| [2350] | 228 |         { | 
|---|
| [8079] | 229 |             const Ogre::Root::PluginInstanceList& plugins = Ogre::Root::getSingleton().getInstalledPlugins(); | 
|---|
| [11071] | 230 |             for (Ogre::Plugin* plugin : plugins) | 
|---|
 | 231 |                 if (plugin->getName() == "Cg Program Manager") | 
|---|
| [8079] | 232 |                     return true; | 
|---|
| [2350] | 233 |         } | 
|---|
| [8079] | 234 |         return false; | 
|---|
| [2350] | 235 |     } | 
|---|
 | 236 | } | 
|---|