reflectionProbe.h 8.1 KB

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