timeOfDay.h

Engine/source/environment/timeOfDay.h

More...

Classes:

Public Typedefs

COLOR_TARGETS 
Signal< void(TimeOfDay *timeOfDay, F32 time)>
TimeOfDayUpdateSignal 

Detailed Description

Public Typedefs

typedef Vector< COLOR_TARGET > COLOR_TARGETS 
typedef Signal< void(TimeOfDay *timeOfDay, F32 time)> TimeOfDayUpdateSignal 
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24
 25#ifndef _TIMEOFDAY_H_
 26#define _TIMEOFDAY_H_
 27
 28#ifndef _SCENEOBJECT_H_
 29#include "scene/sceneObject.h"
 30#endif
 31
 32class Sun;
 33class TimeOfDay;
 34
 35struct COLOR_TARGET
 36{
 37   F32      elevation; // maximum target elevation
 38   LinearColorF   color; //normalized 0 = 1.0 ... 
 39   F32      bandMod;  //6 is max
 40   LinearColorF   bandColor;
 41};
 42
 43typedef Vector<COLOR_TARGET> COLOR_TARGETS;
 44
 45typedef Signal<void(TimeOfDay *timeOfDay, F32 time)> TimeOfDayUpdateSignal;
 46
 47
 48struct TimeOfDayEvent
 49{
 50   // The elevation at which
 51   // this event will fire.
 52   F32 triggerElevation;
 53
 54   // User identifier for the event.
 55   String identifier;
 56
 57   // Remove this event when it fires.
 58   bool oneShot;
 59
 60   // For internal use.
 61   bool deleteMe;
 62};
 63
 64class TimeOfDay : public SceneObject
 65{
 66   typedef SceneObject Parent;
 67
 68public:
 69
 70   static S32 smCurrentTime;   
 71   static F32 smTimeScale; // To pause or resume time flow from outside this object, like in the editor.
 72
 73   TimeOfDay();
 74   virtual ~TimeOfDay();
 75   
 76   // ConsoleObject
 77   static void initPersistFields();
 78   static void consoleInit();
 79   DECLARE_CONOBJECT( TimeOfDay );
 80   void inspectPostApply();
 81
 82   // SimObject
 83   virtual bool onAdd();
 84   virtual void onRemove();
 85
 86   // NetObject
 87   U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream );
 88   void unpackUpdate( NetConnection *conn, BitStream *stream );  
 89
 90   // ProcessObject   
 91   virtual void processTick( const Move *move );
 92
 93   F32 getAzimuthRads() { return mAzimuth; }
 94   F32 getElevationRads() { return mElevation; }
 95   F32 getAzimuthDegrees() { return mRadToDeg(mAzimuth); }
 96   F32 getElevationDegrees() { return mRadToDeg(mElevation); }
 97
 98   /*
 99   // Sun position stuff
100   void UpdateSunPosition(void);
101   // void UpdateSunPosition(fxSunLight *sunLight);
102
103
104   // Scene lighting (Adapted from Joshua Ritter's Day/Night cycle code)
105   // I changed references to pointers on the basis of principle. ;-)
106   void EnableLighting(F32 emissiveScale = 1.0);
107   void DisableLighting();
108   F32 GetIntensity() 
109   { return (mCurrentColor.blue + mCurrentColor.green + mCurrentColor.red) / 3; }
110   */
111   static TimeOfDayUpdateSignal& getTimeOfDayUpdateSignal() { return smTimeOfDayUpdateSignal; } 
112   void getSunColor( LinearColorF *outColor ) const { _getSunColor( outColor ); }
113
114   void addTimeEvent( F32 triggerElevation, const UTF8 *identifier );
115
116   void setTimeOfDay( F32 time );
117   void setPlay( bool play ) { mPlay = play; setMaskBits( OrbitMask ); }
118   void setDayLength( F32 length ) { mDayLen = length; setMaskBits( OrbitMask ); }
119
120   void animate( F32 time, F32 speed );
121
122protected:
123
124   Vector<TimeOfDayEvent> mTimeEvents;
125
126   void _updateTimeEvents();
127   void _onTimeEvent( const String &identifier );
128
129   static TimeOfDayUpdateSignal smTimeOfDayUpdateSignal;
130
131   enum NetMaskBits 
132   {
133      OrbitMask = Parent::NextFreeMask << 0,
134      AnimateMask = Parent::NextFreeMask << 1
135   };
136
137   void _updatePosition();
138
139   void _onGhostAlwaysDone();
140
141   F32 _calcElevation( F32 lat, F32 dec, F32 mer );
142
143   F32 _calcAzimuth( F32 lat, F32 dec, F32 mer );
144
145   /// Adds all of our target colors to our COLOR_TARGETS.
146   void _initColors();
147
148   /// Adds a color target to our set of targets.
149   /// 
150   /// @param ele [in] target sun elevation.
151   /// @param color [in] target color.
152   /// @param bandMod [in]
153   /// @param bandColor [in]
154   void _addColorTarget( F32 ele, const LinearColorF &color, F32 bandMod, const LinearColorF &bandColor );
155
156     // Grab our sun and sky colors based upon sun elevation.
157   void _getSunColor( LinearColorF *outColor ) const;
158
159   static bool setTimeOfDay( void *object, const char *index, const char *data );
160   static bool setPlay( void *object, const char *index, const char *data );
161   static bool setDayLength( void *object, const char *index, const char *data );
162
163   /*
164   // Get a pointer to the sun's light object
165   Sun* GetSunObject();
166   // return number between 0 and 1 representing color variance
167   F32 getColorVariance();
168   */
169
170   // Date tracking stuff  
171   F32 mStartTimeOfDay;    ///< The time of day this object begins at.
172   F32 mDayLen;            ///< length of day in real world seconds.
173   F32 mPrevElevation;      ///< The 0-360 normalized elevation for the previous update.
174   F32 mNextElevation;      ///< The 0-360 normalized elevation for the next update.
175   F32 mTimeOfDay;         ///< The zero to one time of day where zero is the start of a day and one is the end.  
176
177   F32 mAzimuthOverride;  ///< Used to specify an azimuth that will stay constant throughout the day cycle.
178
179   // Global positioning stuff
180   F32 mAxisTilt; // angle between global equator and tropic
181
182   F32 mAzimuth; // Angle from true north of celestial object in radians
183   F32 mElevation; // Angle from horizon of celestial object in radians
184
185   VectorF mZenithDirection; // The direction of celestial object at the zenith of its orbit.
186
187   // Scalar applied to time that elapses while the sun is up.
188   F32 mDayScale;
189   // Scalar applied to time that elapses while the sun is down.
190   F32 mNightScale;
191
192   // color management
193   COLOR_TARGETS mColorTargets;
194
195   F32 mAnimateTime;
196   F32 mAnimateSpeed;
197   bool mAnimate;
198
199   /*
200   LinearColorF mCurrentColor;
201   F32 mBandMod;
202   LinearColorF mCurrentBandColor;
203
204   // PersistFields preparation
205   bool mConvertedToRads;  
206   */
207
208   // Debugging stuff that probably needs to be removed eventaully
209   bool mPlay;
210};
211
212
213#endif // _TIMEOFDAY_H_
214