lightDescription.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #include "platform/platform.h"
  23. #include "T3D/lightDescription.h"
  24. #include "lighting/lightManager.h"
  25. #include "T3D/lightFlareData.h"
  26. #include "T3D/lightAnimData.h"
  27. #include "core/stream/bitStream.h"
  28. #include "lighting/lightInfo.h"
  29. #include "console/engineAPI.h"
  30. LightDescription::LightDescription()
  31. : color( LinearColorF::WHITE ),
  32. brightness( 1.0f ),
  33. range( 5.0f ),
  34. castShadows( false ),
  35. mStaticRefreshFreq( 250 ),
  36. mDynamicRefreshFreq( 8 ),
  37. animationData( NULL ),
  38. animationDataId( 0 ),
  39. animationPeriod( 1.0f ),
  40. animationPhase( 1.0f ),
  41. flareData( NULL ),
  42. flareDataId( 0 ),
  43. flareScale( 1.0f )
  44. {
  45. }
  46. LightDescription::~LightDescription()
  47. {
  48. }
  49. IMPLEMENT_CO_DATABLOCK_V1( LightDescription );
  50. ConsoleDocClass( LightDescription,
  51. "@brief A helper datablock used by classes (such as shapebase) "
  52. "that submit lights to the scene but do not use actual \"LightBase\" objects.\n\n"
  53. "This datablock stores the properties of that light as fields that can be initialized from script."
  54. "@tsexample\n"
  55. "// Declare a light description to be used on a rocket launcher projectile\n"
  56. "datablock LightDescription(RocketLauncherLightDesc)\n"
  57. "{\n"
  58. " range = 4.0;\n"
  59. " color = \"1 1 0\";\n"
  60. " brightness = 5.0;\n"
  61. " animationType = PulseLightAnim;\n"
  62. " animationPeriod = 0.25;\n"
  63. "};\n\n"
  64. "// Declare a ProjectileDatablock which uses the light description\n"
  65. "datablock ProjectileData(RocketLauncherProjectile)\n"
  66. "{\n"
  67. " lightDesc = RocketLauncherLightDesc;\n\n"
  68. " projectileShapeName = \"art/shapes/weapons/SwarmGun/rocket.dts\";\n\n"
  69. " directDamage = 30;\n"
  70. " radiusDamage = 30;\n"
  71. " damageRadius = 5;\n"
  72. " areaImpulse = 2500;\n\n"
  73. " // ... remaining ProjectileData fields not listed for this example\n"
  74. "};\n"
  75. "@endtsexample\n\n"
  76. "@see LightBase\n\n"
  77. "@ingroup Lighting\n"
  78. );
  79. void LightDescription::initPersistFields()
  80. {
  81. docsURL;
  82. addGroup( "Light" );
  83. addField( "color", TypeColorF, Offset( color, LightDescription ), "Changes the base color hue of the light." );
  84. addField( "brightness", TypeF32, Offset( brightness, LightDescription ), "Adjusts the lights power, 0 being off completely." );
  85. addField( "range", TypeF32, Offset( range, LightDescription ), "Controls the size (radius) of the light" );
  86. addField( "castShadows", TypeBool, Offset( castShadows, LightDescription ), "Enables/disabled shadow casts by this light." );
  87. addField( "staticRefreshFreq", TypeS32, Offset( mStaticRefreshFreq, LightDescription ), "static shadow refresh rate (milliseconds)" );
  88. addField( "dynamicRefreshFreq", TypeS32, Offset( mDynamicRefreshFreq, LightDescription ), "dynamic shadow refresh rate (milliseconds)");
  89. endGroup( "Light" );
  90. addGroup( "Light Animation" );
  91. addField( "animationType", TYPEID< LightAnimData >(), Offset( animationData, LightDescription ), "Datablock containing light animation information (LightAnimData)" );
  92. addField( "animationPeriod", TypeF32, Offset( animationPeriod, LightDescription ), "The length of time in seconds for a single playback of the light animation" );
  93. addField( "animationPhase", TypeF32, Offset( animationPhase, LightDescription ), "The phase used to offset the animation start time to vary the animation of nearby lights." );
  94. endGroup( "Light Animation" );
  95. addGroup( "Misc" );
  96. addField( "flareType", TYPEID< LightFlareData >(), Offset( flareData, LightDescription ), "Datablock containing light flare information (LightFlareData)" );
  97. addField( "flareScale", TypeF32, Offset( flareScale, LightDescription ), "Globally scales all features of the light flare" );
  98. endGroup( "Misc" );
  99. LightManager::initLightFields();
  100. Parent::initPersistFields();
  101. }
  102. void LightDescription::inspectPostApply()
  103. {
  104. Parent::inspectPostApply();
  105. // Hack to allow changing properties in game.
  106. // Do the same work as preload.
  107. animationData = NULL;
  108. flareData = NULL;
  109. String str;
  110. _preload( false, str );
  111. }
  112. bool LightDescription::onAdd()
  113. {
  114. if ( !Parent::onAdd() )
  115. return false;
  116. return true;
  117. }
  118. bool LightDescription::preload( bool server, String &errorStr )
  119. {
  120. if ( !Parent::preload( server, errorStr ) )
  121. return false;
  122. return _preload( server, errorStr );
  123. }
  124. void LightDescription::packData( BitStream *stream )
  125. {
  126. Parent::packData( stream );
  127. stream->write( color );
  128. stream->write( brightness );
  129. stream->write( range );
  130. stream->writeFlag( castShadows );
  131. stream->write(mStaticRefreshFreq);
  132. stream->write(mDynamicRefreshFreq);
  133. stream->write( animationPeriod );
  134. stream->write( animationPhase );
  135. stream->write( flareScale );
  136. if ( stream->writeFlag( animationData ) )
  137. {
  138. stream->writeRangedU32( animationData->getId(),
  139. DataBlockObjectIdFirst,
  140. DataBlockObjectIdLast );
  141. }
  142. if ( stream->writeFlag( flareData ) )
  143. {
  144. stream->writeRangedU32( flareData->getId(),
  145. DataBlockObjectIdFirst,
  146. DataBlockObjectIdLast );
  147. }
  148. }
  149. void LightDescription::unpackData( BitStream *stream )
  150. {
  151. Parent::unpackData( stream );
  152. stream->read( &color );
  153. stream->read( &brightness );
  154. stream->read( &range );
  155. castShadows = stream->readFlag();
  156. stream->read(&mStaticRefreshFreq);
  157. stream->read(&mDynamicRefreshFreq);
  158. stream->read( &animationPeriod );
  159. stream->read( &animationPhase );
  160. stream->read( &flareScale );
  161. if ( stream->readFlag() )
  162. animationDataId = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  163. if ( stream->readFlag() )
  164. flareDataId = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  165. }
  166. void LightDescription::submitLight( LightState *state, const MatrixF &xfm, LightManager *lm, SimObject *object )
  167. {
  168. LightInfo *li = state->lightInfo;
  169. li->setRange( range );
  170. li->setColor( color );
  171. li->setCastShadows( castShadows );
  172. li->setStaticRefreshFreq(mStaticRefreshFreq);
  173. li->setDynamicRefreshFreq(mDynamicRefreshFreq);
  174. li->setTransform( xfm );
  175. if ( animationData )
  176. {
  177. LightAnimState *animState = &state->animState;
  178. animState->brightness = brightness;
  179. animState->transform = xfm;
  180. animState->color = color;
  181. animState->animationPeriod = animationPeriod;
  182. animState->animationPhase = animationPhase;
  183. animationData->animate( li, animState );
  184. }
  185. lm->registerGlobalLight( li, object );
  186. }
  187. void LightDescription::prepRender( SceneRenderState *sceneState, LightState *lightState, const MatrixF &xfm )
  188. {
  189. if ( flareData )
  190. {
  191. LightFlareState *flareState = &lightState->flareState;
  192. flareState->fullBrightness = brightness;
  193. flareState->scale = flareScale;
  194. flareState->lightMat = xfm;
  195. flareState->lightInfo = lightState->lightInfo;
  196. flareData->prepRender( sceneState, flareState );
  197. }
  198. }
  199. bool LightDescription::_preload( bool server, String &errorStr )
  200. {
  201. if (!animationData && animationDataId != 0)
  202. if (Sim::findObject(animationDataId, animationData) == false)
  203. Con::errorf(ConsoleLogEntry::General, "LightDescription::onAdd: Invalid packet, bad datablockId(animationData): %d", animationDataId);
  204. if (!flareData && flareDataId != 0)
  205. if (Sim::findObject(flareDataId, flareData) == false)
  206. Con::errorf(ConsoleLogEntry::General, "LightDescription::onAdd: Invalid packet, bad datablockId(flareData): %d", flareDataId);
  207. return true;
  208. }
  209. DefineEngineMethod( LightDescription, apply, void, (),,
  210. "@brief Force an inspectPostApply call for the benefit of tweaking via the console\n\n"
  211. "Normally this functionality is only exposed to objects via the World Editor, once changes have been made. "
  212. "Exposing apply to script allows you to make changes to it on the fly without the World Editor.\n\n"
  213. "@note This is intended for debugging and tweaking, not for game play\n\n"
  214. "@tsexample\n"
  215. "// Change a property of the light description\n"
  216. "RocketLauncherLightDesc.brightness = 10;\n\n"
  217. "// Make it so\n"
  218. "RocketLauncherLightDesc.apply();\n"
  219. "@endtsexample\n\n"
  220. )
  221. {
  222. object->inspectPostApply();
  223. }
  224. //ConsoleMethod( LightDescription, apply, void, 2, 2, "force an inspectPostApply for the benefit of tweaking via the console" )
  225. //{
  226. // object->inspectPostApply();
  227. //}