reflectionProbe.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. enum ReflectionModeType
  58. {
  59. NoReflection = 0,
  60. StaticCubemap = 1,
  61. BakedCubemap = 2,
  62. DynamicCubemap = 5,
  63. };
  64. protected:
  65. // Networking masks
  66. // We need to implement a mask specifically to handle
  67. // updating our transform from the server object to its
  68. // client-side "ghost". We also need to implement a
  69. // maks for handling editor updates to our properties
  70. // (like material).
  71. enum MaskBits
  72. {
  73. TransformMask = Parent::NextFreeMask << 0,
  74. UpdateMask = Parent::NextFreeMask << 1,
  75. EnabledMask = Parent::NextFreeMask << 2,
  76. CubemapMask = Parent::NextFreeMask << 3,
  77. ModeMask = Parent::NextFreeMask << 4,
  78. RadiusMask = Parent::NextFreeMask << 5,
  79. ShapeTypeMask = Parent::NextFreeMask << 6,
  80. BakeInfoMask = Parent::NextFreeMask << 7,
  81. NextFreeMask = Parent::NextFreeMask << 8
  82. };
  83. bool mBake;
  84. bool mEnabled;
  85. bool mDirty;
  86. Resource<TSShape> mEditorShape;
  87. TSShapeInstance* mEditorShapeInst;
  88. //--------------------------------------------------------------------------
  89. // Rendering variables
  90. //--------------------------------------------------------------------------
  91. ProbeRenderInst::ProbeShapeType mProbeShapeType;
  92. ProbeRenderInst* mProbeInfo;
  93. U32 mProbeInfoIdx;
  94. //Reflection Contribution stuff
  95. ReflectionModeType mReflectionModeType;
  96. F32 mRadius;
  97. Point3F mProbeRefOffset;
  98. Point3F mProbeRefScale;
  99. bool mEditPosOffset;
  100. String mCubemapName;
  101. CubemapData *mStaticCubemap;
  102. GFXCubemapHandle mDynamicCubemap;
  103. String cubeDescName;
  104. U32 cubeDescId;
  105. ReflectorDesc *reflectorDesc;
  106. ///Prevents us from saving out the cubemaps(for now) but allows us the full HDR range on the in-memory cubemap captures
  107. bool mUseHDRCaptures;
  108. //irridiance resources
  109. CubemapData *mIrridianceMap;
  110. //prefilter resources
  111. CubemapData *mPrefilterMap;
  112. U32 mPrefilterMipLevels;
  113. U32 mPrefilterSize;
  114. String mProbeUniqueID;
  115. // Define our vertex format here so we don't have to
  116. // change it in multiple spots later
  117. typedef GFXVertexPNTTB VertexType;
  118. // The GFX vertex and primitive buffers
  119. GFXVertexBufferHandle< VertexType > mVertexBuffer;
  120. GFXPrimitiveBufferHandle mPrimitiveBuffer;
  121. U32 mSphereVertCount;
  122. U32 mSpherePrimitiveCount;
  123. //Debug rendering
  124. static bool smRenderPreviewProbes;
  125. U32 mDynamicLastBakeMS;
  126. U32 mRefreshRateMS;
  127. GBitmap* mCubeFaceBitmaps[6];
  128. U32 mCubemapResolution;
  129. F32 mMaxDrawDistance;
  130. bool mResourcesCreated;
  131. U32 mCaptureMask;
  132. public:
  133. ReflectionProbe();
  134. virtual ~ReflectionProbe();
  135. // Declare this object as a ConsoleObject so that we can
  136. // instantiate it into the world and network it
  137. DECLARE_CONOBJECT(ReflectionProbe);
  138. //--------------------------------------------------------------------------
  139. // Object Editing
  140. // Since there is always a server and a client object in Torque and we
  141. // actually edit the server object we need to implement some basic
  142. // networking functions
  143. //--------------------------------------------------------------------------
  144. // Set up any fields that we want to be editable (like position)
  145. static void initPersistFields();
  146. // Allows the object to update its editable settings
  147. // from the server object to the client
  148. virtual void inspectPostApply();
  149. static bool _setEnabled(void *object, const char *index, const char *data);
  150. static bool _doBake(void *object, const char *index, const char *data);
  151. static bool _toggleEditPosOffset(void *object, const char *index, const char *data);
  152. static bool _setRadius(void *object, const char *index, const char *data);
  153. static bool _setReflectionMode(void *object, const char *index, const char *data);
  154. // Handle when we are added to the scene and removed from the scene
  155. bool onAdd();
  156. void onRemove();
  157. virtual void handleDeleteAction();
  158. // Override this so that we can dirty the network flag when it is called
  159. virtual void setTransform(const MatrixF &mat);
  160. virtual const MatrixF& getTransform() const;
  161. virtual void setScale(const VectorF &scale);
  162. virtual const VectorF& getScale() const;
  163. virtual bool writeField(StringTableEntry fieldname, const char *value);
  164. // This function handles sending the relevant data from the server
  165. // object to the client object
  166. U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
  167. // This function handles receiving relevant data from the server
  168. // object and applying it to the client object
  169. void unpackUpdate(NetConnection *conn, BitStream *stream);
  170. //--------------------------------------------------------------------------
  171. // Object Rendering
  172. // Torque utilizes a "batch" rendering system. This means that it builds a
  173. // list of objects that need to render (via RenderInst's) and then renders
  174. // them all in one batch. This allows it to optimized on things like
  175. // minimizing texture, state, and shader switching by grouping objects that
  176. // use the same Materials.
  177. //--------------------------------------------------------------------------
  178. // Create the geometry for rendering
  179. void createGeometry();
  180. // Get the Material instance
  181. void updateCubemaps();
  182. virtual void updateProbeParams();
  183. bool createClientResources();
  184. void processStaticCubemap();
  185. // This is the function that allows this object to submit itself for rendering
  186. void prepRenderImage(SceneRenderState *state);
  187. void _onRenderViz(ObjectRenderInst *ri,
  188. SceneRenderState *state,
  189. BaseMatInstance *overrideMat);
  190. void setPreviewMatParameters(SceneRenderState* renderState, BaseMatInstance* mat);
  191. //Baking
  192. String getPrefilterMapPath();
  193. String getIrradianceMapPath();
  194. void bake();
  195. const U32 getProbeInfoIndex() { return mProbeInfoIdx; }
  196. };
  197. typedef ProbeRenderInst::ProbeShapeType ReflectProbeType;
  198. DefineEnumType(ReflectProbeType);
  199. typedef ReflectionProbe::ReflectionModeType ReflectionModeEnum;
  200. DefineEnumType(ReflectionModeEnum);
  201. #endif // _ReflectionProbe_H_