Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/lod/src/orxonox/graphics/Model.cc @ 6843

Last change on this file since 6843 was 6843, checked in by kolibri7, 14 years ago

fix of the problem of getting the right scale; cast to WorldEntity doesn't work yet!!! (Compiler error)

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