BsRendererCamera.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsRenderBeastPrerequisites.h"
  5. #include "BsPostProcessing.h"
  6. #include "BsObjectRendering.h"
  7. #include "BsRenderQueue.h"
  8. #include "BsRendererObject.h"
  9. #include "BsBounds.h"
  10. namespace bs { namespace ct
  11. {
  12. /** @addtogroup RenderBeast
  13. * @{
  14. */
  15. BS_PARAM_BLOCK_BEGIN(PerCameraParamDef)
  16. BS_PARAM_BLOCK_ENTRY(Vector3, gViewDir)
  17. BS_PARAM_BLOCK_ENTRY(Vector3, gViewOrigin)
  18. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatViewProj)
  19. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatView)
  20. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatProj)
  21. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatInvProj)
  22. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatInvViewProj)
  23. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatScreenToWorld)
  24. BS_PARAM_BLOCK_ENTRY(Vector2, gDeviceZToWorldZ)
  25. BS_PARAM_BLOCK_ENTRY(Vector4, gClipToUVScaleOffset)
  26. BS_PARAM_BLOCK_END
  27. extern PerCameraParamDef gPerCameraParamDef;
  28. /** Shader that renders a skybox using a cubemap. */
  29. class SkyboxMat : public RendererMaterial<SkyboxMat>
  30. {
  31. RMAT_DEF("Skybox.bsl");
  32. public:
  33. SkyboxMat();
  34. /** Binds the material for rendering and sets up any global parameters. */
  35. void bind(const SPtr<GpuParamBlockBuffer>& perCamera);
  36. /** Updates the skybox texture used by the material. */
  37. void setParams(const SPtr<Texture>& texture);
  38. private:
  39. GpuParamTexture mSkyTextureParam;
  40. };
  41. /** Contains information about a Camera, used by the Renderer. */
  42. class RendererCamera
  43. {
  44. public:
  45. RendererCamera();
  46. RendererCamera(const Camera* camera, StateReduction reductionMode);
  47. /** Updates the internal camera data, usually called after source camera changes. */
  48. void update(StateReduction reductionMode);
  49. /** Updates the internal camera post-processing data. */
  50. void updatePP();
  51. /**
  52. * Prepares camera render targets for rendering. When done call endRendering().
  53. *
  54. * @param[in] useRenderTargets If using the internal render targets containing the GBuffer (retrieved via
  55. * getRenderTargets()) while rendering you must set this to true.
  56. */
  57. void beginRendering(bool useRenderTargets);
  58. /** Ends rendering and frees any acquired resources. */
  59. void endRendering();
  60. /** Returns the camera's renderTargets. Only valid if called in-between beginRendering() and endRendering() calls. */
  61. SPtr<RenderTargets> getRenderTargets() const { return mRenderTargets; }
  62. /**
  63. * Returns a render queue containing all opaque objects. Make sure to call determineVisible() beforehand if camera
  64. * or object transforms changed since the last time it was called.
  65. */
  66. const SPtr<RenderQueue>& getOpaqueQueue() const { return mOpaqueQueue; }
  67. /**
  68. * Returns a render queue containing all transparent objects. Make sure to call determineVisible() beforehand if
  69. * camera or object transforms changed since the last time it was called.
  70. */
  71. const SPtr<RenderQueue>& getTransparentQueue() const { return mTransparentQueue; }
  72. /**
  73. * Populates camera render queues by determining visible renderable objects.
  74. *
  75. * @param[in] renderables A set of renderable objects to iterate over and determine visibility for.
  76. * @param[in] renderableBounds A set of world bounds for the provided renderable objects. Must be the same size
  77. * as the @p renderables array.
  78. * @param[in] visibility Output parameter that will have the true bit set for any visible renderable
  79. * object. If the bit for an object is already set to true, the method will never
  80. * change it to false which allows the same bitfield to be provided to multiple
  81. * renderer cameras. Must be the same size as the @p renderables array.
  82. *
  83. * As a side-effect, per-camera visibility data is also calculated and can be
  84. * retrieved by calling getVisibilityMask().
  85. */
  86. void determineVisible(const Vector<RendererObject*>& renderables, const Vector<Bounds>& renderableBounds,
  87. Vector<bool>& visibility);
  88. /** Returns the visibility mask calculated with the last call to determineVisible(). */
  89. const Vector<bool> getVisibilityMask() const { return mVisibility; }
  90. /**
  91. * Returns a structure containing information about post-processing effects. This structure will be modified and
  92. * maintained by the post-processing system.
  93. */
  94. PostProcessInfo& getPPInfo() { return mPostProcessInfo; }
  95. /** Updates the GPU buffer containing per-camera information, with the latest data. */
  96. void updatePerCameraBuffer();
  97. /** Returns a buffer that stores per-camera parameters. */
  98. SPtr<GpuParamBlockBuffer> getPerCameraBuffer() const { return mParamBuffer; }
  99. private:
  100. /**
  101. * Extracts the necessary values from the projection matrix that allow you to transform device Z value into
  102. * world Z value.
  103. *
  104. * @param[in] projMatrix Projection matrix that was used to create the device Z value to transform.
  105. * @return Returns two values that can be used to transform device z to world z using this formula:
  106. * z = (deviceZ + y) * x.
  107. */
  108. Vector2 getDeviceZTransform(const Matrix4& projMatrix) const;
  109. const Camera* mCamera;
  110. SPtr<RenderQueue> mOpaqueQueue;
  111. SPtr<RenderQueue> mTransparentQueue;
  112. SPtr<RenderTargets> mRenderTargets;
  113. PostProcessInfo mPostProcessInfo;
  114. bool mUsingRenderTargets;
  115. SPtr<GpuParamBlockBuffer> mParamBuffer;
  116. Vector<bool> mVisibility;
  117. };
  118. /** @} */
  119. }}