Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Waypoints_HS17/src/orxonox/graphics/Model.cc @ 11823

Last change on this file since 11823 was 11655, checked in by jostoffe, 7 years ago

Sicherung

  • Property svn:eol-style set to native
File size: 9.4 KB
Line 
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#include <OgreProgressiveMesh.h>
33
34#include "core/CoreIncludes.h"
35#include "core/config/ConfigValueIncludes.h"
36#include "core/GameMode.h"
37#include "core/XMLPort.h"
38#include "Scene.h"
39#include "RenderQueueListener.h"
40#include "graphics/MeshLodInformation.h"
41#include "Level.h"
42
43namespace orxonox
44{
45    RegisterClass(Model);
46
47    Model::Model(Context* context) :
48        StaticEntity(context), bCastShadows_(true), renderQueueGroup_(RENDER_QUEUE_MAIN), lodLevel_(5), bLodEnabled_(true), numLodLevels_(10), lodReductionRate_(.15f)
49    {
50        RegisterObject(Model);
51
52        this->setConfigValues();
53        this->registerVariables();
54    }
55
56    Model::~Model()
57    {
58        if (this->isInitialized() && this->mesh_.getEntity())
59            this->detachOgreObject(this->mesh_.getEntity());
60    }
61
62    void Model::setConfigValues()
63    {
64        SetConfigValueExternal(bGlobalEnableLod_, "GraphicsSettings", "enableMeshLoD", true)
65            .description("Enable level of detail for models");
66    }
67
68    void Model::XMLPort(Element& xmlelement, XMLPort::Mode mode)
69    {
70        SUPER(Model, XMLPort, xmlelement, mode);
71
72        XMLPortParam(Model, "lodLevel", setLodLevel, getLodLevel, xmlelement, mode);
73        XMLPortParam(Model, "mesh", setMeshSource, getMeshSource, xmlelement, mode);
74        XMLPortParam(Model, "renderQueueGroup", setRenderQueueGroup, getRenderQueueGroup, xmlelement, mode);
75        XMLPortParam(Model, "material", setMaterial, getMaterial, xmlelement, mode);
76        XMLPortParam(Model, "shadow", setCastShadows, getCastShadows, xmlelement, mode).defaultValues(true);
77    }
78   
79    /**
80    @brief
81        This function turns a string from XML Port into a usable ID for the rendering system
82        It defaults to the main queue if the group isn't recognized.
83       
84    @param renderQueueGroup
85        This is a string representing the render queue group. Accepted values:
86        'main', 'stencil glow', 'stencil object'
87    */
88    const unsigned int Model::getRenderQueueGroupID(const std::string& renderQueueGroup) const
89    {
90        if(renderQueueGroup.compare("stencil glow")==0)
91        {
92            return RENDER_QUEUE_STENCIL_GLOW;
93        }
94        if(renderQueueGroup.compare("stencil object")==0)
95        {
96            return RENDER_QUEUE_STENCIL_OBJECTS;
97        }
98        return RENDER_QUEUE_MAIN;
99    }
100
101    void Model::registerVariables()
102    {
103        registerVariable(this->meshSrc_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMesh));
104        registerVariable(this->renderQueueGroup_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedRenderQueueGroup));
105        registerVariable(this->materialName_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMaterial));
106        registerVariable(this->bCastShadows_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedShadows));
107    }
108
109    float Model::getBiggestScale(Vector3 scale3d)
110    {
111        float scaleFactor = scale3d.x;
112        if(scale3d.y>scaleFactor)
113            scaleFactor = scale3d.y;
114        if(scale3d.z>scaleFactor)
115            scaleFactor = scale3d.z;
116        return scaleFactor;
117    }
118
119    void Model::changedMesh()
120    {
121        if (GameMode::showsGraphics())
122        {
123            if (this->mesh_.getEntity())
124                this->detachOgreObject(this->mesh_.getEntity());
125
126            this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_);
127
128            if (this->mesh_.getEntity())
129            {
130                this->attachOgreObject(this->mesh_.getEntity());
131                this->mesh_.getEntity()->setCastShadows(this->bCastShadows_);
132                this->mesh_.getEntity()->setRenderQueueGroup(this->renderQueueGroup_);
133                this->mesh_.setVisible(this->isVisible());
134
135                if (this->bGlobalEnableLod_)
136                    this->enableLod();
137            }
138        }
139    }
140
141    void Model::changedRenderQueueGroup()
142    {
143        if (GameMode::showsGraphics())
144        {
145            if (this->mesh_.getEntity())
146            {
147                this->mesh_.getEntity()->setRenderQueueGroup(this->renderQueueGroup_);
148            }
149        }
150    }
151
152    void Model::changedMaterial()
153    {
154        this->mesh_.setMaterial(this->materialName_);
155    }
156
157    void Model::changedShadows()
158    {
159        this->mesh_.setCastShadows(this->bCastShadows_);
160    }
161
162    void Model::changedVisibility()
163    {
164        SUPER(Model, changedVisibility);
165
166        this->mesh_.setVisible(this->isVisible());
167    }
168
169    void Model::enableLod()
170    {
171#if defined(ORXONOX_COMPILER_MSVC) && OGRE_VERSION >= 0x010800 && OGRE_VERSION < 0x010900
172        // disable LOD for MSVC and ogre version 1.8 because it leads to crashes
173#else
174        //LOD
175        if( this->mesh_.getEntity()->getMesh()->getNumLodLevels()==1 )
176        {
177            Level* level = this->getLevel();
178
179            assert( level != nullptr );
180
181            MeshLodInformation* lodInfo = level->getLodInfo(this->meshSrc_);
182            if( lodInfo )
183            {
184                setLodLevel(lodInfo->getLodLevel());
185                this->bLodEnabled_ = lodInfo->getEnabled();
186                this->numLodLevels_ = lodInfo->getNumLevels();
187                this->lodReductionRate_ = lodInfo->getReductionRate();
188            }
189            if( this->numLodLevels_>10 )
190            {
191                orxout(internal_warning, context::lod) << "More than 10 LoD levels requested. Creating only 10." << endl;
192                this->numLodLevels_ = 10;
193            }
194            if( this->bLodEnabled_ )
195            {
196                float volume = this->mesh_.getEntity()->getBoundingBox().volume();
197/*
198                float scaleFactor = 1;
199
200                BaseObject* creatorPtr = this;
201
202                while(creatorPtr!=nullptr&&orxonox_cast<WorldEntity*>(creatorPtr))
203                {
204                    scaleFactor *= getBiggestScale(((WorldEntity*) creatorPtr)->getScale3D());
205                    creatorPtr = creatorPtr->getCreator();
206                }
207                orxout() << "name: " << this->meshSrc_ << "scaleFactor: " << scaleFactor << ", volume: " << volume << endl;
208*/
209                orxout(verbose, context::lod) << "Setting lodLevel for " << this->meshSrc_<< " with lodLevel_: " << this->lodLevel_ <<" and volume: "<< volume << ":" << endl;
210
211#if OGRE_VERSION >= 0x010800
212                Ogre::ProgressiveMesh::LodValueList distList;
213#elif OGRE_VERSION >= 0x010700
214                Ogre::Mesh::LodValueList distList;
215#else
216                Ogre::Mesh::LodDistanceList distList;
217#endif
218
219                if( lodLevel_>0 )
220                {
221//                    float factor = scaleFactor*5/lodLevel_;
222                    float factor = pow(volume, 2.0f / 3.0f) * 15.0f / lodLevel_;
223
224                    orxout(verbose, context::lod) << "LodLevel set with factor: " << factor << endl;
225
226                    distList.push_back(70.0f*factor);
227                    distList.push_back(140.0f*factor);
228                    distList.push_back(170.0f*factor);
229                    distList.push_back(200.0f*factor);
230                    distList.push_back(230.0f*factor);
231                    distList.push_back(250.0f*factor);
232                    distList.push_back(270.0f*factor);
233                    distList.push_back(290.0f*factor);
234                    distList.push_back(310.0f*factor);
235                    distList.push_back(330.0f*factor);
236                    while(distList.size()>this->numLodLevels_)
237                        distList.pop_back();
238
239
240                    //Generiert LOD-Levels
241#if OGRE_VERSION >= 0x010800
242                    Ogre::ProgressiveMesh::generateLodLevels(this->mesh_.getEntity()->getMesh().get(), distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL,
243                        this->lodReductionRate_);
244#else
245                    this->mesh_.getEntity()->getMesh()->generateLodLevels(distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL, this->lodReductionRate_);
246#endif
247                }
248                else
249                {
250                    std::string what;
251                    if(lodLevel_>5)
252                        what = ">5";
253                    else
254                        what = "<0";
255
256                    orxout(verbose, context::lod) << "LodLevel not set because lodLevel(" << lodLevel_ << ") was " << what << "." << endl;
257                }
258            }
259            else
260                orxout(verbose, context::lod) << "LodLevel for " << this->meshSrc_ << " not set because is disabled." << endl;
261        }
262#endif
263    }
264}
Note: See TracBrowser for help on using the repository browser.