Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickups2/src/orxonox/objects/worldentities/Light.cc @ 3027

Last change on this file since 3027 was 2896, checked in by landauf, 16 years ago

Merged gui branch back to trunk.

I did 2 small changes in IngameManager.cc on line 777 and 888 (yes, really), because const_reverse_iterator strangely doesn't work on MinGW.

  • Property svn:eol-style set to native
File size: 6.3 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 "Light.h"
31
32#include <sstream>
33#include <cassert>
34
35#include <OgreSceneManager.h>
36
37#include "util/String.h"
38#include "util/Exception.h"
39#include "core/GameMode.h"
40#include "core/CoreIncludes.h"
41#include "core/XMLPort.h"
42#include "objects/Scene.h"
43
44namespace orxonox
45{
46    CreateFactory(Light);
47
48    Light::Light(BaseObject* creator) : StaticEntity(creator)
49    {
50        RegisterObject(Light);
51
52        this->light_ = 0;
53        this->diffuse_ = ColourValue::White;
54        this->specular_ = ColourValue::White;
55        this->type_ = Ogre::Light::LT_POINT;
56        this->attenuation_ = Vector4(100000, 1, 0, 0);
57        this->spotlightRange_ = Vector3(40.0f, 30.0f, 1.0f);
58
59        if (GameMode::showsGraphics())
60        {
61            if (!this->getScene())
62                ThrowException(AbortLoading, "Can't create Light, no scene given.");
63            if (!this->getScene()->getSceneManager())
64                ThrowException(AbortLoading, "Can't create Light, no scene manager given.");
65
66            if (this->getScene() && this->getScene()->getSceneManager())
67            {
68                this->light_ = this->getScene()->getSceneManager()->createLight("Light" + getUniqueNumberString());
69                this->light_->setDirection(WorldEntity::FRONT);
70                this->attachOgreObject(this->light_);
71
72                this->updateType();
73                this->updateDiffuseColour();
74                this->updateSpecularColour();
75                this->updateAttenuation();
76                this->updateSpotlightRange();
77            }
78        }
79
80        this->registerVariables();
81    }
82
83    Light::~Light()
84    {
85        if (this->isInitialized())
86        {
87            if (this->light_ && this->getScene() && this->getScene()->getSceneManager())
88                this->getScene()->getSceneManager()->destroyLight(this->light_);
89        }
90    }
91
92    void Light::XMLPort(Element& xmlelement, XMLPort::Mode mode)
93    {
94        SUPER(Light, XMLPort, xmlelement, mode);
95
96        XMLPortParam(Light, "type",           setTypeString,     getTypeString,     xmlelement, mode).defaultValues("point");
97        XMLPortParam(Light, "diffuse",        setDiffuseColour,  getDiffuseColour,  xmlelement, mode).defaultValues(ColourValue::White);
98        XMLPortParam(Light, "specular",       setSpecularColour, getSpecularColour, xmlelement, mode).defaultValues(ColourValue::White);
99        XMLPortParam(Light, "attenuation",    setAttenuation,    getAttenuation,    xmlelement, mode).defaultValues(Vector4(100000, 1, 0, 0));
100        XMLPortParam(Light, "spotlightrange", setSpotlightRange, getSpotlightRange, xmlelement, mode).defaultValues(Vector3(40.0f, 30.0f, 1.0f));
101    }
102
103    void Light::registerVariables()
104    {
105        registerVariable((int&)this->type_,     variableDirection::toclient, new NetworkCallback<Light>(this, &Light::updateType));
106        registerVariable(this->diffuse_,        variableDirection::toclient, new NetworkCallback<Light>(this, &Light::updateDiffuseColour));
107        registerVariable(this->specular_,       variableDirection::toclient, new NetworkCallback<Light>(this, &Light::updateSpecularColour));
108        registerVariable(this->attenuation_,    variableDirection::toclient, new NetworkCallback<Light>(this, &Light::updateAttenuation));
109        registerVariable(this->spotlightRange_, variableDirection::toclient, new NetworkCallback<Light>(this, &Light::updateSpotlightRange));
110    }
111
112    void Light::updateDiffuseColour()
113    {
114        if (this->light_)
115            this->light_->setDiffuseColour(this->diffuse_);
116    }
117
118    void Light::updateSpecularColour()
119    {
120        if (this->light_)
121            this->light_->setSpecularColour(this->specular_);
122    }
123
124    void Light::updateAttenuation()
125    {
126        if (this->light_ && this->type_ != Ogre::Light::LT_DIRECTIONAL)
127            this->light_->setAttenuation(this->attenuation_.x, this->attenuation_.y, this->attenuation_.z, this->attenuation_.w);
128    }
129
130    void Light::updateSpotlightRange()
131    {
132        if (this->light_ && this->type_ == Ogre::Light::LT_SPOTLIGHT)
133            this->light_->setSpotlightRange(Degree(this->spotlightRange_.x), Degree(this->spotlightRange_.y), this->spotlightRange_.z);
134    }
135
136    void Light::setTypeString(const std::string& type)
137    {
138        if (type == "point")
139            this->setType(Ogre::Light::LT_POINT);
140        else if (type == "directional")
141            this->setType(Ogre::Light::LT_DIRECTIONAL);
142        else if (type == "spotlight")
143            this->setType(Ogre::Light::LT_SPOTLIGHT);
144        else
145            this->setType(Ogre::Light::LT_POINT);
146    }
147
148    std::string Light::getTypeString() const
149    {
150        switch (this->type_)
151        {
152            case Ogre::Light::LT_DIRECTIONAL:
153                return "directional";
154            case Ogre::Light::LT_SPOTLIGHT:
155                return "spotlight";
156            case Ogre::Light::LT_POINT:
157            default:
158                return "point";
159        }
160    }
161
162    void Light::updateType()
163    {
164        if (this->light_)
165        {
166            this->light_->setType(this->type_);
167
168            if (this->type_ != Ogre::Light::LT_DIRECTIONAL)
169                this->updateAttenuation();
170            if (this->type_ == Ogre::Light::LT_SPOTLIGHT)
171                this->updateSpotlightRange();
172        }
173    }
174
175    void Light::changedVisibility()
176    {
177        SUPER(Light, changedVisibility);
178
179        if (this->light_)
180            this->light_->setVisible(this->isVisible());
181    }
182}
Note: See TracBrowser for help on using the repository browser.