reflectionProbe.h 8.2 KB

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