advancedLightManager.cpp 26 KB

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