pointLight.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. docsURL;
  91. addGroup( "Light" );
  92. addField( "radius", TypeF32, Offset( mRadius, PointLight ), "Controls the falloff of the light emission" );
  93. endGroup( "Light" );
  94. // We do the parent fields at the end so that
  95. // they show up that way in the inspector.
  96. Parent::initPersistFields();
  97. // Remove the scale field... it's already
  98. // defined by the light radius.
  99. removeField( "scale" );
  100. //These are particular fields for PSSM, so useless for point lights
  101. removeField("numSplits");
  102. removeField("logWeight");
  103. removeField("lastSplitTerrainOnly");
  104. }
  105. void PointLight::_conformLights()
  106. {
  107. mLight->setTransform( getRenderTransform() );
  108. mLight->setRange( mRadius );
  109. mLight->setColor( mColor );
  110. mLight->setBrightness( mBrightness );
  111. mLight->setCastShadows( mCastShadows );
  112. mLight->setStaticRefreshFreq(mStaticRefreshFreq);
  113. mLight->setDynamicRefreshFreq(mDynamicRefreshFreq);
  114. mLight->setPriority( mPriority );
  115. // Update the bounds and scale to fit our light.
  116. mObjBox.minExtents.set( -1, -1, -1 );
  117. mObjBox.maxExtents.set( 1, 1, 1 );
  118. mObjScale.set( mRadius, mRadius, mRadius );
  119. // Skip our transform... it just dirties mask bits.
  120. Parent::setTransform( mObjToWorld );
  121. }
  122. U32 PointLight::packUpdate(NetConnection *conn, U32 mask, BitStream *stream )
  123. {
  124. if ( stream->writeFlag( mask & UpdateMask ) )
  125. stream->write( mRadius );
  126. return Parent::packUpdate( conn, mask, stream );
  127. }
  128. void PointLight::unpackUpdate( NetConnection *conn, BitStream *stream )
  129. {
  130. if ( stream->readFlag() ) // UpdateMask
  131. stream->read( &mRadius );
  132. Parent::unpackUpdate( conn, stream );
  133. }
  134. void PointLight::setScale( const VectorF &scale )
  135. {
  136. // Use the average of the three coords.
  137. mRadius = ( scale.x + scale.y + scale.z ) / 3.0f;
  138. // We changed our settings so notify the client.
  139. setMaskBits( UpdateMask );
  140. // Let the parent do the final scale.
  141. Parent::setScale( VectorF( mRadius, mRadius, mRadius ) );
  142. }
  143. void PointLight::_renderViz( SceneRenderState *state )
  144. {
  145. GFXDrawUtil *draw = GFX->getDrawUtil();
  146. GFXStateBlockDesc desc;
  147. desc.setZReadWrite( true, false );
  148. desc.setCullMode( GFXCullNone );
  149. desc.setBlend( true );
  150. // Base the sphere color on the light color.
  151. ColorI color = mColor.toColorI();
  152. color.alpha = 16;
  153. draw->drawSphere( desc, mRadius, getPosition(), color );
  154. }