advancedLightManager.cpp 24 KB

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