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