Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/orxonox/graphics/Model.cc @ 7024

Last change on this file since 7024 was 7024, checked in by scheusso, 14 years ago

accidentially disabled lod

  • Property svn:eol-style set to native
File size: 6.7 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/GameMode.h"
35#include "core/XMLPort.h"
36#include "Scene.h"
37#include "graphics/MeshLodInformation.h"
38#include "Level.h"
39
40namespace orxonox
41{
42    CreateFactory(Model);
43
44    Model::Model(BaseObject* creator) : StaticEntity(creator)
45    {
46        RegisterObject(Model);
47
48        this->bCastShadows_ = true;
49
50        this->registerVariables();
51        //LoD
52        this->lodLevel_=5;
53    }
54
55    Model::~Model()
56    {
57        if (this->isInitialized() && this->mesh_.getEntity())
58            this->detachOgreObject(this->mesh_.getEntity());
59    }
60
61    void Model::XMLPort(Element& xmlelement, XMLPort::Mode mode)
62    {
63        SUPER(Model, XMLPort, xmlelement, mode);
64       
65        XMLPortParam(Model, "lodLevel", setLodLevel, getLodLevel, xmlelement, mode);
66       
67        XMLPortParam(Model, "mesh", setMeshSource, getMeshSource, xmlelement, mode);
68        XMLPortParam(Model, "shadow", setCastShadows, getCastShadows, xmlelement, mode).defaultValues(true);
69    }
70
71    void Model::registerVariables()
72    {
73        registerVariable(this->meshSrc_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMesh));
74        registerVariable(this->bCastShadows_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedShadows));
75    }
76
77    float Model::getBiggestScale(Vector3 scale3d)
78    {
79        float scaleFactor = scale3d.x;
80        if(scale3d.y>scaleFactor)
81            scaleFactor = scale3d.y;
82        if(scale3d.z>scaleFactor)
83            scaleFactor = scale3d.z;
84        return scaleFactor;
85    }
86   
87    void Model::changedMesh()
88    {
89        if (GameMode::showsGraphics())
90        {
91            if (this->mesh_.getEntity())
92                this->detachOgreObject(this->mesh_.getEntity());
93           
94            this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_);
95
96            if (this->mesh_.getEntity())
97            {
98                this->attachOgreObject(this->mesh_.getEntity());
99                this->mesh_.getEntity()->setCastShadows(this->bCastShadows_);
100                this->mesh_.setVisible(this->isVisible());
101               
102               
103                //LOD
104                if(this->mesh_.getEntity()->getMesh()->getNumLodLevels()==1
105                    &&this->meshSrc_!="laserbeam.mesh")
106                {
107                    Level* level = this->getLevel();
108                 
109                    assert( level != 0 );
110                   
111                    if( level->getLodInfo(this->meshSrc_)!=0 )
112                        setLodLevel(level->getLodInfo(this->meshSrc_)->getLodLevel());
113                    if( level->getLodInfo(this->meshSrc_)==0 || level->getLodInfo(this->meshSrc_)->getEnabled() )
114                    {
115
116                        float volume = this->mesh_.getEntity()->getBoundingBox().volume();
117    //                     float scaleFactor = 1;
118                       
119    //                     BaseObject* creatorPtr = this;
120    //                     
121    //                     while(creatorPtr!=NULL&&orxonox_cast<WorldEntity*>(creatorPtr))
122    //                     {
123    //                         scaleFactor *= getBiggestScale(((WorldEntity*) creatorPtr)->getScale3D());
124    //                         creatorPtr = creatorPtr->getCreator();
125    //                     }
126    //                     COUT(0) << "name: " << this->meshSrc_ << "scaleFactor: " << scaleFactor << ", volume: " << volume << endl;
127                       
128                        COUT(4) << "Setting lodLevel for " << this->meshSrc_<< " with lodLevel_: " << this->lodLevel_ <<" and volume: "<< volume << ":" << std::endl;
129
130#if OGRE_VERSION >= 0x010700
131                        Ogre::Mesh::LodValueList distList;
132#else
133                        Ogre::Mesh::LodDistanceList distList;
134#endif
135
136                        if( lodLevel_>0 )
137                        {
138    //                         float factor = scaleFactor*5/lodLevel_;
139                            float factor = volume/3/lodLevel_;
140                           
141                            COUT(4)<<"LodLevel set with factor: "<<factor<<std::endl;
142
143                            distList.push_back(70.0f*factor);
144                            distList.push_back(140.0f*factor);
145                            distList.push_back(170.0f*factor);
146                            distList.push_back(200.0f*factor);
147                            distList.push_back(230.0f*factor);
148                            distList.push_back(250.0f*factor);
149                            distList.push_back(270.0f*factor);
150                            distList.push_back(290.0f*factor);
151                            distList.push_back(310.0f*factor);
152                            distList.push_back(330.0f*factor);
153
154                            float reductionValue = 0.15f;
155
156                           
157                            //Generiert LOD-Levels
158                            this->mesh_.getEntity()->getMesh()->generateLodLevels(distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL, reductionValue);
159                        }
160                        else
161                        {
162                            std::string what;
163                            if(lodLevel_>5)
164                                what = ">5";
165                            else
166                                what = "<0";
167                           
168                            COUT(4)<<"LodLevel not set because lodLevel("<<lodLevel_<<") was "<<what<<"."<<std::endl;
169                        }
170                    }
171                }
172            }
173        }
174    }
175
176    void Model::changedShadows()
177    {
178        this->mesh_.setCastShadows(this->bCastShadows_);
179    }
180
181    void Model::changedVisibility()
182    {
183        SUPER(Model, changedVisibility);
184
185        this->mesh_.setVisible(this->isVisible());
186    }
187}
Note: See TracBrowser for help on using the repository browser.