Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/graphics/Model.cc @ 11071

Last change on this file since 11071 was 11071, checked in by landauf, 8 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 7.7 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>
[9675]32#include <OgreProgressiveMesh.h>
[3196]33
34#include "core/CoreIncludes.h"
[9667]35#include "core/config/ConfigValueIncludes.h"
[2896]36#include "core/GameMode.h"
[2072]37#include "core/XMLPort.h"
[5735]38#include "Scene.h"
[7163]39#include "graphics/MeshLodInformation.h"
40#include "Level.h"
[2072]41
42namespace orxonox
43{
[9667]44    RegisterClass(Model);
[2072]45
[9667]46    Model::Model(Context* context) :
47        StaticEntity(context), bCastShadows_(true), lodLevel_(5), bLodEnabled_(true), numLodLevels_(10), lodReductionRate_(.15f)
[2072]48    {
49        RegisterObject(Model);
50
[7166]51        this->setConfigValues();
[2072]52        this->registerVariables();
53    }
54
55    Model::~Model()
56    {
57        if (this->isInitialized() && this->mesh_.getEntity())
[2662]58            this->detachOgreObject(this->mesh_.getEntity());
[2072]59    }
60
[7166]61    void Model::setConfigValues()
62    {
[8079]63        SetConfigValueExternal(bGlobalEnableLod_, "GraphicsSettings", "enableMeshLoD", true)
[7166]64            .description("Enable level of detail for models");
65    }
66
[2072]67    void Model::XMLPort(Element& xmlelement, XMLPort::Mode mode)
68    {
69        SUPER(Model, XMLPort, xmlelement, mode);
70
[7163]71        XMLPortParam(Model, "lodLevel", setLodLevel, getLodLevel, xmlelement, mode);
72
[2072]73        XMLPortParam(Model, "mesh", setMeshSource, getMeshSource, xmlelement, mode);
74        XMLPortParam(Model, "shadow", setCastShadows, getCastShadows, xmlelement, mode).defaultValues(true);
75    }
76
77    void Model::registerVariables()
78    {
[3280]79        registerVariable(this->meshSrc_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMesh));
80        registerVariable(this->bCastShadows_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedShadows));
[2072]81    }
82
[7163]83    float Model::getBiggestScale(Vector3 scale3d)
84    {
85        float scaleFactor = scale3d.x;
86        if(scale3d.y>scaleFactor)
87            scaleFactor = scale3d.y;
88        if(scale3d.z>scaleFactor)
89            scaleFactor = scale3d.z;
90        return scaleFactor;
91    }
92
[2072]93    void Model::changedMesh()
94    {
[2896]95        if (GameMode::showsGraphics())
[2662]96        {
97            if (this->mesh_.getEntity())
98                this->detachOgreObject(this->mesh_.getEntity());
[2072]99
[2662]100            this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_);
[2072]101
[2662]102            if (this->mesh_.getEntity())
103            {
104                this->attachOgreObject(this->mesh_.getEntity());
105                this->mesh_.getEntity()->setCastShadows(this->bCastShadows_);
106                this->mesh_.setVisible(this->isVisible());
[7163]107
[7166]108                if (this->bGlobalEnableLod_)
109                    this->enableLod();
110            }
111        }
112    }
[7163]113
[7166]114    void Model::changedShadows()
115    {
116        this->mesh_.setCastShadows(this->bCastShadows_);
117    }
[7163]118
[7166]119    void Model::changedVisibility()
120    {
121        SUPER(Model, changedVisibility);
[7163]122
[7166]123        this->mesh_.setVisible(this->isVisible());
124    }
[7163]125
[7166]126    void Model::enableLod()
127    {
[10728]128#if defined(ORXONOX_COMPILER_MSVC) && OGRE_VERSION >= 0x010800 && OGRE_VERSION < 0x010900
129        // disable LOD for MSVC and ogre version 1.8 because it leads to crashes
130#else
[7166]131        //LOD
132        if( this->mesh_.getEntity()->getMesh()->getNumLodLevels()==1 )
133        {
134            Level* level = this->getLevel();
[7163]135
[11071]136            assert( level != nullptr );
[7163]137
[7166]138            MeshLodInformation* lodInfo = level->getLodInfo(this->meshSrc_);
139            if( lodInfo )
140            {
141                setLodLevel(lodInfo->getLodLevel());
142                this->bLodEnabled_ = lodInfo->getEnabled();
143                this->numLodLevels_ = lodInfo->getNumLevels();
144                this->lodReductionRate_ = lodInfo->getReductionRate();
145            }
146            if( this->numLodLevels_>10 )
147            {
[8858]148                orxout(internal_warning, context::lod) << "More than 10 LoD levels requested. Creating only 10." << endl;
[7166]149                this->numLodLevels_ = 10;
150            }
151            if( this->bLodEnabled_ )
152            {
153                float volume = this->mesh_.getEntity()->getBoundingBox().volume();
154/*
155                float scaleFactor = 1;
156
157                BaseObject* creatorPtr = this;
158
[11071]159                while(creatorPtr!=nullptr&&orxonox_cast<WorldEntity*>(creatorPtr))
[7166]160                {
161                    scaleFactor *= getBiggestScale(((WorldEntity*) creatorPtr)->getScale3D());
162                    creatorPtr = creatorPtr->getCreator();
163                }
[8858]164                orxout() << "name: " << this->meshSrc_ << "scaleFactor: " << scaleFactor << ", volume: " << volume << endl;
[7166]165*/
[8858]166                orxout(verbose, context::lod) << "Setting lodLevel for " << this->meshSrc_<< " with lodLevel_: " << this->lodLevel_ <<" and volume: "<< volume << ":" << endl;
[7166]167
[9675]168#if OGRE_VERSION >= 0x010800
169                Ogre::ProgressiveMesh::LodValueList distList;
170#elif OGRE_VERSION >= 0x010700
[7166]171                Ogre::Mesh::LodValueList distList;
[7163]172#else
[7166]173                Ogre::Mesh::LodDistanceList distList;
[7163]174#endif
175
[7166]176                if( lodLevel_>0 )
177                {
178//                    float factor = scaleFactor*5/lodLevel_;
179                    float factor = pow(volume, 2.0f / 3.0f) * 15.0f / lodLevel_;
[7163]180
[8858]181                    orxout(verbose, context::lod) << "LodLevel set with factor: " << factor << endl;
[7163]182
[7166]183                    distList.push_back(70.0f*factor);
184                    distList.push_back(140.0f*factor);
185                    distList.push_back(170.0f*factor);
186                    distList.push_back(200.0f*factor);
187                    distList.push_back(230.0f*factor);
188                    distList.push_back(250.0f*factor);
189                    distList.push_back(270.0f*factor);
190                    distList.push_back(290.0f*factor);
191                    distList.push_back(310.0f*factor);
192                    distList.push_back(330.0f*factor);
193                    while(distList.size()>this->numLodLevels_)
194                        distList.pop_back();
[7163]195
196
[7166]197                    //Generiert LOD-Levels
[9675]198#if OGRE_VERSION >= 0x010800
199                    Ogre::ProgressiveMesh::generateLodLevels(this->mesh_.getEntity()->getMesh().get(), distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL,
200                        this->lodReductionRate_);
201#else
[7166]202                    this->mesh_.getEntity()->getMesh()->generateLodLevels(distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL, this->lodReductionRate_);
[9675]203#endif
[7166]204                }
205                else
206                {
207                    std::string what;
208                    if(lodLevel_>5)
209                        what = ">5";
210                    else
211                        what = "<0";
[7163]212
[8858]213                    orxout(verbose, context::lod) << "LodLevel not set because lodLevel(" << lodLevel_ << ") was " << what << "." << endl;
[7163]214                }
[2662]215            }
[7166]216            else
[8858]217                orxout(verbose, context::lod) << "LodLevel for " << this->meshSrc_ << " not set because is disabled." << endl;
[2072]218        }
[10728]219#endif
[2072]220    }
221}
Note: See TracBrowser for help on using the repository browser.