| 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 |  | 
|---|
| 30 | #include "OrxonoxStableHeaders.h" | 
|---|
| 31 | #include "Planet.h" | 
|---|
| 32 |  | 
|---|
| 33 | #include "tinyxml/tinyxml.h" | 
|---|
| 34 | #include "core/CoreIncludes.h" | 
|---|
| 35 | #include "GraphicsEngine.h" | 
|---|
| 36 | #include "core/XMLPort.h" | 
|---|
| 37 | #include "CameraManager.h" | 
|---|
| 38 | #include "objects/Scene.h" | 
|---|
| 39 | #include "Camera.h" | 
|---|
| 40 | #include <math.h> | 
|---|
| 41 |  | 
|---|
| 42 | #include <OgreEntity.h> | 
|---|
| 43 | #include <OgreMesh.h> | 
|---|
| 44 | #include <OgreHardwareVertexBuffer.h> | 
|---|
| 45 | #include <OgreMeshManager.h> | 
|---|
| 46 |  | 
|---|
| 47 |  | 
|---|
| 48 | namespace orxonox | 
|---|
| 49 | { | 
|---|
| 50 |     CreateFactory(Planet);     | 
|---|
| 51 |  | 
|---|
| 52 |     /** | 
|---|
| 53 |      * @brief Constructor | 
|---|
| 54 |      */ | 
|---|
| 55 |     Planet::Planet(BaseObject* creator): MovableEntity(creator) | 
|---|
| 56 |     { | 
|---|
| 57 |         RegisterObject(Planet); | 
|---|
| 58 |         this->registerVariables(); | 
|---|
| 59 |     } | 
|---|
| 60 |  | 
|---|
| 61 |     /** | 
|---|
| 62 |      * @brief Destructor | 
|---|
| 63 |      */ | 
|---|
| 64 |     Planet::~Planet() | 
|---|
| 65 |     { | 
|---|
| 66 |         if (this->isInitialized() && this->mesh_.getEntity()) | 
|---|
| 67 |             this->getNode()->detachObject(this->mesh_.getEntity()); | 
|---|
| 68 |     }     | 
|---|
| 69 |      | 
|---|
| 70 |     void Planet::tick(float dt) | 
|---|
| 71 |     { | 
|---|
| 72 |         if(!this->isVisible()) | 
|---|
| 73 |             return; | 
|---|
| 74 |              | 
|---|
| 75 |         Camera* activeCamera = CameraManager::getInstance().getActiveCamera(); | 
|---|
| 76 |         if(activeCamera) | 
|---|
| 77 |         { | 
|---|
| 78 |             Real distance = this->getPosition().distance( activeCamera->getWorldPosition() ); | 
|---|
| 79 |             COUT(2) << distance << std::endl; | 
|---|
| 80 |             Real planetRadius = this->getScale(); | 
|---|
| 81 |          | 
|---|
| 82 |             Real newScale = 2 * distance / sqrt(distance*distance - planetRadius*planetRadius); | 
|---|
| 83 |             Real tempTest = newScale*(1+Real(this->atmosphereSize)/Real(this->imageSize)); | 
|---|
| 84 |             newScale = tempTest; | 
|---|
| 85 |          | 
|---|
| 86 |             this->billboard_.getBillboardSet()->setDefaultDimensions(newScale, newScale); | 
|---|
| 87 |         } | 
|---|
| 88 |                  | 
|---|
| 89 |         SUPER(Planet, tick, dt); | 
|---|
| 90 |     } | 
|---|
| 91 |    | 
|---|
| 92 |     void Planet::init(){ | 
|---|
| 93 |      | 
|---|
| 94 |         Real 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 |         Real 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->getNode()->attachObject(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 | COUT(0) << "changedMesh callback -===================" << endl; | 
|---|
| 122 |         if (this->mesh_.getEntity()) | 
|---|
| 123 |             this->getNode()->detachObject(this->mesh_.getEntity()); | 
|---|
| 124 |  | 
|---|
| 125 |         this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_); | 
|---|
| 126 |  | 
|---|
| 127 |         if (this->mesh_.getEntity()) | 
|---|
| 128 |         { | 
|---|
| 129 |             this->getNode()->attachObject(this->mesh_.getEntity()); | 
|---|
| 130 |             this->mesh_.getEntity()->setCastShadows(this->bCastShadows_); | 
|---|
| 131 |             this->mesh_.setVisible(this->isVisible()); | 
|---|
| 132 |         } | 
|---|
| 133 |         this->init(); | 
|---|
| 134 |     } | 
|---|
| 135 |  | 
|---|
| 136 |     void Planet::changedShadows() | 
|---|
| 137 |     { | 
|---|
| 138 |         this->mesh_.setCastShadows(this->bCastShadows_); | 
|---|
| 139 |     } | 
|---|
| 140 |  | 
|---|
| 141 |     /** | 
|---|
| 142 |         @brief XML loading and saving. | 
|---|
| 143 |         @param xmlelement The XML-element | 
|---|
| 144 |         @param loading Loading (true) or saving (false) | 
|---|
| 145 |         @return The XML-element | 
|---|
| 146 |     */ | 
|---|
| 147 |     void Planet::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 148 |     { | 
|---|
| 149 |         SUPER(Planet, XMLPort, xmlelement, mode); | 
|---|
| 150 |          | 
|---|
| 151 |         XMLPortParam(Planet, "atmosphere", setAtmosphere, getAtmosphere, xmlelement, mode).defaultValues("planet/Atmosphere"); | 
|---|
| 152 |         XMLPortParam(Planet, "atmospheresize", setAtmosphereSize, getAtmosphereSize, xmlelement,mode);      | 
|---|
| 153 |         XMLPortParam(Planet, "imagesize", setImageSize, getImageSize, xmlelement,mode);          | 
|---|
| 154 |         XMLPortParam(Planet, "mesh", setMeshSource, getMeshSource, xmlelement, mode); | 
|---|
| 155 |         XMLPortParam(Planet, "shadow", setCastShadows, getCastShadows, xmlelement, mode).defaultValues(true); | 
|---|
| 156 |          | 
|---|
| 157 |     } | 
|---|
| 158 |  | 
|---|
| 159 |     void Planet::registerVariables(){ | 
|---|
| 160 |         REGISTERSTRING(this->atmosphere_, direction::toclient); | 
|---|
| 161 |         REGISTERSTRING(this->meshSrc_, direction::toclient, new NetworkCallback<Planet>(this, &Planet::changedMesh)); | 
|---|
| 162 |         REGISTERDATA(this->bCastShadows_, direction::toclient, new NetworkCallback<Planet>(this, &Planet::changedShadows)); | 
|---|
| 163 |         REGISTERDATA(this->atmosphereSize, direction::toclient); | 
|---|
| 164 |         REGISTERDATA(this->imageSize, direction::toclient); | 
|---|
| 165 |     } | 
|---|
| 166 |  | 
|---|
| 167 |     void Planet::changedVisibility() | 
|---|
| 168 |     { | 
|---|
| 169 |         SUPER(Planet, changedVisibility); | 
|---|
| 170 |         if (this->isInitialized()) | 
|---|
| 171 |             this->mesh_.setVisible(this->isVisible()); | 
|---|
| 172 |             this->billboard_.setVisible(this->isVisible()); | 
|---|
| 173 |     } | 
|---|
| 174 | } | 
|---|