pointLight.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/pointLight.h"
  24. #include "console/consoleTypes.h"
  25. #include "core/stream/bitStream.h"
  26. #include "gfx/gfxDrawUtil.h"
  27. IMPLEMENT_CO_NETOBJECT_V1( PointLight );
  28. ConsoleDocClass( PointLight,
  29. "@brief Lighting object that radiates light in all directions.\n\n"
  30. "PointLight is one of the two types of lighting objects that can be added "
  31. "to a Torque 3D level, the other being SpotLight. Unlike directional or conical light, "
  32. "the PointLight emits lighting in all directions. The attenuation is controlled "
  33. "by a single variable: LightObject::radius.\n\n"
  34. "@tsexample\n"
  35. "// Declaration of a point light in script, or created by World Editor\n"
  36. "new PointLight(CrystalLight)\n"
  37. "{\n"
  38. " radius = \"10\";\n"
  39. " isEnabled = \"1\";\n"
  40. " color = \"1 0.905882 0 1\";\n"
  41. " brightness = \"0.5\";\n"
  42. " castShadows = \"1\";\n"
  43. " priority = \"1\";\n"
  44. " animate = \"1\";\n"
  45. " animationType = \"SubtlePulseLightAnim\";\n"
  46. " animationPeriod = \"3\";\n"
  47. " animationPhase = \"3\";\n"
  48. " flareScale = \"1\";\n"
  49. " attenuationRatio = \"0 1 1\";\n"
  50. " shadowType = \"DualParaboloidSinglePass\";\n"
  51. " texSize = \"512\";\n"
  52. " overDarkFactor = \"2000 1000 500 100\";\n"
  53. " shadowDistance = \"400\";\n"
  54. " shadowSoftness = \"0.15\";\n"
  55. " numSplits = \"1\";\n"
  56. " logWeight = \"0.91\";\n"
  57. " fadeStartDistance = \"0\";\n"
  58. " lastSplitTerrainOnly = \"0\";\n"
  59. " splitFadeDistances = \"10 20 30 40\";\n"
  60. " representedInLightmap = \"0\";\n"
  61. " shadowDarkenColor = \"0 0 0 -1\";\n"
  62. " includeLightmappedGeometryInShadow = \"1\";\n"
  63. " position = \"-61.3866 1.69186 5.1464\";\n"
  64. " rotation = \"1 0 0 0\";\n"
  65. "};\n"
  66. "@endtsexample\n\n"
  67. "@see LightBase\n\n"
  68. "@see SpotLight\n\n"
  69. "@ingroup Lighting\n"
  70. );
  71. PointLight::PointLight()
  72. : mRadius( 5.0f )
  73. {
  74. // We set the type here to ensure the extended
  75. // parameter validation works when setting fields.
  76. mLight->setType( LightInfo::Point );
  77. }
  78. PointLight::~PointLight()
  79. {
  80. }
  81. void PointLight::initPersistFields()
  82. {
  83. addGroup( "Light" );
  84. addField( "radius", TypeF32, Offset( mRadius, PointLight ), "Controls the falloff of the light emission" );
  85. endGroup( "Light" );
  86. // We do the parent fields at the end so that
  87. // they show up that way in the inspector.
  88. Parent::initPersistFields();
  89. // Remove the scale field... it's already
  90. // defined by the light radius.
  91. removeField( "scale" );
  92. }
  93. void PointLight::_conformLights()
  94. {
  95. mLight->setTransform( getRenderTransform() );
  96. mLight->setRange( mRadius );
  97. mLight->setColor( mColor );
  98. mLight->setBrightness( mBrightness );
  99. mLight->setCastShadows( mCastShadows );
  100. mLight->setPriority( mPriority );
  101. // Update the bounds and scale to fit our light.
  102. mObjBox.minExtents.set( -1, -1, -1 );
  103. mObjBox.maxExtents.set( 1, 1, 1 );
  104. mObjScale.set( mRadius, mRadius, mRadius );
  105. // Skip our transform... it just dirties mask bits.
  106. Parent::setTransform( mObjToWorld );
  107. }
  108. U32 PointLight::packUpdate(NetConnection *conn, U32 mask, BitStream *stream )
  109. {
  110. if ( stream->writeFlag( mask & UpdateMask ) )
  111. stream->write( mRadius );
  112. return Parent::packUpdate( conn, mask, stream );
  113. }
  114. void PointLight::unpackUpdate( NetConnection *conn, BitStream *stream )
  115. {
  116. if ( stream->readFlag() ) // UpdateMask
  117. stream->read( &mRadius );
  118. Parent::unpackUpdate( conn, stream );
  119. }
  120. void PointLight::setScale( const VectorF &scale )
  121. {
  122. // Use the average of the three coords.
  123. mRadius = ( scale.x + scale.y + scale.z ) / 3.0f;
  124. // We changed our settings so notify the client.
  125. setMaskBits( UpdateMask );
  126. // Let the parent do the final scale.
  127. Parent::setScale( VectorF( mRadius, mRadius, mRadius ) );
  128. }
  129. void PointLight::_renderViz( SceneRenderState *state )
  130. {
  131. GFXDrawUtil *draw = GFX->getDrawUtil();
  132. GFXStateBlockDesc desc;
  133. desc.setZReadWrite( true, false );
  134. desc.setCullMode( GFXCullNone );
  135. desc.setBlend( true );
  136. // Base the sphere color on the light color.
  137. ColorI color( mColor );
  138. color.alpha = 16;
  139. draw->drawSphere( desc, mRadius, getPosition(), color );
  140. }