lightAnimData.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 _LIGHTANIMDATA_H_
  23. #define _LIGHTANIMDATA_H_
  24. #ifndef _SIMDATABLOCK_H_
  25. #include "console/simDatablock.h"
  26. #endif
  27. #ifndef _CONSOLETYPES_H_
  28. #include "console/consoleTypes.h"
  29. #endif
  30. #ifndef _MMATRIX_H_
  31. #include "math/mMatrix.h"
  32. #endif
  33. #ifndef _MPOINT3_H_
  34. #include "math/mPoint3.h"
  35. #endif
  36. #ifndef _COLOR_H_
  37. #include "core/color.h"
  38. #endif
  39. class LightInfo;
  40. /// The light animation state required by LightAnimData.
  41. struct LightAnimState
  42. {
  43. /// Constructor.
  44. LightAnimState()
  45. : active( false ),
  46. animationPhase( 1.0f ),
  47. animationPeriod( 1.0f ),
  48. brightness( 1.0f ),
  49. transform( true ),
  50. color( 0, 0, 0, 0 )
  51. {
  52. }
  53. /// If true light animation should be performed.
  54. bool active;
  55. /// The phase used to offset the animation start
  56. /// time to vary the animation of nearby lights.
  57. F32 animationPhase;
  58. /// The length of time in seconds for a single playback
  59. /// of the light animation.
  60. F32 animationPeriod;
  61. /// The set light brightness before animation occurs.
  62. F32 brightness;
  63. /// The set light transform before animation occurs.
  64. MatrixF transform;
  65. /// The set light color before animation occurs.
  66. LinearColorF color;
  67. };
  68. /// A datablock which defines and performs light animation.
  69. /// @see LightBase, LightDescription
  70. class LightAnimData : public SimDataBlock
  71. {
  72. typedef SimDataBlock Parent;
  73. protected:
  74. /// Called internally to update the key data.
  75. void _updateKeys();
  76. public:
  77. LightAnimData();
  78. virtual ~LightAnimData();
  79. DECLARE_CONOBJECT( LightAnimData );
  80. // SimObject
  81. static void initPersistFields();
  82. virtual void inspectPostApply();
  83. // SimDataBlock
  84. virtual bool preload( bool server, String &errorStr );
  85. virtual void packData( BitStream *stream );
  86. virtual void unpackData( BitStream *stream );
  87. /// Animates parameters on the passed Light's LightInfo object.
  88. virtual void animate( LightInfo *light, LightAnimState *state );
  89. /// Helper class used to keyframe light parameters. It is templatized
  90. /// on the number of parameters to store.
  91. template<U32 COUNT>
  92. struct AnimValue
  93. {
  94. /// Constructor.
  95. AnimValue()
  96. {
  97. dMemset( value1, 0, sizeof( value1 ) );
  98. dMemset( value2, 0, sizeof( value2 ) );
  99. dMemset( period, 0, sizeof( period ) );
  100. dMemset( keys, 0, sizeof( keys ) );
  101. dMemset( smooth, 0, sizeof( smooth ) );
  102. dMemset( keyLen, 0, sizeof( keyLen ) );
  103. dMemset( timeScale, 0, sizeof( timeScale ) );
  104. }
  105. /// The first value associated with the A keyframe.
  106. F32 value1[COUNT];
  107. /// The second value associated with the Z keyframe.
  108. F32 value2[COUNT];
  109. /// The period of the full keyframe sequence.
  110. F32 period[COUNT];
  111. /// The keyframe keys as a string of letters A to Z.
  112. StringTableEntry keys[COUNT];
  113. /// If true the transition between keyframes will be smooth.
  114. bool smooth[COUNT];
  115. /// The calculated length of the keyframe string.
  116. /// @see updateKey
  117. U32 keyLen[COUNT];
  118. /// The scale used to convert time into a keyframe position.
  119. /// @see updateKey
  120. F32 timeScale[COUNT];
  121. /// Performs the animation returning the results in the output if
  122. /// the time scale is greater than zero.
  123. /// @return Returns true if the animation was performed.
  124. bool animate(F32 time, F32 *output, bool multiply = false);
  125. /// Called when the key string is changed to update the
  126. /// key length and time scale.
  127. void updateKey();
  128. /// Write the animation data to the bitstream.
  129. void write( BitStream *stream ) const;
  130. /// Read the animation data from the bitstream.
  131. void read( BitStream *stream );
  132. };
  133. /// The positional animation parameters for x, y, and z.
  134. AnimValue<3> mOffset;
  135. /// The rotational animation parameters for x, y, and z.
  136. AnimValue<3> mRot;
  137. /// The color animation parameters for r, g, and b.
  138. AnimValue<3> mColor;
  139. /// The brightness animation parameter.
  140. AnimValue<1> mBrightness;
  141. };
  142. #endif // _LIGHTANIMDATA_H_