Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/cegui0.8_ogre1.9/src/orxonox/graphics/Model.cc @ 12112

Last change on this file since 12112 was 11795, checked in by landauf, 7 years ago

merged ogre1.9 (including cegui0.8) into new branch

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