reflectionProbe.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. bool mCanDamp;
  94. public:
  95. ProbeInfo()
  96. {
  97. mScore = 0;
  98. mAtten = 0.0f;
  99. mCanDamp = false;
  100. mDirty = false;
  101. mIsEnabled = true;
  102. mObject = NULL;
  103. mPriority = 0;
  104. mProbeShapeType = Box;
  105. mRadius = 10.0f;
  106. }
  107. ~ProbeInfo() {}
  108. // Copies data passed in from light
  109. void set(const ProbeInfo* probeInfo);
  110. // Accessors
  111. const MatrixF& getTransform() const { return mTransform; }
  112. void setTransform(const MatrixF& xfm) { mTransform = xfm; }
  113. Point3F getPosition() const { return mPosition; }
  114. void setPosition(const Point3F& pos) { mPosition = pos; }
  115. void setPriority(F32 priority) { mPriority = priority; }
  116. F32 getPriority() const { return mPriority; }
  117. void clear();
  118. };
  119. protected:
  120. // Networking masks
  121. // We need to implement a mask specifically to handle
  122. // updating our transform from the server object to its
  123. // client-side "ghost". We also need to implement a
  124. // maks for handling editor updates to our properties
  125. // (like material).
  126. enum MaskBits
  127. {
  128. TransformMask = Parent::NextFreeMask << 0,
  129. StaticDataMask = Parent::NextFreeMask << 1,
  130. EnabledMask = Parent::NextFreeMask << 2,
  131. NextFreeMask = Parent::NextFreeMask << 3
  132. };
  133. /// <summary>
  134. /// Only used for interfacing with the editor's inspector bake button
  135. /// </summary>
  136. bool mBakeReflections;
  137. /// <summary>
  138. /// Whether this probe is enabled or not
  139. /// </summary>
  140. bool mEnabled;
  141. F32 mAtten;
  142. bool mDirty;
  143. /// <summary>
  144. /// Whether this probe's cubemap is dirty or not
  145. /// </summary>
  146. bool mCubemapDirty;
  147. #ifdef TORQUE_TOOLS
  148. /// <summary>
  149. /// Used only when the editor is loaded, this is the shape data used for the probe viewing(aka, a sphere)
  150. /// </summary>
  151. Resource<TSShape> mEditorShape;
  152. /// <summary>
  153. /// This is the shape instance of the editor shape data
  154. /// </summary>
  155. TSShapeInstance* mEditorShapeInst;
  156. #endif // TORQUE_TOOLS
  157. //--------------------------------------------------------------------------
  158. // Rendering variables
  159. //--------------------------------------------------------------------------
  160. /// <summary>
  161. /// The shape of the probe
  162. /// </summary>
  163. ProbeInfo::ProbeShapeType mProbeShapeType;
  164. /// <summary>
  165. /// This is effectively a packed cache of the probe data actually utilized for rendering.
  166. /// The RenderProbeManager uses this via the probe calling registerProbe on creation, and unregisterProbe on destruction
  167. /// When the manager goes to render it has the compacted data to read over more efficiently for setting up what probes should
  168. /// Actually render in that frame
  169. /// </summary>
  170. ProbeInfo mProbeInfo;
  171. /// <summary>
  172. /// Used to dictate what sort of cubemap the probes use when using IBL
  173. /// </summary>
  174. ReflectionModeType mReflectionModeType;
  175. /// <summary>
  176. /// The radius of the probe's influence. Only really relevent in Sphere probes
  177. /// </summary>
  178. F32 mRadius;
  179. /// <summary>
  180. /// The reference positional offset for the probe. This is used for adjusting the perceived center and area of influence.
  181. /// Helpful in adjusting parallax issues
  182. /// </summary>
  183. Point3F mProbeRefOffset;
  184. /// <summary>
  185. /// The reference scale for the probe. This is used for adjusting the perceived center and area of influence.
  186. /// Helpful in adjusting parallax issues
  187. /// </summary>
  188. Point3F mProbeRefScale;
  189. /// <summary>
  190. /// Only used for interfacing with the editor's inspector edit offset button
  191. /// </summary>
  192. bool mEditPosOffset;
  193. /// <summary>
  194. /// This is used when a static cubemap is used. The name of the cubemap is looked up and loaded for the IBL calculations
  195. /// </summary>
  196. StringTableEntry mCubemapName;
  197. CubemapData *mStaticCubemap;
  198. GFXCubemapHandle mDynamicCubemap;
  199. //String cubeDescName;
  200. //U32 cubeDescId;
  201. //ReflectorDesc *reflectorDesc;
  202. //Utilized in dynamic reflections
  203. //CubeReflector mCubeReflector;
  204. bool mUseHDRCaptures;
  205. //irridiance resources
  206. CubemapData *mIrridianceMap;
  207. //prefilter resources
  208. CubemapData *mPrefilterMap;
  209. U32 mPrefilterMipLevels;
  210. U32 mPrefilterSize;
  211. /// <summary>
  212. /// This is calculated based on the object's persistantID. Effectively a unique hash ID to set it apart from other probes
  213. /// Used to ensure the cubemaps named when baking are unique
  214. /// </summary>
  215. String mProbeUniqueID;
  216. //Debug rendering
  217. static bool smRenderPreviewProbes;
  218. U32 mDynamicLastBakeMS;
  219. U32 mRefreshRateMS;
  220. bool mResourcesCreated;
  221. U32 mCaptureMask;
  222. bool mCanDamp;
  223. public:
  224. ReflectionProbe();
  225. virtual ~ReflectionProbe();
  226. // Declare this object as a ConsoleObject so that we can
  227. // instantiate it into the world and network it
  228. DECLARE_CONOBJECT(ReflectionProbe);
  229. DECLARE_CATEGORY("UNLISTED");
  230. //--------------------------------------------------------------------------
  231. // Object Editing
  232. // Since there is always a server and a client object in Torque and we
  233. // actually edit the server object we need to implement some basic
  234. // networking functions
  235. //--------------------------------------------------------------------------
  236. // Set up any fields that we want to be editable (like position)
  237. static void initPersistFields();
  238. // Allows the object to update its editable settings
  239. // from the server object to the client
  240. void inspectPostApply() override;
  241. static bool _setEnabled(void *object, const char *index, const char *data);
  242. static bool _doBake(void *object, const char *index, const char *data);
  243. static bool _toggleEditPosOffset(void *object, const char *index, const char *data);
  244. static bool _setRadius(void *object, const char *index, const char *data);
  245. static bool _setReflectionMode(void *object, const char *index, const char *data);
  246. // Handle when we are added to the scene and removed from the scene
  247. bool onAdd() override;
  248. void onRemove() override;
  249. /// <summary>
  250. /// This is called when the object is deleted. It allows us to do special-case cleanup actions
  251. /// In probes' case, it's used to delete baked cubemap files
  252. /// </summary>
  253. void handleDeleteAction() override;
  254. // Override this so that we can dirty the network flag when it is called
  255. void setTransform(const MatrixF &mat) override;
  256. const MatrixF& getTransform() const override;
  257. void setScale(const VectorF &scale) override;
  258. const VectorF& getScale() const override;
  259. bool writeField(StringTableEntry fieldname, const char *value) override;
  260. // This function handles sending the relevant data from the server
  261. // object to the client object
  262. U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream) override;
  263. // This function handles receiving relevant data from the server
  264. // object and applying it to the client object
  265. void unpackUpdate(NetConnection *conn, BitStream *stream) override;
  266. //--------------------------------------------------------------------------
  267. // Object Rendering
  268. // Torque utilizes a "batch" rendering system. This means that it builds a
  269. // list of objects that need to render (via RenderInst's) and then renders
  270. // them all in one batch. This allows it to optimized on things like
  271. // minimizing texture, state, and shader switching by grouping objects that
  272. // use the same Materials.
  273. //--------------------------------------------------------------------------
  274. // Create the geometry for rendering
  275. void createEditorResources();
  276. /// <summary>
  277. /// Updates the probe rendering data
  278. /// </summary>
  279. virtual void updateProbeParams();
  280. bool createClientResources();
  281. /// <summary>
  282. /// Updates the probe's cubemaps in the array when using dynamic reflections
  283. /// </summary>
  284. void processDynamicCubemap();
  285. /// <summary>
  286. /// Updates the probe's cubemaps in the array when using baked cubemaps
  287. /// </summary>
  288. void processBakedCubemap();
  289. /// <summary>
  290. /// Updates the probe's cubemaps in the array when using a static cubemaps
  291. /// </summary>
  292. void processStaticCubemap();
  293. // This is the function that allows this object to submit itself for rendering
  294. void prepRenderImage(SceneRenderState *state) override;
  295. void _onRenderViz(ObjectRenderInst *ri,
  296. SceneRenderState *state,
  297. BaseMatInstance *overrideMat);
  298. void setPreviewMatParameters(SceneRenderState* renderState, BaseMatInstance* mat);
  299. /// <summary>
  300. /// This gets the filepath to the prefilter cubemap associated to this probe.
  301. /// In the event the probe is set to use a static cubemap, it is the prefiltered version of the cubemap's file
  302. /// </summary>
  303. /// <returns>The filepath to the prefilter cubemap</returns>
  304. String getPrefilterMapPath();
  305. /// <summary>
  306. /// This gets the filepath to the irradiance cubemap associated to this probe.
  307. /// In the event the probe is set to use a static cubemap, it is the irradiance version of the cubemap's file
  308. /// </summary>
  309. /// <returns>The filepath to the irradiance cubemap</returns>
  310. String getIrradianceMapPath();
  311. /// <summary>
  312. /// Invokes a cubemap bake action for this probe
  313. /// </summary>
  314. void bake();
  315. ProbeInfo* getProbeInfo() { return &mProbeInfo; }
  316. };
  317. typedef ReflectionProbe::ProbeInfo::ProbeShapeType ReflectProbeType;
  318. DefineEnumType(ReflectProbeType);
  319. typedef ReflectionProbe::ReflectionModeType ReflectionModeEnum;
  320. DefineEnumType(ReflectionModeEnum);
  321. #endif // _ReflectionProbe_H_