advancedLightManager.cpp 24 KB

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