reflectionProbe.h 13 KB

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