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