[11678] | 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 | |
---|
[11678] | 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); |
---|
[2072] | 75 | XMLPortParam(Model, "mesh", setMeshSource, getMeshSource, xmlelement, mode); |
---|
[11080] | 76 | XMLPortParam(Model, "renderQueueGroup", setRenderQueueGroup, getRenderQueueGroup, xmlelement, mode); |
---|
| 77 | XMLPortParam(Model, "material", setMaterial, getMaterial, xmlelement, mode); |
---|
[2072] | 78 | XMLPortParam(Model, "shadow", setCastShadows, getCastShadows, xmlelement, mode).defaultValues(true); |
---|
| 79 | } |
---|
[11080] | 80 | |
---|
| 81 | /** |
---|
| 82 | @brief |
---|
| 83 | This function turns a string from XML Port into a usable ID for the rendering system |
---|
| 84 | It defaults to the main queue if the group isn't recognized. |
---|
| 85 | |
---|
| 86 | @param renderQueueGroup |
---|
| 87 | This is a string representing the render queue group. Accepted values: |
---|
| 88 | 'main', 'stencil glow', 'stencil object' |
---|
| 89 | */ |
---|
| 90 | const unsigned int Model::getRenderQueueGroupID(const std::string& renderQueueGroup) const |
---|
| 91 | { |
---|
| 92 | if(renderQueueGroup.compare("stencil glow")==0) |
---|
| 93 | { |
---|
| 94 | return RENDER_QUEUE_STENCIL_GLOW; |
---|
| 95 | } |
---|
| 96 | if(renderQueueGroup.compare("stencil object")==0) |
---|
| 97 | { |
---|
| 98 | return RENDER_QUEUE_STENCIL_OBJECTS; |
---|
| 99 | } |
---|
| 100 | return RENDER_QUEUE_MAIN; |
---|
| 101 | } |
---|
[2072] | 102 | |
---|
| 103 | void Model::registerVariables() |
---|
| 104 | { |
---|
[3280] | 105 | registerVariable(this->meshSrc_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMesh)); |
---|
[11080] | 106 | registerVariable(this->renderQueueGroup_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedRenderQueueGroup)); |
---|
| 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 | |
---|
[2662] | 128 | this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_); |
---|
[2072] | 129 | |
---|
[2662] | 130 | if (this->mesh_.getEntity()) |
---|
| 131 | { |
---|
| 132 | this->attachOgreObject(this->mesh_.getEntity()); |
---|
| 133 | this->mesh_.getEntity()->setCastShadows(this->bCastShadows_); |
---|
[11080] | 134 | this->mesh_.getEntity()->setRenderQueueGroup(this->renderQueueGroup_); |
---|
[2662] | 135 | this->mesh_.setVisible(this->isVisible()); |
---|
[7163] | 136 | |
---|
[7166] | 137 | if (this->bGlobalEnableLod_) |
---|
| 138 | this->enableLod(); |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | } |
---|
[7163] | 142 | |
---|
[11080] | 143 | void Model::changedRenderQueueGroup() |
---|
| 144 | { |
---|
| 145 | if (GameMode::showsGraphics()) |
---|
| 146 | { |
---|
| 147 | if (this->mesh_.getEntity()) |
---|
| 148 | { |
---|
| 149 | this->mesh_.getEntity()->setRenderQueueGroup(this->renderQueueGroup_); |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | void Model::changedMaterial() |
---|
| 155 | { |
---|
| 156 | this->mesh_.setMaterial(this->materialName_); |
---|
| 157 | } |
---|
| 158 | |
---|
[11678] | 159 | // PRE: a valid Ogre::Entity* entity with a valid subentity at index |
---|
| 160 | // POST: changed material of subentity at index to name |
---|
| 161 | void Model::setSubMaterial(const std::string& name, const int index){ |
---|
| 162 | this->mesh_.setSubMaterial(name, index); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | |
---|
[7166] | 166 | void Model::changedShadows() |
---|
| 167 | { |
---|
| 168 | this->mesh_.setCastShadows(this->bCastShadows_); |
---|
| 169 | } |
---|
[7163] | 170 | |
---|
[7166] | 171 | void Model::changedVisibility() |
---|
| 172 | { |
---|
| 173 | SUPER(Model, changedVisibility); |
---|
[7163] | 174 | |
---|
[7166] | 175 | this->mesh_.setVisible(this->isVisible()); |
---|
| 176 | } |
---|
[7163] | 177 | |
---|
[7166] | 178 | void Model::enableLod() |
---|
| 179 | { |
---|
[10728] | 180 | #if defined(ORXONOX_COMPILER_MSVC) && OGRE_VERSION >= 0x010800 && OGRE_VERSION < 0x010900 |
---|
| 181 | // disable LOD for MSVC and ogre version 1.8 because it leads to crashes |
---|
| 182 | #else |
---|
[7166] | 183 | //LOD |
---|
| 184 | if( this->mesh_.getEntity()->getMesh()->getNumLodLevels()==1 ) |
---|
| 185 | { |
---|
| 186 | Level* level = this->getLevel(); |
---|
[7163] | 187 | |
---|
[11071] | 188 | assert( level != nullptr ); |
---|
[7163] | 189 | |
---|
[7166] | 190 | MeshLodInformation* lodInfo = level->getLodInfo(this->meshSrc_); |
---|
| 191 | if( lodInfo ) |
---|
| 192 | { |
---|
| 193 | setLodLevel(lodInfo->getLodLevel()); |
---|
| 194 | this->bLodEnabled_ = lodInfo->getEnabled(); |
---|
| 195 | this->numLodLevels_ = lodInfo->getNumLevels(); |
---|
| 196 | this->lodReductionRate_ = lodInfo->getReductionRate(); |
---|
| 197 | } |
---|
| 198 | if( this->numLodLevels_>10 ) |
---|
| 199 | { |
---|
[8858] | 200 | orxout(internal_warning, context::lod) << "More than 10 LoD levels requested. Creating only 10." << endl; |
---|
[7166] | 201 | this->numLodLevels_ = 10; |
---|
| 202 | } |
---|
| 203 | if( this->bLodEnabled_ ) |
---|
| 204 | { |
---|
| 205 | float volume = this->mesh_.getEntity()->getBoundingBox().volume(); |
---|
| 206 | /* |
---|
| 207 | float scaleFactor = 1; |
---|
| 208 | |
---|
| 209 | BaseObject* creatorPtr = this; |
---|
| 210 | |
---|
[11071] | 211 | while(creatorPtr!=nullptr&&orxonox_cast<WorldEntity*>(creatorPtr)) |
---|
[7166] | 212 | { |
---|
| 213 | scaleFactor *= getBiggestScale(((WorldEntity*) creatorPtr)->getScale3D()); |
---|
| 214 | creatorPtr = creatorPtr->getCreator(); |
---|
| 215 | } |
---|
[8858] | 216 | orxout() << "name: " << this->meshSrc_ << "scaleFactor: " << scaleFactor << ", volume: " << volume << endl; |
---|
[7166] | 217 | */ |
---|
[8858] | 218 | orxout(verbose, context::lod) << "Setting lodLevel for " << this->meshSrc_<< " with lodLevel_: " << this->lodLevel_ <<" and volume: "<< volume << ":" << endl; |
---|
[7166] | 219 | |
---|
[9675] | 220 | #if OGRE_VERSION >= 0x010800 |
---|
| 221 | Ogre::ProgressiveMesh::LodValueList distList; |
---|
| 222 | #elif OGRE_VERSION >= 0x010700 |
---|
[7166] | 223 | Ogre::Mesh::LodValueList distList; |
---|
[7163] | 224 | #else |
---|
[7166] | 225 | Ogre::Mesh::LodDistanceList distList; |
---|
[7163] | 226 | #endif |
---|
| 227 | |
---|
[7166] | 228 | if( lodLevel_>0 ) |
---|
| 229 | { |
---|
| 230 | // float factor = scaleFactor*5/lodLevel_; |
---|
| 231 | float factor = pow(volume, 2.0f / 3.0f) * 15.0f / lodLevel_; |
---|
[7163] | 232 | |
---|
[8858] | 233 | orxout(verbose, context::lod) << "LodLevel set with factor: " << factor << endl; |
---|
[7163] | 234 | |
---|
[7166] | 235 | distList.push_back(70.0f*factor); |
---|
| 236 | distList.push_back(140.0f*factor); |
---|
| 237 | distList.push_back(170.0f*factor); |
---|
| 238 | distList.push_back(200.0f*factor); |
---|
| 239 | distList.push_back(230.0f*factor); |
---|
| 240 | distList.push_back(250.0f*factor); |
---|
| 241 | distList.push_back(270.0f*factor); |
---|
| 242 | distList.push_back(290.0f*factor); |
---|
| 243 | distList.push_back(310.0f*factor); |
---|
| 244 | distList.push_back(330.0f*factor); |
---|
| 245 | while(distList.size()>this->numLodLevels_) |
---|
| 246 | distList.pop_back(); |
---|
[7163] | 247 | |
---|
| 248 | |
---|
[7166] | 249 | //Generiert LOD-Levels |
---|
[9675] | 250 | #if OGRE_VERSION >= 0x010800 |
---|
| 251 | Ogre::ProgressiveMesh::generateLodLevels(this->mesh_.getEntity()->getMesh().get(), distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL, |
---|
| 252 | this->lodReductionRate_); |
---|
| 253 | #else |
---|
[7166] | 254 | this->mesh_.getEntity()->getMesh()->generateLodLevels(distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL, this->lodReductionRate_); |
---|
[9675] | 255 | #endif |
---|
[7166] | 256 | } |
---|
| 257 | else |
---|
| 258 | { |
---|
| 259 | std::string what; |
---|
| 260 | if(lodLevel_>5) |
---|
| 261 | what = ">5"; |
---|
| 262 | else |
---|
| 263 | what = "<0"; |
---|
[7163] | 264 | |
---|
[8858] | 265 | orxout(verbose, context::lod) << "LodLevel not set because lodLevel(" << lodLevel_ << ") was " << what << "." << endl; |
---|
[7163] | 266 | } |
---|
[2662] | 267 | } |
---|
[7166] | 268 | else |
---|
[8858] | 269 | orxout(verbose, context::lod) << "LodLevel for " << this->meshSrc_ << " not set because is disabled." << endl; |
---|
[2072] | 270 | } |
---|
[10728] | 271 | #endif |
---|
[2072] | 272 | } |
---|
| 273 | } |
---|