lightInfo.cpp
Engine/source/lighting/lightInfo.cpp
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "lighting/lightInfo.h" 26 27#include "math/mMath.h" 28#include "core/color.h" 29#include "gfx/gfxCubemap.h" 30#include "console/simObject.h" 31#include "math/mathUtils.h" 32 33 34LightInfoExType::LightInfoExType( const char *type ) 35{ 36 TypeMap::Iterator iter = getTypeMap().find( type ); 37 if ( iter == getTypeMap().end() ) 38 iter = getTypeMap().insertUnique( type, getTypeMap().size() ); 39 40 mTypeIndex = iter->value; 41} 42 43 44LightInfo::LightInfo() 45 : mColor( 0.0f, 0.0f, 0.0f, 1.0f ), 46 mTransform( true ), 47 mBrightness( 1.0f ), 48 mAmbient( 0.0f, 0.0f, 0.0f, 1.0f ), 49 mRange( 1.0f, 1.0f, 1.0f ), 50 mInnerConeAngle( 90.0f ), 51 mType( Vector ), 52 mOuterConeAngle( 90.0f ), 53 mCastShadows( false ), 54 mStaticRefreshFreq( 250 ), 55 mDynamicRefreshFreq( 8 ), 56 mPriority( 1.0f ), 57 mScore( 0.0f ), 58 mFadeAmount(1.0f), 59 mDebugRender( false ) 60{ 61} 62 63LightInfo::~LightInfo() 64{ 65 deleteAllLightInfoEx(); 66} 67 68void LightInfo::set( const LightInfo *light ) 69{ 70 mTransform = light->mTransform; 71 mColor = light->mColor; 72 mBrightness = light->mBrightness; 73 mAmbient = light->mAmbient; 74 mRange = light->mRange; 75 mInnerConeAngle = light->mInnerConeAngle; 76 mOuterConeAngle = light->mOuterConeAngle; 77 mType = light->mType; 78 mCastShadows = light->mCastShadows; 79 mStaticRefreshFreq = light->mStaticRefreshFreq; 80 mDynamicRefreshFreq = light->mDynamicRefreshFreq; 81 82 for ( U32 i=0; i < mExtended.size(); i++ ) 83 { 84 LightInfoEx *ex = light->mExtended[ i ]; 85 if ( ex ) 86 mExtended[i]->set( ex ); 87 else 88 { 89 delete mExtended[i]; 90 mExtended[i] = NULL; 91 } 92 } 93} 94 95void LightInfo::setDirection( const VectorF &dir ) 96{ 97 MathUtils::getMatrixFromForwardVector( mNormalize( dir ), &mTransform ); 98} 99 100void LightInfo::deleteExtended( const LightInfoExType& type ) 101{ 102 if ( type >= mExtended.size() ) 103 return; 104 105 SAFE_DELETE( mExtended[ type ] ); 106} 107 108void LightInfo::deleteAllLightInfoEx() 109{ 110 for ( U32 i = 0; i < mExtended.size(); i++ ) 111 delete mExtended[ i ]; 112 113 mExtended.clear(); 114} 115 116LightInfoEx* LightInfo::getExtended( const LightInfoExType &type ) const 117{ 118 if ( type >= mExtended.size() ) 119 return NULL; 120 121 return mExtended[ type ]; 122} 123 124void LightInfo::addExtended( LightInfoEx *lightInfoEx ) 125{ 126 AssertFatal( lightInfoEx, "LightInfo::addExtended() - Got null extended light info!" ); 127 128 const LightInfoExType &type = lightInfoEx->getType(); 129 130 while ( mExtended.size() <= type ) 131 mExtended.push_back( NULL ); 132 133 delete mExtended[type]; 134 mExtended[type] = lightInfoEx; 135} 136 137void LightInfo::packExtended( BitStream *stream ) const 138{ 139 for ( U32 i = 0; i < mExtended.size(); i++ ) 140 if ( mExtended[ i ] ) 141 mExtended[ i ]->packUpdate( stream ); 142} 143 144void LightInfo::unpackExtended( BitStream *stream ) 145{ 146 for ( U32 i = 0; i < mExtended.size(); i++ ) 147 if ( mExtended[ i ] ) 148 mExtended[ i ]->unpackUpdate( stream ); 149} 150 151void LightInfo::getWorldToLightProj( MatrixF *outMatrix ) const 152{ 153 if ( mType == Spot ) 154 { 155 // For spots we need to include the cone projection. 156 F32 fov = mDegToRad( getOuterConeAngle() ); 157 F32 range = getRange().x; 158 MatrixF proj; 159 MathUtils::makeProjection( &proj, fov, 1.0f, range * 0.01f, range, true ); 160 161 MatrixF light = getTransform(); 162 light.inverse(); 163 164 *outMatrix = proj * light; 165 return; 166 } 167 else 168 { 169 // The other lights just use the light transform. 170 *outMatrix = getTransform(); 171 outMatrix->inverse(); 172 } 173} 174 175void LightInfoList::registerLight( LightInfo *light ) 176{ 177 if(!light) 178 return; 179 // just add the light, we'll try to scan for dupes later... 180 push_back(light); 181} 182 183void LightInfoList::unregisterLight( LightInfo *light ) 184{ 185 // remove all of them... 186 LightInfoList &list = *this; 187 for(U32 i=0; i<list.size(); i++) 188 { 189 if(list[i] != light) 190 continue; 191 192 // this moves last to i, which allows 193 // the search to continue forward... 194 list.erase_fast(i); 195 // want to check this location again... 196 i--; 197 } 198} 199