00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "CaelumPrecompiled.h"
00022 #include "SkyLight.h"
00023
00024 namespace Caelum
00025 {
00026 const Ogre::Real BaseSkyLight::DEFAULT_AUTO_DISABLE_THRESHOLD = 0.1;
00027
00028 BaseSkyLight::BaseSkyLight (Ogre::SceneManager *sceneMgr, Ogre::SceneNode *caelumRootNode):
00029 mDirection(Ogre::Vector3::ZERO),
00030 mBodyColour(Ogre::ColourValue::White),
00031 mLightColour(Ogre::ColourValue::White),
00032
00033 mDiffuseMultiplier(Ogre::ColourValue(1, 1, 0.9)),
00034 mSpecularMultiplier(Ogre::ColourValue(1, 1, 1)),
00035 mAmbientMultiplier(Ogre::ColourValue(0.2, 0.2, 0.2)),
00036
00037 mAutoDisableLight(false),
00038 mAutoDisableThreshold(DEFAULT_AUTO_DISABLE_THRESHOLD),
00039 mForceDisableLight(false)
00040 {
00041 Ogre::String lightName = "CaelumSkyLight" + Ogre::StringConverter::toString((size_t)this);
00042
00043 mMainLight = sceneMgr->createLight (lightName);
00044 mMainLight->setType (Ogre::Light::LT_DIRECTIONAL);
00045
00046 sceneMgr->getRenderQueue()->getQueueGroup(CAELUM_RENDER_QUEUE_SUN)->setShadowsEnabled(false);
00047
00048 mNode = caelumRootNode->createChildSceneNode ();
00049 }
00050
00051 BaseSkyLight::~BaseSkyLight () {
00052 if (mNode) {
00053 static_cast<Ogre::SceneNode *>(mNode->getParent ())->removeAndDestroyChild (mNode->getName ());
00054 mNode = 0;
00055 }
00056
00057 if (mMainLight) {
00058 mMainLight->_getManager ()->destroyLight (mMainLight);
00059 mMainLight = 0;
00060 }
00061 }
00062
00063 void BaseSkyLight::setFarRadius (Ogre::Real radius) {
00064 CameraBoundElement::setFarRadius(radius);
00065 mRadius = radius;
00066 }
00067
00068 void BaseSkyLight::update (
00069 const Ogre::Vector3& direction,
00070 const Ogre::ColourValue &lightColour,
00071 const Ogre::ColourValue &bodyColour)
00072 {
00073 setLightDirection(direction);
00074 setLightColour(lightColour);
00075 setBodyColour(bodyColour);
00076 }
00077
00078 const Ogre::Vector3 BaseSkyLight::getLightDirection () const {
00079 return mDirection;
00080 }
00081
00082 void BaseSkyLight::setLightDirection (const Ogre::Vector3 &dir) {
00083 mDirection = dir;
00084 if (mMainLight != 0) {
00085 mMainLight->setDirection (mNode->_getDerivedOrientation() * dir);
00086 }
00087 }
00088
00089 void BaseSkyLight::setBodyColour (const Ogre::ColourValue &colour) {
00090
00091 mBodyColour = colour;
00092 }
00093
00094 const Ogre::ColourValue BaseSkyLight::getBodyColour () const {
00095 return mBodyColour;
00096 }
00097
00098 void BaseSkyLight::setLightColour (const Ogre::ColourValue &colour) {
00099
00100 mLightColour = colour;
00101
00102 setMainLightColour(colour);
00103 }
00104
00105 void BaseSkyLight::setMainLightColour (const Ogre::ColourValue &colour) {
00106
00107 bool enable = shouldEnableLight (colour);
00108 if (enable) {
00109 mMainLight->setVisible(true);
00110 mMainLight->setDiffuseColour (colour * mDiffuseMultiplier);
00111 mMainLight->setSpecularColour (colour * mSpecularMultiplier);
00112 } else {
00113 mMainLight->setVisible(false);
00114 }
00115 }
00116
00117 const Ogre::ColourValue BaseSkyLight::getLightColour () const {
00118 return mLightColour;
00119 }
00120
00121 void BaseSkyLight::setDiffuseMultiplier (const Ogre::ColourValue &diffuse) {
00122 mDiffuseMultiplier = diffuse;
00123 }
00124
00125 const Ogre::ColourValue BaseSkyLight::getDiffuseMultiplier () const {
00126 return mDiffuseMultiplier;
00127 }
00128
00129 void BaseSkyLight::setSpecularMultiplier (const Ogre::ColourValue &specular) {
00130 mSpecularMultiplier = specular;
00131 }
00132
00133 const Ogre::ColourValue BaseSkyLight::getSpecularMultiplier () const {
00134 return mSpecularMultiplier;
00135 }
00136
00137 void BaseSkyLight::setAmbientMultiplier (const Ogre::ColourValue &ambient) {
00138 mAmbientMultiplier = ambient;
00139 }
00140
00141 const Ogre::ColourValue BaseSkyLight::getAmbientMultiplier () const {
00142 return mAmbientMultiplier;
00143 }
00144
00145 Ogre::Light* BaseSkyLight::getMainLight() const {
00146 return mMainLight;
00147 }
00148
00149 bool BaseSkyLight::shouldEnableLight(const Ogre::ColourValue &colour) {
00150 if (mForceDisableLight) {
00151 return false;
00152 }
00153 if (mAutoDisableLight) {
00154 Ogre::Real sum = colour.r + colour.g + colour.b;
00155 return sum >= mAutoDisableThreshold;
00156 } else {
00157 return true;
00158 }
00159 }
00160 }