lightInfo.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "lighting/lightInfo.h"
  24. #include "math/mMath.h"
  25. #include "core/color.h"
  26. #include "gfx/gfxCubemap.h"
  27. #include "console/simObject.h"
  28. #include "math/mathUtils.h"
  29. LightInfoExType::LightInfoExType( const char *type )
  30. {
  31. TypeMap::Iterator iter = getTypeMap().find( type );
  32. if ( iter == getTypeMap().end() )
  33. iter = getTypeMap().insertUnique( type, getTypeMap().size() );
  34. mTypeIndex = iter->value;
  35. }
  36. LightInfo::LightInfo()
  37. : mColor( 0.0f, 0.0f, 0.0f, 1.0f ),
  38. mTransform( true ),
  39. mBrightness( 1.0f ),
  40. mAmbient( 0.0f, 0.0f, 0.0f, 1.0f ),
  41. mRange( 1.0f, 1.0f, 1.0f ),
  42. mInnerConeAngle( 90.0f ),
  43. mType( Vector ),
  44. mOuterConeAngle( 90.0f ),
  45. mCastShadows( false ),
  46. mStaticRefreshFreq( 250 ),
  47. mDynamicRefreshFreq( 8 ),
  48. mPriority( 1.0f ),
  49. mScore( 0.0f ),
  50. mFadeAmount(1.0f),
  51. mDebugRender( false )
  52. {
  53. }
  54. LightInfo::~LightInfo()
  55. {
  56. deleteAllLightInfoEx();
  57. }
  58. void LightInfo::set( const LightInfo *light )
  59. {
  60. mTransform = light->mTransform;
  61. mColor = light->mColor;
  62. mBrightness = light->mBrightness;
  63. mAmbient = light->mAmbient;
  64. mRange = light->mRange;
  65. mInnerConeAngle = light->mInnerConeAngle;
  66. mOuterConeAngle = light->mOuterConeAngle;
  67. mType = light->mType;
  68. mCastShadows = light->mCastShadows;
  69. mStaticRefreshFreq = light->mStaticRefreshFreq;
  70. mDynamicRefreshFreq = light->mDynamicRefreshFreq;
  71. for ( U32 i=0; i < mExtended.size(); i++ )
  72. {
  73. LightInfoEx *ex = light->mExtended[ i ];
  74. if ( ex )
  75. mExtended[i]->set( ex );
  76. else
  77. {
  78. delete mExtended[i];
  79. mExtended[i] = NULL;
  80. }
  81. }
  82. }
  83. void LightInfo::setDirection( const VectorF &dir )
  84. {
  85. MathUtils::getMatrixFromForwardVector( mNormalize( dir ), &mTransform );
  86. }
  87. void LightInfo::deleteExtended( const LightInfoExType& type )
  88. {
  89. if ( type >= mExtended.size() )
  90. return;
  91. SAFE_DELETE( mExtended[ type ] );
  92. }
  93. void LightInfo::deleteAllLightInfoEx()
  94. {
  95. for ( U32 i = 0; i < mExtended.size(); i++ )
  96. delete mExtended[ i ];
  97. mExtended.clear();
  98. }
  99. LightInfoEx* LightInfo::getExtended( const LightInfoExType &type ) const
  100. {
  101. if ( type >= mExtended.size() )
  102. return NULL;
  103. return mExtended[ type ];
  104. }
  105. void LightInfo::addExtended( LightInfoEx *lightInfoEx )
  106. {
  107. AssertFatal( lightInfoEx, "LightInfo::addExtended() - Got null extended light info!" );
  108. const LightInfoExType &type = lightInfoEx->getType();
  109. while ( mExtended.size() <= type )
  110. mExtended.push_back( NULL );
  111. delete mExtended[type];
  112. mExtended[type] = lightInfoEx;
  113. }
  114. void LightInfo::packExtended( BitStream *stream ) const
  115. {
  116. for ( U32 i = 0; i < mExtended.size(); i++ )
  117. if ( mExtended[ i ] )
  118. mExtended[ i ]->packUpdate( stream );
  119. }
  120. void LightInfo::unpackExtended( BitStream *stream )
  121. {
  122. for ( U32 i = 0; i < mExtended.size(); i++ )
  123. if ( mExtended[ i ] )
  124. mExtended[ i ]->unpackUpdate( stream );
  125. }
  126. void LightInfo::getWorldToLightProj( MatrixF *outMatrix ) const
  127. {
  128. if ( mType == Spot )
  129. {
  130. // For spots we need to include the cone projection.
  131. F32 fov = mDegToRad( getOuterConeAngle() );
  132. F32 range = getRange().x;
  133. MatrixF proj;
  134. MathUtils::makeProjection( &proj, fov, 1.0f, range * 0.01f, range, true );
  135. MatrixF light = getTransform();
  136. light.inverse();
  137. *outMatrix = proj * light;
  138. return;
  139. }
  140. else
  141. {
  142. // The other lights just use the light transform.
  143. *outMatrix = getTransform();
  144. outMatrix->inverse();
  145. }
  146. }
  147. void LightInfoList::registerLight( LightInfo *light )
  148. {
  149. if(!light)
  150. return;
  151. // just add the light, we'll try to scan for dupes later...
  152. push_back(light);
  153. }
  154. void LightInfoList::unregisterLight( LightInfo *light )
  155. {
  156. // remove all of them...
  157. LightInfoList &list = *this;
  158. for(U32 i=0; i<list.size(); i++)
  159. {
  160. if(list[i] != light)
  161. continue;
  162. // this moves last to i, which allows
  163. // the search to continue forward...
  164. list.erase_fast(i);
  165. // want to check this location again...
  166. i--;
  167. }
  168. }