Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/graphics/Light.cc @ 11675

Last change on this file since 11675 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
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 <OgreSceneManager.h>
[3196]32#include <OgreLight.h>
[2072]33
[3280]34#include "util/StringUtils.h"
[2662]35#include "util/Exception.h"
[3196]36#include "core/CoreIncludes.h"
[2896]37#include "core/GameMode.h"
[2072]38#include "core/XMLPort.h"
[5735]39#include "Scene.h"
[2072]40
41namespace orxonox
42{
[9667]43    RegisterClass(Light);
[2072]44
[3196]45    // Be sure we don't do bad conversions
[11071]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");
[3196]49
[9667]50    Light::Light(Context* context) : StaticEntity(context)
[2072]51    {
52        RegisterObject(Light);
53
[11071]54        this->light_ = nullptr;
[2662]55        this->diffuse_ = ColourValue::White;
56        this->specular_ = ColourValue::White;
[11071]57        this->type_ = Type::Point;
[2662]58        this->attenuation_ = Vector4(100000, 1, 0, 0);
59        this->spotlightRange_ = Vector3(40.0f, 30.0f, 1.0f);
[2072]60
[2896]61        if (GameMode::showsGraphics())
[2662]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
[2072]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
[2662]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));
[2072]103    }
104
105    void Light::registerVariables()
106    {
[11071]107        registerVariable(this->type_,           VariableDirection::ToClient, new NetworkCallback<Light>(this, &Light::updateType));
[3280]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));
[2072]112    }
113
[2662]114    void Light::updateDiffuseColour()
[2072]115    {
116        if (this->light_)
[2662]117            this->light_->setDiffuseColour(this->diffuse_);
[2072]118    }
119
[2662]120    void Light::updateSpecularColour()
[2072]121    {
122        if (this->light_)
[2662]123            this->light_->setSpecularColour(this->specular_);
[2072]124    }
125
[2662]126    void Light::updateAttenuation()
[2072]127    {
[11071]128        if (this->light_ && this->type_ != Type::Directional)
[2662]129            this->light_->setAttenuation(this->attenuation_.x, this->attenuation_.y, this->attenuation_.z, this->attenuation_.w);
[2072]130    }
131
[2662]132    void Light::updateSpotlightRange()
[2072]133    {
[11071]134        if (this->light_ && this->type_ == Type::Spotlight)
[2662]135            this->light_->setSpotlightRange(Degree(this->spotlightRange_.x), Degree(this->spotlightRange_.y), this->spotlightRange_.z);
[2072]136    }
137
138    void Light::setTypeString(const std::string& type)
139    {
140        if (type == "point")
[11071]141            this->setType(Type::Point);
[2072]142        else if (type == "directional")
[11071]143            this->setType(Type::Directional);
[2072]144        else if (type == "spotlight")
[11071]145            this->setType(Type::Spotlight);
[2072]146        else
[11071]147            this->setType(Type::Point);
[2072]148    }
149
150    std::string Light::getTypeString() const
151    {
152        switch (this->type_)
153        {
[11071]154            case Type::Directional:
[2072]155                return "directional";
[11071]156            case Type::Spotlight:
[2072]157                return "spotlight";
[11071]158            case Type::Point:
[2072]159            default:
[2662]160                return "point";
[2072]161        }
162    }
163
[2662]164    void Light::updateType()
[2072]165    {
[2662]166        if (this->light_)
167        {
[3196]168            this->light_->setType(static_cast<Ogre::Light::LightTypes>(this->type_));
[2662]169
[11071]170            if (this->type_ != Type::Directional)
[2662]171                this->updateAttenuation();
[11071]172            if (this->type_ == Type::Spotlight)
[2662]173                this->updateSpotlightRange();
174        }
[2072]175    }
176
177    void Light::changedVisibility()
178    {
179        SUPER(Light, changedVisibility);
180
[2662]181        if (this->light_)
182            this->light_->setVisible(this->isVisible());
[2072]183    }
184}
Note: See TracBrowser for help on using the repository browser.