Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7495 was 5781, checked in by rgrieder, 15 years ago

Reverted trunk again. We might want to find a way to delete these revisions again (x3n's changes are still available as diff in the commit mails).

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