BsSkybox.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "Reflection/BsIReflectable.h"
  6. #include "CoreThread/BsCoreObject.h"
  7. namespace bs
  8. {
  9. namespace ct
  10. {
  11. class RendererTask;
  12. }
  13. /** @addtogroup Implementation
  14. * @{
  15. */
  16. /** Signals which portion of a skybox is dirty. */
  17. enum class SkyboxDirtyFlag
  18. {
  19. Texture = 0x01,
  20. Everything = 0x02
  21. };
  22. /** Base class for both core and sim thread implementations of a skybox. */
  23. class BS_CORE_EXPORT SkyboxBase
  24. {
  25. public:
  26. SkyboxBase();
  27. virtual ~SkyboxBase() { }
  28. /** Checks whether the skybox should be used or not. */
  29. bool getIsActive() const { return mIsActive; }
  30. /** Sets whether the skybox should be used or not. */
  31. void setIsActive(bool active) { mIsActive = active; _markCoreDirty(); }
  32. /**
  33. * Brightness multiplier that will be applied to skybox values before they're being used. Allows you to make the
  34. * skybox more or less bright. Equal to one by default.
  35. */
  36. void setBrightness(float brightness) { mBrightness = brightness; _markCoreDirty(); }
  37. /** @see setBrightness */
  38. float getBrightness() const { return mBrightness; }
  39. /**
  40. * Marks the simulation thread object as dirty and notifies the system its data should be synced with its core
  41. * thread counterpart.
  42. */
  43. virtual void _markCoreDirty(SkyboxDirtyFlag flags = SkyboxDirtyFlag::Everything) { }
  44. protected:
  45. bool mIsActive; /**< Determines whether the skybox should be rendered or not. */
  46. float mBrightness; /**< Multiplier to apply to evaluated skybox values before using them. */
  47. };
  48. /** Templated base class for both core and sim thread implementations of a skybox. */
  49. template<bool Core>
  50. class BS_CORE_EXPORT TSkybox : public SkyboxBase
  51. {
  52. typedef typename TTextureType<Core>::Type TextureType;
  53. public:
  54. TSkybox();
  55. virtual ~TSkybox() { }
  56. /**
  57. * Assigns an environment map to use for sampling skybox radiance. Must be a cube-map texture, and should ideally
  58. * contain HDR data.
  59. */
  60. void setTexture(const TextureType& texture) { mTexture = texture; _markCoreDirty(SkyboxDirtyFlag::Texture); }
  61. /** Gets the texture assigned through setTexture(). */
  62. TextureType getTexture() const { return mTexture; }
  63. protected:
  64. TextureType mTexture;
  65. };
  66. /** @} */
  67. /** @addtogroup Renderer-Internal
  68. * @{
  69. */
  70. namespace ct { class Skybox; }
  71. /** Allows you to specify an environment map to use for sampling radiance of the sky. */
  72. class BS_CORE_EXPORT Skybox : public IReflectable, public CoreObject, public TSkybox<false>
  73. {
  74. public:
  75. ~Skybox();
  76. /** Retrieves an implementation of the skybox usable only from the core thread. */
  77. SPtr<ct::Skybox> getCore() const;
  78. /** Creates a new skybox. */
  79. static SPtr<Skybox> create();
  80. protected:
  81. Skybox();
  82. /**
  83. * Filters the skybox radiance texture, generating filtered radiance (for reflections) and irradiance. Should be
  84. * called any time the skybox texture changes.
  85. */
  86. void filterTexture();
  87. /** @copydoc CoreObject::createCore */
  88. SPtr<ct::CoreObject> createCore() const override;
  89. /** @copydoc SkyboxBase::_markCoreDirty */
  90. void _markCoreDirty(SkyboxDirtyFlag flags = SkyboxDirtyFlag::Everything) override;
  91. /** @copydoc CoreObject::syncToCore */
  92. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  93. SPtr<Texture> mFilteredRadiance;
  94. SPtr<Texture> mIrradiance;
  95. SPtr<ct::RendererTask> mRendererTask;
  96. /************************************************************************/
  97. /* RTTI */
  98. /************************************************************************/
  99. public:
  100. friend class SkyboxRTTI;
  101. static RTTITypeBase* getRTTIStatic();
  102. RTTITypeBase* getRTTI() const override;
  103. };
  104. namespace ct
  105. {
  106. /** Core thread usable version of a bs::Skybox */
  107. class BS_CORE_EXPORT Skybox : public CoreObject, public TSkybox<true>
  108. {
  109. public:
  110. ~Skybox();
  111. /**
  112. * Returns a texture containing filtered version of the radiance texture used for reflections. This might not
  113. * be available if it hasn't been generated yet.
  114. */
  115. SPtr<Texture> getFilteredRadiance() const { return mFilteredRadiance; }
  116. /**
  117. * Returns a texture containing sky irradiance. This might not be available if it hasn't been generated yet.
  118. */
  119. SPtr<Texture> getIrradiance() const { return mIrradiance; }
  120. protected:
  121. friend class bs::Skybox;
  122. Skybox(const SPtr<Texture>& filteredRadiance, const SPtr<Texture>& irradiance);
  123. /** @copydoc CoreObject::initialize */
  124. void initialize() override;
  125. /** @copydoc CoreObject::syncToCore */
  126. void syncToCore(const CoreSyncData& data) override;
  127. SPtr<Texture> mFilteredRadiance;
  128. SPtr<Texture> mIrradiance;
  129. };
  130. }
  131. /** @} */
  132. }