Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/graphics/Backlight.cc @ 7613

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

Reverted trunk again. We might want to find a way to delete these revisions again (x3n's changes are still available as diff in the commit mails).

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