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 "SkyColourModel.h"
00023 #include "ImageHelper.h"
00024 
00025 namespace caelum {
00026 
00027 SkyColourModel::SkyColourModel
00028 (
00029         const Ogre::String &gradients,
00030         const Ogre::String &sunColours
00031 ):
00032         mSkyGradientsImage(0),
00033         mSunColoursImage(0)
00034 {
00035     mSkyGradientsImage = 0;
00036     mFogDensityMultiplier = 1;
00037     setSkyGradientsImage(gradients);
00038     setSunColoursImage(sunColours);
00039 }
00040 
00041 SkyColourModel::~SkyColourModel () {
00042     if (mSkyGradientsImage != 0) {
00043         delete mSkyGradientsImage;
00044         mSkyGradientsImage = 0;
00045     }
00046     if (mSunColoursImage != 0) {
00047         delete mSunColoursImage;
00048         mSunColoursImage = 0;
00049     }
00050 }
00051 
00052 void SkyColourModel::setSkyGradientsImage (const Ogre::String &filename) {
00053     if (mSkyGradientsImage != 0) {
00054         delete mSkyGradientsImage;
00055         mSkyGradientsImage = 0;
00056     }
00057 
00058     mSkyGradientsImage = new Ogre::Image ();
00059     mSkyGradientsImage->load (filename, RESOURCE_GROUP_NAME);
00060 }
00061 
00062 void SkyColourModel::setSunColoursImage (const Ogre::String &filename) {
00063     if (mSunColoursImage != 0) {
00064         delete mSunColoursImage;
00065         mSunColoursImage = 0;
00066     }
00067 
00068     mSunColoursImage = new Ogre::Image ();
00069     mSunColoursImage->load (filename, RESOURCE_GROUP_NAME);
00070 }
00071 
00072 Ogre::ColourValue SkyColourModel::getFogColour (float time, const Ogre::Vector3 &sunDir) {
00073     float elevation = sunDir.dotProduct (Ogre::Vector3::UNIT_Y) * 0.5 + 0.5;
00074     Ogre::ColourValue col = getInterpolatedColour (elevation, 1, mSkyGradientsImage, false);
00075     return col;
00076 }
00077 
00078 float SkyColourModel::getFogDensity (float time, const Ogre::Vector3 &sunDir) {
00079     float elevation = sunDir.dotProduct (Ogre::Vector3::UNIT_Y) * 0.5 + 0.5;
00080     Ogre::ColourValue col = getInterpolatedColour (elevation, 1, mSkyGradientsImage, false);
00081     return col.a;
00082 }
00083 
00084 Ogre::ColourValue SkyColourModel::getSunSphereColour (float time, const Ogre::Vector3 &sunDir) {
00085     float elevation = sunDir.dotProduct (Ogre::Vector3::UNIT_Y);
00086     elevation = elevation * 2 + 0.4;
00087 
00088     if (mSunColoursImage == 0) {
00089         return Ogre::ColourValue::White;
00090     } else {
00091         return getInterpolatedColour (elevation, 1, mSunColoursImage, false);
00092     }
00093 }
00094 
00095 Ogre::ColourValue SkyColourModel::getSunLightColour (float time, const Ogre::Vector3 &sunDir) {
00096     float elevation = sunDir.dotProduct (Ogre::Vector3::UNIT_Y) * 0.5 + 0.5;
00097 
00098     
00099     
00100     Ogre::ColourValue col = getInterpolatedColour (elevation, elevation, mSkyGradientsImage, false);
00101     double val = (col.r + col.g + col.b) / 3;
00102     col = Ogre::ColourValue(val, val, val, 1.0);
00103     assert(Ogre::Math::RealEqual(col.a, 1));
00104     return col;
00105 }
00106 
00107 Ogre::ColourValue SkyColourModel::getMoonBodyColour (const Ogre::Vector3 &moonDir) {
00108     return Ogre::ColourValue::White;
00109 }
00110 
00111 Ogre::ColourValue SkyColourModel::getMoonLightColour (const Ogre::Vector3 &moonDir) {
00112     
00113     float elevation = moonDir.dotProduct (Ogre::Vector3::UNIT_Y) * 0.5 + 0.5;
00114     Ogre::ColourValue col = getInterpolatedColour (elevation, elevation, mSkyGradientsImage, false);
00115     double val = (col.r + col.g + col.b) / 3;
00116     col = Ogre::ColourValue(val/2.5f, val/2.5f, val/2.5f, 1.0);
00117     assert(Ogre::Math::RealEqual(col.a, 1));
00118     return col;
00119 }
00120 
00121 }