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