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