Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/orxonox/objects/Backlight.cc @ 1997

Last change on this file since 1997 was 1697, checked in by rgrieder, 16 years ago

Minor gcc/CMake 'adjustments'

  • Property svn:eol-style set to native
File size: 5.5 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 "OrxonoxStableHeaders.h"
30#include "Backlight.h"
31
32#include <OgreBillboard.h>
33#include <OgreRibbonTrail.h>
34#include <OgreSceneManager.h>
35
36#include "core/CoreIncludes.h"
37#include "core/ConfigValueIncludes.h"
38#include "core/Executor.h"
39#include "util/Math.h"
40#include "GraphicsEngine.h"
41
42namespace orxonox
43{
44    CreateFactory(Backlight);
45
46    float Backlight::timeFactor_s = 1.0;
47
48    Backlight::Backlight(float maxspeed, float brakingtime, float scale)
49    {
50        RegisterObject(Backlight);
51
52        this->setConfigValues();
53        this->traillength_ = 1;
54
55        this->getNode()->setInheritScale(false);
56
57        this->billboard_.setBillboardSet("Flares/backlightflare");
58        this->attachObject(this->billboard_.getBillboardSet());
59
60        this->ribbonTrail_ = GraphicsEngine::getInstance().getLevelSceneManager()->createRibbonTrail(this->getName() + "RibbonTrail");
61        this->ribbonTrailNode_ = GraphicsEngine::getInstance().getLevelSceneManager()->getRootSceneNode()->createChildSceneNode(this->getName() + "RibbonTrailNode");
62        this->ribbonTrailNode_->attachObject(this->ribbonTrail_);
63        this->ribbonTrail_->addNode(this->getNode());
64
65        this->configure(maxspeed, brakingtime, scale);
66
67        this->ribbonTrail_->setTrailLength(this->maxTraillength_);
68        this->ribbonTrail_->setMaterialName("Trail/backlighttrail");
69
70        //this->setTimeFactor(Orxonox::getInstance().getTimeFactor());
71        this->setTimeFactor(1.0f);
72    }
73
74    Backlight::~Backlight()
75    {
76        if (this->isInitialized())
77        {
78            this->detachObject(this->billboard_.getBillboardSet());
79            GraphicsEngine::getInstance().getLevelSceneManager()->destroySceneNode(this->getName() + "RibbonTrailNode");
80            GraphicsEngine::getInstance().getLevelSceneManager()->destroyRibbonTrail(this->ribbonTrail_);
81        }
82    }
83
84    void Backlight::setConfigValues()
85    {
86        SetConfigValue(maxLifeTime_, 4.0).description("The maximal amount of seconds the trail behind a SpaceShip stays visible");
87        SetConfigValue(trailSegmentLength_, 50).description("The length of one segment of the trail behind a SpaceShip (lower values make it more smooth)");
88        SetConfigValue(width_, 7.0).description("The width of the trail");
89    }
90
91    void Backlight::setTimeFactor(float factor)
92    {
93        Backlight::timeFactor_s = factor;
94        float change = Backlight::timeFactor_s / this->maxLifeTime_;
95        this->ribbonTrail_->setWidthChange(0, this->width_ * change);
96        this->updateColourChange();
97    }
98
99    void Backlight::updateColourChange()
100    {
101        this->ribbonTrail_->setColourChange(0, ColourValue(0, 0, 0, this->maxTraillength_ / this->traillength_ / this->maxLifeTime_ * Backlight::timeFactor_s));
102    }
103
104    void Backlight::tick(float dt)
105    {
106        WorldEntity::tick(dt);
107
108        if (this->isActive())
109        {
110            if (this->traillength_ < this->maxTraillength_)
111            {
112                this->traillength_ = min<float>(this->maxTraillength_, this->traillength_ + dt * this->maxTraillength_ / this->maxLifeTime_);
113                this->updateColourChange();
114            }
115        }
116        else
117        {
118            if (this->traillength_ > 1)
119            {
120                this->traillength_ = max<float>(1, this->traillength_ - this->brakefactor_ * dt * this->maxTraillength_ / this->maxLifeTime_);
121                this->updateColourChange();
122            }
123        }
124
125        this->ribbonTrail_->setTrailLength(this->traillength_);
126    }
127
128    void Backlight::setColour(const ColourValue& colour)
129    {
130        this->billboard_.getBillboardSet()->getBillboard(0)->setColour(colour);
131        this->ribbonTrail_->setInitialColour(0, ColourValue(colour.r / 4 + 0.75, colour.g / 4 + 0.75, colour.b / 4 + 0.75));
132    }
133
134    void Backlight::configure(float maxspeed, float brakingtime, float scale)
135    {
136        this->maxTraillength_ = this->maxLifeTime_ * maxspeed;
137        this->maxTrailsegments_ = (size_t)(this->maxTraillength_ / this->trailSegmentLength_);
138        this->ribbonTrail_->setMaxChainElements(this->maxTrailsegments_);
139        this->ribbonTrail_->setTrailLength(this->traillength_ = 2 * this->maxTrailsegments_);
140
141        this->brakefactor_ = this->maxLifeTime_ / brakingtime;
142
143        this->scale(scale);
144        this->ribbonTrail_->setInitialWidth(0, this->width_ * scale);
145        this->ribbonTrail_->setWidthChange(0, this->width_ * scale / this->maxLifeTime_ * Backlight::timeFactor_s);
146    }
147
148    void Backlight::changedVisibility()
149    {
150        WorldEntity::changedVisibility();
151
152        this->billboard_.setVisible(this->isVisible());
153        this->ribbonTrail_->setVisible(this->isVisible());
154    }
155}
Note: See TracBrowser for help on using the repository browser.