reflectionProbe.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 REFLECTIONPROBE_H
  23. #define REFLECTIONPROBE_H
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _GFXVERTEXBUFFER_H_
  28. #include "gfx/gfxVertexBuffer.h"
  29. #endif
  30. #ifndef _GFXPRIMITIVEBUFFER_H_
  31. #include "gfx/gfxPrimitiveBuffer.h"
  32. #endif
  33. #ifndef _TSSHAPEINSTANCE_H_
  34. #include "ts/tsShapeInstance.h"
  35. #endif
  36. #include "lighting/lightInfo.h"
  37. #ifndef _RENDERPASSMANAGER_H_
  38. #include "renderInstance/renderPassManager.h"
  39. #endif
  40. class BaseMatInstance;
  41. //-----------------------------------------------------------------------------
  42. // This class implements a basic SceneObject that can exist in the world at a
  43. // 3D position and render itself. There are several valid ways to render an
  44. // object in Torque. This class implements the preferred rendering method which
  45. // is to submit a MeshRenderInst along with a Material, vertex buffer,
  46. // primitive buffer, and transform and allow the RenderMeshMgr handle the
  47. // actual setup and rendering for you.
  48. //-----------------------------------------------------------------------------
  49. class ReflectionProbe : public SceneObject
  50. {
  51. typedef SceneObject Parent;
  52. friend class RenderProbeMgr;
  53. public:
  54. /// <summary>
  55. /// Used to dictate what sort of cubemap the probes use when using IBL
  56. /// </summary>
  57. enum ReflectionModeType
  58. {
  59. NoReflection = 0,
  60. StaticCubemap = 1,
  61. BakedCubemap = 2,
  62. DynamicCubemap = 5,
  63. };
  64. /// <summary>
  65. /// This contains all the important data the Probe uses for rendering.
  66. /// </summary>
  67. struct ProbeInfo
  68. {
  69. bool mIsEnabled;
  70. MatrixF mTransform;
  71. ReflectionProbe* mObject;
  72. F32 mRadius;
  73. bool mDirty;
  74. Box3F mBounds;
  75. Point3F mExtents;
  76. Point3F mPosition;
  77. Point3F mProbeRefOffset;
  78. Point3F mProbeRefScale;
  79. F32 mAtten;
  80. F32 mScore;
  81. GFXCubemapHandle mPrefilterCubemap;
  82. GFXCubemapHandle mIrradianceCubemap;
  83. /// The priority of this light used for
  84. /// light and shadow scoring.
  85. F32 mPriority;
  86. enum ProbeShapeType
  87. {
  88. Box = 0,
  89. Sphere = 1,
  90. Skylight = 2
  91. };
  92. ProbeShapeType mProbeShapeType;
  93. public:
  94. ProbeInfo() : mScore(0) {}
  95. ~ProbeInfo() {}
  96. // Copies data passed in from light
  97. void set(const ProbeInfo* probeInfo);
  98. // Accessors
  99. const MatrixF& getTransform() const { return mTransform; }
  100. void setTransform(const MatrixF& xfm) { mTransform = xfm; }
  101. Point3F getPosition() const { return mPosition; }
  102. void setPosition(const Point3F& pos) { mPosition = pos; }
  103. void setPriority(F32 priority) { mPriority = priority; }
  104. F32 getPriority() const { return mPriority; }
  105. void clear();
  106. };
  107. protected:
  108. // Networking masks
  109. // We need to implement a mask specifically to handle
  110. // updating our transform from the server object to its
  111. // client-side "ghost". We also need to implement a
  112. // maks for handling editor updates to our properties
  113. // (like material).
  114. enum MaskBits
  115. {
  116. TransformMask = Parent::NextFreeMask << 0,
  117. StaticDataMask = Parent::NextFreeMask << 1,
  118. EnabledMask = Parent::NextFreeMask << 2,
  119. NextFreeMask = Parent::NextFreeMask << 3
  120. };
  121. /// <summary>
  122. /// Only used for interfacing with the editor's inspector bake button
  123. /// </summary>
  124. bool mBakeReflections;
  125. /// <summary>
  126. /// Whether this probe is enabled or not
  127. /// </summary>
  128. bool mEnabled;
  129. bool mDirty;
  130. /// <summary>
  131. /// Whether this probe's cubemap is dirty or not
  132. /// </summary>
  133. bool mCubemapDirty;
  134. #ifdef TORQUE_TOOLS
  135. /// <summary>
  136. /// Used only when the editor is loaded, this is the shape data used for the probe viewing(aka, a sphere)
  137. /// </summary>
  138. Resource<TSShape> mEditorShape;
  139. /// <summary>
  140. /// This is the shape instance of the editor shape data
  141. /// </summary>
  142. TSShapeInstance* mEditorShapeInst;
  143. #endif // TORQUE_TOOLS
  144. //--------------------------------------------------------------------------
  145. // Rendering variables
  146. //--------------------------------------------------------------------------
  147. /// <summary>
  148. /// The shape of the probe
  149. /// </summary>
  150. ProbeInfo::ProbeShapeType mProbeShapeType;
  151. /// <summary>
  152. /// This is effectively a packed cache of the probe data actually utilized for rendering.
  153. /// The RenderProbeManager uses this via the probe calling registerProbe on creation, and unregisterProbe on destruction
  154. /// When the manager goes to render it has the compacted data to read over more efficiently for setting up what probes should
  155. /// Actually render in that frame
  156. /// </summary>
  157. ProbeInfo mProbeInfo;
  158. /// <summary>
  159. /// Used to dictate what sort of cubemap the probes use when using IBL
  160. /// </summary>
  161. ReflectionModeType mReflectionModeType;
  162. /// <summary>
  163. /// The radius of the probe's influence. Only really relevent in Sphere probes
  164. /// </summary>
  165. F32 mRadius;
  166. /// <summary>
  167. /// The reference positional offset for the probe. This is used for adjusting the perceived center and area of influence.
  168. /// Helpful in adjusting parallax issues
  169. /// </summary>
  170. Point3F mProbeRefOffset;
  171. /// <summary>
  172. /// The reference scale for the probe. This is used for adjusting the perceived center and area of influence.
  173. /// Helpful in adjusting parallax issues
  174. /// </summary>
  175. Point3F mProbeRefScale;
  176. /// <summary>
  177. /// Only used for interfacing with the editor's inspector edit offset button
  178. /// </summary>
  179. bool mEditPosOffset;
  180. /// <summary>
  181. /// This is used when a static cubemap is used. The name of the cubemap is looked up and loaded for the IBL calculations
  182. /// </summary>
  183. StringTableEntry mCubemapName;
  184. CubemapData *mStaticCubemap;
  185. GFXCubemapHandle mDynamicCubemap;
  186. //String cubeDescName;
  187. //U32 cubeDescId;
  188. //ReflectorDesc *reflectorDesc;
  189. //Utilized in dynamic reflections
  190. //CubeReflector mCubeReflector;
  191. bool mUseHDRCaptures;
  192. //irridiance resources
  193. CubemapData *mIrridianceMap;
  194. //prefilter resources
  195. CubemapData *mPrefilterMap;
  196. U32 mPrefilterMipLevels;
  197. U32 mPrefilterSize;
  198. /// <summary>
  199. /// This is calculated based on the object's persistantID. Effectively a unique hash ID to set it apart from other probes
  200. /// Used to ensure the cubemaps named when baking are unique
  201. /// </summary>
  202. String mProbeUniqueID;
  203. //Debug rendering
  204. static bool smRenderPreviewProbes;
  205. U32 mDynamicLastBakeMS;
  206. U32 mRefreshRateMS;
  207. bool mResourcesCreated;
  208. U32 mCaptureMask;
  209. public:
  210. ReflectionProbe();
  211. virtual ~ReflectionProbe();
  212. // Declare this object as a ConsoleObject so that we can
  213. // instantiate it into the world and network it
  214. DECLARE_CONOBJECT(ReflectionProbe);
  215. //--------------------------------------------------------------------------
  216. // Object Editing
  217. // Since there is always a server and a client object in Torque and we
  218. // actually edit the server object we need to implement some basic
  219. // networking functions
  220. //--------------------------------------------------------------------------
  221. // Set up any fields that we want to be editable (like position)
  222. static void initPersistFields();
  223. // Allows the object to update its editable settings
  224. // from the server object to the client
  225. virtual void inspectPostApply();
  226. static bool _setEnabled(void *object, const char *index, const char *data);
  227. static bool _doBake(void *object, const char *index, const char *data);
  228. static bool _toggleEditPosOffset(void *object, const char *index, const char *data);
  229. static bool _setRadius(void *object, const char *index, const char *data);
  230. static bool _setReflectionMode(void *object, const char *index, const char *data);
  231. // Handle when we are added to the scene and removed from the scene
  232. bool onAdd();
  233. void onRemove();
  234. /// <summary>
  235. /// This is called when the object is deleted. It allows us to do special-case cleanup actions
  236. /// In probes' case, it's used to delete baked cubemap files
  237. /// </summary>
  238. virtual void handleDeleteAction();
  239. // Override this so that we can dirty the network flag when it is called
  240. virtual void setTransform(const MatrixF &mat);
  241. virtual const MatrixF& getTransform() const;
  242. virtual void setScale(const VectorF &scale);
  243. virtual const VectorF& getScale() const;
  244. virtual bool writeField(StringTableEntry fieldname, const char *value);
  245. // This function handles sending the relevant data from the server
  246. // object to the client object
  247. U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
  248. // This function handles receiving relevant data from the server
  249. // object and applying it to the client object
  250. void unpackUpdate(NetConnection *conn, BitStream *stream);
  251. //--------------------------------------------------------------------------
  252. // Object Rendering
  253. // Torque utilizes a "batch" rendering system. This means that it builds a
  254. // list of objects that need to render (via RenderInst's) and then renders
  255. // them all in one batch. This allows it to optimized on things like
  256. // minimizing texture, state, and shader switching by grouping objects that
  257. // use the same Materials.
  258. //--------------------------------------------------------------------------
  259. // Create the geometry for rendering
  260. void createEditorResources();
  261. /// <summary>
  262. /// Updates the probe rendering data
  263. /// </summary>
  264. virtual void updateProbeParams();
  265. bool createClientResources();
  266. /// <summary>
  267. /// Updates the probe's cubemaps in the array when using dynamic reflections
  268. /// </summary>
  269. void processDynamicCubemap();
  270. /// <summary>
  271. /// Updates the probe's cubemaps in the array when using baked cubemaps
  272. /// </summary>
  273. void processBakedCubemap();
  274. /// <summary>
  275. /// Updates the probe's cubemaps in the array when using a static cubemaps
  276. /// </summary>
  277. void processStaticCubemap();
  278. // This is the function that allows this object to submit itself for rendering
  279. void prepRenderImage(SceneRenderState *state);
  280. void _onRenderViz(ObjectRenderInst *ri,
  281. SceneRenderState *state,
  282. BaseMatInstance *overrideMat);
  283. void setPreviewMatParameters(SceneRenderState* renderState, BaseMatInstance* mat);
  284. /// <summary>
  285. /// This gets the filepath to the prefilter cubemap associated to this probe.
  286. /// In the event the probe is set to use a static cubemap, it is the prefiltered version of the cubemap's file
  287. /// </summary>
  288. /// <returns>The filepath to the prefilter cubemap</returns>
  289. String getPrefilterMapPath();
  290. /// <summary>
  291. /// This gets the filepath to the irradiance cubemap associated to this probe.
  292. /// In the event the probe is set to use a static cubemap, it is the irradiance version of the cubemap's file
  293. /// </summary>
  294. /// <returns>The filepath to the irradiance cubemap</returns>
  295. String getIrradianceMapPath();
  296. /// <summary>
  297. /// Invokes a cubemap bake action for this probe
  298. /// </summary>
  299. void bake();
  300. };
  301. typedef ReflectionProbe::ProbeInfo::ProbeShapeType ReflectProbeType;
  302. DefineEnumType(ReflectProbeType);
  303. typedef ReflectionProbe::ReflectionModeType ReflectionModeEnum;
  304. DefineEnumType(ReflectionModeEnum);
  305. #endif // _ReflectionProbe_H_