Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/worldentities/Light.cc @ 3143

Last change on this file since 3143 was 3110, checked in by rgrieder, 15 years ago

Removed old msvc specific support for precompiled header files.

  • 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 "Light.h"
30
31#include <sstream>
32#include <cassert>
33
34#include <OgreSceneManager.h>
35
36#include "util/String.h"
37#include "util/Exception.h"
38#include "core/GameMode.h"
39#include "core/CoreIncludes.h"
40#include "core/XMLPort.h"
41#include "objects/Scene.h"
42
43namespace orxonox
44{
45    CreateFactory(Light);
46
47    Light::Light(BaseObject* creator) : StaticEntity(creator)
48    {
49        RegisterObject(Light);
50
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);
57
58        if (GameMode::showsGraphics())
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
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
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));
100    }
101
102    void Light::registerVariables()
103    {
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));
109    }
110
111    void Light::updateDiffuseColour()
112    {
113        if (this->light_)
114            this->light_->setDiffuseColour(this->diffuse_);
115    }
116
117    void Light::updateSpecularColour()
118    {
119        if (this->light_)
120            this->light_->setSpecularColour(this->specular_);
121    }
122
123    void Light::updateAttenuation()
124    {
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);
127    }
128
129    void Light::updateSpotlightRange()
130    {
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);
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:
157                return "point";
158        }
159    }
160
161    void Light::updateType()
162    {
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        }
172    }
173
174    void Light::changedVisibility()
175    {
176        SUPER(Light, changedVisibility);
177
178        if (this->light_)
179            this->light_->setVisible(this->isVisible());
180    }
181}
Note: See TracBrowser for help on using the repository browser.