afxLightBase_T3D.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  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. #include "afx/arcaneFX.h"
  25. #include "console/consoleTypes.h"
  26. #include "core/stream/bitStream.h"
  27. #include "T3D/lightAnimData.h"
  28. #include "afx/ce/afxLightBase_T3D.h"
  29. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  30. // afxT3DLightBaseData
  31. IMPLEMENT_CO_DATABLOCK_V1(afxT3DLightBaseData);
  32. ConsoleDocClass( afxT3DLightBaseData,
  33. "@brief A datablock baseclass for afxT3DPointLightData and afxT3DSpotLightData.\n\n"
  34. "Not intended to be used directly, afxT3DLightBaseData exists to provide base member variables and generic functionality "
  35. "for the derived classes afxT3DPointLightData and afxT3DSpotLightData."
  36. "\n\n"
  37. "@see afxT3DPointLightData\n\n"
  38. "@see afxT3DSpotLightData\n\n"
  39. "@see PointLight\n\n"
  40. "@see SpotLight\n\n"
  41. "@ingroup afxEffects\n"
  42. "@ingroup AFX\n"
  43. "@ingroup Datablocks\n"
  44. );
  45. afxT3DLightBaseData::afxT3DLightBaseData()
  46. : mIsEnabled( true ),
  47. mColor( LinearColorF::WHITE ),
  48. mBrightness( 1.0f ),
  49. mCastShadows( false ),
  50. mPriority( 1.0f ),
  51. mAnimationData( NULL ),
  52. mFlareData( NULL ),
  53. mFlareScale( 1.0f )
  54. {
  55. mLocalRenderViz = false;
  56. // marked true if datablock ids need to
  57. // be converted into pointers
  58. do_id_convert = false;
  59. }
  60. afxT3DLightBaseData::afxT3DLightBaseData(const afxT3DLightBaseData& other, bool temp_clone) : GameBaseData(other, temp_clone)
  61. {
  62. mIsEnabled = other.mIsEnabled;
  63. mColor = other.mColor;
  64. mBrightness = other.mBrightness;
  65. mCastShadows = other.mCastShadows;
  66. mPriority = other.mPriority;
  67. mAnimationData = other.mAnimationData;
  68. mAnimState = other.mAnimState;
  69. mFlareData = other.mFlareData;
  70. mFlareScale = other.mFlareScale;
  71. mLocalRenderViz = other.mLocalRenderViz;
  72. do_id_convert = other.do_id_convert;
  73. }
  74. //
  75. // NOTE: keep this as consistent as possible with LightBase::initPersistFields()
  76. //
  77. void afxT3DLightBaseData::initPersistFields()
  78. {
  79. // We only add the basic lighting options that all lighting
  80. // systems would use... the specific lighting system options
  81. // are injected at runtime by the lighting system itself.
  82. addGroup( "Light" );
  83. addField( "isEnabled", TypeBool, Offset( mIsEnabled, afxT3DLightBaseData ),
  84. "Enables/Disables the object rendering and functionality in the scene.");
  85. addField( "color", TypeColorF, Offset( mColor, afxT3DLightBaseData ),
  86. "Changes the base color hue of the light.");
  87. addField( "brightness", TypeF32, Offset( mBrightness, afxT3DLightBaseData ),
  88. "Adjusts the lights power, 0 being off completely.");
  89. addField( "castShadows", TypeBool, Offset( mCastShadows, afxT3DLightBaseData ),
  90. "Enables/disables shadow casts by this light.");
  91. addField( "priority", TypeF32, Offset( mPriority, afxT3DLightBaseData ),
  92. "Used for sorting of lights by the light manager. Priority determines if a light "
  93. "has a stronger effect than, those with a lower value");
  94. addField( "localRenderViz", TypeBool, Offset( mLocalRenderViz, afxT3DLightBaseData ),
  95. "Enables/disables a semi-transparent geometry to help visualize the light's "
  96. "range and placement.");
  97. endGroup( "Light" );
  98. addGroup( "Light Animation" );
  99. addField( "animate", TypeBool, Offset( mAnimState.active, afxT3DLightBaseData ),
  100. "Toggles animation for the light on and off");
  101. addField( "animationType", TYPEID<LightAnimData>(), Offset( mAnimationData, afxT3DLightBaseData ),
  102. "Datablock containing light animation information (LightAnimData)");
  103. addField( "animationPeriod", TypeF32, Offset( mAnimState.animationPeriod, afxT3DLightBaseData ),
  104. "The length of time in seconds for a single playback of the light animation");
  105. addField( "animationPhase", TypeF32, Offset( mAnimState.animationPhase, afxT3DLightBaseData ),
  106. "The phase used to offset the animation start time to vary the animation of "
  107. "nearby lights.");
  108. endGroup( "Light Animation" );
  109. addGroup( "Misc" );
  110. addField( "flareType", TYPEID<LightFlareData>(), Offset( mFlareData, afxT3DLightBaseData ),
  111. "Datablock containing light flare information (LightFlareData)");
  112. addField( "flareScale", TypeF32, Offset( mFlareScale, afxT3DLightBaseData ),
  113. "Globally scales all features of the light flare");
  114. endGroup( "Misc" );
  115. /*
  116. // Now inject any light manager specific fields.
  117. LightManager::initLightFields();
  118. */
  119. // We do the parent fields at the end so that
  120. // they show up that way in the inspector.
  121. Parent::initPersistFields();
  122. }
  123. bool afxT3DLightBaseData::onAdd()
  124. {
  125. if (Parent::onAdd() == false)
  126. return false;
  127. return true;
  128. }
  129. void afxT3DLightBaseData::packData(BitStream* stream)
  130. {
  131. Parent::packData(stream);
  132. // note: BitStream's overloaded write() for LinearColorF will convert
  133. // to ColorI for transfer and then back to LinearColorF. This is fine
  134. // for most color usage but for lighting colors we want to preserve
  135. // "pushed" color values which may be greater than 1.0 so the color
  136. // is instead sent as individual color primaries.
  137. stream->write( mColor.red );
  138. stream->write( mColor.green );
  139. stream->write( mColor.blue );
  140. stream->write( mBrightness );
  141. stream->writeFlag( mCastShadows );
  142. stream->write( mAnimState.animationPeriod );
  143. stream->write( mAnimState.animationPhase );
  144. stream->write( mFlareScale );
  145. writeDatablockID(stream, mAnimationData, mPacked);
  146. writeDatablockID(stream, mFlareData, mPacked);
  147. }
  148. void afxT3DLightBaseData::unpackData(BitStream* stream)
  149. {
  150. Parent::unpackData(stream);
  151. stream->read( &mColor.red );
  152. stream->read( &mColor.green );
  153. stream->read( &mColor.blue );
  154. mColor.alpha = 1.0f;
  155. stream->read( &mBrightness );
  156. mCastShadows = stream->readFlag();
  157. stream->read( &mAnimState.animationPeriod );
  158. stream->read( &mAnimState.animationPhase );
  159. stream->read( &mFlareScale );
  160. mAnimationData = (LightAnimData*)(uintptr_t)readDatablockID(stream);
  161. mFlareData = (LightFlareData*)(uintptr_t)readDatablockID(stream);
  162. do_id_convert = true;
  163. }
  164. bool afxT3DLightBaseData::preload(bool server, String &errorStr)
  165. {
  166. if (!Parent::preload(server, errorStr))
  167. return false;
  168. // Resolve objects transmitted from server
  169. if (!server)
  170. {
  171. if (do_id_convert)
  172. {
  173. SimObjectId anim_id = SimObjectId((uintptr_t)mAnimationData);
  174. if (anim_id != 0)
  175. {
  176. // try to convert id to pointer
  177. if (!Sim::findObject(anim_id, mAnimationData))
  178. {
  179. Con::errorf(ConsoleLogEntry::General,
  180. "afxT3DLightBaseData::preload() -- bad datablockId: 0x%x (animationType)",
  181. anim_id);
  182. }
  183. }
  184. SimObjectId flare_id = SimObjectId((uintptr_t)mFlareData);
  185. if (flare_id != 0)
  186. {
  187. // try to convert id to pointer
  188. if (!Sim::findObject(flare_id, mFlareData))
  189. {
  190. Con::errorf(ConsoleLogEntry::General,
  191. "afxT3DLightBaseData::preload() -- bad datablockId: 0x%x (flareType)",
  192. flare_id);
  193. }
  194. }
  195. do_id_convert = false;
  196. }
  197. }
  198. return true;
  199. }
  200. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//