[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 <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 | |
---|
[3196] | 41 | #include "core/CoreIncludes.h" |
---|
| 42 | #include "core/GameMode.h" |
---|
[3370] | 43 | #include "core/GraphicsManager.h" |
---|
[3196] | 44 | |
---|
[2350] | 45 | namespace orxonox |
---|
| 46 | { |
---|
| 47 | bool Shader::bLoadedCgPlugin_s = false; |
---|
| 48 | Shader::MaterialMap Shader::parameters_s; |
---|
| 49 | |
---|
[2422] | 50 | Shader::Shader(Ogre::SceneManager* scenemanager) : compositorInstance_(0) |
---|
[2350] | 51 | { |
---|
| 52 | RegisterObject(Shader); |
---|
| 53 | |
---|
| 54 | this->scenemanager_ = scenemanager; |
---|
| 55 | this->bVisible_ = true; |
---|
[2896] | 56 | this->bLoadCompositor_ = GameMode::showsGraphics(); |
---|
[2350] | 57 | |
---|
[7972] | 58 | static bool hasCgProgramManager = Shader::hasCgProgramManager(); |
---|
| 59 | Shader::bLoadedCgPlugin_s = hasCgProgramManager; |
---|
[2350] | 60 | |
---|
| 61 | this->bLoadCompositor_ &= Shader::bLoadedCgPlugin_s; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | Shader::~Shader() |
---|
| 65 | { |
---|
[7972] | 66 | if (this->compositorInstance_ && GraphicsManager::getInstance().getViewport()) |
---|
| 67 | Ogre::CompositorManager::getSingleton().removeCompositor(GraphicsManager::getInstance().getViewport(), this->compositorName_); |
---|
[2350] | 68 | } |
---|
| 69 | |
---|
[7966] | 70 | void Shader::cameraChanged(Ogre::Viewport* viewport, Ogre::Camera* oldCamera) |
---|
| 71 | { |
---|
[7972] | 72 | if (!this->bLoadCompositor_ || !this->scenemanager_) |
---|
[7966] | 73 | return; |
---|
| 74 | |
---|
[7972] | 75 | // load the compositor if not already done |
---|
| 76 | if (!this->compositorName_.empty() && !this->compositorInstance_) |
---|
| 77 | this->changedCompositorName(viewport); |
---|
| 78 | |
---|
[7966] | 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()) |
---|
[7972] | 91 | Ogre::CompositorManager::getSingleton().setCompositorEnabled(viewport, this->compositorName_, false); |
---|
[7966] | 92 | |
---|
| 93 | if (viewport->getCamera() && this->scenemanager_ == viewport->getCamera()->getSceneManager()) |
---|
[7972] | 94 | Ogre::CompositorManager::getSingleton().setCompositorEnabled(viewport, this->compositorName_, this->isVisible()); |
---|
[7966] | 95 | } |
---|
[7972] | 96 | |
---|
| 97 | void Shader::changedCompositorName() |
---|
[2350] | 98 | { |
---|
[7972] | 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 | } |
---|
[2361] | 104 | |
---|
[7972] | 105 | void Shader::changedCompositorName(Ogre::Viewport* viewport) |
---|
[2350] | 106 | { |
---|
| 107 | if (this->bLoadCompositor_) |
---|
| 108 | { |
---|
| 109 | assert(viewport); |
---|
[7972] | 110 | if (!this->oldcompositorName_.empty()) |
---|
[2350] | 111 | { |
---|
[7972] | 112 | Ogre::CompositorManager::getSingleton().removeCompositor(viewport, this->oldcompositorName_); |
---|
[2350] | 113 | this->compositorInstance_ = 0; |
---|
| 114 | } |
---|
[7972] | 115 | if (!this->compositorName_.empty()) |
---|
[2350] | 116 | { |
---|
[7972] | 117 | this->compositorInstance_ = Ogre::CompositorManager::getSingleton().addCompositor(viewport, this->compositorName_); |
---|
[2350] | 118 | if (!this->compositorInstance_) |
---|
[7972] | 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()); |
---|
[2350] | 122 | } |
---|
[7972] | 123 | this->oldcompositorName_ = this->compositorName_; |
---|
[2350] | 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | void Shader::updateVisibility() |
---|
| 128 | { |
---|
[7972] | 129 | if (this->compositorInstance_) |
---|
| 130 | Ogre::CompositorManager::getSingleton().setCompositorEnabled(GraphicsManager::getInstance().getViewport(), this->compositorName_, this->isVisible()); |
---|
[2350] | 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 | { |
---|
[7972] | 137 | if (this->compositorInstance_ && this->isVisible()) |
---|
[2350] | 138 | { |
---|
[7972] | 139 | Ogre::CompositorManager::getSingleton().setCompositorEnabled(GraphicsManager::getInstance().getViewport(), this->compositorName_, false); |
---|
| 140 | Ogre::CompositorManager::getSingleton().setCompositorEnabled(GraphicsManager::getInstance().getViewport(), this->compositorName_, true); |
---|
[2350] | 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 | { |
---|
[7972] | 149 | if (this->compositorInstance_ && this->isVisible()) |
---|
[2350] | 150 | { |
---|
[7972] | 151 | Ogre::CompositorManager::getSingleton().setCompositorEnabled(GraphicsManager::getInstance().getViewport(), this->compositorName_, false); |
---|
| 152 | Ogre::CompositorManager::getSingleton().setCompositorEnabled(GraphicsManager::getInstance().getViewport(), this->compositorName_, true); |
---|
[2350] | 153 | } |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
[7972] | 157 | /* static */ bool Shader::_setParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter, float value) |
---|
[2350] | 158 | { |
---|
| 159 | ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter); |
---|
| 160 | if (pointer) |
---|
| 161 | { |
---|
| 162 | if (pointer->first) |
---|
| 163 | { |
---|
[3301] | 164 | if ((*static_cast<float*>(pointer->second)) != value) |
---|
[2350] | 165 | { |
---|
[3301] | 166 | (*static_cast<float*>(pointer->second)) = value; |
---|
[2350] | 167 | return true; |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | else |
---|
| 171 | { |
---|
[3301] | 172 | if ((*static_cast<int*>(pointer->second)) != static_cast<int>(value)) |
---|
[2350] | 173 | { |
---|
[3301] | 174 | (*static_cast<int*>(pointer->second)) = static_cast<int>(value); |
---|
[2350] | 175 | return true; |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | return false; |
---|
| 180 | } |
---|
| 181 | |
---|
[7972] | 182 | /* static */ bool Shader::_setParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter, int value) |
---|
[2350] | 183 | { |
---|
| 184 | ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter); |
---|
| 185 | if (pointer) |
---|
| 186 | { |
---|
| 187 | if (pointer->first) |
---|
| 188 | { |
---|
[3301] | 189 | if ((*static_cast<float*>(pointer->second)) != static_cast<float>(value)) |
---|
[2350] | 190 | { |
---|
[3301] | 191 | (*static_cast<float*>(pointer->second)) = static_cast<float>(value); |
---|
[2350] | 192 | return true; |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | else |
---|
| 196 | { |
---|
[3301] | 197 | if ((*static_cast<int*>(pointer->second)) != value) |
---|
[2350] | 198 | { |
---|
[3301] | 199 | (*static_cast<int*>(pointer->second)) = value; |
---|
[2350] | 200 | return true; |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | return false; |
---|
| 205 | } |
---|
| 206 | |
---|
[7972] | 207 | /* static */ float Shader::getParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter) |
---|
[2350] | 208 | { |
---|
| 209 | ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter); |
---|
| 210 | if (pointer) |
---|
| 211 | { |
---|
| 212 | if (pointer->first) |
---|
[3301] | 213 | return (*static_cast<float*>(pointer->second)); |
---|
[2350] | 214 | else |
---|
[3301] | 215 | return static_cast<float>(*static_cast<int*>(pointer->second)); |
---|
[2350] | 216 | } |
---|
| 217 | else |
---|
| 218 | return 0; |
---|
| 219 | } |
---|
| 220 | |
---|
[7972] | 221 | /* static */ bool Shader::getParameterIsFloat(const std::string& material, size_t technique, size_t pass, const std::string& parameter) |
---|
[2350] | 222 | { |
---|
| 223 | ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter); |
---|
| 224 | if (pointer) |
---|
| 225 | return pointer->first; |
---|
| 226 | else |
---|
| 227 | return false; |
---|
| 228 | } |
---|
| 229 | |
---|
[7972] | 230 | /* static */ bool Shader::getParameterIsInt(const std::string& material, size_t technique, size_t pass, const std::string& parameter) |
---|
[2350] | 231 | { |
---|
| 232 | ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter); |
---|
| 233 | if (pointer) |
---|
| 234 | return (!pointer->first); |
---|
| 235 | else |
---|
| 236 | return false; |
---|
| 237 | } |
---|
| 238 | |
---|
[7972] | 239 | /* static */ Shader::ParameterPointer* Shader::getParameterPointer(const std::string& material, size_t technique, size_t pass, const std::string& parameter) |
---|
[2350] | 240 | { |
---|
[2896] | 241 | if (!GameMode::showsGraphics() || !Shader::bLoadedCgPlugin_s) |
---|
[2350] | 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 (¶meter_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 | |
---|
[6417] | 298 | if (!pass_pointer->getFragmentProgramName().empty()) |
---|
[2350] | 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()) |
---|
[3301] | 308 | ? static_cast<void*>(parameter_pointer->getFloatPointer(definition_iterator->second.physicalIndex)) |
---|
| 309 | : static_cast<void*>(parameter_pointer->getIntPointer(definition_iterator->second.physicalIndex)); |
---|
[2350] | 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 | } |
---|
[7972] | 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 | } |
---|
[2350] | 341 | } |
---|