[11534] | 1 | |
---|
[2072] | 2 | /* |
---|
| 3 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 4 | * > www.orxonox.net < |
---|
| 5 | * |
---|
| 6 | * |
---|
| 7 | * License notice: |
---|
| 8 | * |
---|
| 9 | * This program is free software; you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License |
---|
| 11 | * as published by the Free Software Foundation; either version 2 |
---|
| 12 | * of the License, or (at your option) any later version. |
---|
| 13 | * |
---|
| 14 | * This program is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this program; if not, write to the Free Software |
---|
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 22 | * |
---|
| 23 | * Author: |
---|
| 24 | * Fabian 'x3n' Landau |
---|
| 25 | * Co-authors: |
---|
| 26 | * ... |
---|
| 27 | * |
---|
| 28 | */ |
---|
| 29 | |
---|
[3196] | 30 | #include "Model.h" |
---|
[2072] | 31 | |
---|
[2662] | 32 | #include <OgreEntity.h> |
---|
[9675] | 33 | #include <OgreProgressiveMesh.h> |
---|
[3196] | 34 | |
---|
| 35 | #include "core/CoreIncludes.h" |
---|
[9667] | 36 | #include "core/config/ConfigValueIncludes.h" |
---|
[2896] | 37 | #include "core/GameMode.h" |
---|
[2072] | 38 | #include "core/XMLPort.h" |
---|
[5735] | 39 | #include "Scene.h" |
---|
[11080] | 40 | #include "RenderQueueListener.h" |
---|
[7163] | 41 | #include "graphics/MeshLodInformation.h" |
---|
| 42 | #include "Level.h" |
---|
[2072] | 43 | |
---|
[11517] | 44 | |
---|
[2072] | 45 | namespace orxonox |
---|
| 46 | { |
---|
[9667] | 47 | RegisterClass(Model); |
---|
[2072] | 48 | |
---|
[9667] | 49 | Model::Model(Context* context) : |
---|
[11080] | 50 | StaticEntity(context), bCastShadows_(true), renderQueueGroup_(RENDER_QUEUE_MAIN), lodLevel_(5), bLodEnabled_(true), numLodLevels_(10), lodReductionRate_(.15f) |
---|
[2072] | 51 | { |
---|
| 52 | RegisterObject(Model); |
---|
| 53 | |
---|
[7166] | 54 | this->setConfigValues(); |
---|
[2072] | 55 | this->registerVariables(); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | Model::~Model() |
---|
| 59 | { |
---|
| 60 | if (this->isInitialized() && this->mesh_.getEntity()) |
---|
[2662] | 61 | this->detachOgreObject(this->mesh_.getEntity()); |
---|
[2072] | 62 | } |
---|
| 63 | |
---|
[7166] | 64 | void Model::setConfigValues() |
---|
| 65 | { |
---|
[8079] | 66 | SetConfigValueExternal(bGlobalEnableLod_, "GraphicsSettings", "enableMeshLoD", true) |
---|
[7166] | 67 | .description("Enable level of detail for models"); |
---|
| 68 | } |
---|
| 69 | |
---|
[2072] | 70 | void Model::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 71 | { |
---|
| 72 | SUPER(Model, XMLPort, xmlelement, mode); |
---|
| 73 | |
---|
[7163] | 74 | XMLPortParam(Model, "lodLevel", setLodLevel, getLodLevel, xmlelement, mode); |
---|
| 75 | |
---|
[2072] | 76 | XMLPortParam(Model, "mesh", setMeshSource, getMeshSource, xmlelement, mode); |
---|
[11080] | 77 | XMLPortParam(Model, "renderQueueGroup", setRenderQueueGroup, getRenderQueueGroup, xmlelement, mode); |
---|
| 78 | XMLPortParam(Model, "material", setMaterial, getMaterial, xmlelement, mode); |
---|
[2072] | 79 | XMLPortParam(Model, "shadow", setCastShadows, getCastShadows, xmlelement, mode).defaultValues(true); |
---|
| 80 | } |
---|
[11080] | 81 | |
---|
| 82 | /** |
---|
| 83 | @brief |
---|
| 84 | This function turns a string from XML Port into a usable ID for the rendering system |
---|
| 85 | It defaults to the main queue if the group isn't recognized. |
---|
| 86 | |
---|
| 87 | @param renderQueueGroup |
---|
| 88 | This is a string representing the render queue group. Accepted values: |
---|
| 89 | 'main', 'stencil glow', 'stencil object' |
---|
| 90 | */ |
---|
| 91 | const unsigned int Model::getRenderQueueGroupID(const std::string& renderQueueGroup) const |
---|
| 92 | { |
---|
| 93 | if(renderQueueGroup.compare("stencil glow")==0) |
---|
| 94 | { |
---|
| 95 | return RENDER_QUEUE_STENCIL_GLOW; |
---|
| 96 | } |
---|
| 97 | if(renderQueueGroup.compare("stencil object")==0) |
---|
| 98 | { |
---|
| 99 | return RENDER_QUEUE_STENCIL_OBJECTS; |
---|
| 100 | } |
---|
| 101 | return RENDER_QUEUE_MAIN; |
---|
| 102 | } |
---|
[2072] | 103 | |
---|
| 104 | void Model::registerVariables() |
---|
| 105 | { |
---|
[3280] | 106 | registerVariable(this->meshSrc_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMesh)); |
---|
[11080] | 107 | registerVariable(this->renderQueueGroup_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedRenderQueueGroup)); |
---|
| 108 | registerVariable(this->materialName_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMaterial)); |
---|
[3280] | 109 | registerVariable(this->bCastShadows_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedShadows)); |
---|
[2072] | 110 | } |
---|
| 111 | |
---|
[7163] | 112 | float Model::getBiggestScale(Vector3 scale3d) |
---|
| 113 | { |
---|
| 114 | float scaleFactor = scale3d.x; |
---|
| 115 | if(scale3d.y>scaleFactor) |
---|
| 116 | scaleFactor = scale3d.y; |
---|
| 117 | if(scale3d.z>scaleFactor) |
---|
| 118 | scaleFactor = scale3d.z; |
---|
| 119 | return scaleFactor; |
---|
| 120 | } |
---|
| 121 | |
---|
[2072] | 122 | void Model::changedMesh() |
---|
| 123 | { |
---|
[2896] | 124 | if (GameMode::showsGraphics()) |
---|
[2662] | 125 | { |
---|
| 126 | if (this->mesh_.getEntity()) |
---|
| 127 | this->detachOgreObject(this->mesh_.getEntity()); |
---|
[2072] | 128 | |
---|
[2662] | 129 | this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_); |
---|
[2072] | 130 | |
---|
[2662] | 131 | if (this->mesh_.getEntity()) |
---|
| 132 | { |
---|
| 133 | this->attachOgreObject(this->mesh_.getEntity()); |
---|
| 134 | this->mesh_.getEntity()->setCastShadows(this->bCastShadows_); |
---|
[11080] | 135 | this->mesh_.getEntity()->setRenderQueueGroup(this->renderQueueGroup_); |
---|
[2662] | 136 | this->mesh_.setVisible(this->isVisible()); |
---|
[7163] | 137 | |
---|
[7166] | 138 | if (this->bGlobalEnableLod_) |
---|
| 139 | this->enableLod(); |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | } |
---|
[7163] | 143 | |
---|
[11080] | 144 | void Model::changedRenderQueueGroup() |
---|
| 145 | { |
---|
| 146 | if (GameMode::showsGraphics()) |
---|
| 147 | { |
---|
| 148 | if (this->mesh_.getEntity()) |
---|
| 149 | { |
---|
| 150 | this->mesh_.getEntity()->setRenderQueueGroup(this->renderQueueGroup_); |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | void Model::changedMaterial() |
---|
| 156 | { |
---|
| 157 | this->mesh_.setMaterial(this->materialName_); |
---|
| 158 | } |
---|
| 159 | |
---|
[11534] | 160 | // PRE: a valid Ogre::Entity* entity with a valid subentity at index |
---|
| 161 | // POST: changed material of subentity at index to name |
---|
| 162 | void Model::setSubMaterial(const std::string& name, const int index){ |
---|
| 163 | this->mesh_.setSubMaterial(name, index); |
---|
| 164 | } |
---|
[11517] | 165 | |
---|
[11534] | 166 | |
---|
[7166] | 167 | void Model::changedShadows() |
---|
| 168 | { |
---|
| 169 | this->mesh_.setCastShadows(this->bCastShadows_); |
---|
| 170 | } |
---|
[7163] | 171 | |
---|
[7166] | 172 | void Model::changedVisibility() |
---|
| 173 | { |
---|
| 174 | SUPER(Model, changedVisibility); |
---|
[7163] | 175 | |
---|
[7166] | 176 | this->mesh_.setVisible(this->isVisible()); |
---|
| 177 | } |
---|
[7163] | 178 | |
---|
[7166] | 179 | void Model::enableLod() |
---|
| 180 | { |
---|
[10728] | 181 | #if defined(ORXONOX_COMPILER_MSVC) && OGRE_VERSION >= 0x010800 && OGRE_VERSION < 0x010900 |
---|
| 182 | // disable LOD for MSVC and ogre version 1.8 because it leads to crashes |
---|
| 183 | #else |
---|
[7166] | 184 | //LOD |
---|
| 185 | if( this->mesh_.getEntity()->getMesh()->getNumLodLevels()==1 ) |
---|
| 186 | { |
---|
| 187 | Level* level = this->getLevel(); |
---|
[7163] | 188 | |
---|
[11071] | 189 | assert( level != nullptr ); |
---|
[7163] | 190 | |
---|
[7166] | 191 | MeshLodInformation* lodInfo = level->getLodInfo(this->meshSrc_); |
---|
| 192 | if( lodInfo ) |
---|
| 193 | { |
---|
| 194 | setLodLevel(lodInfo->getLodLevel()); |
---|
| 195 | this->bLodEnabled_ = lodInfo->getEnabled(); |
---|
| 196 | this->numLodLevels_ = lodInfo->getNumLevels(); |
---|
| 197 | this->lodReductionRate_ = lodInfo->getReductionRate(); |
---|
| 198 | } |
---|
| 199 | if( this->numLodLevels_>10 ) |
---|
| 200 | { |
---|
[8858] | 201 | orxout(internal_warning, context::lod) << "More than 10 LoD levels requested. Creating only 10." << endl; |
---|
[7166] | 202 | this->numLodLevels_ = 10; |
---|
| 203 | } |
---|
| 204 | if( this->bLodEnabled_ ) |
---|
| 205 | { |
---|
| 206 | float volume = this->mesh_.getEntity()->getBoundingBox().volume(); |
---|
| 207 | /* |
---|
| 208 | float scaleFactor = 1; |
---|
| 209 | |
---|
| 210 | BaseObject* creatorPtr = this; |
---|
| 211 | |
---|
[11071] | 212 | while(creatorPtr!=nullptr&&orxonox_cast<WorldEntity*>(creatorPtr)) |
---|
[7166] | 213 | { |
---|
| 214 | scaleFactor *= getBiggestScale(((WorldEntity*) creatorPtr)->getScale3D()); |
---|
| 215 | creatorPtr = creatorPtr->getCreator(); |
---|
| 216 | } |
---|
[8858] | 217 | orxout() << "name: " << this->meshSrc_ << "scaleFactor: " << scaleFactor << ", volume: " << volume << endl; |
---|
[7166] | 218 | */ |
---|
[8858] | 219 | orxout(verbose, context::lod) << "Setting lodLevel for " << this->meshSrc_<< " with lodLevel_: " << this->lodLevel_ <<" and volume: "<< volume << ":" << endl; |
---|
[7166] | 220 | |
---|
[9675] | 221 | #if OGRE_VERSION >= 0x010800 |
---|
| 222 | Ogre::ProgressiveMesh::LodValueList distList; |
---|
| 223 | #elif OGRE_VERSION >= 0x010700 |
---|
[7166] | 224 | Ogre::Mesh::LodValueList distList; |
---|
[7163] | 225 | #else |
---|
[7166] | 226 | Ogre::Mesh::LodDistanceList distList; |
---|
[7163] | 227 | #endif |
---|
| 228 | |
---|
[7166] | 229 | if( lodLevel_>0 ) |
---|
| 230 | { |
---|
| 231 | // float factor = scaleFactor*5/lodLevel_; |
---|
| 232 | float factor = pow(volume, 2.0f / 3.0f) * 15.0f / lodLevel_; |
---|
[7163] | 233 | |
---|
[8858] | 234 | orxout(verbose, context::lod) << "LodLevel set with factor: " << factor << endl; |
---|
[7163] | 235 | |
---|
[7166] | 236 | distList.push_back(70.0f*factor); |
---|
| 237 | distList.push_back(140.0f*factor); |
---|
| 238 | distList.push_back(170.0f*factor); |
---|
| 239 | distList.push_back(200.0f*factor); |
---|
| 240 | distList.push_back(230.0f*factor); |
---|
| 241 | distList.push_back(250.0f*factor); |
---|
| 242 | distList.push_back(270.0f*factor); |
---|
| 243 | distList.push_back(290.0f*factor); |
---|
| 244 | distList.push_back(310.0f*factor); |
---|
| 245 | distList.push_back(330.0f*factor); |
---|
| 246 | while(distList.size()>this->numLodLevels_) |
---|
| 247 | distList.pop_back(); |
---|
[7163] | 248 | |
---|
| 249 | |
---|
[7166] | 250 | //Generiert LOD-Levels |
---|
[9675] | 251 | #if OGRE_VERSION >= 0x010800 |
---|
| 252 | Ogre::ProgressiveMesh::generateLodLevels(this->mesh_.getEntity()->getMesh().get(), distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL, |
---|
| 253 | this->lodReductionRate_); |
---|
| 254 | #else |
---|
[7166] | 255 | this->mesh_.getEntity()->getMesh()->generateLodLevels(distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL, this->lodReductionRate_); |
---|
[9675] | 256 | #endif |
---|
[7166] | 257 | } |
---|
| 258 | else |
---|
| 259 | { |
---|
| 260 | std::string what; |
---|
| 261 | if(lodLevel_>5) |
---|
| 262 | what = ">5"; |
---|
| 263 | else |
---|
| 264 | what = "<0"; |
---|
[7163] | 265 | |
---|
[8858] | 266 | orxout(verbose, context::lod) << "LodLevel not set because lodLevel(" << lodLevel_ << ") was " << what << "." << endl; |
---|
[7163] | 267 | } |
---|
[2662] | 268 | } |
---|
[7166] | 269 | else |
---|
[8858] | 270 | orxout(verbose, context::lod) << "LodLevel for " << this->meshSrc_ << " not set because is disabled." << endl; |
---|
[2072] | 271 | } |
---|
[10728] | 272 | #endif |
---|
[2072] | 273 | } |
---|
| 274 | } |
---|