2
0

lightBase.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. addField( "brightness", TypeF32, Offset( mBrightness, LightBase ), "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. addField( "priority", TypeF32, Offset( mPriority, LightBase ), "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", TypeF32, Offset( mAnimState.animationPeriod, LightBase ), &CommonValidators::PositiveNonZeroFloat, "The length of time in seconds for a single playback of the light animation (must be > 0)" );
  94. addField( "animationPhase", TypeF32, Offset( mAnimState.animationPhase, LightBase ), "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. addField( "flareScale", TypeF32, Offset( mFlareScale, LightBase ), "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. return true;
  121. }
  122. void LightBase::onRemove()
  123. {
  124. removeFromScene();
  125. Parent::onRemove();
  126. }
  127. void LightBase::submitLights( LightManager *lm, bool staticLighting )
  128. {
  129. if ( !mIsEnabled || staticLighting )
  130. return;
  131. if ( mAnimState.active &&
  132. mAnimState.animationPeriod > 0.0f &&
  133. mAnimationData )
  134. {
  135. mAnimState.brightness = mBrightness;
  136. mAnimState.transform = getRenderTransform();
  137. mAnimState.color = mColor;
  138. mAnimationData->animate( mLight, &mAnimState );
  139. }
  140. lm->registerGlobalLight( mLight, this );
  141. }
  142. void LightBase::inspectPostApply()
  143. {
  144. // We do not call the parent here as it
  145. // will call setScale() and screw up the
  146. // real sizing fields on the light.
  147. // Ok fine... then we must set MountedMask ourself.
  148. _conformLights();
  149. setMaskBits( EnabledMask | UpdateMask | TransformMask | DatablockMask | MountedMask );
  150. }
  151. void LightBase::setTransform( const MatrixF &mat )
  152. {
  153. setMaskBits( TransformMask );
  154. Parent::setTransform( mat );
  155. }
  156. void LightBase::prepRenderImage( SceneRenderState *state )
  157. {
  158. if ( mIsEnabled && mFlareData )
  159. {
  160. mFlareState.fullBrightness = mBrightness;
  161. mFlareState.scale = mFlareScale;
  162. mFlareState.lightInfo = mLight;
  163. mFlareState.lightMat = getRenderTransform();
  164. mFlareData->prepRender( state, &mFlareState );
  165. }
  166. if ( !state->isDiffusePass() )
  167. return;
  168. const bool isSelectedInEditor = ( gEditingMission && isSelected() );
  169. // If the light is selected or light visualization
  170. // is enabled then register the callback.
  171. if ( mLocalRenderViz || smRenderViz || isSelectedInEditor )
  172. {
  173. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  174. ri->renderDelegate.bind( this, &LightBase::_onRenderViz );
  175. ri->type = RenderPassManager::RIT_Editor;
  176. state->getRenderPass()->addInst( ri );
  177. }
  178. }
  179. void LightBase::_onRenderViz( ObjectRenderInst *ri,
  180. SceneRenderState *state,
  181. BaseMatInstance *overrideMat )
  182. {
  183. if ( overrideMat )
  184. return;
  185. _renderViz( state );
  186. }
  187. void LightBase::_onSelected()
  188. {
  189. #ifdef TORQUE_DEBUG
  190. // Enable debug rendering on the light.
  191. if( isClientObject() )
  192. mLight->enableDebugRendering( true );
  193. #endif
  194. Parent::_onSelected();
  195. }
  196. void LightBase::_onUnselected()
  197. {
  198. #ifdef TORQUE_DEBUG
  199. // Disable debug rendering on the light.
  200. if( isClientObject() )
  201. mLight->enableDebugRendering( false );
  202. #endif
  203. Parent::_onUnselected();
  204. }
  205. void LightBase::interpolateTick( F32 delta )
  206. {
  207. }
  208. void LightBase::processTick()
  209. {
  210. }
  211. void LightBase::advanceTime( F32 timeDelta )
  212. {
  213. if ( isMounted() )
  214. {
  215. MatrixF mat( true );
  216. mMount.object->getRenderMountTransform( 0.f, mMount.node, mMount.xfm, &mat );
  217. mLight->setTransform( mat );
  218. Parent::setTransform( mat );
  219. }
  220. }
  221. U32 LightBase::packUpdate( NetConnection *conn, U32 mask, BitStream *stream )
  222. {
  223. U32 retMask = Parent::packUpdate( conn, mask, stream );
  224. stream->writeFlag( mIsEnabled );
  225. if ( stream->writeFlag( mask & TransformMask ) )
  226. stream->writeAffineTransform( mObjToWorld );
  227. if ( stream->writeFlag( mask & UpdateMask ) )
  228. {
  229. stream->write( mColor );
  230. stream->write( mBrightness );
  231. stream->writeFlag( mCastShadows );
  232. stream->write(mStaticRefreshFreq);
  233. stream->write(mDynamicRefreshFreq);
  234. stream->write( mPriority );
  235. mLight->packExtended( stream );
  236. stream->writeFlag( mAnimState.active );
  237. stream->write( mAnimState.animationPeriod );
  238. stream->write( mAnimState.animationPhase );
  239. stream->write( mFlareScale );
  240. }
  241. if ( stream->writeFlag( mask & DatablockMask ) )
  242. {
  243. if ( stream->writeFlag( mAnimationData ) )
  244. {
  245. stream->writeRangedU32( mAnimationData->getId(),
  246. DataBlockObjectIdFirst,
  247. DataBlockObjectIdLast );
  248. }
  249. if ( stream->writeFlag( mFlareData ) )
  250. {
  251. stream->writeRangedU32( mFlareData->getId(),
  252. DataBlockObjectIdFirst,
  253. DataBlockObjectIdLast );
  254. }
  255. }
  256. return retMask;
  257. }
  258. void LightBase::unpackUpdate( NetConnection *conn, BitStream *stream )
  259. {
  260. Parent::unpackUpdate( conn, stream );
  261. mIsEnabled = stream->readFlag();
  262. if ( stream->readFlag() ) // TransformMask
  263. stream->readAffineTransform( &mObjToWorld );
  264. if ( stream->readFlag() ) // UpdateMask
  265. {
  266. stream->read( &mColor );
  267. stream->read( &mBrightness );
  268. mCastShadows = stream->readFlag();
  269. stream->read(&mStaticRefreshFreq);
  270. stream->read(&mDynamicRefreshFreq);
  271. stream->read( &mPriority );
  272. mLight->unpackExtended( stream );
  273. mAnimState.active = stream->readFlag();
  274. stream->read( &mAnimState.animationPeriod );
  275. stream->read( &mAnimState.animationPhase );
  276. stream->read( &mFlareScale );
  277. }
  278. if ( stream->readFlag() ) // DatablockMask
  279. {
  280. if ( stream->readFlag() )
  281. {
  282. SimObjectId id = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  283. LightAnimData *datablock = NULL;
  284. if ( Sim::findObject( id, datablock ) )
  285. mAnimationData = datablock;
  286. else
  287. {
  288. conn->setLastError( "Light::unpackUpdate() - invalid LightAnimData!" );
  289. mAnimationData = NULL;
  290. }
  291. }
  292. else
  293. mAnimationData = NULL;
  294. if ( stream->readFlag() )
  295. {
  296. SimObjectId id = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  297. LightFlareData *datablock = NULL;
  298. if ( Sim::findObject( id, datablock ) )
  299. mFlareData = datablock;
  300. else
  301. {
  302. conn->setLastError( "Light::unpackUpdate() - invalid LightCoronaData!" );
  303. mFlareData = NULL;
  304. }
  305. }
  306. else
  307. mFlareData = NULL;
  308. }
  309. if ( isProperlyAdded() )
  310. _conformLights();
  311. }
  312. void LightBase::setLightEnabled( bool enabled )
  313. {
  314. if ( mIsEnabled != enabled )
  315. {
  316. mIsEnabled = enabled;
  317. setMaskBits( EnabledMask );
  318. }
  319. }
  320. DefineEngineMethod( LightBase, setLightEnabled, void, ( bool state ),,
  321. "@brief Toggles the light on and off\n\n"
  322. "@param state Turns the light on (true) or off (false)\n"
  323. "@tsexample\n"
  324. "// Disable the light\n"
  325. "CrystalLight.setLightEnabled(false);\n\n"
  326. "// Renable the light\n"
  327. "CrystalLight.setLightEnabled(true);\n"
  328. "@endtsexample\n\n"
  329. )
  330. {
  331. object->setLightEnabled( state );
  332. }
  333. //ConsoleMethod( LightBase, setLightEnabled, void, 3, 3, "( bool enabled )\t"
  334. // "Toggles the light on and off." )
  335. //{
  336. // object->setLightEnabled( dAtob( argv[2] ) );
  337. //}
  338. static ConsoleDocFragment _lbplayAnimation1(
  339. "@brief Plays the light animation assigned to this light with the existing LightAnimData datablock.\n\n"
  340. "@tsexample\n"
  341. "// Play the animation assigned to this light\n"
  342. "CrystalLight.playAnimation();\n"
  343. "@endtsexample\n\n",
  344. "LightBase",
  345. "void playAnimation();"
  346. );
  347. static ConsoleDocFragment _lbplayAnimation2(
  348. "@brief Plays the light animation on this light using a new LightAnimData. If no LightAnimData "
  349. "is passed the existing one is played.\n\n"
  350. "@param anim Name of the LightAnimData datablock to be played\n\n"
  351. "@tsexample\n"
  352. "// Play the animation using a new LightAnimData datablock\n"
  353. "CrystalLight.playAnimation(SubtlePulseLightAnim);\n"
  354. "@endtsexample\n\n",
  355. "LightBase",
  356. "void playAnimation(LightAnimData anim);"
  357. );
  358. DefineEngineMethod( LightBase, playAnimation, void, (const char * anim), (""), "( [LightAnimData anim] )\t"
  359. "Plays a light animation on the light. If no LightAnimData is passed the "
  360. "existing one is played."
  361. "@hide")
  362. {
  363. if ( String::isEmpty(anim) )
  364. {
  365. object->playAnimation();
  366. return;
  367. }
  368. LightAnimData *animData;
  369. if ( !Sim::findObject( anim, animData ) )
  370. {
  371. Con::errorf( "LightBase::playAnimation() - Invalid LightAnimData '%s'.", anim );
  372. return;
  373. }
  374. // Play Animation.
  375. object->playAnimation( animData );
  376. }
  377. void LightBase::playAnimation( void )
  378. {
  379. if ( !mAnimState.active )
  380. {
  381. mAnimState.active = true;
  382. setMaskBits( UpdateMask );
  383. }
  384. }
  385. void LightBase::playAnimation( LightAnimData *animData )
  386. {
  387. // Play Animation.
  388. playAnimation();
  389. // Update Datablock?
  390. if ( mAnimationData != animData )
  391. {
  392. mAnimationData = animData;
  393. setMaskBits( DatablockMask );
  394. }
  395. }
  396. DefineEngineMethod( LightBase, pauseAnimation, void, (), , "Stops the light animation." )
  397. {
  398. object->pauseAnimation();
  399. }
  400. void LightBase::pauseAnimation( void )
  401. {
  402. if ( mAnimState.active )
  403. {
  404. mAnimState.active = false;
  405. setMaskBits( UpdateMask );
  406. }
  407. }