reflectionProbe.h 11 KB

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