CFBO.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "common.h"
  2. #include "CFBO.h"
  3. using namespace WallpaperEngine::Render;
  4. CFBO::CFBO (
  5. std::string name,
  6. ITexture::TextureFormat format,
  7. ITexture::TextureFlags flags,
  8. float scale,
  9. uint32_t realWidth, uint32_t realHeight,
  10. uint32_t textureWidth, uint32_t textureHeight
  11. ) :
  12. m_name (std::move (name)),
  13. m_format (format),
  14. m_scale (scale),
  15. m_flags (flags),
  16. m_framebuffer (GL_NONE),
  17. m_depthbuffer (GL_NONE),
  18. m_texture (GL_NONE),
  19. m_resolution ()
  20. {
  21. // create an empty texture that'll be free'd so the FBO is transparent
  22. GLenum drawBuffers [1] = {GL_COLOR_ATTACHMENT0};
  23. // create the main framebuffer
  24. glGenFramebuffers (1, &this->m_framebuffer);
  25. glBindFramebuffer (GL_FRAMEBUFFER, this->m_framebuffer);
  26. // create the main texture
  27. glGenTextures (1, &this->m_texture);
  28. // bind the new texture to set settings on it
  29. glBindTexture (GL_TEXTURE_2D, this->m_texture);
  30. // give OpenGL an empty image
  31. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  32. // label stuff for debugging
  33. #if !NDEBUG
  34. glObjectLabel (GL_TEXTURE, this->m_texture, -1, this->m_name.c_str ());
  35. #endif /* DEBUG */
  36. // set filtering parameters, otherwise the texture is not rendered
  37. if (flags & TextureFlags::ClampUVs)
  38. {
  39. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  40. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  41. }
  42. else if (flags & TextureFlags::ClampUVsBorder)
  43. {
  44. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  45. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  46. }
  47. else
  48. {
  49. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  50. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  51. }
  52. if (flags & TextureFlags::NoInterpolation)
  53. {
  54. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  55. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  56. }
  57. else
  58. {
  59. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  60. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  61. }
  62. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY, 8.0f);
  63. // set the texture as the colour attachmend #0
  64. glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->m_texture, 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. sLog.exception ("Framebuffers are not properly set");
  70. // clear the framebuffer
  71. glClear (GL_COLOR_BUFFER_BIT);
  72. this->m_resolution = {
  73. textureWidth, textureHeight,
  74. realWidth, realHeight
  75. };
  76. // create the textureframe entries
  77. auto* frame = new TextureFrame;
  78. frame->frameNumber = 0;
  79. frame->frametime = 0;
  80. frame->height1 = textureHeight;
  81. frame->height2 = realHeight;
  82. frame->width1 = textureWidth;
  83. frame->width2 = realWidth;
  84. frame->x = 0;
  85. frame->y = 0;
  86. this->m_frames.push_back (frame);
  87. }
  88. const std::string& CFBO::getName () const
  89. {
  90. return this->m_name;
  91. }
  92. const float& CFBO::getScale () const
  93. {
  94. return this->m_scale;
  95. }
  96. const ITexture::TextureFormat CFBO::getFormat () const
  97. {
  98. return this->m_format;
  99. }
  100. const ITexture::TextureFlags CFBO::getFlags () const
  101. {
  102. return this->m_flags;
  103. }
  104. GLuint CFBO::getFramebuffer () const
  105. {
  106. return this->m_framebuffer;
  107. }
  108. GLuint CFBO::getDepthbuffer () const
  109. {
  110. return this->m_depthbuffer;
  111. }
  112. const GLuint CFBO::getTextureID (uint32_t imageIndex) const
  113. {
  114. return this->m_texture;
  115. }
  116. const uint32_t CFBO::getTextureWidth (uint32_t imageIndex) const
  117. {
  118. return this->m_resolution.x;
  119. }
  120. const uint32_t CFBO::getTextureHeight (uint32_t imageIndex) const
  121. {
  122. return this->m_resolution.y;
  123. }
  124. const uint32_t CFBO::getRealWidth () const
  125. {
  126. return this->m_resolution.z;
  127. }
  128. const uint32_t CFBO::getRealHeight () const
  129. {
  130. return this->m_resolution.w;
  131. }
  132. const std::vector<ITexture::TextureFrame*>& CFBO::getFrames () const
  133. {
  134. return this->m_frames;
  135. }
  136. const glm::vec4* CFBO::getResolution () const
  137. {
  138. return &this->m_resolution;
  139. }
  140. const bool CFBO::isAnimated () const
  141. {
  142. return false;
  143. }