2
0

lightBase.cpp 16 KB

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