lightBase.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #include "platform/platform.h"
  27. #include "T3D/lightBase.h"
  28. #include "console/consoleTypes.h"
  29. #include "console/typeValidators.h"
  30. #include "core/stream/bitStream.h"
  31. #include "sim/netConnection.h"
  32. #include "lighting/lightManager.h"
  33. #include "lighting/shadowMap/lightShadowMap.h"
  34. #include "scene/sceneRenderState.h"
  35. #include "renderInstance/renderPassManager.h"
  36. #include "console/engineAPI.h"
  37. #include "gfx/gfxDrawUtil.h"
  38. extern bool gEditingMission;
  39. bool LightBase::smRenderViz = false;
  40. IMPLEMENT_CONOBJECT( LightBase );
  41. ConsoleDocClass( LightBase,
  42. "@brief This is the base class for light objects.\n\n"
  43. "It is *NOT* intended to be used directly in script, but exists to provide the base member variables "
  44. "and generic functionality. You should be using the derived classes PointLight and SpotLight, which "
  45. "can be declared in TorqueScript or added from the World Editor.\n\n"
  46. "For this class, we only add basic lighting options that all lighting systems would use. "
  47. "The specific lighting system options are injected at runtime by the lighting system itself.\n\n"
  48. "@see PointLight\n\n"
  49. "@see SpotLight\n\n"
  50. "@ingroup Lighting\n"
  51. );
  52. LightBase::LightBase()
  53. : mIsEnabled( true ),
  54. mColor( LinearColorF::WHITE ),
  55. mBrightness( 1.0f ),
  56. mCastShadows( false ),
  57. mStaticRefreshFreq( 250 ),
  58. mDynamicRefreshFreq( 8 ),
  59. mPriority( 1.0f ),
  60. mAnimationData( NULL ),
  61. mFlareData( NULL ),
  62. mFlareScale( 1.0f )
  63. {
  64. mNetFlags.set( Ghostable | ScopeAlways );
  65. mTypeMask = LightObjectType;
  66. mLight = LightManager::createLightInfo();
  67. mFlareState.clear();
  68. mLocalRenderViz = false;
  69. }
  70. LightBase::~LightBase()
  71. {
  72. SAFE_DELETE( mLight );
  73. }
  74. void LightBase::initPersistFields()
  75. {
  76. // We only add the basic lighting options that all lighting
  77. // systems would use... the specific lighting system options
  78. // are injected at runtime by the lighting system itself.
  79. addGroup( "Light" );
  80. addField( "isEnabled", TypeBool, Offset( mIsEnabled, LightBase ), "Enables/Disables the object rendering and functionality in the scene." );
  81. addField( "color", TypeColorF, Offset( mColor, LightBase ), "Changes the base color hue of the light." );
  82. addField( "brightness", TypeF32, Offset( mBrightness, LightBase ), "Adjusts the lights power, 0 being off completely." );
  83. addField( "castShadows", TypeBool, Offset( mCastShadows, LightBase ), "Enables/disabled shadow casts by this light." );
  84. //addField( "staticRefreshFreq", TypeS32, Offset( mStaticRefreshFreq, LightBase ), "static shadow refresh rate (milliseconds)" );
  85. //addField( "dynamicRefreshFreq", TypeS32, Offset( mDynamicRefreshFreq, LightBase ), "dynamic shadow refresh rate (milliseconds)");
  86. addField( "priority", TypeF32, Offset( mPriority, LightBase ), "Used for sorting of lights by the light manager. "
  87. "Priority determines if a light has a stronger effect than, those with a lower value" );
  88. endGroup( "Light" );
  89. addGroup( "Light Animation" );
  90. addField( "animate", TypeBool, Offset( mAnimState.active, LightBase ), "Toggles animation for the light on and off" );
  91. addField( "animationType", TYPEID< LightAnimData >(), Offset( mAnimationData, LightBase ), "Datablock containing light animation information (LightAnimData)" );
  92. addFieldV( "animationPeriod", TypeF32, Offset( mAnimState.animationPeriod, LightBase ), &CommonValidators::PositiveNonZeroFloat, "The length of time in seconds for a single playback of the light animation (must be > 0)" );
  93. addField( "animationPhase", TypeF32, Offset( mAnimState.animationPhase, LightBase ), "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( mFlareData, LightBase ), "Datablock containing light flare information (LightFlareData)" );
  97. addField( "flareScale", TypeF32, Offset( mFlareScale, LightBase ), "Globally scales all features of the light flare" );
  98. endGroup( "Misc" );
  99. // Now inject any light manager specific fields.
  100. LightManager::initLightFields();
  101. // We do the parent fields at the end so that
  102. // they show up that way in the inspector.
  103. Parent::initPersistFields();
  104. Con::addVariable( "$Light::renderViz", TypeBool, &smRenderViz,
  105. "Toggles visualization of light object's radius or cone.\n"
  106. "@ingroup Lighting");
  107. Con::addVariable( "$Light::renderLightFrustums", TypeBool, &LightShadowMap::smDebugRenderFrustums,
  108. "Toggles rendering of light frustums when the light is selected in the editor.\n\n"
  109. "@note Only works for shadow mapped lights.\n\n"
  110. "@ingroup Lighting" );
  111. }
  112. bool LightBase::onAdd()
  113. {
  114. if ( !Parent::onAdd() )
  115. return false;
  116. // Update the light parameters.
  117. _conformLights();
  118. addToScene();
  119. return true;
  120. }
  121. void LightBase::onRemove()
  122. {
  123. removeFromScene();
  124. Parent::onRemove();
  125. }
  126. void LightBase::submitLights( LightManager *lm, bool staticLighting )
  127. {
  128. if ( !mIsEnabled || staticLighting )
  129. return;
  130. if ( mAnimState.active &&
  131. mAnimState.animationPeriod > 0.0f &&
  132. mAnimationData )
  133. {
  134. mAnimState.brightness = mBrightness;
  135. mAnimState.transform = getRenderTransform();
  136. mAnimState.color = mColor;
  137. mAnimationData->animate( mLight, &mAnimState );
  138. }
  139. lm->registerGlobalLight( mLight, this );
  140. }
  141. void LightBase::inspectPostApply()
  142. {
  143. // We do not call the parent here as it
  144. // will call setScale() and screw up the
  145. // real sizing fields on the light.
  146. // Ok fine... then we must set MountedMask ourself.
  147. _conformLights();
  148. setMaskBits( EnabledMask | UpdateMask | TransformMask | DatablockMask | MountedMask );
  149. }
  150. void LightBase::setTransform( const MatrixF &mat )
  151. {
  152. setMaskBits( TransformMask );
  153. Parent::setTransform( mat );
  154. }
  155. void LightBase::prepRenderImage( SceneRenderState *state )
  156. {
  157. if ( mIsEnabled && mFlareData )
  158. {
  159. mFlareState.fullBrightness = mBrightness;
  160. mFlareState.scale = mFlareScale;
  161. mFlareState.lightInfo = mLight;
  162. mFlareState.lightMat = getRenderTransform();
  163. mFlareData->prepRender( state, &mFlareState );
  164. }
  165. if ( !state->isDiffusePass() )
  166. return;
  167. const bool isSelectedInEditor = ( gEditingMission && isSelected() );
  168. // If the light is selected or light visualization
  169. // is enabled then register the callback.
  170. if ( mLocalRenderViz || smRenderViz || isSelectedInEditor )
  171. {
  172. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  173. ri->renderDelegate.bind( this, &LightBase::_onRenderViz );
  174. ri->type = RenderPassManager::RIT_Editor;
  175. state->getRenderPass()->addInst( ri );
  176. }
  177. }
  178. void LightBase::_onRenderViz( ObjectRenderInst *ri,
  179. SceneRenderState *state,
  180. BaseMatInstance *overrideMat )
  181. {
  182. if ( overrideMat )
  183. return;
  184. _renderViz( state );
  185. }
  186. void LightBase::_onSelected()
  187. {
  188. #ifdef TORQUE_DEBUG
  189. // Enable debug rendering on the light.
  190. if( isClientObject() )
  191. mLight->enableDebugRendering( true );
  192. #endif
  193. Parent::_onSelected();
  194. }
  195. void LightBase::_onUnselected()
  196. {
  197. #ifdef TORQUE_DEBUG
  198. // Disable debug rendering on the light.
  199. if( isClientObject() )
  200. mLight->enableDebugRendering( false );
  201. #endif
  202. Parent::_onUnselected();
  203. }
  204. void LightBase::interpolateTick( F32 delta )
  205. {
  206. }
  207. void LightBase::processTick()
  208. {
  209. }
  210. void LightBase::advanceTime( F32 timeDelta )
  211. {
  212. if ( isMounted() )
  213. {
  214. MatrixF mat( true );
  215. mMount.object->getRenderMountTransform( 0.f, mMount.node, mMount.xfm, &mat );
  216. mLight->setTransform( mat );
  217. Parent::setTransform( mat );
  218. }
  219. }
  220. U32 LightBase::packUpdate( NetConnection *conn, U32 mask, BitStream *stream )
  221. {
  222. U32 retMask = Parent::packUpdate( conn, mask, stream );
  223. stream->writeFlag( mIsEnabled );
  224. if ( stream->writeFlag( mask & TransformMask ) )
  225. stream->writeAffineTransform( mObjToWorld );
  226. if ( stream->writeFlag( mask & UpdateMask ) )
  227. {
  228. stream->write( mColor );
  229. stream->write( mBrightness );
  230. stream->writeFlag( mCastShadows );
  231. stream->write(mStaticRefreshFreq);
  232. stream->write(mDynamicRefreshFreq);
  233. stream->write( mPriority );
  234. mLight->packExtended( stream );
  235. stream->writeFlag( mAnimState.active );
  236. stream->write( mAnimState.animationPeriod );
  237. stream->write( mAnimState.animationPhase );
  238. stream->write( mFlareScale );
  239. }
  240. if ( stream->writeFlag( mask & DatablockMask ) )
  241. {
  242. if ( stream->writeFlag( mAnimationData ) )
  243. {
  244. stream->writeRangedU32( mAnimationData->getId(),
  245. DataBlockObjectIdFirst,
  246. DataBlockObjectIdLast );
  247. }
  248. if ( stream->writeFlag( mFlareData ) )
  249. {
  250. stream->writeRangedU32( mFlareData->getId(),
  251. DataBlockObjectIdFirst,
  252. DataBlockObjectIdLast );
  253. }
  254. }
  255. return retMask;
  256. }
  257. void LightBase::unpackUpdate( NetConnection *conn, BitStream *stream )
  258. {
  259. Parent::unpackUpdate( conn, stream );
  260. mIsEnabled = stream->readFlag();
  261. if ( stream->readFlag() ) // TransformMask
  262. stream->readAffineTransform( &mObjToWorld );
  263. if ( stream->readFlag() ) // UpdateMask
  264. {
  265. stream->read( &mColor );
  266. stream->read( &mBrightness );
  267. mCastShadows = stream->readFlag();
  268. stream->read(&mStaticRefreshFreq);
  269. stream->read(&mDynamicRefreshFreq);
  270. stream->read( &mPriority );
  271. mLight->unpackExtended( stream );
  272. mAnimState.active = stream->readFlag();
  273. stream->read( &mAnimState.animationPeriod );
  274. stream->read( &mAnimState.animationPhase );
  275. stream->read( &mFlareScale );
  276. }
  277. if ( stream->readFlag() ) // DatablockMask
  278. {
  279. if ( stream->readFlag() )
  280. {
  281. SimObjectId id = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  282. LightAnimData *datablock = NULL;
  283. if ( Sim::findObject( id, datablock ) )
  284. mAnimationData = datablock;
  285. else
  286. {
  287. conn->setLastError( "Light::unpackUpdate() - invalid LightAnimData!" );
  288. mAnimationData = NULL;
  289. }
  290. }
  291. else
  292. mAnimationData = NULL;
  293. if ( stream->readFlag() )
  294. {
  295. SimObjectId id = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  296. LightFlareData *datablock = NULL;
  297. if ( Sim::findObject( id, datablock ) )
  298. mFlareData = datablock;
  299. else
  300. {
  301. conn->setLastError( "Light::unpackUpdate() - invalid LightCoronaData!" );
  302. mFlareData = NULL;
  303. }
  304. }
  305. else
  306. mFlareData = NULL;
  307. }
  308. if ( isProperlyAdded() )
  309. _conformLights();
  310. }
  311. void LightBase::setLightEnabled( bool enabled )
  312. {
  313. if ( mIsEnabled != enabled )
  314. {
  315. mIsEnabled = enabled;
  316. setMaskBits( EnabledMask );
  317. }
  318. }
  319. DefineEngineMethod( LightBase, setLightEnabled, void, ( bool state ),,
  320. "@brief Toggles the light on and off\n\n"
  321. "@param state Turns the light on (true) or off (false)\n"
  322. "@tsexample\n"
  323. "// Disable the light\n"
  324. "CrystalLight.setLightEnabled(false);\n\n"
  325. "// Renable the light\n"
  326. "CrystalLight.setLightEnabled(true);\n"
  327. "@endtsexample\n\n"
  328. )
  329. {
  330. object->setLightEnabled( state );
  331. }
  332. //ConsoleMethod( LightBase, setLightEnabled, void, 3, 3, "( bool enabled )\t"
  333. // "Toggles the light on and off." )
  334. //{
  335. // object->setLightEnabled( dAtob( argv[2] ) );
  336. //}
  337. static ConsoleDocFragment _lbplayAnimation1(
  338. "@brief Plays the light animation assigned to this light with the existing LightAnimData datablock.\n\n"
  339. "@tsexample\n"
  340. "// Play the animation assigned to this light\n"
  341. "CrystalLight.playAnimation();\n"
  342. "@endtsexample\n\n",
  343. "LightBase",
  344. "void playAnimation();"
  345. );
  346. static ConsoleDocFragment _lbplayAnimation2(
  347. "@brief Plays the light animation on this light using a new LightAnimData. If no LightAnimData "
  348. "is passed the existing one is played.\n\n"
  349. "@param anim Name of the LightAnimData datablock to be played\n\n"
  350. "@tsexample\n"
  351. "// Play the animation using a new LightAnimData datablock\n"
  352. "CrystalLight.playAnimation(SubtlePulseLightAnim);\n"
  353. "@endtsexample\n\n",
  354. "LightBase",
  355. "void playAnimation(LightAnimData anim);"
  356. );
  357. DefineEngineMethod( LightBase, playAnimation, void, (const char * anim), (""), "( [LightAnimData anim] )\t"
  358. "Plays a light animation on the light. If no LightAnimData is passed the "
  359. "existing one is played."
  360. "@hide")
  361. {
  362. if ( String::isEmpty(anim) )
  363. {
  364. object->playAnimation();
  365. return;
  366. }
  367. LightAnimData *animData;
  368. if ( !Sim::findObject( anim, animData ) )
  369. {
  370. Con::errorf( "LightBase::playAnimation() - Invalid LightAnimData '%s'.", anim );
  371. return;
  372. }
  373. // Play Animation.
  374. object->playAnimation( animData );
  375. }
  376. void LightBase::playAnimation( void )
  377. {
  378. if ( !mAnimState.active )
  379. {
  380. mAnimState.active = true;
  381. setMaskBits( UpdateMask );
  382. }
  383. }
  384. void LightBase::playAnimation( LightAnimData *animData )
  385. {
  386. // Play Animation.
  387. playAnimation();
  388. // Update Datablock?
  389. if ( mAnimationData != animData )
  390. {
  391. mAnimationData = animData;
  392. setMaskBits( DatablockMask );
  393. }
  394. }
  395. DefineEngineMethod( LightBase, pauseAnimation, void, (), , "Stops the light animation." )
  396. {
  397. object->pauseAnimation();
  398. }
  399. void LightBase::pauseAnimation( void )
  400. {
  401. if ( mAnimState.active )
  402. {
  403. mAnimState.active = false;
  404. setMaskBits( UpdateMask );
  405. }
  406. }