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