advancedLightManager.cpp 25 KB

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