Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Presentation_HS17_merge/src/orxonox/graphics/Model.cc @ 11766

Last change on this file since 11766 was 11766, checked in by landauf, 6 years ago

merged SOBv2_HS17

  • Property svn:eol-style set to native
File size: 9.6 KB
Line 
1
2/*
3 *   ORXONOX - the hottest 3D action shooter ever to exist
4 *                    > www.orxonox.net <
5 *
6 *
7 *   License notice:
8 *
9 *   This program is free software; you can redistribute it and/or
10 *   modify it under the terms of the GNU General Public License
11 *   as published by the Free Software Foundation; either version 2
12 *   of the License, or (at your option) any later version.
13 *
14 *   This program is distributed in the hope that it will be useful,
15 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *   GNU General Public License for more details.
18 *
19 *   You should have received a copy of the GNU General Public License
20 *   along with this program; if not, write to the Free Software
21 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 *
23 *   Author:
24 *      Fabian 'x3n' Landau
25 *   Co-authors:
26 *      ...
27 *
28 */
29
30#include "Model.h"
31
32#include <OgreEntity.h>
33#include <OgreProgressiveMesh.h>
34
35#include "core/CoreIncludes.h"
36#include "core/config/ConfigValueIncludes.h"
37#include "core/GameMode.h"
38#include "core/XMLPort.h"
39#include "Scene.h"
40#include "RenderQueueListener.h"
41#include "graphics/MeshLodInformation.h"
42#include "Level.h"
43
44
45namespace orxonox
46{
47    RegisterClass(Model);
48
49    Model::Model(Context* context) :
50        StaticEntity(context), bCastShadows_(true), renderQueueGroup_(RENDER_QUEUE_MAIN), lodLevel_(5), bLodEnabled_(true), numLodLevels_(10), lodReductionRate_(.15f)
51    {
52        RegisterObject(Model);
53
54        this->setConfigValues();
55        this->registerVariables();
56    }
57
58    Model::~Model()
59    {
60        if (this->isInitialized() && this->mesh_.getEntity())
61            this->detachOgreObject(this->mesh_.getEntity());
62    }
63
64    void Model::setConfigValues()
65    {
66        SetConfigValueExternal(bGlobalEnableLod_, "GraphicsSettings", "enableMeshLoD", true)
67            .description("Enable level of detail for models");
68    }
69
70    void Model::XMLPort(Element& xmlelement, XMLPort::Mode mode)
71    {
72        SUPER(Model, XMLPort, xmlelement, mode);
73
74        XMLPortParam(Model, "lodLevel", setLodLevel, getLodLevel, xmlelement, mode);
75
76        XMLPortParam(Model, "mesh", setMeshSource, getMeshSource, xmlelement, mode);
77        XMLPortParam(Model, "renderQueueGroup", setRenderQueueGroup, getRenderQueueGroup, xmlelement, mode);
78        XMLPortParam(Model, "material", setMaterial, getMaterial, xmlelement, mode);
79        XMLPortParam(Model, "shadow", setCastShadows, getCastShadows, xmlelement, mode).defaultValues(true);
80    }
81   
82    /**
83    @brief
84        This function turns a string from XML Port into a usable ID for the rendering system
85        It defaults to the main queue if the group isn't recognized.
86       
87    @param renderQueueGroup
88        This is a string representing the render queue group. Accepted values:
89        'main', 'stencil glow', 'stencil object'
90    */
91    const unsigned int Model::getRenderQueueGroupID(const std::string& renderQueueGroup) const
92    {
93        if(renderQueueGroup.compare("stencil glow")==0)
94        {
95            return RENDER_QUEUE_STENCIL_GLOW;
96        }
97        if(renderQueueGroup.compare("stencil object")==0)
98        {
99            return RENDER_QUEUE_STENCIL_OBJECTS;
100        }
101        return RENDER_QUEUE_MAIN;
102    }
103
104    void Model::registerVariables()
105    {
106        registerVariable(this->meshSrc_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMesh));
107        registerVariable(this->renderQueueGroup_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedRenderQueueGroup));
108        registerVariable(this->materialName_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMaterial));
109        registerVariable(this->bCastShadows_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedShadows));
110    }
111
112    float Model::getBiggestScale(Vector3 scale3d)
113    {
114        float scaleFactor = scale3d.x;
115        if(scale3d.y>scaleFactor)
116            scaleFactor = scale3d.y;
117        if(scale3d.z>scaleFactor)
118            scaleFactor = scale3d.z;
119        return scaleFactor;
120    }
121
122    void Model::changedMesh()
123    {
124        if (GameMode::showsGraphics())
125        {
126            if (this->mesh_.getEntity())
127                this->detachOgreObject(this->mesh_.getEntity());
128
129            this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_);
130
131            if (this->mesh_.getEntity())
132            {
133                this->attachOgreObject(this->mesh_.getEntity());
134                this->mesh_.getEntity()->setCastShadows(this->bCastShadows_);
135                this->mesh_.getEntity()->setRenderQueueGroup(this->renderQueueGroup_);
136                this->mesh_.setVisible(this->isVisible());
137
138                if (this->bGlobalEnableLod_)
139                    this->enableLod();
140            }
141        }
142    }
143
144    void Model::changedRenderQueueGroup()
145    {
146        if (GameMode::showsGraphics())
147        {
148            if (this->mesh_.getEntity())
149            {
150                this->mesh_.getEntity()->setRenderQueueGroup(this->renderQueueGroup_);
151            }
152        }
153    }
154
155    void Model::changedMaterial()
156    {
157        this->mesh_.setMaterial(this->materialName_);
158    }
159
160    // PRE: a valid  Ogre::Entity* entity with a valid subentity at index
161    // POST: changed material of subentity at index to name
162    void Model::setSubMaterial(const std::string& name, const int index){
163        this->mesh_.setSubMaterial(name, index);
164    }
165
166
167    void Model::changedShadows()
168    {
169        this->mesh_.setCastShadows(this->bCastShadows_);
170    }
171
172    void Model::changedVisibility()
173    {
174        SUPER(Model, changedVisibility);
175
176        this->mesh_.setVisible(this->isVisible());
177    }
178
179    void Model::enableLod()
180    {
181#if defined(ORXONOX_COMPILER_MSVC) && OGRE_VERSION >= 0x010800 && OGRE_VERSION < 0x010900
182        // disable LOD for MSVC and ogre version 1.8 because it leads to crashes
183#else
184        //LOD
185        if( this->mesh_.getEntity()->getMesh()->getNumLodLevels()==1 )
186        {
187            Level* level = this->getLevel();
188
189            assert( level != nullptr );
190
191            MeshLodInformation* lodInfo = level->getLodInfo(this->meshSrc_);
192            if( lodInfo )
193            {
194                setLodLevel(lodInfo->getLodLevel());
195                this->bLodEnabled_ = lodInfo->getEnabled();
196                this->numLodLevels_ = lodInfo->getNumLevels();
197                this->lodReductionRate_ = lodInfo->getReductionRate();
198            }
199            if( this->numLodLevels_>10 )
200            {
201                orxout(internal_warning, context::lod) << "More than 10 LoD levels requested. Creating only 10." << endl;
202                this->numLodLevels_ = 10;
203            }
204            if( this->bLodEnabled_ )
205            {
206                float volume = this->mesh_.getEntity()->getBoundingBox().volume();
207/*
208                float scaleFactor = 1;
209
210                BaseObject* creatorPtr = this;
211
212                while(creatorPtr!=nullptr&&orxonox_cast<WorldEntity*>(creatorPtr))
213                {
214                    scaleFactor *= getBiggestScale(((WorldEntity*) creatorPtr)->getScale3D());
215                    creatorPtr = creatorPtr->getCreator();
216                }
217                orxout() << "name: " << this->meshSrc_ << "scaleFactor: " << scaleFactor << ", volume: " << volume << endl;
218*/
219                orxout(verbose, context::lod) << "Setting lodLevel for " << this->meshSrc_<< " with lodLevel_: " << this->lodLevel_ <<" and volume: "<< volume << ":" << endl;
220
221#if OGRE_VERSION >= 0x010800
222                Ogre::ProgressiveMesh::LodValueList distList;
223#elif OGRE_VERSION >= 0x010700
224                Ogre::Mesh::LodValueList distList;
225#else
226                Ogre::Mesh::LodDistanceList distList;
227#endif
228
229                if( lodLevel_>0 )
230                {
231//                    float factor = scaleFactor*5/lodLevel_;
232                    float factor = pow(volume, 2.0f / 3.0f) * 15.0f / lodLevel_;
233
234                    orxout(verbose, context::lod) << "LodLevel set with factor: " << factor << endl;
235
236                    distList.push_back(70.0f*factor);
237                    distList.push_back(140.0f*factor);
238                    distList.push_back(170.0f*factor);
239                    distList.push_back(200.0f*factor);
240                    distList.push_back(230.0f*factor);
241                    distList.push_back(250.0f*factor);
242                    distList.push_back(270.0f*factor);
243                    distList.push_back(290.0f*factor);
244                    distList.push_back(310.0f*factor);
245                    distList.push_back(330.0f*factor);
246                    while(distList.size()>this->numLodLevels_)
247                        distList.pop_back();
248
249
250                    //Generiert LOD-Levels
251#if OGRE_VERSION >= 0x010800
252                    Ogre::ProgressiveMesh::generateLodLevels(this->mesh_.getEntity()->getMesh().get(), distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL,
253                        this->lodReductionRate_);
254#else
255                    this->mesh_.getEntity()->getMesh()->generateLodLevels(distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL, this->lodReductionRate_);
256#endif
257                }
258                else
259                {
260                    std::string what;
261                    if(lodLevel_>5)
262                        what = ">5";
263                    else
264                        what = "<0";
265
266                    orxout(verbose, context::lod) << "LodLevel not set because lodLevel(" << lodLevel_ << ") was " << what << "." << endl;
267                }
268            }
269            else
270                orxout(verbose, context::lod) << "LodLevel for " << this->meshSrc_ << " not set because is disabled." << endl;
271        }
272#endif
273    }
274}
Note: See TracBrowser for help on using the repository browser.