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