CWallpaper.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "CWallpaper.h"
  2. #include <utility>
  3. #include <glm/glm.hpp>
  4. using namespace WallpaperEngine::Render;
  5. CWallpaper::CWallpaper (Core::CWallpaper* wallpaperData, std::string type, CContainer* container) :
  6. m_container (container),
  7. m_wallpaperData (wallpaperData),
  8. m_type (std::move(type))
  9. {
  10. this->setupFramebuffers ();
  11. }
  12. CWallpaper::~CWallpaper ()
  13. {
  14. }
  15. CContainer* CWallpaper::getContainer () const
  16. {
  17. return this->m_container;
  18. }
  19. WallpaperEngine::Core::CWallpaper* CWallpaper::getWallpaperData ()
  20. {
  21. return this->m_wallpaperData;
  22. }
  23. void CWallpaper::pinpongFramebuffer (GLuint* drawTo, GLuint* inputTexture)
  24. {
  25. // get current main framebuffer and texture so we can swap them
  26. GLuint currentMainFramebuffer = this->m_mainFramebuffer;
  27. GLuint currentMainTexture = this->m_mainTexture;
  28. GLuint currentSubFramebuffer = this->m_subFramebuffer;
  29. GLuint currentSubTexture = this->m_subTexture;
  30. if (drawTo != nullptr)
  31. *drawTo = currentSubFramebuffer;
  32. if (inputTexture != nullptr)
  33. *inputTexture = currentMainTexture;
  34. // swap the textures
  35. this->m_mainFramebuffer = currentSubFramebuffer;
  36. this->m_mainTexture = currentSubTexture;
  37. this->m_subFramebuffer = currentMainFramebuffer;
  38. this->m_subTexture = currentMainTexture;
  39. }
  40. void CWallpaper::setupFramebuffers ()
  41. {
  42. // TODO: ENSURE THE WINDOW WIDTH AND HEIGHT IS CORRECT AND UPDATE THEM WHEN THE SCREEN CHANGES
  43. GLenum drawBuffers [1] = {GL_COLOR_ATTACHMENT0};
  44. // create the main framebuffer
  45. glGenFramebuffers (1, &this->m_mainFramebuffer);
  46. glBindFramebuffer (GL_FRAMEBUFFER, this->m_mainFramebuffer);
  47. // create the main texture
  48. glGenTextures (1, &this->m_mainTexture);
  49. // bind the new texture to set settings on it
  50. glBindTexture (GL_TEXTURE_2D, this->m_mainTexture);
  51. // give OpenGL an empty image
  52. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, 1920, 1080, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  53. // set filtering parameters, otherwise the texture is not rendered
  54. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  55. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  56. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  57. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  58. // create the depth render buffer for the main framebuffer
  59. glGenRenderbuffers (1, &this->m_mainDepthBuffer);
  60. glBindRenderbuffer (GL_RENDERBUFFER, this->m_mainDepthBuffer);
  61. glRenderbufferStorage (GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 1920, 1080);
  62. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, this->m_mainDepthBuffer);
  63. // set the texture as the colour attachmend #0
  64. glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->m_mainTexture, 0);
  65. // finally set the list of draw buffers
  66. glDrawBuffers (1, drawBuffers);
  67. // ensure first framebuffer is okay
  68. if (glCheckFramebufferStatus (GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
  69. throw std::runtime_error ("Framebuffers are not properly set");
  70. // create the sub framebuffer
  71. glGenFramebuffers (1, &this->m_subFramebuffer);
  72. glBindFramebuffer (GL_FRAMEBUFFER, this->m_subFramebuffer);
  73. // create the sub texture
  74. glGenTextures (1, &this->m_subTexture);
  75. // bind the new texture to set settings on it
  76. glBindTexture (GL_TEXTURE_2D, this->m_subTexture);
  77. // give OpenGL an empty image
  78. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, 1920, 1080, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  79. // set filtering parameters, otherwise the texture is not rendered
  80. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  81. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  82. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  83. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  84. // create the depth render buffer for the main framebuffer
  85. glGenRenderbuffers (1, &this->m_subDepthBuffer);
  86. glBindRenderbuffer (GL_RENDERBUFFER, this->m_subDepthBuffer);
  87. glRenderbufferStorage (GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 1920, 1080);
  88. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, this->m_subDepthBuffer);
  89. // set the texture as the colour attachmend #0
  90. glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->m_subTexture, 0);
  91. // finally set the list of draw buffers
  92. glDrawBuffers (1, drawBuffers);
  93. // ensure second framebuffer is okay
  94. if (glCheckFramebufferStatus (GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
  95. throw std::runtime_error ("Framebuffers are not properly set");
  96. }