sceneLighting.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 _SCENELIGHTING_H_
  23. #define _SCENELIGHTING_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _SGSCENEPERSIST_H_
  28. #include "lighting/common/scenePersist.h"
  29. #endif
  30. #ifndef _LIGHTINFO_H_
  31. #include "lighting/lightInfo.h"
  32. #endif
  33. class ShadowVolumeBSP;
  34. class LightInfo;
  35. class AvailableSLInterfaces;
  36. class SceneLighting : public SimObject
  37. {
  38. typedef SimObject Parent;
  39. protected:
  40. AvailableSLInterfaces* mLightingInterfaces;
  41. virtual void getMLName(const char* misName, const U32 missionCRC, const U32 buffSize, char* filenameBuffer);
  42. public:
  43. S32 sgTimeTemp;
  44. S32 sgTimeTemp2;
  45. virtual void sgNewEvent(U32 light, S32 object, U32 event);
  46. virtual void sgLightingStartEvent();
  47. virtual void sgLightingCompleteEvent();
  48. virtual void sgTGEPassSetupEvent();
  49. virtual void sgTGELightStartEvent(U32 light);
  50. virtual void sgTGELightProcessEvent(U32 light, S32 object);
  51. virtual void sgTGELightCompleteEvent(U32 light);
  52. virtual void sgTGESetProgress(U32 light, S32 object);
  53. virtual void sgSGPassSetupEvent();
  54. virtual void sgSGObjectStartEvent(S32 object);
  55. virtual void sgSGObjectProcessEvent(U32 light, S32 object);
  56. virtual void sgSGObjectCompleteEvent(S32 object);
  57. virtual void sgSGSetProgress(U32 light, S32 object);
  58. // 'sg' prefix omitted to conform with existing 'addInterior' method...
  59. void addStatic(ShadowVolumeBSP *shadowVolume, SceneObject *sceneobject, LightInfo *light, S32 level);
  60. // persist objects moved to 'sgScenePersist.h' for clarity...
  61. // everything below this line should be original code...
  62. U32 calcMissionCRC();
  63. bool verifyMissionInfo(PersistInfo::PersistChunk *);
  64. bool getMissionInfo(PersistInfo::PersistChunk *);
  65. bool loadPersistInfo(const char *);
  66. bool savePersistInfo(const char *);
  67. class ObjectProxy;
  68. enum {
  69. SHADOW_DETAIL = -1
  70. };
  71. //------------------------------------------------------------------------------
  72. /// Create a proxy for each object to store data.
  73. class ObjectProxy
  74. {
  75. public:
  76. SimObjectPtr<SceneObject> mObj;
  77. U32 mChunkCRC;
  78. ObjectProxy(SceneObject * obj) : mObj(obj){mChunkCRC = 0;}
  79. virtual ~ObjectProxy(){}
  80. SceneObject * operator->() {return(mObj);}
  81. SceneObject * getObject() {return(mObj);}
  82. /// @name Lighting Interface
  83. /// @{
  84. virtual bool loadResources() {return(true);}
  85. virtual void init() {}
  86. virtual bool tgePreLight(LightInfo* light) { return preLight(light); }
  87. virtual bool preLight(LightInfo *) {return(false);}
  88. virtual void light(LightInfo *) {}
  89. virtual void postLight(bool lastLight) {}
  90. /// @}
  91. /// @name Lighting events
  92. /// @{
  93. // Called when the lighting process begins
  94. virtual void processLightingStart() {}
  95. // Called when a TGELight event is started, return true if status has been reported to console
  96. virtual void processTGELightProcessEvent(U32 curr, U32 max, LightInfo*) { Con::printf(" Lighting object %d of %d...", (curr+1), max); }
  97. // Called for lighting kit lights
  98. virtual bool processStartObjectLightingEvent(U32 current, U32 max) { Con::printf(" Lighting object %d of %d... %s: %s", (current+1), max, mObj->getClassName(), mObj->getName()); return true; }
  99. // Called once per object and SG light - used for calling light on an object
  100. virtual void processSGObjectProcessEvent(LightInfo* currLight) { light(currLight); };
  101. /// @}
  102. /// @name Persistence
  103. ///
  104. /// We cache lighting information to cut down on load times.
  105. ///
  106. /// There are flags such as ForceAlways and LoadOnly which allow you
  107. /// to control this behaviour.
  108. /// @{
  109. bool calcValidation();
  110. bool isValidChunk(PersistInfo::PersistChunk *);
  111. virtual U32 getResourceCRC() = 0;
  112. virtual bool setPersistInfo(PersistInfo::PersistChunk *);
  113. virtual bool getPersistInfo(PersistInfo::PersistChunk *);
  114. /// @}
  115. // Called to figure out if this object should be added to the shadow volume
  116. virtual bool supportsShadowVolume() { return false; }
  117. // Called to retrieve the clip planes of the object. Currently used for terrain lighting, but could be used to speed up other
  118. // lighting calculations.
  119. virtual void getClipPlanes(Vector<PlaneF>& planes) { }
  120. // Called to add the object to the shadow volume
  121. virtual void addToShadowVolume(ShadowVolumeBSP * shadowVolume, LightInfo * light, S32 level) { } ;
  122. };
  123. typedef Vector<ObjectProxy*> ObjectProxyList;
  124. ObjectProxyList mSceneObjects;
  125. ObjectProxyList mLitObjects;
  126. LightInfoList mLights;
  127. SceneLighting(AvailableSLInterfaces* lightingInterfaces);
  128. ~SceneLighting();
  129. enum Flags {
  130. ForceAlways = BIT(0), ///< Regenerate the scene lighting no matter what.
  131. ForceWritable = BIT(1), ///< Regenerate the scene lighting only if we can write to the lighting cache files.
  132. LoadOnly = BIT(2), ///< Just load cached lighting data.
  133. };
  134. bool lightScene(const char *, BitSet32 flags = 0);
  135. bool isLighting();
  136. S32 mStartTime;
  137. char mFileName[1024];
  138. SceneManager * mSceneManager;
  139. bool light(BitSet32);
  140. void completed(bool success);
  141. void processEvent(U32 light, S32 object);
  142. void processCache();
  143. };
  144. class sgSceneLightingProcessEvent : public SimEvent
  145. {
  146. private:
  147. U32 sgLightIndex;
  148. S32 sgObjectIndex;
  149. U32 sgEvent;
  150. public:
  151. enum sgEventTypes
  152. {
  153. sgLightingStartEventType,
  154. sgLightingCompleteEventType,
  155. sgSGPassSetupEventType,
  156. sgSGObjectStartEventType,
  157. sgSGObjectCompleteEventType,
  158. sgSGObjectProcessEventType,
  159. sgTGEPassSetupEventType,
  160. sgTGELightStartEventType,
  161. sgTGELightCompleteEventType,
  162. sgTGELightProcessEventType
  163. };
  164. sgSceneLightingProcessEvent(U32 lightIndex, S32 objectIndex, U32 event)
  165. {
  166. sgLightIndex = lightIndex;
  167. sgObjectIndex = objectIndex;
  168. sgEvent = event;
  169. }
  170. void process(SimObject * object)
  171. {
  172. AssertFatal(object, "SceneLightingProcessEvent:: null event object!");
  173. if(!object)
  174. return;
  175. SceneLighting *sl = static_cast<SceneLighting*>(object);
  176. switch(sgEvent)
  177. {
  178. case sgLightingStartEventType:
  179. sl->sgLightingStartEvent();
  180. break;
  181. case sgLightingCompleteEventType:
  182. sl->sgLightingCompleteEvent();
  183. break;
  184. case sgTGEPassSetupEventType:
  185. sl->sgTGEPassSetupEvent();
  186. break;
  187. case sgTGELightStartEventType:
  188. sl->sgTGELightStartEvent(sgLightIndex);
  189. break;
  190. case sgTGELightProcessEventType:
  191. sl->sgTGELightProcessEvent(sgLightIndex, sgObjectIndex);
  192. break;
  193. case sgTGELightCompleteEventType:
  194. sl->sgTGELightCompleteEvent(sgLightIndex);
  195. break;
  196. case sgSGPassSetupEventType:
  197. sl->sgSGPassSetupEvent();
  198. break;
  199. case sgSGObjectStartEventType:
  200. sl->sgSGObjectStartEvent(sgObjectIndex);
  201. break;
  202. case sgSGObjectProcessEventType:
  203. sl->sgSGObjectProcessEvent(sgLightIndex, sgObjectIndex);
  204. break;
  205. case sgSGObjectCompleteEventType:
  206. sl->sgSGObjectCompleteEvent(sgObjectIndex);
  207. break;
  208. default:
  209. return;
  210. };
  211. };
  212. };
  213. extern SceneLighting *gLighting;
  214. #endif//_SGSCENELIGHTING_H_