CWallpaper.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #pragma once
  2. #include <GL/glew.h>
  3. #include <GLFW/glfw3.h>
  4. #include "WallpaperEngine/Audio/AudioContext.h"
  5. #include "WallpaperEngine/Render/CFBO.h"
  6. #include "WallpaperEngine/Render/Helpers/ContextAware.h"
  7. #include "WallpaperEngine/Render/RenderContext.h"
  8. #include "WallpaperEngine/Data/Model/Wallpaper.h"
  9. #include "FBOProvider.h"
  10. #include "WallpaperState.h"
  11. namespace WallpaperEngine::Application {
  12. class WallpaperApplication;
  13. }
  14. namespace WallpaperEngine::WebBrowser {
  15. class WebBrowserContext;
  16. }
  17. namespace WallpaperEngine::Render {
  18. namespace Helpers {
  19. class ContextAware;
  20. }
  21. using namespace WallpaperEngine::Render;
  22. using namespace WallpaperEngine::Audio;
  23. using namespace WallpaperEngine::Data::Model;
  24. using namespace WallpaperEngine::FileSystem;
  25. class CWallpaper : public Helpers::ContextAware, public FBOProvider {
  26. friend class WallpaperEngine::Application::WallpaperApplication;
  27. public:
  28. template <class T> [[nodiscard]] const T* as () const {
  29. if (is<T> ()) {
  30. return static_cast<const T*> (this);
  31. }
  32. throw std::bad_cast ();
  33. }
  34. template <class T> [[nodiscard]] T* as () {
  35. if (is<T> ()) {
  36. return static_cast<T*> (this);
  37. }
  38. throw std::bad_cast ();
  39. }
  40. template <class T> [[nodiscard]] bool is () const { return typeid (*this) == typeid (T); }
  41. virtual ~CWallpaper () override;
  42. /**
  43. * Performs a render pass of the wallpaper
  44. */
  45. void render (const glm::ivec4& viewport, const bool vflip);
  46. /**
  47. * Pause the renderer
  48. */
  49. virtual void setPause (bool newState);
  50. /**
  51. * @return The container to resolve files for this wallpaper
  52. */
  53. [[nodiscard]] const AssetLocator& getAssetLocator () const;
  54. /**
  55. * @return The current audio context for this wallpaper
  56. */
  57. AudioContext& getAudioContext () const;
  58. /**
  59. * @return The wallpaper state
  60. */
  61. [[nodiscard]] const WallpaperState& getState () const;
  62. /**
  63. * @return The scene's framebuffer
  64. */
  65. [[nodiscard]] virtual GLuint getWallpaperFramebuffer () const;
  66. /**
  67. * @return The scene's texture
  68. */
  69. [[nodiscard]] virtual GLuint getWallpaperTexture () const;
  70. /**
  71. * Searches the FBO list for the given FBO
  72. *
  73. * @param name
  74. * @return
  75. */
  76. [[nodiscard]] std::shared_ptr<const CFBO> findFBO (const std::string& name) const;
  77. /**
  78. * @return The main FBO of this wallpaper
  79. */
  80. [[nodiscard]] std::shared_ptr<const CFBO> getFBO () const;
  81. /**
  82. * Updates the UVs coordinates if window/screen/vflip/projection has changed
  83. */
  84. void updateUVs (const glm::ivec4& viewport, const bool vflip);
  85. /**
  86. * Updates the destination framebuffer for this wallpaper
  87. *
  88. * @param framebuffer
  89. */
  90. void setDestinationFramebuffer (GLuint framebuffer);
  91. /**
  92. * @return The width of this wallpaper
  93. */
  94. [[nodiscard]] virtual int getWidth () const = 0;
  95. /**
  96. * @return The height of this wallpaper
  97. */
  98. [[nodiscard]] virtual int getHeight () const = 0;
  99. /**
  100. * Creates a new instance of CWallpaper based on the information provided by the read backgrounds
  101. *
  102. * @param wallpaper
  103. * @param context
  104. * @param audioContext
  105. * @param scalingMode
  106. *
  107. * @return
  108. */
  109. static std::unique_ptr<CWallpaper> fromWallpaper (
  110. const Wallpaper& wallpaper, RenderContext& context, AudioContext& audioContext,
  111. WebBrowser::WebBrowserContext* browserContext, const WallpaperState::TextureUVsScaling& scalingMode,
  112. const uint32_t& clampMode
  113. );
  114. protected:
  115. CWallpaper (
  116. const Wallpaper& wallpaperData, RenderContext& context, AudioContext& audioContext,
  117. const WallpaperState::TextureUVsScaling& scalingMode, const uint32_t& clampMode
  118. );
  119. /**
  120. * Renders a frame of the wallpaper
  121. */
  122. virtual void renderFrame (const glm::ivec4& viewport) = 0;
  123. /**
  124. * Setups OpenGL's framebuffers for ping-pong and scene rendering
  125. */
  126. void setupFramebuffers ();
  127. const Wallpaper& m_wallpaperData;
  128. [[nodiscard]] const Wallpaper& getWallpaperData () const;
  129. /** The FBO used for scene output */
  130. std::shared_ptr<const CFBO> m_sceneFBO = nullptr;
  131. GLuint m_vaoBuffer = GL_NONE;
  132. private:
  133. /** The texture used for the scene output */
  134. GLuint m_texCoordBuffer = GL_NONE;
  135. GLuint m_positionBuffer = GL_NONE;
  136. GLuint m_shader = GL_NONE;
  137. // shader variables
  138. GLint g_Texture0 = GL_NONE;
  139. GLint a_Position = GL_NONE;
  140. GLint a_TexCoord = GL_NONE;
  141. /** The framebuffer to draw the background to */
  142. GLuint m_destFramebuffer = GL_NONE;
  143. /** Setups OpenGL's shaders for this wallpaper backbuffer */
  144. void setupShaders ();
  145. /** List of FBOs registered for this wallpaper */
  146. std::map<std::string, std::shared_ptr<const CFBO>> m_fbos = {};
  147. /** Audio context that is using this wallpaper */
  148. AudioContext& m_audioContext;
  149. /** Current Wallpaper state */
  150. WallpaperState m_state;
  151. };
  152. } // namespace WallpaperEngine::Render