pointLight.cpp 6.1 KB

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