Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1688 was 1686, checked in by rgrieder, 16 years ago

Moved most of the GraphicsEngine code to GSRoot and GSGraphics.
GraphicsEngine is now more of a legacy object to ensure functionality until there is a new solution.

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