| [1608] | 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 | *      Fabian 'x3n' Landau | 
|---|
|  | 24 | *   Co-authors: | 
|---|
|  | 25 | *      ... | 
|---|
|  | 26 | * | 
|---|
|  | 27 | */ | 
|---|
|  | 28 |  | 
|---|
|  | 29 | #include "Backlight.h" | 
|---|
|  | 30 |  | 
|---|
| [3280] | 31 | #include <algorithm> | 
|---|
| [1608] | 32 | #include <OgreRibbonTrail.h> | 
|---|
| [1755] | 33 | #include <OgreSceneManager.h> | 
|---|
| [3196] | 34 | #include <OgreSceneNode.h> | 
|---|
| [8424] | 35 | // windows.h is getting included above on Windows... | 
|---|
|  | 36 | #undef max | 
|---|
|  | 37 | #undef min | 
|---|
| [1608] | 38 |  | 
|---|
| [3196] | 39 | #include "util/Exception.h" | 
|---|
|  | 40 | #include "core/CoreIncludes.h" | 
|---|
| [2896] | 41 | #include "core/GameMode.h" | 
|---|
| [2662] | 42 | #include "core/XMLPort.h" | 
|---|
| [5735] | 43 | #include "Scene.h" | 
|---|
| [1608] | 44 |  | 
|---|
|  | 45 | namespace orxonox | 
|---|
|  | 46 | { | 
|---|
|  | 47 | CreateFactory(Backlight); | 
|---|
|  | 48 |  | 
|---|
| [2662] | 49 | Backlight::Backlight(BaseObject* creator) : FadingBillboard(creator) | 
|---|
| [1608] | 50 | { | 
|---|
|  | 51 | RegisterObject(Backlight); | 
|---|
|  | 52 |  | 
|---|
| [2662] | 53 | this->ribbonTrail_ = 0; | 
|---|
|  | 54 | this->ribbonTrailNode_ = 0; | 
|---|
| [1608] | 55 |  | 
|---|
| [2662] | 56 | this->width_ = 0; | 
|---|
|  | 57 | this->length_ = 1.0f; | 
|---|
|  | 58 | this->lifetime_ = 0.001f; | 
|---|
|  | 59 | this->maxelements_ = 1; | 
|---|
| [1608] | 60 |  | 
|---|
| [2662] | 61 | this->tickcount_ = 0; | 
|---|
| [1608] | 62 |  | 
|---|
| [2896] | 63 | if (GameMode::showsGraphics()) | 
|---|
| [2662] | 64 | { | 
|---|
|  | 65 | if (!this->getScene()) | 
|---|
|  | 66 | ThrowException(AbortLoading, "Can't create Backlight, no scene given."); | 
|---|
|  | 67 | if (!this->getScene()->getSceneManager()) | 
|---|
|  | 68 | ThrowException(AbortLoading, "Can't create Backlight, no scene manager given."); | 
|---|
|  | 69 | if (!this->getScene()->getRootSceneNode()) | 
|---|
|  | 70 | ThrowException(AbortLoading, "Can't create Backlight, no root scene node given."); | 
|---|
| [1608] | 71 |  | 
|---|
| [2662] | 72 | this->ribbonTrail_ = this->getScene()->getSceneManager()->createRibbonTrail(this->getNode()->getName()); | 
|---|
| [1608] | 73 |  | 
|---|
| [2662] | 74 | this->ribbonTrailNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode(); | 
|---|
|  | 75 | this->ribbonTrailNode_->attachObject(this->ribbonTrail_); | 
|---|
| [1608] | 76 |  | 
|---|
| [2662] | 77 | this->ribbonTrail_->setMaxChainElements(this->maxelements_); | 
|---|
|  | 78 | this->ribbonTrail_->setTrailLength(this->length_); | 
|---|
|  | 79 | this->ribbonTrail_->setInitialWidth(0, 0); | 
|---|
|  | 80 | } | 
|---|
|  | 81 |  | 
|---|
|  | 82 | this->registerVariables(); | 
|---|
| [1608] | 83 | } | 
|---|
|  | 84 |  | 
|---|
|  | 85 | Backlight::~Backlight() | 
|---|
|  | 86 | { | 
|---|
|  | 87 | if (this->isInitialized()) | 
|---|
|  | 88 | { | 
|---|
| [2662] | 89 | if (this->ribbonTrail_) | 
|---|
|  | 90 | { | 
|---|
|  | 91 | if (this->ribbonTrailNode_) | 
|---|
|  | 92 | { | 
|---|
|  | 93 | this->ribbonTrailNode_->detachObject(this->ribbonTrail_); | 
|---|
|  | 94 | this->getScene()->getSceneManager()->destroySceneNode(this->ribbonTrailNode_->getName()); | 
|---|
|  | 95 | } | 
|---|
|  | 96 | this->getScene()->getSceneManager()->destroyRibbonTrail(this->ribbonTrail_); | 
|---|
|  | 97 | } | 
|---|
| [1608] | 98 | } | 
|---|
|  | 99 | } | 
|---|
|  | 100 |  | 
|---|
| [2662] | 101 | void Backlight::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| [1608] | 102 | { | 
|---|
| [2662] | 103 | SUPER(Backlight, XMLPort, xmlelement, mode); | 
|---|
|  | 104 |  | 
|---|
|  | 105 | XMLPortParam(Backlight, "length",        setLength,        getLength,        xmlelement, mode).defaultValues(100.0f); | 
|---|
|  | 106 | XMLPortParam(Backlight, "width",         setWidth,         getWidth,         xmlelement, mode).defaultValues(1.0f); | 
|---|
|  | 107 | XMLPortParam(Backlight, "elements",      setMaxElements,   getMaxElements,   xmlelement, mode).defaultValues(10); | 
|---|
|  | 108 | XMLPortParam(Backlight, "lifetime",      setLifetime,      getLifetime,      xmlelement, mode).defaultValues(1.0f); | 
|---|
|  | 109 | XMLPortParam(Backlight, "trailmaterial", setTrailMaterial, getTrailMaterial, xmlelement, mode); | 
|---|
| [1608] | 110 | } | 
|---|
|  | 111 |  | 
|---|
| [2662] | 112 | void Backlight::registerVariables() | 
|---|
| [1608] | 113 | { | 
|---|
| [3280] | 114 | registerVariable(this->width_,         VariableDirection::ToClient, new NetworkCallback<Backlight>(this, &Backlight::update_width)); | 
|---|
|  | 115 | registerVariable(this->lifetime_,      VariableDirection::ToClient, new NetworkCallback<Backlight>(this, &Backlight::update_lifetime)); | 
|---|
|  | 116 | registerVariable(this->length_,        VariableDirection::ToClient, new NetworkCallback<Backlight>(this, &Backlight::update_length)); | 
|---|
|  | 117 | registerVariable(this->maxelements_,   VariableDirection::ToClient, new NetworkCallback<Backlight>(this, &Backlight::update_maxelements)); | 
|---|
|  | 118 | registerVariable(this->trailmaterial_, VariableDirection::ToClient, new NetworkCallback<Backlight>(this, &Backlight::update_trailmaterial)); | 
|---|
| [1608] | 119 | } | 
|---|
|  | 120 |  | 
|---|
| [2662] | 121 | void Backlight::changedColour() | 
|---|
| [1608] | 122 | { | 
|---|
| [2662] | 123 | FadingBillboard::changedColour(); | 
|---|
|  | 124 |  | 
|---|
|  | 125 | if (this->ribbonTrail_ && this->tickcount_ >= 2) | 
|---|
|  | 126 | this->ribbonTrail_->setInitialColour(0, this->getFadedColour()); | 
|---|
| [1608] | 127 | } | 
|---|
| [2662] | 128 |  | 
|---|
|  | 129 | void Backlight::update_width() | 
|---|
|  | 130 | { | 
|---|
|  | 131 | if (this->ribbonTrail_ && this->tickcount_ >= 2) | 
|---|
|  | 132 | this->ribbonTrail_->setInitialWidth(0, this->width_ * this->getWorldScale()); | 
|---|
|  | 133 | this->update_lifetime(); | 
|---|
| [1907] | 134 | } | 
|---|
| [1608] | 135 |  | 
|---|
| [2662] | 136 | void Backlight::update_lifetime() | 
|---|
| [1608] | 137 | { | 
|---|
| [2662] | 138 | if (this->ribbonTrail_ && this->tickcount_ >= 2) | 
|---|
| [1608] | 139 | { | 
|---|
| [2662] | 140 | this->ribbonTrail_->setWidthChange(0, this->width_ * this->getWorldScale() / this->lifetime_ * this->getTimeFactor()); | 
|---|
|  | 141 | this->ribbonTrail_->setColourChange(0, 0, 0, 0, 1.0f / this->lifetime_ * this->getTimeFactor()); | 
|---|
| [1608] | 142 | } | 
|---|
| [2662] | 143 | } | 
|---|
| [1608] | 144 |  | 
|---|
| [2662] | 145 | void Backlight::update_length() | 
|---|
|  | 146 | { | 
|---|
|  | 147 | if (this->ribbonTrail_ && this->tickcount_ >= 2) | 
|---|
|  | 148 | this->ribbonTrail_->setTrailLength(this->length_ * this->getWorldScale()); | 
|---|
| [1608] | 149 | } | 
|---|
|  | 150 |  | 
|---|
| [2662] | 151 | void Backlight::update_maxelements() | 
|---|
| [1608] | 152 | { | 
|---|
| [2662] | 153 | if (this->ribbonTrail_ && this->tickcount_ >= 2) | 
|---|
|  | 154 | this->ribbonTrail_->setMaxChainElements(this->maxelements_); | 
|---|
| [1608] | 155 | } | 
|---|
|  | 156 |  | 
|---|
| [2662] | 157 | void Backlight::update_trailmaterial() | 
|---|
| [1608] | 158 | { | 
|---|
| [2662] | 159 | if (this->ribbonTrail_ && this->tickcount_ >= 2) | 
|---|
|  | 160 | this->ribbonTrail_->setMaterialName(this->trailmaterial_); | 
|---|
| [1608] | 161 | } | 
|---|
|  | 162 |  | 
|---|
|  | 163 | void Backlight::changedVisibility() | 
|---|
|  | 164 | { | 
|---|
| [1747] | 165 | SUPER(Backlight, changedVisibility); | 
|---|
| [1608] | 166 |  | 
|---|
| [2662] | 167 | if (this->ribbonTrail_) | 
|---|
|  | 168 | this->ribbonTrail_->setVisible(this->isVisible()); | 
|---|
| [1608] | 169 | } | 
|---|
| [2662] | 170 |  | 
|---|
|  | 171 | void Backlight::startturnonoff() | 
|---|
|  | 172 | { | 
|---|
|  | 173 | FadingBillboard::startturnonoff(); | 
|---|
|  | 174 |  | 
|---|
|  | 175 | if (this->ribbonTrail_ && this->isActive() && this->isVisible()) | 
|---|
|  | 176 | this->ribbonTrail_->setVisible(true); | 
|---|
|  | 177 | } | 
|---|
|  | 178 |  | 
|---|
|  | 179 | void Backlight::stopturnonoff() | 
|---|
|  | 180 | { | 
|---|
| [3280] | 181 | this->postprocessingtime_ = std::max(0.0f, this->lifetime_ - this->turnofftime_); | 
|---|
| [2662] | 182 |  | 
|---|
|  | 183 | FadingBillboard::stopturnonoff(); | 
|---|
|  | 184 |  | 
|---|
|  | 185 | if (this->ribbonTrail_) | 
|---|
|  | 186 | this->ribbonTrail_->setInitialColour(0, this->getFadedColour()); | 
|---|
|  | 187 | } | 
|---|
|  | 188 |  | 
|---|
|  | 189 | void Backlight::poststopturnonoff() | 
|---|
|  | 190 | { | 
|---|
|  | 191 | FadingBillboard::poststopturnonoff(); | 
|---|
|  | 192 |  | 
|---|
|  | 193 | if (this->ribbonTrail_) | 
|---|
|  | 194 | this->ribbonTrail_->setVisible(false); | 
|---|
|  | 195 | } | 
|---|
|  | 196 |  | 
|---|
|  | 197 | void Backlight::changedScale() | 
|---|
|  | 198 | { | 
|---|
|  | 199 | SUPER(Backlight, changedScale); | 
|---|
|  | 200 |  | 
|---|
|  | 201 | this->update_width(); | 
|---|
|  | 202 | this->update_length(); | 
|---|
|  | 203 | } | 
|---|
|  | 204 |  | 
|---|
|  | 205 | void Backlight::tick(float dt) | 
|---|
|  | 206 | { | 
|---|
|  | 207 | if (this->tickcount_ < 2) | 
|---|
|  | 208 | { | 
|---|
|  | 209 | ++this->tickcount_; | 
|---|
|  | 210 | if (this->tickcount_ == 2) | 
|---|
|  | 211 | { | 
|---|
|  | 212 | this->changedColour(); | 
|---|
|  | 213 | this->update_width(); | 
|---|
|  | 214 | this->update_lifetime(); | 
|---|
|  | 215 | this->update_length(); | 
|---|
|  | 216 | this->update_maxelements(); | 
|---|
|  | 217 | this->update_trailmaterial(); | 
|---|
|  | 218 | if (this->ribbonTrail_) | 
|---|
|  | 219 | this->ribbonTrail_->addNode(this->node_); | 
|---|
|  | 220 | } | 
|---|
|  | 221 | } | 
|---|
|  | 222 |  | 
|---|
| [2809] | 223 | SUPER(Backlight, tick, dt); | 
|---|
| [2662] | 224 |  | 
|---|
|  | 225 | if (this->ribbonTrail_ && this->changedirection_ != 0) | 
|---|
|  | 226 | { | 
|---|
|  | 227 | // we use alpha_blend, only adjust alpha | 
|---|
|  | 228 | const ColourValue& colour = this->getColour(); | 
|---|
|  | 229 | this->ribbonTrail_->setInitialColour(0, colour.r, colour.g, colour.b, this->getFadedColour().a); | 
|---|
|  | 230 | } | 
|---|
|  | 231 | } | 
|---|
|  | 232 |  | 
|---|
|  | 233 | void Backlight::changedTimeFactor(float factor_new, float factor_old) | 
|---|
|  | 234 | { | 
|---|
|  | 235 | this->update_lifetime(); | 
|---|
|  | 236 | } | 
|---|
| [1608] | 237 | } | 
|---|