Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/shaders/src/orxonox/graphics/Model.cc @ 9448

Last change on this file since 9448 was 9428, checked in by davidsa, 12 years ago

FIX: changed standard render group to main, this prevents that existing alpha blending effects get broken

  • Property svn:eol-style set to native
File size: 8.8 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>
[3196]32
33#include "core/CoreIncludes.h"
[7166]34#include "core/ConfigValueIncludes.h"
[2896]35#include "core/GameMode.h"
[2072]36#include "core/XMLPort.h"
[5735]37#include "Scene.h"
[9422]38#include "RenderQueueListener.h"
[7163]39#include "graphics/MeshLodInformation.h"
40#include "Level.h"
[2072]41
42namespace orxonox
43{
44    CreateFactory(Model);
45
[7163]46    Model::Model(BaseObject* creator) :
[9428]47        StaticEntity(creator), bCastShadows_(true), renderQueueGroup_(RENDER_QUEUE_MAIN), 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);
[9407]74        XMLPortParam(Model, "renderQueueGroup", setRenderQueueGroup, getRenderQueueGroup, xmlelement, mode);
[9402]75        XMLPortParam(Model, "material", setMaterial, getMaterial, xmlelement, mode);
[2072]76        XMLPortParam(Model, "shadow", setCastShadows, getCastShadows, xmlelement, mode).defaultValues(true);
77    }
[9422]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    }
[2072]100
101    void Model::registerVariables()
102    {
[3280]103        registerVariable(this->meshSrc_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMesh));
[9407]104        registerVariable(this->renderQueueGroup_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedRenderQueueGroup));
105        registerVariable(this->materialName_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMaterial));
[3280]106        registerVariable(this->bCastShadows_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedShadows));
[2072]107    }
108
[7163]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
[2072]119    void Model::changedMesh()
120    {
[2896]121        if (GameMode::showsGraphics())
[2662]122        {
123            if (this->mesh_.getEntity())
124                this->detachOgreObject(this->mesh_.getEntity());
[2072]125
[2662]126            this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_);
[2072]127
[2662]128            if (this->mesh_.getEntity())
129            {
130                this->attachOgreObject(this->mesh_.getEntity());
131                this->mesh_.getEntity()->setCastShadows(this->bCastShadows_);
[9422]132                this->mesh_.getEntity()->setRenderQueueGroup(this->renderQueueGroup_);
[2662]133                this->mesh_.setVisible(this->isVisible());
[7163]134
[7166]135                if (this->bGlobalEnableLod_)
136                    this->enableLod();
137            }
138        }
139    }
[7163]140
[9407]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
[9402]152    void Model::changedMaterial()
153    {
154        this->mesh_.setMaterial(this->materialName_);
155    }
156
[7166]157    void Model::changedShadows()
158    {
159        this->mesh_.setCastShadows(this->bCastShadows_);
160    }
[7163]161
[7166]162    void Model::changedVisibility()
163    {
164        SUPER(Model, changedVisibility);
[7163]165
[7166]166        this->mesh_.setVisible(this->isVisible());
167    }
[7163]168
[7166]169    void Model::enableLod()
170    {
171        //LOD
172        if( this->mesh_.getEntity()->getMesh()->getNumLodLevels()==1 )
173        {
174            Level* level = this->getLevel();
[7163]175
[7166]176            assert( level != 0 );
[7163]177
[7166]178            MeshLodInformation* lodInfo = level->getLodInfo(this->meshSrc_);
179            if( lodInfo )
180            {
181                setLodLevel(lodInfo->getLodLevel());
182                this->bLodEnabled_ = lodInfo->getEnabled();
183                this->numLodLevels_ = lodInfo->getNumLevels();
184                this->lodReductionRate_ = lodInfo->getReductionRate();
185            }
186            if( this->numLodLevels_>10 )
187            {
[8858]188                orxout(internal_warning, context::lod) << "More than 10 LoD levels requested. Creating only 10." << endl;
[7166]189                this->numLodLevels_ = 10;
190            }
191            if( this->bLodEnabled_ )
192            {
193                float volume = this->mesh_.getEntity()->getBoundingBox().volume();
194/*
195                float scaleFactor = 1;
196
197                BaseObject* creatorPtr = this;
198
199                while(creatorPtr!=NULL&&orxonox_cast<WorldEntity*>(creatorPtr))
200                {
201                    scaleFactor *= getBiggestScale(((WorldEntity*) creatorPtr)->getScale3D());
202                    creatorPtr = creatorPtr->getCreator();
203                }
[8858]204                orxout() << "name: " << this->meshSrc_ << "scaleFactor: " << scaleFactor << ", volume: " << volume << endl;
[7166]205*/
[8858]206                orxout(verbose, context::lod) << "Setting lodLevel for " << this->meshSrc_<< " with lodLevel_: " << this->lodLevel_ <<" and volume: "<< volume << ":" << endl;
[7166]207
[7163]208#if OGRE_VERSION >= 0x010700
[7166]209                Ogre::Mesh::LodValueList distList;
[7163]210#else
[7166]211                Ogre::Mesh::LodDistanceList distList;
[7163]212#endif
213
[7166]214                if( lodLevel_>0 )
215                {
216//                    float factor = scaleFactor*5/lodLevel_;
217                    float factor = pow(volume, 2.0f / 3.0f) * 15.0f / lodLevel_;
[7163]218
[8858]219                    orxout(verbose, context::lod) << "LodLevel set with factor: " << factor << endl;
[7163]220
[7166]221                    distList.push_back(70.0f*factor);
222                    distList.push_back(140.0f*factor);
223                    distList.push_back(170.0f*factor);
224                    distList.push_back(200.0f*factor);
225                    distList.push_back(230.0f*factor);
226                    distList.push_back(250.0f*factor);
227                    distList.push_back(270.0f*factor);
228                    distList.push_back(290.0f*factor);
229                    distList.push_back(310.0f*factor);
230                    distList.push_back(330.0f*factor);
231                    while(distList.size()>this->numLodLevels_)
232                        distList.pop_back();
[7163]233
234
[7166]235                    //Generiert LOD-Levels
236                    this->mesh_.getEntity()->getMesh()->generateLodLevels(distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL, this->lodReductionRate_);
237                }
238                else
239                {
240                    std::string what;
241                    if(lodLevel_>5)
242                        what = ">5";
243                    else
244                        what = "<0";
[7163]245
[8858]246                    orxout(verbose, context::lod) << "LodLevel not set because lodLevel(" << lodLevel_ << ") was " << what << "." << endl;
[7163]247                }
[2662]248            }
[7166]249            else
[8858]250                orxout(verbose, context::lod) << "LodLevel for " << this->meshSrc_ << " not set because is disabled." << endl;
[2072]251        }
252    }
253}
Note: See TracBrowser for help on using the repository browser.