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