skylight.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/Skylight.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. bool Skylight::smRenderSkylights = true;
  54. IMPLEMENT_CO_NETOBJECT_V1(Skylight);
  55. ConsoleDocClass(Skylight,
  56. "@brief An example scene object which renders a mesh.\n\n"
  57. "This class implements a basic SceneObject that can exist in the world at a "
  58. "3D position and render itself. There are several valid ways to render an "
  59. "object in Torque. This class implements the preferred rendering method which "
  60. "is to submit a MeshRenderInst along with a Material, vertex buffer, "
  61. "primitive buffer, and transform and allow the RenderMeshMgr handle the "
  62. "actual setup and rendering for you.\n\n"
  63. "See the C++ code for implementation details.\n\n"
  64. "@ingroup Examples\n");
  65. //-----------------------------------------------------------------------------
  66. // Object setup and teardown
  67. //-----------------------------------------------------------------------------
  68. Skylight::Skylight() : ReflectionProbe()
  69. {
  70. mCaptureMask = SKYLIGHT_CAPTURE_TYPEMASK;
  71. }
  72. Skylight::~Skylight()
  73. {
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Object Editing
  77. //-----------------------------------------------------------------------------
  78. void Skylight::initPersistFields()
  79. {
  80. // SceneObject already handles exposing the transform
  81. Parent::initPersistFields();
  82. removeField("radius");
  83. removeField("scale");
  84. removeField("EditPosOffset");
  85. removeField("refOffset");
  86. removeField("refScale");
  87. }
  88. void Skylight::inspectPostApply()
  89. {
  90. Parent::inspectPostApply();
  91. mDirty = true;
  92. // Flag the network mask to send the updates
  93. // to the client object
  94. setMaskBits(-1);
  95. }
  96. bool Skylight::onAdd()
  97. {
  98. if (!Parent::onAdd())
  99. return false;
  100. return true;
  101. }
  102. void Skylight::onRemove()
  103. {
  104. Parent::onRemove();
  105. }
  106. void Skylight::setTransform(const MatrixF & mat)
  107. {
  108. // Let SceneObject handle all of the matrix manipulation
  109. Parent::setTransform(mat);
  110. mDirty = true;
  111. // Dirty our network mask so that the new transform gets
  112. // transmitted to the client object
  113. setMaskBits(TransformMask);
  114. }
  115. U32 Skylight::packUpdate(NetConnection *conn, U32 mask, BitStream *stream)
  116. {
  117. // Allow the Parent to get a crack at writing its info
  118. U32 retMask = Parent::packUpdate(conn, mask, stream);
  119. return retMask;
  120. }
  121. void Skylight::unpackUpdate(NetConnection *conn, BitStream *stream)
  122. {
  123. // Let the Parent read any info it sent
  124. Parent::unpackUpdate(conn, stream);
  125. }
  126. //-----------------------------------------------------------------------------
  127. // Object Rendering
  128. //-----------------------------------------------------------------------------
  129. void Skylight::updateProbeParams()
  130. {
  131. Parent::updateProbeParams();
  132. mProbeInfo->mProbeShapeType = ProbeRenderInst::Skylight;
  133. mProbeInfo->setPosition(getPosition());
  134. // Skip our transform... it just dirties mask bits.
  135. Parent::setTransform(mObjToWorld);
  136. resetWorldBox();
  137. F32 visDist = gClientSceneGraph->getVisibleDistance();
  138. Box3F skylightBounds = Box3F(visDist * 2);
  139. skylightBounds.setCenter(Point3F::Zero);
  140. mProbeInfo->setPosition(Point3F::Zero);
  141. mProbeInfo->mBounds = skylightBounds;
  142. setGlobalBounds();
  143. mProbeInfo->mIsSkylight = true;
  144. mProbeInfo->mScore = -1.0f; //sky comes first
  145. PROBEMGR->updateProbes();
  146. updateCubemaps();
  147. }
  148. void Skylight::prepRenderImage(SceneRenderState *state)
  149. {
  150. if (!mEnabled || !Skylight::smRenderSkylights)
  151. return;
  152. //special hook-in for skylights
  153. Point3F camPos = state->getCameraPosition();
  154. mProbeInfo->mBounds.setCenter(camPos);
  155. mProbeInfo->setPosition(camPos);
  156. //Submit our probe to actually do the probe action
  157. // Get a handy pointer to our RenderPassmanager
  158. //RenderPassManager *renderPass = state->getRenderPass();
  159. //PROBEMGR->registerSkylight(mProbeInfo, this);
  160. if (Skylight::smRenderPreviewProbes && gEditingMission && mEditorShapeInst && mPrefilterMap != nullptr)
  161. {
  162. GFXTransformSaver saver;
  163. // Calculate the distance of this object from the camera
  164. Point3F cameraOffset;
  165. getRenderTransform().getColumn(3, &cameraOffset);
  166. cameraOffset -= state->getDiffuseCameraPosition();
  167. F32 dist = cameraOffset.len();
  168. if (dist < 0.01f)
  169. dist = 0.01f;
  170. // Set up the LOD for the shape
  171. F32 invScale = (1.0f / getMax(getMax(mObjScale.x, mObjScale.y), mObjScale.z));
  172. mEditorShapeInst->setDetailFromDistance(state, dist * invScale);
  173. // Make sure we have a valid level of detail
  174. if (mEditorShapeInst->getCurrentDetail() < 0)
  175. return;
  176. BaseMatInstance* probePrevMat = mEditorShapeInst->getMaterialList()->getMaterialInst(0);
  177. setPreviewMatParameters(state, probePrevMat);
  178. // GFXTransformSaver is a handy helper class that restores
  179. // the current GFX matrices to their original values when
  180. // it goes out of scope at the end of the function
  181. // Set up our TS render state
  182. TSRenderState rdata;
  183. rdata.setSceneState(state);
  184. rdata.setFadeOverride(1.0f);
  185. // We might have some forward lit materials
  186. // so pass down a query to gather lights.
  187. LightQuery query;
  188. query.init(getWorldSphere());
  189. rdata.setLightQuery(&query);
  190. // Set the world matrix to the objects render transform
  191. MatrixF mat = getRenderTransform();
  192. mat.scale(Point3F(1, 1, 1));
  193. GFX->setWorldMatrix(mat);
  194. // Animate the the shape
  195. mEditorShapeInst->animate();
  196. // Allow the shape to submit the RenderInst(s) for itself
  197. mEditorShapeInst->render(rdata);
  198. saver.restore();
  199. }
  200. // If the light is selected or light visualization
  201. // is enabled then register the callback.
  202. const bool isSelectedInEditor = (gEditingMission && isSelected());
  203. if (isSelectedInEditor)
  204. {
  205. }
  206. }
  207. void Skylight::setPreviewMatParameters(SceneRenderState* renderState, BaseMatInstance* mat)
  208. {
  209. Parent::setPreviewMatParameters(renderState, mat);
  210. }
  211. DefineEngineMethod(Skylight, postApply, void, (), ,
  212. "A utility method for forcing a network update.\n")
  213. {
  214. object->inspectPostApply();
  215. }