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 | |
---|
29 | #include "Model.h" |
---|
30 | |
---|
31 | #include <OgreEntity.h> |
---|
32 | |
---|
33 | #include "core/CoreIncludes.h" |
---|
34 | #include "core/GameMode.h" |
---|
35 | #include "core/XMLPort.h" |
---|
36 | #include "Scene.h" |
---|
37 | #include "graphics/MeshLodInformation.h" |
---|
38 | #include "Level.h" |
---|
39 | |
---|
40 | namespace orxonox |
---|
41 | { |
---|
42 | CreateFactory(Model); |
---|
43 | |
---|
44 | Model::Model(BaseObject* creator) : |
---|
45 | StaticEntity(creator), bCastShadows_(true), lodLevel_(5), bLodEnabled_(true), numLodLevels_(10), lodReductionRate_(.15) |
---|
46 | { |
---|
47 | RegisterObject(Model); |
---|
48 | |
---|
49 | this->registerVariables(); |
---|
50 | } |
---|
51 | |
---|
52 | Model::~Model() |
---|
53 | { |
---|
54 | if (this->isInitialized() && this->mesh_.getEntity()) |
---|
55 | this->detachOgreObject(this->mesh_.getEntity()); |
---|
56 | } |
---|
57 | |
---|
58 | void Model::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
59 | { |
---|
60 | SUPER(Model, XMLPort, xmlelement, mode); |
---|
61 | |
---|
62 | XMLPortParam(Model, "lodLevel", setLodLevel, getLodLevel, xmlelement, mode); |
---|
63 | |
---|
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 | { |
---|
70 | registerVariable(this->meshSrc_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMesh)); |
---|
71 | registerVariable(this->bCastShadows_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedShadows)); |
---|
72 | } |
---|
73 | |
---|
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 | } |
---|
83 | |
---|
84 | void Model::changedMesh() |
---|
85 | { |
---|
86 | if (GameMode::showsGraphics()) |
---|
87 | { |
---|
88 | if (this->mesh_.getEntity()) |
---|
89 | this->detachOgreObject(this->mesh_.getEntity()); |
---|
90 | |
---|
91 | this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_); |
---|
92 | |
---|
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()); |
---|
98 | |
---|
99 | |
---|
100 | //LOD |
---|
101 | if( this->mesh_.getEntity()->getMesh()->getNumLodLevels()==1 ) |
---|
102 | { |
---|
103 | Level* level = this->getLevel(); |
---|
104 | |
---|
105 | assert( level != 0 ); |
---|
106 | |
---|
107 | MeshLodInformation* lodInfo = level->getLodInfo(this->meshSrc_); |
---|
108 | if( lodInfo ) |
---|
109 | { |
---|
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 | { |
---|
122 | float volume = this->mesh_.getEntity()->getBoundingBox().volume(); |
---|
123 | // float scaleFactor = 1; |
---|
124 | |
---|
125 | // BaseObject* creatorPtr = this; |
---|
126 | // |
---|
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; |
---|
133 | |
---|
134 | COUT(4) << "Setting lodLevel for " << this->meshSrc_<< " with lodLevel_: " << this->lodLevel_ <<" and volume: "<< volume << ":" << std::endl; |
---|
135 | |
---|
136 | #if OGRE_VERSION >= 0x010700 |
---|
137 | Ogre::Mesh::LodValueList distList; |
---|
138 | #else |
---|
139 | Ogre::Mesh::LodDistanceList distList; |
---|
140 | #endif |
---|
141 | |
---|
142 | if( lodLevel_>0 ) |
---|
143 | { |
---|
144 | // float factor = scaleFactor*5/lodLevel_; |
---|
145 | float factor = volume*5/lodLevel_; |
---|
146 | |
---|
147 | COUT(4) << "LodLevel set with factor: " << factor << endl; |
---|
148 | |
---|
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); |
---|
159 | while(distList.size()>this->numLodLevels_) |
---|
160 | distList.pop_back(); |
---|
161 | |
---|
162 | |
---|
163 | //Generiert LOD-Levels |
---|
164 | this->mesh_.getEntity()->getMesh()->generateLodLevels(distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL, this->lodReductionRate_); |
---|
165 | } |
---|
166 | else |
---|
167 | { |
---|
168 | std::string what; |
---|
169 | if(lodLevel_>5) |
---|
170 | what = ">5"; |
---|
171 | else |
---|
172 | what = "<0"; |
---|
173 | |
---|
174 | COUT(4)<<"LodLevel not set because lodLevel("<<lodLevel_<<") was "<<what<<"." << endl; |
---|
175 | } |
---|
176 | } |
---|
177 | else |
---|
178 | COUT(4) << "LodLevel for " << this->meshSrc_ << " not set because is disabled." << endl; |
---|
179 | } |
---|
180 | } |
---|
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 | } |
---|