Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/worldentities/Planet.cc @ 3175

Last change on this file since 3175 was 3175, checked in by rgrieder, 15 years ago

Fixed dedicated (at least for presentation09.oxw).

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