advancedLightManager.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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/advanced/advancedLightManager.h"
  24. #include "lighting/advanced/advancedLightBinManager.h"
  25. #include "lighting/advanced/advancedLightingFeatures.h"
  26. #include "lighting/shadowMap/shadowMapManager.h"
  27. #include "lighting/shadowMap/lightShadowMap.h"
  28. #include "lighting/common/sceneLighting.h"
  29. #include "lighting/common/lightMapParams.h"
  30. #include "core/util/safeDelete.h"
  31. #include "renderInstance/renderDeferredMgr.h"
  32. #include "materials/materialManager.h"
  33. #include "math/util/sphereMesh.h"
  34. #include "console/consoleTypes.h"
  35. #include "console/engineAPI.h"
  36. #include "scene/sceneRenderState.h"
  37. #include "gfx/gfxCardProfile.h"
  38. #include "gfx/gfxTextureProfile.h"
  39. #ifndef TORQUE_BASIC_LIGHTING
  40. F32 AdvancedLightManager::smProjectedShadowFilterDistance = 40.0f;
  41. #endif
  42. ImplementEnumType( ShadowType,
  43. "\n\n"
  44. "@ingroup AdvancedLighting" )
  45. { ShadowType_Spot, "Spot" },
  46. { ShadowType_PSSM, "PSSM" },
  47. { ShadowType_Paraboloid, "Paraboloid" },
  48. { ShadowType_DualParaboloidSinglePass, "DualParaboloidSinglePass" },
  49. { ShadowType_DualParaboloid, "DualParaboloid" },
  50. { ShadowType_CubeMap, "CubeMap" },
  51. EndImplementEnumType;
  52. AdvancedLightManager AdvancedLightManager::smSingleton;
  53. AdvancedLightManager::AdvancedLightManager()
  54. : LightManager( "Advanced Lighting", "ADVLM" )
  55. {
  56. mLightBinManager = NULL;
  57. mLastShader = NULL;
  58. mAvailableSLInterfaces = NULL;
  59. }
  60. AdvancedLightManager::~AdvancedLightManager()
  61. {
  62. mLastShader = NULL;
  63. mLastConstants = NULL;
  64. for (LightConstantMap::Iterator i = mConstantLookup.begin(); i != mConstantLookup.end(); i++)
  65. {
  66. if (i->value)
  67. SAFE_DELETE(i->value);
  68. }
  69. mConstantLookup.clear();
  70. }
  71. bool AdvancedLightManager::isCompatible() const
  72. {
  73. // TODO: We need at least 3.0 shaders at the moment
  74. // but this should be relaxed to 2.0 soon.
  75. if ( GFX->getPixelShaderVersion() < 3.0 )
  76. return false;
  77. // TODO: Test for the necessary texture formats!
  78. bool autoMips;
  79. if(!GFX->getCardProfiler()->checkFormat(GFXFormatR16F, &GFXRenderTargetProfile, autoMips))
  80. return false;
  81. return true;
  82. }
  83. void AdvancedLightManager::activate( SceneManager *sceneManager )
  84. {
  85. Parent::activate( sceneManager );
  86. GFXShader::addGlobalMacro( "TORQUE_ADVANCED_LIGHTING" );
  87. sceneManager->setPostEffectFog( true );
  88. SHADOWMGR->activate();
  89. // Find a target format that supports blending...
  90. // we prefer the floating point format if it works.
  91. Vector<GFXFormat> formats;
  92. formats.push_back( GFXFormatR16G16B16A16F );
  93. //formats.push_back( GFXFormatR16G16B16A16 );
  94. GFXFormat blendTargetFormat = GFX->selectSupportedFormat( &GFXRenderTargetProfile,
  95. formats,
  96. true,
  97. true,
  98. false );
  99. // First look for the deferred bin...
  100. RenderDeferredMgr *deferredBin = _findDeferredRenderBin();
  101. // If we didn't find the deferred bin then add one.
  102. if (!deferredBin)
  103. {
  104. deferredBin = new RenderDeferredMgr(true, blendTargetFormat);
  105. deferredBin->assignName("AL_DeferredBin");
  106. deferredBin->registerObject();
  107. getSceneManager()->getDefaultRenderPass()->addManager(deferredBin);
  108. mDeferredRenderBin = deferredBin;
  109. }
  110. mLightBinManager = new AdvancedLightBinManager( this, SHADOWMGR, blendTargetFormat );
  111. mLightBinManager->assignName( "AL_LightBinMgr" );
  112. // Tell the material manager that deferred is enabled.
  113. MATMGR->setDeferredEnabled( true );
  114. // Insert our light bin manager.
  115. mLightBinManager->setRenderOrder( deferredBin->getRenderOrder() + 0.01f );
  116. getSceneManager()->getDefaultRenderPass()->addManager( mLightBinManager );
  117. AdvancedLightingFeatures::registerFeatures(mDeferredRenderBin->getTargetFormat(), blendTargetFormat);
  118. // Last thing... let everyone know we're active.
  119. smActivateSignal.trigger( getId(), true );
  120. }
  121. void AdvancedLightManager::deactivate()
  122. {
  123. Parent::deactivate();
  124. GFXShader::removeGlobalMacro( "TORQUE_ADVANCED_LIGHTING" );
  125. // Release our bin manager... it will take care of
  126. // removing itself from the render passes.
  127. if( mLightBinManager )
  128. {
  129. mLightBinManager->MRTLightmapsDuringDeferred(false);
  130. mLightBinManager->deleteObject();
  131. }
  132. mLightBinManager = NULL;
  133. if ( mDeferredRenderBin )
  134. mDeferredRenderBin->deleteObject();
  135. mDeferredRenderBin = NULL;
  136. SHADOWMGR->deactivate();
  137. mLastShader = NULL;
  138. mLastConstants = NULL;
  139. for (LightConstantMap::Iterator i = mConstantLookup.begin(); i != mConstantLookup.end(); i++)
  140. {
  141. if (i->value)
  142. SAFE_DELETE(i->value);
  143. }
  144. mConstantLookup.clear();
  145. mSphereGeometry = NULL;
  146. mSphereIndices = NULL;
  147. mConeGeometry = NULL;
  148. mConeIndices = NULL;
  149. AdvancedLightingFeatures::unregisterFeatures();
  150. // Now let everyone know we've deactivated.
  151. smActivateSignal.trigger( getId(), false );
  152. }
  153. void AdvancedLightManager::_addLightInfoEx( LightInfo *lightInfo )
  154. {
  155. lightInfo->addExtended( new ShadowMapParams( lightInfo ) );
  156. lightInfo->addExtended( new LightMapParams( lightInfo ) );
  157. }
  158. void AdvancedLightManager::_initLightFields()
  159. {
  160. #define DEFINE_LIGHT_FIELD( var, type, enum_ ) \
  161. static inline const char* _get##var##Field( void *obj, const char *data ) \
  162. { \
  163. ShadowMapParams *p = _getShadowMapParams( obj ); \
  164. if ( p ) \
  165. return Con::getData( type, &p->var, 0, enum_ ); \
  166. else \
  167. return ""; \
  168. } \
  169. \
  170. static inline bool _set##var##Field( void *object, const char *index, const char *data ) \
  171. { \
  172. ShadowMapParams *p = _getShadowMapParams( object ); \
  173. if ( p ) \
  174. { \
  175. Con::setData( type, &p->var, 0, 1, &data, enum_ ); \
  176. p->_validate(); \
  177. } \
  178. return false; \
  179. }
  180. #define DEFINE_LIGHTMAP_FIELD( var, type, enum_ ) \
  181. static inline const char* _get##var##Field( void *obj, const char *data ) \
  182. { \
  183. LightMapParams *p = _getLightMapParams( obj ); \
  184. if ( p ) \
  185. return Con::getData( type, &p->var, 0, enum_ ); \
  186. else \
  187. return ""; \
  188. } \
  189. \
  190. static inline bool _set##var##Field( void *object, const char *index, const char *data ) \
  191. { \
  192. LightMapParams *p = _getLightMapParams( object ); \
  193. if ( p ) \
  194. { \
  195. Con::setData( type, &p->var, 0, 1, &data, enum_ ); \
  196. } \
  197. return false; \
  198. }
  199. #define ADD_LIGHT_FIELD( field, type, var, desc ) \
  200. ConsoleObject::addProtectedField( field, type, 0, \
  201. &Dummy::_set##var##Field, &Dummy::_get##var##Field, desc )
  202. // Our dummy adaptor class which we hide in here
  203. // to keep from poluting the global namespace.
  204. class Dummy
  205. {
  206. protected:
  207. static inline ShadowMapParams* _getShadowMapParams( void *obj )
  208. {
  209. ISceneLight *sceneLight = dynamic_cast<ISceneLight*>( (SimObject*)obj );
  210. if ( sceneLight )
  211. {
  212. LightInfo *lightInfo = sceneLight->getLight();
  213. if ( lightInfo )
  214. return lightInfo->getExtended<ShadowMapParams>();
  215. }
  216. return NULL;
  217. }
  218. static inline LightMapParams* _getLightMapParams( void *obj )
  219. {
  220. ISceneLight *sceneLight = dynamic_cast<ISceneLight*>( (SimObject*)obj );
  221. if ( sceneLight )
  222. {
  223. LightInfo *lightInfo = sceneLight->getLight();
  224. if ( lightInfo )
  225. return lightInfo->getExtended<LightMapParams>();
  226. }
  227. return NULL;
  228. }
  229. public:
  230. DEFINE_LIGHT_FIELD( attenuationRatio, TypePoint3F, NULL );
  231. DEFINE_LIGHT_FIELD( shadowType, TYPEID< ShadowType >(), ConsoleBaseType::getType( TYPEID< ShadowType >() )->getEnumTable() );
  232. DEFINE_LIGHT_FIELD( texSize, TypeS32, NULL );
  233. DEFINE_LIGHT_FIELD( cookie, TypeStringFilename, NULL );
  234. DEFINE_LIGHT_FIELD( numSplits, TypeS32, NULL );
  235. DEFINE_LIGHT_FIELD( logWeight, TypeF32, NULL );
  236. DEFINE_LIGHT_FIELD( overDarkFactor, TypePoint4F, NULL);
  237. DEFINE_LIGHT_FIELD( shadowDistance, TypeF32, NULL );
  238. DEFINE_LIGHT_FIELD( shadowSoftness, TypeF32, NULL );
  239. DEFINE_LIGHT_FIELD( fadeStartDist, TypeF32, NULL );
  240. DEFINE_LIGHT_FIELD( lastSplitTerrainOnly, TypeBool, NULL );
  241. DEFINE_LIGHTMAP_FIELD( representedInLightmap, TypeBool, NULL );
  242. DEFINE_LIGHTMAP_FIELD( shadowDarkenColor, TypeColorF, NULL );
  243. DEFINE_LIGHTMAP_FIELD( includeLightmappedGeometryInShadow, TypeBool, NULL );
  244. };
  245. ConsoleObject::addGroup( "Advanced Lighting" );
  246. ADD_LIGHT_FIELD( "attenuationRatio", TypePoint3F, attenuationRatio,
  247. "The proportions of constant, linear, and quadratic attenuation to use for "
  248. "the falloff for point and spot lights." );
  249. ADD_LIGHT_FIELD( "shadowType", TYPEID< ShadowType >(), shadowType,
  250. "The type of shadow to use on this light." );
  251. ADD_LIGHT_FIELD( "cookie", TypeStringFilename, cookie,
  252. "A custom pattern texture which is projected from the light." );
  253. ADD_LIGHT_FIELD( "texSize", TypeS32, texSize,
  254. "The texture size of the shadow map." );
  255. ADD_LIGHT_FIELD( "overDarkFactor", TypePoint4F, overDarkFactor,
  256. "The ESM shadow darkening factor");
  257. ADD_LIGHT_FIELD( "shadowDistance", TypeF32, shadowDistance,
  258. "The distance from the camera to extend the PSSM shadow." );
  259. ADD_LIGHT_FIELD( "shadowSoftness", TypeF32, shadowSoftness,
  260. "" );
  261. ADD_LIGHT_FIELD( "numSplits", TypeS32, numSplits,
  262. "The logrithmic PSSM split distance factor." );
  263. ADD_LIGHT_FIELD( "logWeight", TypeF32, logWeight,
  264. "The logrithmic PSSM split distance factor." );
  265. ADD_LIGHT_FIELD( "fadeStartDistance", TypeF32, fadeStartDist,
  266. "Start fading shadows out at this distance. 0 = auto calculate this distance.");
  267. ADD_LIGHT_FIELD( "lastSplitTerrainOnly", TypeBool, lastSplitTerrainOnly,
  268. "This toggles only terrain being rendered to the last split of a PSSM shadow map.");
  269. ConsoleObject::endGroup( "Advanced Lighting" );
  270. ConsoleObject::addGroup( "Advanced Lighting Lightmap" );
  271. ADD_LIGHT_FIELD( "representedInLightmap", TypeBool, representedInLightmap,
  272. "This light is represented in lightmaps (static light, default: false)");
  273. ADD_LIGHT_FIELD( "shadowDarkenColor", TypeColorF, shadowDarkenColor,
  274. "The color that should be used to multiply-blend dynamic shadows onto lightmapped geometry (ignored if 'representedInLightmap' is false)");
  275. ADD_LIGHT_FIELD( "includeLightmappedGeometryInShadow", TypeBool, includeLightmappedGeometryInShadow,
  276. "This light should render lightmapped geometry during its shadow-map update (ignored if 'representedInLightmap' is false)");
  277. ConsoleObject::endGroup( "Advanced Lighting Lightmap" );
  278. #undef DEFINE_LIGHT_FIELD
  279. #undef ADD_LIGHT_FIELD
  280. }
  281. void AdvancedLightManager::setLightInfo( ProcessedMaterial *pmat,
  282. const Material *mat,
  283. const SceneData &sgData,
  284. const SceneRenderState *state,
  285. U32 pass,
  286. GFXShaderConstBuffer *shaderConsts)
  287. {
  288. // Skip this if we're rendering from the deferred bin.
  289. if ( sgData.binType == SceneData::DeferredBin )
  290. return;
  291. PROFILE_SCOPE(AdvancedLightManager_setLightInfo);
  292. LightingShaderConstants *lsc = getLightingShaderConstants(shaderConsts);
  293. LightShadowMap *lsm = SHADOWMGR->getCurrentShadowMap();
  294. LightShadowMap *dynamicShadowMap = SHADOWMGR->getCurrentDynamicShadowMap();
  295. LightInfo *light;
  296. if (lsm)
  297. light = lsm->getLightInfo();
  298. else if ( dynamicShadowMap )
  299. light = dynamicShadowMap->getLightInfo();
  300. else
  301. {
  302. light = sgData.lights[0];
  303. if ( !light )
  304. light = getDefaultLight();
  305. }
  306. // NOTE: If you encounter a crash from this point forward
  307. // while setting a shader constant its probably because the
  308. // mConstantLookup has bad shaders/constants in it.
  309. //
  310. // This is a known crash bug that can occur if materials/shaders
  311. // are reloaded and the light manager is not reset.
  312. //
  313. // We should look to fix this by clearing the table.
  314. // Update the forward shading light constants.
  315. _update4LightConsts( sgData,
  316. lsc->mLightPositionSC,
  317. lsc->mLightDiffuseSC,
  318. lsc->mLightAmbientSC,
  319. lsc->mLightInvRadiusSqSC,
  320. lsc->mLightSpotDirSC,
  321. lsc->mLightSpotAngleSC,
  322. lsc->mLightSpotFalloffSC,
  323. shaderConsts );
  324. // Static
  325. if (lsm && light->getCastShadows())
  326. {
  327. if ( lsc->mWorldToLightProjSC->isValid() )
  328. shaderConsts->set( lsc->mWorldToLightProjSC,
  329. lsm->getWorldToLightProj(),
  330. lsc->mWorldToLightProjSC->getType() );
  331. if ( lsc->mViewToLightProjSC->isValid() )
  332. {
  333. // TODO: Should probably cache these results and
  334. // not do this mul here on every material that needs
  335. // this transform.
  336. shaderConsts->set( lsc->mViewToLightProjSC,
  337. lsm->getWorldToLightProj() * state->getCameraTransform(),
  338. lsc->mViewToLightProjSC->getType() );
  339. }
  340. shaderConsts->setSafe( lsc->mShadowMapSizeSC, 1.0f / (F32)lsm->getTexSize() );
  341. // Do this last so that overrides can properly override parameters previously set
  342. lsm->setShaderParameters(shaderConsts, lsc);
  343. }
  344. else
  345. {
  346. if ( lsc->mViewToLightProjSC->isValid() )
  347. {
  348. // TODO: Should probably cache these results and
  349. // not do this mul here on every material that needs
  350. // this transform.
  351. MatrixF proj;
  352. light->getWorldToLightProj( &proj );
  353. shaderConsts->set( lsc->mViewToLightProjSC,
  354. proj * state->getCameraTransform(),
  355. lsc->mViewToLightProjSC->getType() );
  356. }
  357. }
  358. // Dynamic
  359. if ( dynamicShadowMap )
  360. {
  361. if ( lsc->mDynamicWorldToLightProjSC->isValid() )
  362. shaderConsts->set( lsc->mDynamicWorldToLightProjSC,
  363. dynamicShadowMap->getWorldToLightProj(),
  364. lsc->mDynamicWorldToLightProjSC->getType() );
  365. if ( lsc->mDynamicViewToLightProjSC->isValid() )
  366. {
  367. // TODO: Should probably cache these results and
  368. // not do this mul here on every material that needs
  369. // this transform.
  370. shaderConsts->set( lsc->mDynamicViewToLightProjSC,
  371. dynamicShadowMap->getWorldToLightProj() * state->getCameraTransform(),
  372. lsc->mDynamicViewToLightProjSC->getType() );
  373. }
  374. shaderConsts->setSafe( lsc->mShadowMapSizeSC, 1.0f / (F32)dynamicShadowMap->getTexSize() );
  375. // Do this last so that overrides can properly override parameters previously set
  376. dynamicShadowMap->setShaderParameters(shaderConsts, lsc);
  377. }
  378. else
  379. {
  380. if ( lsc->mDynamicViewToLightProjSC->isValid() )
  381. {
  382. // TODO: Should probably cache these results and
  383. // not do this mul here on every material that needs
  384. // this transform.
  385. MatrixF proj;
  386. light->getWorldToLightProj( &proj );
  387. shaderConsts->set( lsc->mDynamicViewToLightProjSC,
  388. proj * state->getCameraTransform(),
  389. lsc->mDynamicViewToLightProjSC->getType() );
  390. }
  391. }
  392. }
  393. void AdvancedLightManager::registerGlobalLight(LightInfo *light, SimObject *obj)
  394. {
  395. Parent::registerGlobalLight( light, obj );
  396. // Pass the volume lights to the bin manager.
  397. if ( mLightBinManager &&
  398. ( light->getType() == LightInfo::Point ||
  399. light->getType() == LightInfo::Spot ) )
  400. mLightBinManager->addLight( light );
  401. }
  402. void AdvancedLightManager::unregisterAllLights()
  403. {
  404. Parent::unregisterAllLights();
  405. if ( mLightBinManager )
  406. mLightBinManager->clearAllLights();
  407. }
  408. bool AdvancedLightManager::setTextureStage( const SceneData &sgData,
  409. const U32 currTexFlag,
  410. const U32 textureSlot,
  411. GFXShaderConstBuffer *shaderConsts,
  412. ShaderConstHandles *handles )
  413. {
  414. LightShadowMap* lsm = SHADOWMGR->getCurrentShadowMap();
  415. LightShadowMap* dynamicShadowMap = SHADOWMGR->getCurrentDynamicShadowMap();
  416. // Assign Shadowmap, if it exists
  417. LightingShaderConstants* lsc = getLightingShaderConstants(shaderConsts);
  418. if ( !lsc )
  419. return false;
  420. if ( currTexFlag == Material::DynamicLight )
  421. {
  422. // Static
  423. if ( lsm && lsm->getLightInfo()->getCastShadows() )
  424. return lsm->setTextureStage( currTexFlag, lsc );
  425. S32 reg = lsc->mShadowMapSC->getSamplerRegister();
  426. if ( reg != -1 )
  427. GFX->setTexture( reg, GFXTexHandle::ONE );
  428. return true;
  429. } else if ( currTexFlag == Material::DynamicShadowMap )
  430. {
  431. // Dynamic
  432. if ( dynamicShadowMap )
  433. return dynamicShadowMap->setTextureStage( currTexFlag, lsc );
  434. S32 reg = lsc->mDynamicShadowMapSC->getSamplerRegister();
  435. if ( reg != -1 )
  436. GFX->setTexture( reg, GFXTexHandle::ONE );
  437. return true;
  438. }
  439. else if ( currTexFlag == Material::DynamicLightMask )
  440. {
  441. S32 reg = lsc->mCookieMapSC->getSamplerRegister();
  442. if ( reg != -1 && sgData.lights[0] )
  443. {
  444. ShadowMapParams *p = sgData.lights[0]->getExtended<ShadowMapParams>();
  445. if ( lsc->mCookieMapSC->getType() == GFXSCT_SamplerCube )
  446. GFX->setCubeTexture( reg, p->getCookieCubeTex() );
  447. else
  448. GFX->setTexture( reg, p->getCookieTex() );
  449. }
  450. return true;
  451. }
  452. return false;
  453. }
  454. LightingShaderConstants* AdvancedLightManager::getLightingShaderConstants(GFXShaderConstBuffer* buffer)
  455. {
  456. if ( !buffer )
  457. return NULL;
  458. PROFILE_SCOPE( AdvancedLightManager_GetLightingShaderConstants );
  459. GFXShader* shader = buffer->getShader();
  460. // Check to see if this is the same shader, we'll get hit repeatedly by
  461. // the same one due to the render bin loops.
  462. if ( mLastShader.getPointer() != shader )
  463. {
  464. LightConstantMap::Iterator iter = mConstantLookup.find(shader);
  465. if ( iter != mConstantLookup.end() )
  466. {
  467. mLastConstants = iter->value;
  468. }
  469. else
  470. {
  471. LightingShaderConstants* lsc = new LightingShaderConstants();
  472. mConstantLookup[shader] = lsc;
  473. mLastConstants = lsc;
  474. }
  475. // Set our new shader
  476. mLastShader = shader;
  477. }
  478. // Make sure that our current lighting constants are initialized
  479. if (!mLastConstants->mInit)
  480. mLastConstants->init(shader);
  481. return mLastConstants;
  482. }
  483. GFXVertexBufferHandle<AdvancedLightManager::LightVertex> AdvancedLightManager::getSphereMesh(U32 &outNumPrimitives, GFXPrimitiveBuffer *&outPrimitives)
  484. {
  485. static SphereMesh sSphereMesh;
  486. if( mSphereGeometry.isNull() )
  487. {
  488. const SphereMesh::TriangleMesh * sphereMesh = sSphereMesh.getMesh(3);
  489. S32 numPoly = sphereMesh->numPoly;
  490. mSpherePrimitiveCount = 0;
  491. mSphereGeometry.set(GFX, numPoly*3, GFXBufferTypeStatic);
  492. mSphereGeometry.lock();
  493. S32 vertexIndex = 0;
  494. for (S32 i=0; i<numPoly; i++)
  495. {
  496. mSpherePrimitiveCount++;
  497. mSphereGeometry[vertexIndex].point = sphereMesh->poly[i].pnt[0];
  498. mSphereGeometry[vertexIndex].color = ColorI::WHITE;
  499. vertexIndex++;
  500. mSphereGeometry[vertexIndex].point = sphereMesh->poly[i].pnt[1];
  501. mSphereGeometry[vertexIndex].color = ColorI::WHITE;
  502. vertexIndex++;
  503. mSphereGeometry[vertexIndex].point = sphereMesh->poly[i].pnt[2];
  504. mSphereGeometry[vertexIndex].color = ColorI::WHITE;
  505. vertexIndex++;
  506. }
  507. mSphereGeometry.unlock();
  508. }
  509. outNumPrimitives = mSpherePrimitiveCount;
  510. outPrimitives = NULL; // For now
  511. return mSphereGeometry;
  512. }
  513. GFXVertexBufferHandle<AdvancedLightManager::LightVertex> AdvancedLightManager::getConeMesh(U32 &outNumPrimitives, GFXPrimitiveBuffer *&outPrimitives )
  514. {
  515. static const Point2F circlePoints[] =
  516. {
  517. Point2F(0.707107f, 0.707107f),
  518. Point2F(0.923880f, 0.382683f),
  519. Point2F(1.000000f, 0.000000f),
  520. Point2F(0.923880f, -0.382684f),
  521. Point2F(0.707107f, -0.707107f),
  522. Point2F(0.382683f, -0.923880f),
  523. Point2F(0.000000f, -1.000000f),
  524. Point2F(-0.382683f, -0.923880f),
  525. Point2F(-0.707107f, -0.707107f),
  526. Point2F(-0.923880f, -0.382684f),
  527. Point2F(-1.000000f, 0.000000f),
  528. Point2F(-0.923879f, 0.382684f),
  529. Point2F(-0.707107f, 0.707107f),
  530. Point2F(-0.382683f, 0.923880f),
  531. Point2F(0.000000f, 1.000000f),
  532. Point2F(0.382684f, 0.923879f)
  533. };
  534. const S32 numPoints = sizeof(circlePoints)/sizeof(Point2F);
  535. if ( mConeGeometry.isNull() )
  536. {
  537. mConeGeometry.set(GFX, numPoints + 1, GFXBufferTypeStatic);
  538. mConeGeometry.lock();
  539. mConeGeometry[0].point = Point3F(0.0f,0.0f,0.0f);
  540. for (S32 i=1; i<numPoints + 1; i++)
  541. {
  542. S32 imod = (i - 1) % numPoints;
  543. mConeGeometry[i].point = Point3F(circlePoints[imod].x,circlePoints[imod].y, -1.0f);
  544. mConeGeometry[i].color = ColorI::WHITE;
  545. }
  546. mConeGeometry.unlock();
  547. mConePrimitiveCount = numPoints * 2 - 1;
  548. // Now build the index buffer
  549. mConeIndices.set(GFX, mConePrimitiveCount * 3, mConePrimitiveCount, GFXBufferTypeStatic);
  550. U16 *idx = NULL;
  551. mConeIndices.lock( &idx );
  552. // Build the cone
  553. U32 idxIdx = 0;
  554. for( U32 i = 1; i < numPoints + 1; i++ )
  555. {
  556. idx[idxIdx++] = 0; // Triangles on cone start at top point
  557. idx[idxIdx++] = i;
  558. idx[idxIdx++] = ( i + 1 > numPoints ) ? 1 : i + 1;
  559. }
  560. // Build the bottom of the cone (reverse winding order)
  561. for( U32 i = 1; i < numPoints - 1; i++ )
  562. {
  563. idx[idxIdx++] = 1;
  564. idx[idxIdx++] = i + 2;
  565. idx[idxIdx++] = i + 1;
  566. }
  567. mConeIndices.unlock();
  568. }
  569. outNumPrimitives = mConePrimitiveCount;
  570. outPrimitives = mConeIndices.getPointer();
  571. return mConeGeometry;
  572. }
  573. LightShadowMap* AdvancedLightManager::findShadowMapForObject( SimObject *object )
  574. {
  575. if ( !object )
  576. return NULL;
  577. ISceneLight *sceneLight = dynamic_cast<ISceneLight*>( object );
  578. if ( !sceneLight || !sceneLight->getLight() )
  579. return NULL;
  580. return sceneLight->getLight()->getExtended<ShadowMapParams>()->getShadowMap();
  581. }
  582. DefineEngineFunction( setShadowVizLight, const char*, (const char* name), (""), "")
  583. {
  584. static const String DebugTargetName( "AL_ShadowVizTexture" );
  585. NamedTexTarget *target = NamedTexTarget::find( DebugTargetName );
  586. if ( target )
  587. target->unregister();
  588. AdvancedLightManager *lm = dynamic_cast<AdvancedLightManager*>( LIGHTMGR );
  589. if ( !lm )
  590. return 0;
  591. SimObject *object;
  592. Sim::findObject( name, object );
  593. LightShadowMap *lightShadowMap = lm->findShadowMapForObject( object );
  594. if ( !lightShadowMap || !lightShadowMap->getTexture() )
  595. return 0;
  596. lightShadowMap->setDebugTarget( DebugTargetName );
  597. GFXTextureObject *texObject = lightShadowMap->getTexture();
  598. const Point3I &size = texObject->getSize();
  599. F32 aspect = (F32)size.x / (F32)size.y;
  600. static const U32 bufSize = 64;
  601. char *result = Con::getReturnBuffer( bufSize );
  602. dSprintf( result, bufSize, "%d %d %g", size.x, size.y, aspect );
  603. return result;
  604. }