timeOfDay.h 6.5 KB

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