Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/mac_osx2/src/modules/objects/Planet.cc @ 8389

Last change on this file since 8389 was 8389, checked in by youngk, 13 years ago

Adding Collision Shapes to Planets. Doesn't currently work.

  • 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 *      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 "objects/collisionshapes/SphereCollisionShape.h"
40#include "graphics/Camera.h"
41#include "CameraManager.h"
42
43namespace orxonox
44{
45    CreateFactory(Planet);
46
47    /**
48     * @brief Constructor
49     */
50    Planet::Planet(BaseObject* creator) : MovableEntity(creator)
51    {
52        RegisterObject(Planet);
53        this->registerVariables();
54       
55        // Get notification about collisions
56        if (GameMode::isMaster())
57        {
58            //this->setMass(1.0);
59            this->enableCollisionCallback();
60            this->setCollisionResponse(false);
61            this->setCollisionType(Static);
62           
63            SphereCollisionShape* shape = new SphereCollisionShape(this);
64            //shape->setRadius(20);
65            this->attachCollisionShape(shape);
66        }
67    }
68
69    /**
70     * @brief Destructor
71     */
72    Planet::~Planet()
73    {
74        if (this->isInitialized() && this->mesh_.getEntity())
75            this->detachOgreObject(this->mesh_.getEntity());
76    }
77
78    void Planet::tick(float dt)
79    {
80        if (!this->isVisible())
81            return;
82
83        if (GameMode::showsGraphics())
84        {
85            Camera* activeCamera = CameraManager::getInstance().getActiveCamera();
86            if (activeCamera && this->billboard_.getBillboardSet())
87            {
88                float distance = this->getPosition().distance( activeCamera->getWorldPosition() );
89                //             COUT(2) << distance << std::endl;
90                float planetRadius = this->getScale();
91
92                float newScale = 2 * distance / sqrt(distance*distance - planetRadius*planetRadius);
93                float tempTest = newScale*(1+float(this->atmosphereSize)/float(this->imageSize));
94                newScale = tempTest;
95
96                this->billboard_.getBillboardSet()->setDefaultDimensions(newScale, newScale);
97            }
98        }
99
100        SUPER(Planet, tick, dt);
101    }
102
103    void Planet::init()
104    {
105    }
106
107    void Planet::changedMesh()
108    {
109        if( GameMode::showsGraphics() )
110        {
111            if (this->mesh_.getEntity())
112                this->detachOgreObject(this->mesh_.getEntity());
113
114            this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_);
115
116            if (this->mesh_.getEntity())
117            {
118                this->attachOgreObject(this->mesh_.getEntity());
119                this->mesh_.getEntity()->setCastShadows(this->bCastShadows_);
120                this->mesh_.setVisible(this->isVisible());
121
122                float scaleFactor = this->getScale();
123
124    #if OGRE_VERSION >= 0x010700
125                Ogre::Mesh::LodValueList distList;
126    #else
127                Ogre::Mesh::LodDistanceList distList;
128    #endif
129
130                distList.push_back(10.0f*scaleFactor);
131                distList.push_back(19.0f*scaleFactor);
132                distList.push_back(27.0f*scaleFactor);
133                distList.push_back(34.0f*scaleFactor);
134                distList.push_back(40.0f*scaleFactor);
135                distList.push_back(45.0f*scaleFactor);
136                distList.push_back(49.0f*scaleFactor);
137                distList.push_back(52.0f*scaleFactor);
138                distList.push_back(54.0f*scaleFactor);
139                distList.push_back(55.0f*scaleFactor);
140
141                float reductionValue = 0.2f;
142
143                this->mesh_.getEntity()->getMesh()->generateLodLevels(distList, Ogre::ProgressiveMesh::VRQ_PROPORTIONAL, reductionValue);
144                billboard_.setBillboardSet(this->getScene()->getSceneManager(), this->atmosphere_, Vector3(0,0,0));
145
146                this->attachOgreObject(this->billboard_.getBillboardSet());
147                this->billboard_.getBillboardSet()->setUseAccurateFacing(true);
148                this->setCastShadows(true);
149                this->billboard_.getBillboardSet()->setRenderQueueGroup(this->mesh_.getEntity()->getRenderQueueGroup());
150                this->mesh_.setCastShadows(true);
151            }
152        }
153    }
154
155    void Planet::changedShadows()
156    {
157        this->mesh_.setCastShadows(this->bCastShadows_);
158    }
159
160    void Planet::XMLPort(Element& xmlelement, XMLPort::Mode mode)
161    {
162        SUPER(Planet, XMLPort, xmlelement, mode);
163
164        XMLPortParam(Planet, "atmosphere", setAtmosphere, getAtmosphere, xmlelement, mode).defaultValues("planet/Atmosphere");
165        XMLPortParam(Planet, "atmospheresize", setAtmosphereSize, getAtmosphereSize, xmlelement,mode);
166        XMLPortParam(Planet, "imagesize", setImageSize, getImageSize, xmlelement,mode);
167        XMLPortParam(Planet, "mesh", setMeshSource, getMeshSource, xmlelement, mode);
168        XMLPortParam(Planet, "shadow", setCastShadows, getCastShadows, xmlelement, mode).defaultValues(true);
169    }
170
171    void Planet::registerVariables()
172    {
173        registerVariable(this->atmosphere_, VariableDirection::ToClient);
174        registerVariable(this->atmosphereSize, VariableDirection::ToClient);
175        registerVariable(this->imageSize, VariableDirection::ToClient);
176        // Note: the meshSrc should be synchronised after atmosphere and other values, because the meshSrc callback setts the atmosphere billboards
177        registerVariable(this->meshSrc_, VariableDirection::ToClient, new NetworkCallback<Planet>(this, &Planet::changedMesh));
178        registerVariable(this->bCastShadows_, VariableDirection::ToClient, new NetworkCallback<Planet>(this, &Planet::changedShadows));
179    }
180
181    void Planet::changedVisibility()
182    {
183        SUPER(Planet, changedVisibility);
184        if (this->isInitialized())
185        {
186            this->mesh_.setVisible(this->isVisible());
187            this->billboard_.setVisible(this->isVisible());
188        }
189    }
190}
Note: See TracBrowser for help on using the repository browser.