advancedLightManager.cpp 25 KB

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