Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial6/src/orxonox/graphics/Backlight.cc @ 12412

Last change on this file since 12412 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 8.0 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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "Backlight.h"
30
31#include <algorithm>
32#include <OgreRibbonTrail.h>
33#include <OgreSceneManager.h>
34#include <OgreSceneNode.h>
35// windows.h is getting included above on Windows...
36#undef max
37#undef min
38
39#include "util/Exception.h"
40#include "core/CoreIncludes.h"
41#include "core/GameMode.h"
42#include "core/XMLPort.h"
43#include "Scene.h"
44
45namespace orxonox
46{
47    RegisterClass(Backlight);
48
49    Backlight::Backlight(Context* context) : FadingBillboard(context)
50    {
51        RegisterObject(Backlight);
52
53        this->ribbonTrail_ = nullptr;
54        this->ribbonTrailNode_ = nullptr;
55
56        this->width_ = 0;
57        this->length_ = 1.0f;
58        this->lifetime_ = 0.001f;
59        this->maxelements_ = 1;
60
61        this->tickcount_ = 0;
62
63        if (GameMode::showsGraphics())
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.");
71
72            this->ribbonTrail_ = this->getScene()->getSceneManager()->createRibbonTrail(this->getNode()->getName());
73
74            this->ribbonTrailNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode();
75            this->ribbonTrailNode_->attachObject(this->ribbonTrail_);
76
77            this->ribbonTrail_->setMaxChainElements(this->maxelements_);
78            this->ribbonTrail_->setTrailLength(this->length_);
79            this->ribbonTrail_->setInitialWidth(0, 0);
80        }
81
82        this->registerVariables();
83    }
84
85    Backlight::~Backlight()
86    {
87        if (this->isInitialized())
88        {
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            }
98        }
99    }
100
101    void Backlight::XMLPort(Element& xmlelement, XMLPort::Mode mode)
102    {
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);
110    }
111
112    void Backlight::registerVariables()
113    {
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));
119    }
120
121    void Backlight::changedColour()
122    {
123        FadingBillboard::changedColour();
124
125        if (this->ribbonTrail_ && this->tickcount_ >= 2)
126            this->ribbonTrail_->setInitialColour(0, this->getFadedColour());
127    }
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();
134    }
135
136    void Backlight::update_lifetime()
137    {
138        if (this->ribbonTrail_ && this->tickcount_ >= 2)
139        {
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());
142        }
143    }
144
145    void Backlight::update_length()
146    {
147        if (this->ribbonTrail_ && this->tickcount_ >= 2)
148            this->ribbonTrail_->setTrailLength(this->length_ * this->getWorldScale());
149    }
150
151    void Backlight::update_maxelements()
152    {
153        if (this->ribbonTrail_ && this->tickcount_ >= 2)
154            this->ribbonTrail_->setMaxChainElements(this->maxelements_);
155    }
156
157    void Backlight::update_trailmaterial()
158    {
159        if (this->ribbonTrail_ && this->tickcount_ >= 2)
160            this->ribbonTrail_->setMaterialName(this->trailmaterial_);
161    }
162
163    void Backlight::changedVisibility()
164    {
165        SUPER(Backlight, changedVisibility);
166
167        if (this->ribbonTrail_)
168            this->ribbonTrail_->setVisible(this->isVisible());
169    }
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    {
181        this->postprocessingtime_ = std::max(0.0f, this->lifetime_ - this->turnofftime_);
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
223        SUPER(Backlight, tick, dt);
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    }
237}
Note: See TracBrowser for help on using the repository browser.