Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/mergeFS18/src/orxonox/graphics/Model.cc @ 12384

Last change on this file since 12384 was 12027, checked in by merholzl, 6 years ago

Merged Masterserver, refresh button had to be removed

  • Property svn:eol-style set to native
File size: 10.3 KB
RevLine 
[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
[2662]31#include <OgreEntity.h>
[11795]32#if OGRE_VERSION >= 0x010900
33#   include <OgreProgressiveMeshGenerator.h>
34#   include <OgreDistanceLodStrategy.h>
35#else
36#   include <OgreProgressiveMesh.h>
37#endif
[3196]38
39#include "core/CoreIncludes.h"
[9667]40#include "core/config/ConfigValueIncludes.h"
[2896]41#include "core/GameMode.h"
[2072]42#include "core/XMLPort.h"
[5735]43#include "Scene.h"
[11080]44#include "RenderQueueListener.h"
[7163]45#include "graphics/MeshLodInformation.h"
46#include "Level.h"
[2072]47
48namespace orxonox
49{
[9667]50    RegisterClass(Model);
[2072]51
[9667]52    Model::Model(Context* context) :
[11080]53        StaticEntity(context), bCastShadows_(true), renderQueueGroup_(RENDER_QUEUE_MAIN), lodLevel_(5), bLodEnabled_(true), numLodLevels_(10), lodReductionRate_(.15f)
[2072]54    {
55        RegisterObject(Model);
56
[7166]57        this->setConfigValues();
[2072]58        this->registerVariables();
59    }
60
61    Model::~Model()
62    {
63        if (this->isInitialized() && this->mesh_.getEntity())
[2662]64            this->detachOgreObject(this->mesh_.getEntity());
[2072]65    }
66
[7166]67    void Model::setConfigValues()
68    {
[8079]69        SetConfigValueExternal(bGlobalEnableLod_, "GraphicsSettings", "enableMeshLoD", true)
[7166]70            .description("Enable level of detail for models");
71    }
72
[2072]73    void Model::XMLPort(Element& xmlelement, XMLPort::Mode mode)
74    {
75        SUPER(Model, XMLPort, xmlelement, mode);
76
[7163]77        XMLPortParam(Model, "lodLevel", setLodLevel, getLodLevel, xmlelement, mode);
78
[2072]79        XMLPortParam(Model, "mesh", setMeshSource, getMeshSource, xmlelement, mode);
[11080]80        XMLPortParam(Model, "renderQueueGroup", setRenderQueueGroup, getRenderQueueGroup, xmlelement, mode);
81        XMLPortParam(Model, "material", setMaterial, getMaterial, xmlelement, mode);
[2072]82        XMLPortParam(Model, "shadow", setCastShadows, getCastShadows, xmlelement, mode).defaultValues(true);
83    }
[11080]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    }
[2072]106
107    void Model::registerVariables()
108    {
[3280]109        registerVariable(this->meshSrc_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMesh));
[11080]110        registerVariable(this->renderQueueGroup_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedRenderQueueGroup));
[12027]111        //Commented out, since this causes texture load failures on client side
112        //registerVariable(this->materialName_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMaterial));
[3280]113        registerVariable(this->bCastShadows_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedShadows));
[2072]114    }
115
[7163]116    float Model::getBiggestScale(Vector3 scale3d)
117    {
118        float scaleFactor = scale3d.x;
119        if(scale3d.y>scaleFactor)
120            scaleFactor = scale3d.y;
121        if(scale3d.z>scaleFactor)
122            scaleFactor = scale3d.z;
123        return scaleFactor;
124    }
125
[2072]126    void Model::changedMesh()
127    {
[2896]128        if (GameMode::showsGraphics())
[2662]129        {
130            if (this->mesh_.getEntity())
131                this->detachOgreObject(this->mesh_.getEntity());
[2072]132
[12027]133                // Refuse to set empty mesh sources
134                if (this->meshSrc_ != "")
135                    this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_);
[2072]136
[2662]137            if (this->mesh_.getEntity())
138            {
139                this->attachOgreObject(this->mesh_.getEntity());
140                this->mesh_.getEntity()->setCastShadows(this->bCastShadows_);
[11080]141                this->mesh_.getEntity()->setRenderQueueGroup(this->renderQueueGroup_);
[2662]142                this->mesh_.setVisible(this->isVisible());
[7163]143
[7166]144                if (this->bGlobalEnableLod_)
145                    this->enableLod();
146            }
147        }
148    }
[7163]149
[11080]150    void Model::changedRenderQueueGroup()
151    {
152        if (GameMode::showsGraphics())
153        {
154            if (this->mesh_.getEntity())
155            {
156                this->mesh_.getEntity()->setRenderQueueGroup(this->renderQueueGroup_);
157            }
158        }
159    }
160
161    void Model::changedMaterial()
162    {
163        this->mesh_.setMaterial(this->materialName_);
164    }
165
[11783]166    // PRE: a valid  Ogre::Entity* entity with a valid subentity at index
167    // POST: changed material of subentity at index to name
168    void Model::setSubMaterial(const std::string& name, const int index){
169        this->mesh_.setSubMaterial(name, index);
170    }
171
172
[7166]173    void Model::changedShadows()
174    {
175        this->mesh_.setCastShadows(this->bCastShadows_);
176    }
[7163]177
[7166]178    void Model::changedVisibility()
179    {
180        SUPER(Model, changedVisibility);
[7163]181
[7166]182        this->mesh_.setVisible(this->isVisible());
183    }
[7163]184
[7166]185    void Model::enableLod()
186    {
[10728]187#if defined(ORXONOX_COMPILER_MSVC) && OGRE_VERSION >= 0x010800 && OGRE_VERSION < 0x010900
188        // disable LOD for MSVC and ogre version 1.8 because it leads to crashes
189#else
[7166]190        //LOD
191        if( this->mesh_.getEntity()->getMesh()->getNumLodLevels()==1 )
192        {
193            Level* level = this->getLevel();
[7163]194
[11071]195            assert( level != nullptr );
[7163]196
[7166]197            MeshLodInformation* lodInfo = level->getLodInfo(this->meshSrc_);
198            if( lodInfo )
199            {
200                setLodLevel(lodInfo->getLodLevel());
201                this->bLodEnabled_ = lodInfo->getEnabled();
202                this->numLodLevels_ = lodInfo->getNumLevels();
203                this->lodReductionRate_ = lodInfo->getReductionRate();
204            }
205            if( this->numLodLevels_>10 )
206            {
[8858]207                orxout(internal_warning, context::lod) << "More than 10 LoD levels requested. Creating only 10." << endl;
[7166]208                this->numLodLevels_ = 10;
209            }
210            if( this->bLodEnabled_ )
211            {
212                float volume = this->mesh_.getEntity()->getBoundingBox().volume();
[8858]213                orxout(verbose, context::lod) << "Setting lodLevel for " << this->meshSrc_<< " with lodLevel_: " << this->lodLevel_ <<" and volume: "<< volume << ":" << endl;
[7166]214
[11795]215#if OGRE_VERSION >= 0x010900
216                Ogre::LodConfig::LodLevelList distList;
217#elif OGRE_VERSION >= 0x010800
[9675]218                Ogre::ProgressiveMesh::LodValueList distList;
219#elif OGRE_VERSION >= 0x010700
[7166]220                Ogre::Mesh::LodValueList distList;
[7163]221#else
[7166]222                Ogre::Mesh::LodDistanceList distList;
[7163]223#endif
224
[7166]225                if( lodLevel_>0 )
226                {
227                    float factor = pow(volume, 2.0f / 3.0f) * 15.0f / lodLevel_;
[7163]228
[8858]229                    orxout(verbose, context::lod) << "LodLevel set with factor: " << factor << endl;
[7163]230
[11795]231                    addLodDistanceToList(distList, 70.0f*factor);
232                    addLodDistanceToList(distList, 140.0f*factor);
233                    addLodDistanceToList(distList, 170.0f*factor);
234                    addLodDistanceToList(distList, 200.0f*factor);
235                    addLodDistanceToList(distList, 230.0f*factor);
236                    addLodDistanceToList(distList, 250.0f*factor);
237                    addLodDistanceToList(distList, 270.0f*factor);
238                    addLodDistanceToList(distList, 290.0f*factor);
239                    addLodDistanceToList(distList, 310.0f*factor);
240                    addLodDistanceToList(distList, 330.0f*factor);
[7166]241                    while(distList.size()>this->numLodLevels_)
242                        distList.pop_back();
[7163]243
244
[7166]245                    //Generiert LOD-Levels
[11795]246#if OGRE_VERSION >= 0x010900
247                    Ogre::LodConfig config;
248                    config.mesh = this->mesh_.getEntity()->getMesh();
249                    config.levels = distList;
250                    config.strategy = Ogre::DistanceLodSphereStrategy::getSingletonPtr();
251
252                    Ogre::ProgressiveMeshGenerator generator;
253                    generator.generateLodLevels(config);
254#elif OGRE_VERSION >= 0x010800
[9675]255                    Ogre::ProgressiveMesh::generateLodLevels(this->mesh_.getEntity()->getMesh().get(), distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL,
256                        this->lodReductionRate_);
257#else
[7166]258                    this->mesh_.getEntity()->getMesh()->generateLodLevels(distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL, this->lodReductionRate_);
[9675]259#endif
[7166]260                }
261                else
262                {
263                    std::string what;
264                    if(lodLevel_>5)
265                        what = ">5";
266                    else
267                        what = "<0";
[7163]268
[8858]269                    orxout(verbose, context::lod) << "LodLevel not set because lodLevel(" << lodLevel_ << ") was " << what << "." << endl;
[7163]270                }
[2662]271            }
[7166]272            else
[8858]273                orxout(verbose, context::lod) << "LodLevel for " << this->meshSrc_ << " not set because is disabled." << endl;
[2072]274        }
[10728]275#endif
[2072]276    }
[11795]277
278    template <class T>
279    void Model::addLodDistanceToList(T& list, float distance)
280    {
281#if OGRE_VERSION >= 0x010900
282        list.push_back({distance, Ogre::LodLevel::VRM_COLLAPSE_COST, this->lodReductionRate_, 0, false});
283#else
284        list.push_back(distance);
285#endif
286    }
[2072]287}
Note: See TracBrowser for help on using the repository browser.