Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals

/Users/blackie/Documents/myRepository/phobosengine-vc2005/phobosengine/phobosengine/DayNightCycle.h

Go to the documentation of this file.
00001 
00002 
00003 #ifndef DAYNIGHTCYCLE_H
00004 #define DAYNIGHTCYCLE_H
00005 
00006 
00007 #include "MathUtils.h"
00008 #include "ValueFader.h"
00009 #include "Vector3f.h"
00010 
00011 
00012 namespace pge {
00013 
00014 
00015         class DayNightCycle {
00016 
00017         public:
00018                 //************************************************************************
00019                 //
00020                 // Destructor
00021                 //
00022                 //************************************************************************
00024                 virtual ~DayNightCycle(void) {
00025                 }
00026 
00027 
00028                 //************************************************************************
00029                 //
00030                 // Functions
00031                 //
00032                 //************************************************************************
00033                 static DayNightCycle* getInstance(void) {
00034                         static DayNightCycle instance;
00035                         return &instance;
00036                 }
00037 
00038 
00039                 void setToNight(void) {
00040                         // TODO: sun direction / position, bool value lefttoright
00041                         m_currentState = STATE_NIGHT;
00042                         m_timePassed = 0;
00043                         m_fader->setValue(0.0f);
00044                 }
00045 
00046                 
00047                 void setToDay(void) {
00048                         // TODO: sun direction / position, bool value lefttoright
00049                         m_currentState = STATE_DAY;
00050                         m_timePassed = 0;
00051                         m_fader->setValue(1.0f);
00052                 }
00053 
00054 
00055                 void setDayNightCycle(float dayNightCycle) {
00056                         m_fader->setValue(dayNightCycle);
00057                 }
00058 
00059 
00060                 float getDayNightCycle(void) {
00061                         return m_fader->getValue();
00062                 }
00063 
00064 
00065                 Vector3f getAmbientLight(void) {
00066                         // TODO: performance. put everything in update.
00067                         Vector3f baseAmbient = Vector3f(1.0f, 1.0f, 1.0f);
00068                         float ambientFactor = mathutils::mapValueToInterval(0.0f, 1.0f, 0.2f, 1.0f, m_fader->getValue());
00069 
00070                         return baseAmbient.multiply(ambientFactor);
00071                 }
00072 
00073 
00074                 Vector3f getSunPosition(void) {
00075                         return m_sunPosition;
00076                 }
00077 
00078 
00079                 Vector3f getSunDirection(void) {
00080                         return m_sunDirection;
00081                 }
00082 
00083 
00084                 void update(unsigned int delay) {
00085                         float x = 0.0f;
00086                         float y = 0.0f;
00087 
00088 
00089                         switch(m_currentState) {
00090                                 case STATE_DAY:
00091                                 // Count daytime and change state if day is done.
00092                                 m_timePassed += delay;
00093                                 if(m_timePassed >= m_dayTimeLength) {
00094                                         m_timePassed = 0;
00095                                         m_leftToRight = !m_leftToRight;
00096                                         m_currentState = STATE_CHANGE_FROM_DAY_TO_NIGHT;
00097                                 }
00098                                 break;
00099                                 case STATE_NIGHT:
00100                                         // Count nighttime and change state if night is done.
00101                                         m_timePassed += delay;
00102                                         if(m_timePassed >= m_nightTimeLength) {
00103                                                 m_timePassed = 0;
00104                                                 m_currentState = STATE_CHANGE_FROM_NIGHT_TO_DAY;
00105                                         }
00106                                         break;
00107                                 case STATE_CHANGE_FROM_DAY_TO_NIGHT:
00108                                         // Update fader for day / night cycle.
00109                                         m_fader->update();
00110                                         if(m_fader->getValue() <= 0.0f) {
00111                                                 m_currentState = STATE_NIGHT;
00112                                                 m_fader->setValue(0.0f);
00113                                         }
00114 
00115                                         // Update sun position.
00116                                         if(m_leftToRight) {
00117                                                 x = mathutils::mapValueToInterval(0.0f, 1.0f, -500.0f, 0.0f, m_fader->getValue());
00118                                         } else {
00119                                                 x = mathutils::mapValueToInterval(0.0f, 1.0f, 500.0f, 0.0f, m_fader->getValue());
00120                                         }
00121                                         y = (float)sqrt(m_sunRadius * m_sunRadius - x * x);
00122                                         m_sunPosition.m_v[0] = x;
00123                                         m_sunPosition.m_v[1] = y;
00124 
00125                                         // Update sun direction.
00126                                         m_sunDirection.m_v[0] = m_sunPosition.m_v[0];
00127                                         m_sunDirection.m_v[1] = m_sunPosition.m_v[1];
00128                                         m_sunDirection.normalize();
00129                                         break;
00130                 
00131                                 case STATE_CHANGE_FROM_NIGHT_TO_DAY:
00132                                         // Update fader for day / night cycle.
00133                                         m_fader->update();
00134                                         if(m_fader->getValue() >= 1.0f) {
00135                                                 m_currentState = STATE_DAY;
00136                                                 m_fader->setValue(1.0f);
00137                                         }
00138 
00139                                         // Update sun position.
00140                                         if(m_leftToRight) {
00141                                                 x = mathutils::mapValueToInterval(0.0f, 1.0f, -500.0f, 0.0f, m_fader->getValue());
00142                                         } else {
00143                                                 x = mathutils::mapValueToInterval(0.0f, 1.0f, 500.0f, 0.0f, m_fader->getValue());
00144                                         }
00145                                         y = (float)sqrt(m_sunRadius * m_sunRadius - x * x);
00146                                         m_sunPosition.m_v[0] = x;
00147                                         m_sunPosition.m_v[1] = y;
00148 
00149                                         // Update sun direction.
00150                                         m_sunDirection.m_v[0] = m_sunPosition.m_v[0];
00151                                         m_sunDirection.m_v[1] = m_sunPosition.m_v[1];
00152                                         m_sunDirection.normalize();
00153                                         break;
00154                 
00155                                 default:
00156                                         break;
00157                         }
00158                 }
00159 
00160         private:
00161                 //************************************************************************
00162                 //
00163                 // Constructor
00164                 //
00165                 //************************************************************************
00167                 DayNightCycle(void) {
00168                         m_dayTimeLength = 30000;
00169                         m_nightTimeLength = 30000;
00170                         m_timePassed = 0;
00171                         m_leftToRight = true;
00172                         m_fader = new ValueFader(0.0f, 1.0f);
00173                         m_fader->setChange(0.001f);
00174 
00175                         // Start with day.
00176                         //m_currentState = STATE_DAY;
00177                         //m_fader->setValue(1.0f);
00178 
00179                         setToDay();
00180 
00181                         m_sunRadius = 500.0f;
00182                         m_sunPosition = Vector3f(-m_sunRadius, 0.0f, 0.0f);
00183                         m_sunDirection = Vector3f(0.0f, 1.0f, 0.0f);
00184                 }
00185 
00186 
00187                 //************************************************************************
00188                 //
00189                 // Functions
00190                 //
00191                 //************************************************************************
00192 
00193 
00194                 //************************************************************************
00195                 //
00196                 // Variables
00197                 //
00198                 //************************************************************************
00199                 ValueFader *m_fader;
00200                 float m_sunRadius;
00201                 Vector3f m_sunPosition;
00202                 Vector3f m_sunDirection;
00203 
00204                 // State machine for night / day cycle.
00205                 const static int STATE_NIGHT = 0;
00206                 const static int STATE_DAY = 1;
00207                 const static int STATE_CHANGE_FROM_DAY_TO_NIGHT = 2;
00208                 const static int STATE_CHANGE_FROM_NIGHT_TO_DAY = 3;
00209                 int m_currentState;
00210 
00211                 int m_dayTimeLength;
00212                 int m_nightTimeLength;
00213                 int m_timePassed;
00214                 bool m_leftToRight;
00215         };
00216 };
00217 
00218 #endif

Generated on Mon Oct 16 12:08:10 2006 for Phobosengine by doxygen 1.3.4