Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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