Caelum
SkyLight.cpp
1 /*
2 This file is part of Caelum.
3 See http://www.ogre3d.org/wiki/index.php/Caelum
4 
5 Copyright (c) 2008 Caelum team. See Contributors.txt for details.
6 
7 Caelum is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Caelum is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with Caelum. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "CaelumPrecompiled.h"
22 #include "SkyLight.h"
23 
24 namespace Caelum
25 {
26  const Ogre::Real BaseSkyLight::DEFAULT_AUTO_DISABLE_THRESHOLD = 0.1;
27 
28  BaseSkyLight::BaseSkyLight (Ogre::SceneManager *sceneMgr, Ogre::SceneNode *caelumRootNode):
29  mDirection(Ogre::Vector3::ZERO),
30  mBodyColour(Ogre::ColourValue::White),
31  mLightColour(Ogre::ColourValue::White),
32 
33  mDiffuseMultiplier(Ogre::ColourValue(1, 1, 0.9)),
34  mSpecularMultiplier(Ogre::ColourValue(1, 1, 1)),
35  mAmbientMultiplier(Ogre::ColourValue(0.2, 0.2, 0.2)),
36 
37  mAutoDisableLight(false),
38  mAutoDisableThreshold(DEFAULT_AUTO_DISABLE_THRESHOLD),
39  mForceDisableLight(false)
40  {
41  Ogre::String lightName = "CaelumSkyLight" + Ogre::StringConverter::toString((size_t)this);
42 
43  mMainLight = sceneMgr->createLight (lightName);
44  mMainLight->setType (Ogre::Light::LT_DIRECTIONAL);
45 
46  sceneMgr->getRenderQueue()->getQueueGroup(CAELUM_RENDER_QUEUE_SUN)->setShadowsEnabled(false);
47 
48  mNode = caelumRootNode->createChildSceneNode ();
49  }
50 
52  if (mNode) {
53  static_cast<Ogre::SceneNode *>(mNode->getParent ())->removeAndDestroyChild (mNode->getName ());
54  mNode = 0;
55  }
56 
57  if (mMainLight) {
58  mMainLight->_getManager ()->destroyLight (mMainLight);
59  mMainLight = 0;
60  }
61  }
62 
63  void BaseSkyLight::setFarRadius (Ogre::Real radius) {
65  mRadius = radius;
66  }
67 
69  const Ogre::Vector3& direction,
70  const Ogre::ColourValue &lightColour,
71  const Ogre::ColourValue &bodyColour)
72  {
73  setLightDirection(direction);
74  setLightColour(lightColour);
75  setBodyColour(bodyColour);
76  }
77 
78  const Ogre::Vector3 BaseSkyLight::getLightDirection () const {
79  return mDirection;
80  }
81 
82  void BaseSkyLight::setLightDirection (const Ogre::Vector3 &dir) {
83  mDirection = dir;
84  if (mMainLight != 0) {
85  mMainLight->setDirection (mNode->_getDerivedOrientation() * dir);
86  }
87  }
88 
89  void BaseSkyLight::setBodyColour (const Ogre::ColourValue &colour) {
90  // Store this last colour
91  mBodyColour = colour;
92  }
93 
94  const Ogre::ColourValue BaseSkyLight::getBodyColour () const {
95  return mBodyColour;
96  }
97 
98  void BaseSkyLight::setLightColour (const Ogre::ColourValue &colour) {
99  // Store this last colour
100  mLightColour = colour;
101  // Apply change
102  setMainLightColour(colour);
103  }
104 
105  void BaseSkyLight::setMainLightColour (const Ogre::ColourValue &colour) {
106  // Set light colours.
107  bool enable = shouldEnableLight (colour);
108  if (enable) {
109  mMainLight->setVisible(true);
110  mMainLight->setDiffuseColour (colour * mDiffuseMultiplier);
111  mMainLight->setSpecularColour (colour * mSpecularMultiplier);
112  } else {
113  mMainLight->setVisible(false);
114  }
115  }
116 
117  const Ogre::ColourValue BaseSkyLight::getLightColour () const {
118  return mLightColour;
119  }
120 
121  void BaseSkyLight::setDiffuseMultiplier (const Ogre::ColourValue &diffuse) {
122  mDiffuseMultiplier = diffuse;
123  }
124 
125  const Ogre::ColourValue BaseSkyLight::getDiffuseMultiplier () const {
126  return mDiffuseMultiplier;
127  }
128 
129  void BaseSkyLight::setSpecularMultiplier (const Ogre::ColourValue &specular) {
130  mSpecularMultiplier = specular;
131  }
132 
133  const Ogre::ColourValue BaseSkyLight::getSpecularMultiplier () const {
134  return mSpecularMultiplier;
135  }
136 
137  void BaseSkyLight::setAmbientMultiplier (const Ogre::ColourValue &ambient) {
138  mAmbientMultiplier = ambient;
139  }
140 
141  const Ogre::ColourValue BaseSkyLight::getAmbientMultiplier () const {
142  return mAmbientMultiplier;
143  }
144 
145  Ogre::Light* BaseSkyLight::getMainLight() const {
146  return mMainLight;
147  }
148 
149  bool BaseSkyLight::shouldEnableLight(const Ogre::ColourValue &colour) {
150  if (mForceDisableLight) {
151  return false;
152  }
153  if (mAutoDisableLight) {
154  Ogre::Real sum = colour.r + colour.g + colour.b;
155  return sum >= mAutoDisableThreshold;
156  } else {
157  return true;
158  }
159  }
160 }
Ogre::Real mAutoDisableThreshold
Threshold beneath which the light is automatically disabled.
Definition: SkyLight.h:67
virtual void setLightDirection(const Ogre::Vector3 &dir)
Set the sun direction.
Definition: SkyLight.cpp:82
virtual void update(const Ogre::Vector3 &direction, const Ogre::ColourValue &lightColour, const Ogre::ColourValue &bodyColour)
Updates skylight parameters.
Definition: SkyLight.cpp:68
void setDiffuseMultiplier(const Ogre::ColourValue &diffuse)
Set diffuse multiplier for light colour.
Definition: SkyLight.cpp:121
void setMainLightColour(const Ogre::ColourValue &colour)
Temporary change main light color.
Definition: SkyLight.cpp:105
const Ogre::ColourValue getDiffuseMultiplier() const
Set diffuse multiplier for light colour.
Definition: SkyLight.cpp:125
bool shouldEnableLight(const Ogre::ColourValue &colour)
If the light should be enabled for a certain value.
Definition: SkyLight.cpp:149
float mRadius
Base distance of the light.
Definition: SkyLight.h:41
const Ogre::ColourValue getAmbientMultiplier() const
Set ambient multiplier for light colour.
Definition: SkyLight.cpp:141
Ogre::SceneNode * mNode
The sun scene node.
Definition: SkyLight.h:38
bool mForceDisableLight
If the light is always disabled. Separate from the mAutoDisable mechanism.
Definition: SkyLight.h:70
bool mAutoDisableLight
If the light is automatically disabled beneath mAutoDisableThreshold.
Definition: SkyLight.h:64
BaseSkyLight(Ogre::SceneManager *sceneMgr, Ogre::SceneNode *caelumRootNode)
Constructor.
Definition: SkyLight.cpp:28
const Ogre::ColourValue getLightColour() const
Get current light colour, as set in setLightColour.
Definition: SkyLight.cpp:117
void setSpecularMultiplier(const Ogre::ColourValue &specular)
Set specular multiplier for light colour.
Definition: SkyLight.cpp:129
Ogre::ColourValue mLightColour
Sun light colour, as set by setLightColour.
Definition: SkyLight.h:50
virtual void setBodyColour(const Ogre::ColourValue &colour)
Sets the colour to draw the light&#39;s body with.
Definition: SkyLight.cpp:89
Ogre::ColourValue mDiffuseMultiplier
Colour multiplier for light diffuse colour.
Definition: SkyLight.h:53
const Ogre::Vector3 getLightDirection() const
Retrieves the latest light direction.
Definition: SkyLight.cpp:78
virtual void setFarRadius(Ogre::Real radius)
Abstract method to set the radius for this elements Derived classes should override this and resize t...
Ogre::ColourValue mBodyColour
Body sphere colour, as set by setBodyColour.
Definition: SkyLight.h:47
const Ogre::ColourValue getSpecularMultiplier() const
Set specular multiplier for light colour.
Definition: SkyLight.cpp:133
Ogre::Light * mMainLight
The main directional light.
Definition: SkyLight.h:35
Ogre::ColourValue mSpecularMultiplier
Colour multiplier for light specular colour.
Definition: SkyLight.h:56
Ogre::Vector3 mDirection
The latest normalised sun direction.
Definition: SkyLight.h:44
Ogre::ColourValue mAmbientMultiplier
Colour multiplier for ambient light colour.
Definition: SkyLight.h:61
virtual ~BaseSkyLight()=0
Destructor.
Definition: SkyLight.cpp:51
virtual void setLightColour(const Ogre::ColourValue &colour)
Sets the skylight colour.
Definition: SkyLight.cpp:98
Ogre::Light * getMainLight() const
Direct access to the Ogre::Light.
Definition: SkyLight.cpp:145
void setAmbientMultiplier(const Ogre::ColourValue &ambient)
Set ambient multiplier for light colour This value is only stored here; the SceneManager is not touch...
Definition: SkyLight.cpp:137
virtual void setFarRadius(Ogre::Real radius)
Handle far radius.
Definition: SkyLight.cpp:63
Caelum namespace.
Definition: Astronomy.cpp:24
const Ogre::ColourValue getBodyColour() const
Get current body colour, as set in setBodyColour.
Definition: SkyLight.cpp:94