advancedLightManager.cpp 24 KB

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