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