lightManager.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #ifndef _LIGHTMANAGER_H_
  23. #define _LIGHTMANAGER_H_
  24. #ifndef _TORQUE_STRING_H_
  25. #include "core/util/str.h"
  26. #endif
  27. #ifndef _TSIGNAL_H_
  28. #include "core/util/tSignal.h"
  29. #endif
  30. #ifndef _LIGHTINFO_H_
  31. #include "lighting/lightInfo.h"
  32. #endif
  33. #ifndef _LIGHTQUERY_H_
  34. #include "lighting/lightQuery.h"
  35. #endif
  36. class SimObject;
  37. class LightManager;
  38. class Material;
  39. class ProcessedMaterial;
  40. class SceneManager;
  41. struct SceneData;
  42. class Point3F;
  43. class AvailableSLInterfaces;
  44. class SceneObject;
  45. class GFXShaderConstBuffer;
  46. class GFXShaderConstHandle;
  47. class ShaderConstHandles;
  48. class SceneRenderState;
  49. class RenderDeferredMgr;
  50. class Frustum;
  51. ///
  52. typedef Map<String,LightManager*> LightManagerMap;
  53. class LightManager
  54. {
  55. public:
  56. enum SpecialLightTypesEnum
  57. {
  58. slSunLightType,
  59. slSpecialLightTypesCount
  60. };
  61. LightManager( const char *name, const char *id );
  62. virtual ~LightManager();
  63. ///
  64. static void initLightFields();
  65. ///
  66. static LightInfo* createLightInfo(LightInfo* light = NULL);
  67. ///
  68. static LightManager* findByName( const char *name );
  69. /// Returns a tab seperated list of available light managers.
  70. static void getLightManagerNames( String *outString );
  71. /// The light manager activation signal.
  72. static Signal<void(const char*,bool)> smActivateSignal;
  73. /// Returns the active LM.
  74. static inline LightManager* getActiveLM() { return smActiveLM; }
  75. /// Return an id string used to load different versions of light manager
  76. /// specific assets. It shoud be short, contain no spaces, and be safe
  77. /// for filename use.
  78. const char* getName() const { return mName.c_str(); }
  79. /// Return an id string used to load different versions of light manager
  80. /// specific assets. It shoud be short, contain no spaces, and be safe
  81. /// for filename use.
  82. const char* getId() const { return mId.c_str(); }
  83. // Returns the scene manager passed at activation.
  84. SceneManager* getSceneManager() { return mSceneManager; }
  85. // Should return true if this light manager is compatible
  86. // on the current platform and GFX device.
  87. virtual bool isCompatible() const = 0;
  88. // Called when the lighting manager should become active
  89. virtual void activate( SceneManager *sceneManager );
  90. // Called when we don't want the light manager active (should clean up)
  91. virtual void deactivate();
  92. // Returns the active scene lighting interface for this light manager.
  93. virtual AvailableSLInterfaces* getSceneLightingInterface();
  94. // Returns a "default" light info that callers should not free. Used for instances where we don't actually care about
  95. // the light (for example, setting default data for SceneData)
  96. virtual LightInfo* getDefaultLight();
  97. /// Returns the special light or the default light if useDefault is true.
  98. /// @see getDefaultLight
  99. virtual LightInfo* getSpecialLight( SpecialLightTypesEnum type,
  100. bool useDefault = true );
  101. /// Set a special light type.
  102. virtual void setSpecialLight( SpecialLightTypesEnum type, LightInfo *light );
  103. // registered before scene traversal...
  104. virtual void registerGlobalLight( LightInfo *light, SimObject *obj );
  105. virtual void unregisterGlobalLight( LightInfo *light );
  106. // registered per object...
  107. virtual void registerLocalLight( LightInfo *light );
  108. virtual void unregisterLocalLight( LightInfo *light );
  109. virtual void registerGlobalLights( const Frustum *frustum, bool staticlighting, bool enableZoneLightCulling = false );
  110. virtual void unregisterAllLights();
  111. /// Returns all unsorted and un-scored lights (both global and local).
  112. void getAllUnsortedLights( Vector<LightInfo*> *list ) const;
  113. /// Sets shader constants / textures for light infos
  114. virtual void setLightInfo( ProcessedMaterial *pmat,
  115. const Material *mat,
  116. const SceneData &sgData,
  117. const SceneRenderState *state,
  118. U32 pass,
  119. GFXShaderConstBuffer *shaderConsts ) = 0;
  120. /// Allows us to set textures during the Material::setTextureStage call, return true if we've done work.
  121. virtual bool setTextureStage( const SceneData &sgData,
  122. const U32 currTexFlag,
  123. const U32 textureSlot,
  124. GFXShaderConstBuffer *shaderConsts,
  125. ShaderConstHandles *handles ) = 0;
  126. /// Called when the static scene lighting (aka lightmaps) should be computed.
  127. virtual bool lightScene( const char* callback, const char* param );
  128. /// Returns true if this light manager is active
  129. virtual bool isActive() const { return mIsActive; }
  130. protected:
  131. /// The current active light manager.
  132. static LightManager *smActiveLM;
  133. /// Find the pre-pass render bin on the scene's default render pass.
  134. RenderDeferredMgr* _findDeferredRenderBin();
  135. /// This helper function sets the shader constansts
  136. /// for the stock 4 light forward lighting code.
  137. static void _update4LightConsts( const SceneData &sgData,
  138. GFXShaderConstHandle *lightPositionSC,
  139. GFXShaderConstHandle *lightDiffuseSC,
  140. GFXShaderConstHandle *lightAmbientSC,
  141. GFXShaderConstHandle *lightInvRadiusSqSC,
  142. GFXShaderConstHandle *lightSpotDirSC,
  143. GFXShaderConstHandle * lightSpotParamsSC,
  144. GFXShaderConstHandle* hasVectorLightSC,
  145. GFXShaderConstHandle* vectorLightDirectionSC,
  146. GFXShaderConstHandle* vectorLightColorSC,
  147. GFXShaderConstHandle* vectorLightBrightnessSC,
  148. GFXShaderConstBuffer *shaderConsts );
  149. /// A dummy default light used when no lights
  150. /// happen to be registered with the manager.
  151. LightInfo *mDefaultLight;
  152. /// The list of global registered lights which is
  153. /// initialized before the scene is rendered.
  154. LightInfoList mRegisteredLights;
  155. /// The registered special light list.
  156. LightInfo *mSpecialLights[slSpecialLightTypesCount];
  157. /// The root culling position used for
  158. /// special sun light placement.
  159. /// @see setSpecialLight
  160. Point3F mCullPos;
  161. /// The scene lighting interfaces for
  162. /// lightmap generation.
  163. AvailableSLInterfaces *mAvailableSLInterfaces;
  164. /// Attaches any LightInfoEx data for this manager
  165. /// to the light info object.
  166. virtual void _addLightInfoEx( LightInfo *lightInfo ) = 0;
  167. ///
  168. virtual void _initLightFields() = 0;
  169. /// Returns the static light manager map.
  170. static LightManagerMap& _getLightManagers();
  171. /// The constant light manager name initialized
  172. /// in the constructor.
  173. const String mName;
  174. /// The constant light manager identifier initialized
  175. /// in the constructor.
  176. const String mId;
  177. /// Is true if this light manager has been activated.
  178. bool mIsActive;
  179. /// The scene graph the light manager is associated with.
  180. SceneManager *mSceneManager;
  181. };
  182. /// Returns the current active light manager.
  183. #define LIGHTMGR LightManager::getActiveLM()
  184. #endif // _LIGHTMANAGER_H_