[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 | |
---|
| 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 | { |
---|
[6412] | 79 | if (this->compositorInstance_ && this->bLoadCompositor_) |
---|
[2350] | 80 | { |
---|
[2896] | 81 | Ogre::Viewport* viewport = GraphicsManager::getInstance().getViewport(); |
---|
[2350] | 82 | assert(viewport); |
---|
| 83 | Ogre::CompositorManager::getSingleton().removeCompositor(viewport, this->compositor_); |
---|
| 84 | } |
---|
[2396] | 85 | |
---|
[2350] | 86 | } |
---|
| 87 | |
---|
| 88 | void Shader::setSceneManager(Ogre::SceneManager* scenemanager) |
---|
| 89 | { |
---|
| 90 | this->scenemanager_ = scenemanager; |
---|
| 91 | this->bViewportInitialized_ = false; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | void Shader::tick(float dt) |
---|
| 95 | { |
---|
[2361] | 96 | SUPER(Shader, tick, dt); |
---|
| 97 | |
---|
[2350] | 98 | if (this->bLoadCompositor_ && !this->bViewportInitialized_ && this->scenemanager_ && this->scenemanager_->getCurrentViewport()) |
---|
| 99 | { |
---|
| 100 | this->bViewportInitialized_ = true; |
---|
| 101 | this->updateVisibility(); |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | void Shader::changedCompositor() |
---|
| 106 | { |
---|
| 107 | if (this->bLoadCompositor_) |
---|
| 108 | { |
---|
[2896] | 109 | Ogre::Viewport* viewport = GraphicsManager::getInstance().getViewport(); |
---|
[2350] | 110 | assert(viewport); |
---|
[6412] | 111 | if (!this->oldcompositor_.empty()) |
---|
[2350] | 112 | { |
---|
| 113 | Ogre::CompositorManager::getSingleton().removeCompositor(viewport, this->oldcompositor_); |
---|
| 114 | this->compositorInstance_ = 0; |
---|
| 115 | } |
---|
[6412] | 116 | if (!this->compositor_.empty()) |
---|
[2350] | 117 | { |
---|
| 118 | this->compositorInstance_ = Ogre::CompositorManager::getSingleton().addCompositor(viewport, this->compositor_); |
---|
| 119 | if (!this->compositorInstance_) |
---|
| 120 | COUT(2) << "Warning: Couldn't load compositor with name \"" << this->compositor_ << "\"." << std::endl; |
---|
| 121 | Ogre::CompositorManager::getSingleton().setCompositorEnabled(viewport, this->compositor_, this->bViewportInitialized_ && this->isVisible()); |
---|
| 122 | } |
---|
| 123 | this->oldcompositor_ = this->compositor_; |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | void Shader::updateVisibility() |
---|
| 128 | { |
---|
| 129 | if (this->compositorInstance_ && this->scenemanager_) |
---|
| 130 | this->compositorInstance_->setEnabled(this->scenemanager_->getCurrentViewport() && this->isVisible()); |
---|
| 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 | { |
---|
| 137 | if (this->bViewportInitialized_ && this->compositorInstance_ && this->isVisible()) |
---|
| 138 | { |
---|
| 139 | this->compositorInstance_->setEnabled(false); |
---|
| 140 | this->compositorInstance_->setEnabled(true); |
---|
| 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 | { |
---|
| 149 | if (this->bViewportInitialized_ && this->compositorInstance_ && this->isVisible()) |
---|
| 150 | { |
---|
| 151 | this->compositorInstance_->setEnabled(false); |
---|
| 152 | this->compositorInstance_->setEnabled(true); |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | bool Shader::_setParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter, float value) |
---|
| 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 | |
---|
| 182 | bool Shader::_setParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter, int value) |
---|
| 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 | |
---|
| 207 | float Shader::getParameter(const std::string& material, size_t technique, size_t pass, const std::string& parameter) |
---|
| 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 | |
---|
| 221 | bool Shader::getParameterIsFloat(const std::string& material, size_t technique, size_t pass, const std::string& parameter) |
---|
| 222 | { |
---|
| 223 | ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter); |
---|
| 224 | if (pointer) |
---|
| 225 | return pointer->first; |
---|
| 226 | else |
---|
| 227 | return false; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | bool Shader::getParameterIsInt(const std::string& material, size_t technique, size_t pass, const std::string& parameter) |
---|
| 231 | { |
---|
| 232 | ParameterPointer* pointer = Shader::getParameterPointer(material, technique, pass, parameter); |
---|
| 233 | if (pointer) |
---|
| 234 | return (!pointer->first); |
---|
| 235 | else |
---|
| 236 | return false; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | Shader::ParameterPointer* Shader::getParameterPointer(const std::string& material, size_t technique, size_t pass, const std::string& parameter) |
---|
| 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 | |
---|
[6412] | 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 | } |
---|
| 329 | } |
---|