Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/objects/Planet.cc @ 11578

Last change on this file since 11578 was 10624, checked in by landauf, 9 years ago

merged branch core7 back to trunk

  • Property svn:eol-style set to native
File size: 6.4 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 *      Marian Runo
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "Planet.h"
30
31#include <OgreEntity.h>
32#include <OgreBillboardSet.h>
33#include <OgreProgressiveMesh.h>
34
35#include "core/CoreIncludes.h"
36#include "core/GameMode.h"
37#include "core/XMLPort.h"
38#include "Scene.h"
39#include "graphics/Camera.h"
40#include "CameraManager.h"
41
42namespace orxonox
43{
44    RegisterClass(Planet);
45
46    /**
47     * @brief Constructor
48     */
49    Planet::Planet(Context* context) : MovableEntity(context)
50    {
51        RegisterObject(Planet);
52        this->registerVariables();
53    }
54
55    /**
56     * @brief Destructor
57     */
58    Planet::~Planet()
59    {
60        if (this->isInitialized() && this->mesh_.getEntity())
61            this->detachOgreObject(this->mesh_.getEntity());
62    }
63
64    void Planet::tick(float dt)
65    {
66        if (!this->isVisible())
67            return;
68
69        if (GameMode::showsGraphics())
70        {
71            Camera* activeCamera = CameraManager::getInstance().getActiveCamera();
72            if (activeCamera && this->billboard_.getBillboardSet())
73            {
74                float distance = this->getPosition().distance( activeCamera->getWorldPosition() );
75                //             orxout(internal_warning) << distance << endl;
76                float planetRadius = this->getScale();
77
78                float newScale = 2 * distance / sqrt(distance*distance - planetRadius*planetRadius);
79                float tempTest = newScale*(1+float(this->atmosphereSize)/float(this->imageSize));
80                newScale = tempTest;
81
82                this->billboard_.getBillboardSet()->setDefaultDimensions(newScale, newScale);
83            }
84        }
85
86        SUPER(Planet, tick, dt);
87    }
88
89    void Planet::changedMesh()
90    {
91        if( GameMode::showsGraphics() )
92        {
93            if (this->mesh_.getEntity())
94                this->detachOgreObject(this->mesh_.getEntity());
95
96            this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_);
97
98            if (this->mesh_.getEntity())
99            {
100                this->attachOgreObject(this->mesh_.getEntity());
101                this->mesh_.getEntity()->setCastShadows(this->bCastShadows_);
102                this->mesh_.setVisible(this->isVisible());
103
104                float scaleFactor = this->getScale();
105
106#if OGRE_VERSION >= 0x010800
107                Ogre::ProgressiveMesh::LodValueList distList;
108#elif OGRE_VERSION >= 0x010700
109                Ogre::Mesh::LodValueList distList;
110#else
111                Ogre::Mesh::LodDistanceList distList;
112#endif
113
114                distList.push_back(10.0f*scaleFactor);
115                distList.push_back(19.0f*scaleFactor);
116                distList.push_back(27.0f*scaleFactor);
117                distList.push_back(34.0f*scaleFactor);
118                distList.push_back(40.0f*scaleFactor);
119                distList.push_back(45.0f*scaleFactor);
120                distList.push_back(49.0f*scaleFactor);
121                distList.push_back(52.0f*scaleFactor);
122                distList.push_back(54.0f*scaleFactor);
123                distList.push_back(55.0f*scaleFactor);
124
125                float reductionValue = 0.2f;
126
127#if OGRE_VERSION >= 0x010800
128                Ogre::ProgressiveMesh::generateLodLevels(this->mesh_.getEntity()->getMesh().get(), distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL,
129                    reductionValue);
130#else
131                this->mesh_.getEntity()->getMesh()->generateLodLevels(distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL, reductionValue);
132#endif
133                billboard_.setBillboardSet(this->getScene()->getSceneManager(), this->atmosphere_, Vector3(0,0,0));
134
135                this->attachOgreObject(this->billboard_.getBillboardSet());
136                this->billboard_.getBillboardSet()->setUseAccurateFacing(true);
137                this->setCastShadows(true);
138                this->billboard_.getBillboardSet()->setRenderQueueGroup(this->mesh_.getEntity()->getRenderQueueGroup());
139                this->mesh_.setCastShadows(true);
140            }
141        }
142    }
143
144    void Planet::changedShadows()
145    {
146        this->mesh_.setCastShadows(this->bCastShadows_);
147    }
148
149    void Planet::XMLPort(Element& xmlelement, XMLPort::Mode mode)
150    {
151        SUPER(Planet, XMLPort, xmlelement, mode);
152
153        XMLPortParam(Planet, "atmosphere", setAtmosphere, getAtmosphere, xmlelement, mode).defaultValues("planet/Atmosphere");
154        XMLPortParam(Planet, "atmospheresize", setAtmosphereSize, getAtmosphereSize, xmlelement,mode);
155        XMLPortParam(Planet, "imagesize", setImageSize, getImageSize, xmlelement,mode);
156        XMLPortParam(Planet, "mesh", setMeshSource, getMeshSource, xmlelement, mode);
157        XMLPortParam(Planet, "shadow", setCastShadows, getCastShadows, xmlelement, mode).defaultValues(true);
158    }
159
160    void Planet::registerVariables()
161    {
162        registerVariable(this->atmosphere_, VariableDirection::ToClient);
163        registerVariable(this->atmosphereSize, VariableDirection::ToClient);
164        registerVariable(this->imageSize, VariableDirection::ToClient);
165        // Note: the meshSrc should be synchronised after atmosphere and other values, because the meshSrc callback setts the atmosphere billboards
166        registerVariable(this->meshSrc_, VariableDirection::ToClient, new NetworkCallback<Planet>(this, &Planet::changedMesh));
167        registerVariable(this->bCastShadows_, VariableDirection::ToClient, new NetworkCallback<Planet>(this, &Planet::changedShadows));
168    }
169
170    void Planet::changedVisibility()
171    {
172        SUPER(Planet, changedVisibility);
173        if (this->isInitialized())
174        {
175            this->mesh_.setVisible(this->isVisible());
176            this->billboard_.setVisible(this->isVisible());
177        }
178    }
179}
Note: See TracBrowser for help on using the repository browser.