sphereEnvironmentProbe.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #include "T3D/lighting/sphereEnvironmentProbe.h"
  23. #include "math/mathIO.h"
  24. #include "scene/sceneRenderState.h"
  25. #include "console/consoleTypes.h"
  26. #include "core/stream/bitStream.h"
  27. #include "materials/baseMatInstance.h"
  28. #include "console/engineAPI.h"
  29. #include "gfx/gfxDrawUtil.h"
  30. #include "gfx/gfxDebugEvent.h"
  31. #include "gfx/gfxTransformSaver.h"
  32. #include "math/mathUtils.h"
  33. #include "gfx/bitmap/gBitmap.h"
  34. #include "core/stream/fileStream.h"
  35. #include "core/fileObject.h"
  36. #include "core/resourceManager.h"
  37. #include "console/simPersistID.h"
  38. #include "T3D/gameFunctions.h"
  39. #include "postFx/postEffect.h"
  40. #include "renderInstance/renderProbeMgr.h"
  41. #include "renderInstance/renderProbeMgr.h"
  42. #include "math/util/sphereMesh.h"
  43. #include "materials/materialManager.h"
  44. #include "math/util/matrixSet.h"
  45. #include "gfx/bitmap/cubemapSaver.h"
  46. #include "materials/materialFeatureTypes.h"
  47. #include "materials/shaderData.h"
  48. #include "gfx/gfxTextureManager.h"
  49. #include "gfx/bitmap/imageUtils.h"
  50. #include "T3D/lighting/IBLUtilities.h"
  51. extern bool gEditingMission;
  52. extern ColorI gCanvasClearColor;
  53. IMPLEMENT_CO_NETOBJECT_V1(SphereEnvironmentProbe);
  54. ConsoleDocClass(SphereEnvironmentProbe,
  55. "@brief An example scene object which renders a mesh.\n\n"
  56. "This class implements a basic SceneObject that can exist in the world at a "
  57. "3D position and render itself. There are several valid ways to render an "
  58. "object in Torque. This class implements the preferred rendering method which "
  59. "is to submit a MeshRenderInst along with a Material, vertex buffer, "
  60. "primitive buffer, and transform and allow the RenderMeshMgr handle the "
  61. "actual setup and rendering for you.\n\n"
  62. "See the C++ code for implementation details.\n\n"
  63. "@ingroup Examples\n");
  64. //-----------------------------------------------------------------------------
  65. // Object setup and teardown
  66. //-----------------------------------------------------------------------------
  67. SphereEnvironmentProbe::SphereEnvironmentProbe() : ReflectionProbe()
  68. {
  69. mCaptureMask = REFLECTION_PROBE_CAPTURE_TYPEMASK;
  70. mProbeShapeType = ProbeRenderInst::Sphere;
  71. }
  72. SphereEnvironmentProbe::~SphereEnvironmentProbe()
  73. {
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Object Editing
  77. //-----------------------------------------------------------------------------
  78. void SphereEnvironmentProbe::initPersistFields()
  79. {
  80. // SceneObject already handles exposing the transform
  81. Parent::initPersistFields();
  82. removeField("scale");
  83. }
  84. void SphereEnvironmentProbe::inspectPostApply()
  85. {
  86. Parent::inspectPostApply();
  87. mDirty = true;
  88. // Flag the network mask to send the updates
  89. // to the client object
  90. setMaskBits(-1);
  91. }
  92. bool SphereEnvironmentProbe::onAdd()
  93. {
  94. if (!Parent::onAdd())
  95. return false;
  96. return true;
  97. }
  98. void SphereEnvironmentProbe::onRemove()
  99. {
  100. Parent::onRemove();
  101. }
  102. U32 SphereEnvironmentProbe::packUpdate(NetConnection *conn, U32 mask, BitStream *stream)
  103. {
  104. // Allow the Parent to get a crack at writing its info
  105. U32 retMask = Parent::packUpdate(conn, mask, stream);
  106. return retMask;
  107. }
  108. void SphereEnvironmentProbe::unpackUpdate(NetConnection *conn, BitStream *stream)
  109. {
  110. // Let the Parent read any info it sent
  111. Parent::unpackUpdate(conn, stream);
  112. if (mDirty)
  113. {
  114. updateProbeParams();
  115. }
  116. }
  117. //-----------------------------------------------------------------------------
  118. // Object Rendering
  119. //-----------------------------------------------------------------------------
  120. void SphereEnvironmentProbe::updateProbeParams()
  121. {
  122. mProbeShapeType = ProbeRenderInst::Sphere;
  123. Parent::updateProbeParams();
  124. }
  125. void SphereEnvironmentProbe::prepRenderImage(SceneRenderState *state)
  126. {
  127. if (!mEnabled || !ReflectionProbe::smRenderPreviewProbes)
  128. return;
  129. #ifdef TORQUE_TOOLS
  130. if (ReflectionProbe::smRenderPreviewProbes && gEditingMission && mEditorShapeInst && mPrefilterMap != nullptr)
  131. {
  132. GFXTransformSaver saver;
  133. // Calculate the distance of this object from the camera
  134. Point3F cameraOffset;
  135. getRenderTransform().getColumn(3, &cameraOffset);
  136. cameraOffset -= state->getDiffuseCameraPosition();
  137. F32 dist = cameraOffset.len();
  138. if (dist < 0.01f)
  139. dist = 0.01f;
  140. // Set up the LOD for the shape
  141. F32 invScale = (1.0f / getMax(getMax(mObjScale.x, mObjScale.y), mObjScale.z));
  142. mEditorShapeInst->setDetailFromDistance(state, dist * invScale);
  143. // Make sure we have a valid level of detail
  144. if (mEditorShapeInst->getCurrentDetail() < 0)
  145. return;
  146. BaseMatInstance* probePrevMat = mEditorShapeInst->getMaterialList()->getMaterialInst(0);
  147. setPreviewMatParameters(state, probePrevMat);
  148. // GFXTransformSaver is a handy helper class that restores
  149. // the current GFX matrices to their original values when
  150. // it goes out of scope at the end of the function
  151. // Set up our TS render state
  152. TSRenderState rdata;
  153. rdata.setSceneState(state);
  154. rdata.setFadeOverride(1.0f);
  155. // We might have some forward lit materials
  156. // so pass down a query to gather lights.
  157. LightQuery query;
  158. query.init(getWorldSphere());
  159. rdata.setLightQuery(&query);
  160. // Set the world matrix to the objects render transform
  161. MatrixF mat = getRenderTransform();
  162. mat.scale(Point3F(1, 1, 1));
  163. GFX->setWorldMatrix(mat);
  164. // Animate the the shape
  165. mEditorShapeInst->animate();
  166. // Allow the shape to submit the RenderInst(s) for itself
  167. mEditorShapeInst->render(rdata);
  168. saver.restore();
  169. }
  170. // If the light is selected or light visualization
  171. // is enabled then register the callback.
  172. const bool isSelectedInEditor = (gEditingMission && isSelected());
  173. if (isSelectedInEditor)
  174. {
  175. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  176. ri->renderDelegate.bind(this, &ReflectionProbe::_onRenderViz);
  177. ri->type = RenderPassManager::RIT_Editor;
  178. state->getRenderPass()->addInst(ri);
  179. }
  180. #endif
  181. }
  182. void SphereEnvironmentProbe::setPreviewMatParameters(SceneRenderState* renderState, BaseMatInstance* mat)
  183. {
  184. Parent::setPreviewMatParameters(renderState, mat);
  185. }