Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/worldentities/Light.cc @ 3116

Last change on this file since 3116 was 3110, checked in by rgrieder, 16 years ago

Removed old msvc specific support for precompiled header files.

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