Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Presentation_HS17_merge/src/orxonox/graphics/Light.cc @ 11758

Last change on this file since 11758 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

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