reflectionProbe.h 8.5 KB

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