Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9407 was 9407, checked in by davidsa, 13 years ago

orxonox::RenderQueueListener: Implemented a rudimentary RenderQueueListener to enable the use of stencil buffer for elaborate alpha blending shaders without creating artifacts from overlapping faces. Also added a XML Port to assign a Model to a certain RenderQueueGroup. Needs to be improved to allow the use of strings for choosing the group instead of a static int which may change in the feature.

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