| [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> | 
|---|
| [3196] | 32 |  | 
|---|
 | 33 | #include "core/CoreIncludes.h" | 
|---|
| [7166] | 34 | #include "core/ConfigValueIncludes.h" | 
|---|
| [2896] | 35 | #include "core/GameMode.h" | 
|---|
| [2072] | 36 | #include "core/XMLPort.h" | 
|---|
| [5735] | 37 | #include "Scene.h" | 
|---|
| [7163] | 38 | #include "graphics/MeshLodInformation.h" | 
|---|
 | 39 | #include "Level.h" | 
|---|
| [2072] | 40 |  | 
|---|
 | 41 | namespace orxonox | 
|---|
 | 42 | { | 
|---|
 | 43 |     CreateFactory(Model); | 
|---|
 | 44 |  | 
|---|
| [7163] | 45 |     Model::Model(BaseObject* creator) : | 
|---|
| [7183] | 46 |         StaticEntity(creator), bCastShadows_(true), lodLevel_(5), bLodEnabled_(true), numLodLevels_(10), lodReductionRate_(.15f) | 
|---|
| [2072] | 47 |     { | 
|---|
 | 48 |         RegisterObject(Model); | 
|---|
 | 49 |  | 
|---|
| [7166] | 50 |         this->setConfigValues(); | 
|---|
| [2072] | 51 |         this->registerVariables(); | 
|---|
 | 52 |     } | 
|---|
 | 53 |  | 
|---|
 | 54 |     Model::~Model() | 
|---|
 | 55 |     { | 
|---|
 | 56 |         if (this->isInitialized() && this->mesh_.getEntity()) | 
|---|
| [2662] | 57 |             this->detachOgreObject(this->mesh_.getEntity()); | 
|---|
| [2072] | 58 |     } | 
|---|
 | 59 |  | 
|---|
| [7166] | 60 |     void Model::setConfigValues() | 
|---|
 | 61 |     { | 
|---|
| [8034] | 62 |         SetConfigValueExternal(bGlobalEnableLod_, "GraphicsSettings", "enableMeshLoD", true) | 
|---|
| [7166] | 63 |             .description("Enable level of detail for models"); | 
|---|
 | 64 |     } | 
|---|
 | 65 |  | 
|---|
| [2072] | 66 |     void Model::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
 | 67 |     { | 
|---|
 | 68 |         SUPER(Model, XMLPort, xmlelement, mode); | 
|---|
 | 69 |  | 
|---|
| [7163] | 70 |         XMLPortParam(Model, "lodLevel", setLodLevel, getLodLevel, xmlelement, mode); | 
|---|
 | 71 |  | 
|---|
| [2072] | 72 |         XMLPortParam(Model, "mesh", setMeshSource, getMeshSource, xmlelement, mode); | 
|---|
 | 73 |         XMLPortParam(Model, "shadow", setCastShadows, getCastShadows, xmlelement, mode).defaultValues(true); | 
|---|
 | 74 |     } | 
|---|
 | 75 |  | 
|---|
 | 76 |     void Model::registerVariables() | 
|---|
 | 77 |     { | 
|---|
| [3280] | 78 |         registerVariable(this->meshSrc_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMesh)); | 
|---|
 | 79 |         registerVariable(this->bCastShadows_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedShadows)); | 
|---|
| [2072] | 80 |     } | 
|---|
 | 81 |  | 
|---|
| [7163] | 82 |     float Model::getBiggestScale(Vector3 scale3d) | 
|---|
 | 83 |     { | 
|---|
 | 84 |         float scaleFactor = scale3d.x; | 
|---|
 | 85 |         if(scale3d.y>scaleFactor) | 
|---|
 | 86 |             scaleFactor = scale3d.y; | 
|---|
 | 87 |         if(scale3d.z>scaleFactor) | 
|---|
 | 88 |             scaleFactor = scale3d.z; | 
|---|
 | 89 |         return scaleFactor; | 
|---|
 | 90 |     } | 
|---|
 | 91 |  | 
|---|
| [2072] | 92 |     void Model::changedMesh() | 
|---|
 | 93 |     { | 
|---|
| [2896] | 94 |         if (GameMode::showsGraphics()) | 
|---|
| [2662] | 95 |         { | 
|---|
 | 96 |             if (this->mesh_.getEntity()) | 
|---|
 | 97 |                 this->detachOgreObject(this->mesh_.getEntity()); | 
|---|
| [2072] | 98 |  | 
|---|
| [2662] | 99 |             this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_); | 
|---|
| [2072] | 100 |  | 
|---|
| [2662] | 101 |             if (this->mesh_.getEntity()) | 
|---|
 | 102 |             { | 
|---|
 | 103 |                 this->attachOgreObject(this->mesh_.getEntity()); | 
|---|
 | 104 |                 this->mesh_.getEntity()->setCastShadows(this->bCastShadows_); | 
|---|
 | 105 |                 this->mesh_.setVisible(this->isVisible()); | 
|---|
| [7163] | 106 |  | 
|---|
| [7166] | 107 |                 if (this->bGlobalEnableLod_) | 
|---|
 | 108 |                     this->enableLod(); | 
|---|
 | 109 |             } | 
|---|
 | 110 |         } | 
|---|
 | 111 |     } | 
|---|
| [7163] | 112 |  | 
|---|
| [7166] | 113 |     void Model::changedShadows() | 
|---|
 | 114 |     { | 
|---|
 | 115 |         this->mesh_.setCastShadows(this->bCastShadows_); | 
|---|
 | 116 |     } | 
|---|
| [7163] | 117 |  | 
|---|
| [7166] | 118 |     void Model::changedVisibility() | 
|---|
 | 119 |     { | 
|---|
 | 120 |         SUPER(Model, changedVisibility); | 
|---|
| [7163] | 121 |  | 
|---|
| [7166] | 122 |         this->mesh_.setVisible(this->isVisible()); | 
|---|
 | 123 |     } | 
|---|
| [7163] | 124 |  | 
|---|
| [7166] | 125 |     void Model::enableLod() | 
|---|
 | 126 |     { | 
|---|
 | 127 |         //LOD | 
|---|
 | 128 |         if( this->mesh_.getEntity()->getMesh()->getNumLodLevels()==1 ) | 
|---|
 | 129 |         { | 
|---|
 | 130 |             Level* level = this->getLevel(); | 
|---|
| [7163] | 131 |  | 
|---|
| [7166] | 132 |             assert( level != 0 ); | 
|---|
| [7163] | 133 |  | 
|---|
| [7166] | 134 |             MeshLodInformation* lodInfo = level->getLodInfo(this->meshSrc_); | 
|---|
 | 135 |             if( lodInfo ) | 
|---|
 | 136 |             { | 
|---|
 | 137 |                 setLodLevel(lodInfo->getLodLevel()); | 
|---|
 | 138 |                 this->bLodEnabled_ = lodInfo->getEnabled(); | 
|---|
 | 139 |                 this->numLodLevels_ = lodInfo->getNumLevels(); | 
|---|
 | 140 |                 this->lodReductionRate_ = lodInfo->getReductionRate(); | 
|---|
 | 141 |             } | 
|---|
 | 142 |             if( this->numLodLevels_>10 ) | 
|---|
 | 143 |             { | 
|---|
 | 144 |                 CCOUT(2) << "More than 10 LoD levels requested. Creating only 10." << endl; | 
|---|
 | 145 |                 this->numLodLevels_ = 10; | 
|---|
 | 146 |             } | 
|---|
 | 147 |             if( this->bLodEnabled_ ) | 
|---|
 | 148 |             { | 
|---|
 | 149 |                 float volume = this->mesh_.getEntity()->getBoundingBox().volume(); | 
|---|
 | 150 | /* | 
|---|
 | 151 |                 float scaleFactor = 1; | 
|---|
 | 152 |  | 
|---|
 | 153 |                 BaseObject* creatorPtr = this; | 
|---|
 | 154 |  | 
|---|
 | 155 |                 while(creatorPtr!=NULL&&orxonox_cast<WorldEntity*>(creatorPtr)) | 
|---|
 | 156 |                 { | 
|---|
 | 157 |                     scaleFactor *= getBiggestScale(((WorldEntity*) creatorPtr)->getScale3D()); | 
|---|
 | 158 |                     creatorPtr = creatorPtr->getCreator(); | 
|---|
 | 159 |                 } | 
|---|
 | 160 |                 COUT(0) << "name: " << this->meshSrc_ << "scaleFactor: " << scaleFactor << ", volume: " << volume << endl; | 
|---|
 | 161 | */ | 
|---|
 | 162 |                 COUT(4) << "Setting lodLevel for " << this->meshSrc_<< " with lodLevel_: " << this->lodLevel_ <<" and volume: "<< volume << ":" << std::endl; | 
|---|
 | 163 |  | 
|---|
| [7163] | 164 | #if OGRE_VERSION >= 0x010700 | 
|---|
| [7166] | 165 |                 Ogre::Mesh::LodValueList distList; | 
|---|
| [7163] | 166 | #else | 
|---|
| [7166] | 167 |                 Ogre::Mesh::LodDistanceList distList; | 
|---|
| [7163] | 168 | #endif | 
|---|
 | 169 |  | 
|---|
| [7166] | 170 |                 if( lodLevel_>0 ) | 
|---|
 | 171 |                 { | 
|---|
 | 172 | //                    float factor = scaleFactor*5/lodLevel_; | 
|---|
 | 173 |                     float factor = pow(volume, 2.0f / 3.0f) * 15.0f / lodLevel_; | 
|---|
| [7163] | 174 |  | 
|---|
| [7166] | 175 |                     COUT(4) << "LodLevel set with factor: " << factor << endl; | 
|---|
| [7163] | 176 |  | 
|---|
| [7166] | 177 |                     distList.push_back(70.0f*factor); | 
|---|
 | 178 |                     distList.push_back(140.0f*factor); | 
|---|
 | 179 |                     distList.push_back(170.0f*factor); | 
|---|
 | 180 |                     distList.push_back(200.0f*factor); | 
|---|
 | 181 |                     distList.push_back(230.0f*factor); | 
|---|
 | 182 |                     distList.push_back(250.0f*factor); | 
|---|
 | 183 |                     distList.push_back(270.0f*factor); | 
|---|
 | 184 |                     distList.push_back(290.0f*factor); | 
|---|
 | 185 |                     distList.push_back(310.0f*factor); | 
|---|
 | 186 |                     distList.push_back(330.0f*factor); | 
|---|
 | 187 |                     while(distList.size()>this->numLodLevels_) | 
|---|
 | 188 |                         distList.pop_back(); | 
|---|
| [7163] | 189 |  | 
|---|
 | 190 |  | 
|---|
| [7166] | 191 |                     //Generiert LOD-Levels | 
|---|
 | 192 |                     this->mesh_.getEntity()->getMesh()->generateLodLevels(distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL, this->lodReductionRate_); | 
|---|
 | 193 |                 } | 
|---|
 | 194 |                 else | 
|---|
 | 195 |                 { | 
|---|
 | 196 |                     std::string what; | 
|---|
 | 197 |                     if(lodLevel_>5) | 
|---|
 | 198 |                         what = ">5"; | 
|---|
 | 199 |                     else | 
|---|
 | 200 |                         what = "<0"; | 
|---|
| [7163] | 201 |  | 
|---|
| [7166] | 202 |                     COUT(4)<<"LodLevel not set because lodLevel("<<lodLevel_<<") was "<<what<<"." << endl; | 
|---|
| [7163] | 203 |                 } | 
|---|
| [2662] | 204 |             } | 
|---|
| [7166] | 205 |             else | 
|---|
 | 206 |                 COUT(4) << "LodLevel for " << this->meshSrc_ << " not set because is disabled." << endl; | 
|---|
| [2072] | 207 |         } | 
|---|
 | 208 |     } | 
|---|
 | 209 | } | 
|---|