CWallpaper.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include "CWallpaper.h"
  2. #include "CScene.h"
  3. #include "CVideo.h"
  4. #include <utility>
  5. #include <glm/glm.hpp>
  6. #include <glm/gtc/type_ptr.hpp>
  7. using namespace WallpaperEngine::Render;
  8. CWallpaper::CWallpaper (Core::CWallpaper* wallpaperData, std::string type, CContainer* container) :
  9. m_container (container),
  10. m_wallpaperData (wallpaperData),
  11. m_type (std::move(type))
  12. {
  13. this->setupShaders ();
  14. GLfloat texCoords [] = {
  15. 0.0f, 0.0f,
  16. 1.0f, 0.0f,
  17. 0.0f, 1.0f,
  18. 0.0f, 1.0f,
  19. 1.0f, 0.0f,
  20. 1.0f, 1.0f
  21. };
  22. // inverted positions so the final texture is rendered properly
  23. GLfloat position [] = {
  24. -1.0f, 1.0f, 0.0f,
  25. 1.0, 1.0f, 0.0f,
  26. -1.0f, -1.0f, 0.0f,
  27. -1.0f, -1.0f, 0.0f,
  28. 1.0f, 1.0f, 0.0f,
  29. 1.0f, -1.0f, 0.0f
  30. };
  31. glGenBuffers (1, &this->m_texCoordBuffer);
  32. glBindBuffer (GL_ARRAY_BUFFER, this->m_texCoordBuffer);
  33. glBufferData (GL_ARRAY_BUFFER, sizeof (texCoords), texCoords, GL_STATIC_DRAW);
  34. glGenBuffers (1, &this->m_positionBuffer);
  35. glBindBuffer (GL_ARRAY_BUFFER, this->m_positionBuffer);
  36. glBufferData (GL_ARRAY_BUFFER, sizeof (position), position, GL_STATIC_DRAW);
  37. }
  38. CWallpaper::~CWallpaper ()
  39. {
  40. }
  41. CContainer* CWallpaper::getContainer () const
  42. {
  43. return this->m_container;
  44. }
  45. WallpaperEngine::Core::CWallpaper* CWallpaper::getWallpaperData ()
  46. {
  47. return this->m_wallpaperData;
  48. }
  49. GLuint CWallpaper::getWallpaperFramebuffer () const
  50. {
  51. return this->m_sceneFBO->getFramebuffer ();
  52. }
  53. GLuint CWallpaper::getWallpaperTexture () const
  54. {
  55. return this->m_sceneFBO->getTextureID();
  56. }
  57. void CWallpaper::setupShaders ()
  58. {
  59. // reserve shaders in OpenGL
  60. GLuint vertexShaderID = glCreateShader (GL_VERTEX_SHADER);
  61. // give shader's source code to OpenGL to be compiled
  62. const char* sourcePointer = "#version 120\n"
  63. "attribute vec3 a_Position;\n"
  64. "attribute vec2 a_TexCoord;\n"
  65. "varying vec2 v_TexCoord;\n"
  66. "void main () {\n"
  67. "gl_Position = vec4 (a_Position, 1.0);\n"
  68. "v_TexCoord = a_TexCoord;\n"
  69. "}";
  70. glShaderSource (vertexShaderID, 1, &sourcePointer, nullptr);
  71. glCompileShader (vertexShaderID);
  72. GLint result = GL_FALSE;
  73. int infoLogLength = 0;
  74. // ensure the vertex shader was correctly compiled
  75. glGetShaderiv (vertexShaderID, GL_COMPILE_STATUS, &result);
  76. glGetShaderiv (vertexShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
  77. if (infoLogLength > 0)
  78. {
  79. char* logBuffer = new char [infoLogLength + 1];
  80. // ensure logBuffer ends with a \0
  81. memset (logBuffer, 0, infoLogLength + 1);
  82. // get information about the error
  83. glGetShaderInfoLog (vertexShaderID, infoLogLength, nullptr, logBuffer);
  84. // throw an exception about the issue
  85. std::string message = logBuffer;
  86. // free the buffer
  87. delete[] logBuffer;
  88. // throw an exception
  89. throw std::runtime_error (message);
  90. }
  91. // reserve shaders in OpenGL
  92. GLuint fragmentShaderID = glCreateShader (GL_FRAGMENT_SHADER);
  93. // give shader's source code to OpenGL to be compiled
  94. sourcePointer = "#version 120\n"
  95. "uniform sampler2D g_Texture0;\n"
  96. "varying vec2 v_TexCoord;\n"
  97. "void main () {\n"
  98. "gl_FragColor = texture2D (g_Texture0, v_TexCoord);\n"
  99. "}";
  100. glShaderSource (fragmentShaderID, 1, &sourcePointer, nullptr);
  101. glCompileShader (fragmentShaderID);
  102. result = GL_FALSE;
  103. infoLogLength = 0;
  104. // ensure the vertex shader was correctly compiled
  105. glGetShaderiv (fragmentShaderID, GL_COMPILE_STATUS, &result);
  106. glGetShaderiv (fragmentShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
  107. if (infoLogLength > 0)
  108. {
  109. char* logBuffer = new char [infoLogLength + 1];
  110. // ensure logBuffer ends with a \0
  111. memset (logBuffer, 0, infoLogLength + 1);
  112. // get information about the error
  113. glGetShaderInfoLog (fragmentShaderID, infoLogLength, nullptr, logBuffer);
  114. // throw an exception about the issue
  115. std::string message = logBuffer;
  116. // free the buffer
  117. delete[] logBuffer;
  118. // throw an exception
  119. throw std::runtime_error (message);
  120. }
  121. // create the final program
  122. this->m_shader = glCreateProgram ();
  123. // link the shaders together
  124. glAttachShader (this->m_shader, vertexShaderID);
  125. glAttachShader (this->m_shader, fragmentShaderID);
  126. glLinkProgram (this->m_shader);
  127. // check that the shader was properly linked
  128. result = GL_FALSE;
  129. infoLogLength = 0;
  130. glGetProgramiv (this->m_shader, GL_LINK_STATUS, &result);
  131. glGetProgramiv (this->m_shader, GL_INFO_LOG_LENGTH, &infoLogLength);
  132. if (infoLogLength > 0)
  133. {
  134. char* logBuffer = new char [infoLogLength + 1];
  135. // ensure logBuffer ends with a \0
  136. memset (logBuffer, 0, infoLogLength + 1);
  137. // get information about the error
  138. glGetProgramInfoLog (this->m_shader, infoLogLength, nullptr, logBuffer);
  139. // throw an exception about the issue
  140. std::string message = logBuffer;
  141. // free the buffer
  142. delete[] logBuffer;
  143. // throw an exception
  144. throw std::runtime_error (message);
  145. }
  146. // after being liked shaders can be dettached and deleted
  147. glDetachShader (this->m_shader, vertexShaderID);
  148. glDetachShader (this->m_shader, fragmentShaderID);
  149. glDeleteShader (vertexShaderID);
  150. glDeleteShader (fragmentShaderID);
  151. // get textures
  152. this->g_Texture0 = glGetUniformLocation (this->m_shader, "g_Texture0");
  153. this->a_Position = glGetAttribLocation (this->m_shader, "a_Position");
  154. this->a_TexCoord = glGetAttribLocation (this->m_shader, "a_TexCoord");
  155. }
  156. void CWallpaper::render (glm::vec4 viewport, bool newFrame)
  157. {
  158. if (newFrame == true)
  159. this->renderFrame ();
  160. int windowWidth = 1920;
  161. int windowHeight = 1080;
  162. if (this->getWallpaperData ()->is <WallpaperEngine::Core::CScene> ())
  163. {
  164. auto projection = this->getWallpaperData ()->as <WallpaperEngine::Core::CScene> ()->getOrthogonalProjection ();
  165. windowWidth = projection->getWidth ();
  166. windowHeight = projection->getHeight ();
  167. }
  168. else if (this->is <WallpaperEngine::Render::CVideo> ())
  169. {
  170. auto video = this->as <WallpaperEngine::Render::CVideo> ();
  171. windowWidth = video->getWidth ();
  172. windowHeight = video->getHeight ();
  173. }
  174. float widthRatio = windowWidth / viewport.z;
  175. float heightRatio = windowHeight / viewport.w;
  176. if (widthRatio > 1.0f)
  177. {
  178. float diff = widthRatio - 1.0f;
  179. widthRatio -= diff;
  180. heightRatio -= diff;
  181. }
  182. if (heightRatio > 1.0f)
  183. {
  184. float diff = heightRatio - 1.0f;
  185. widthRatio -= diff;
  186. heightRatio -= diff;
  187. }
  188. if (widthRatio < 1.0f)
  189. {
  190. float diff = 1.0f - widthRatio;
  191. widthRatio += diff;
  192. heightRatio += diff;
  193. }
  194. if (heightRatio < 1.0f)
  195. {
  196. float diff = 1.0f - heightRatio;
  197. widthRatio += diff;
  198. heightRatio += diff;
  199. }
  200. if (widthRatio < 0.0f) widthRatio = -widthRatio;
  201. if (heightRatio < 0.0f) heightRatio = -heightRatio;
  202. GLfloat position [] = {
  203. widthRatio * -1.0f, heightRatio * 1.0f, 0.0f,
  204. widthRatio * 1.0f, heightRatio * 1.0f, 0.0f,
  205. widthRatio * -1.0f, heightRatio * -1.0f, 0.0f,
  206. widthRatio * -1.0f, heightRatio * -1.0f, 0.0f,
  207. widthRatio * 1.0f, heightRatio * 1.0f, 0.0f,
  208. widthRatio * 1.0f, heightRatio * -1.0f, 0.0f
  209. };
  210. glBindBuffer (GL_ARRAY_BUFFER, this->m_positionBuffer);
  211. glBufferData (GL_ARRAY_BUFFER, sizeof (position), position, GL_STATIC_DRAW);
  212. glViewport (viewport.x, viewport.y, viewport.z, viewport.w);
  213. // write to default's framebuffer
  214. glBindFramebuffer (GL_FRAMEBUFFER, GL_NONE);
  215. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  216. glDisable (GL_BLEND);
  217. glDisable (GL_DEPTH_TEST);
  218. // do not use any shader
  219. glUseProgram (this->m_shader);
  220. // activate scene texture
  221. glActiveTexture (GL_TEXTURE0);
  222. glBindTexture (GL_TEXTURE_2D, this->getWallpaperTexture ());
  223. // set uniforms and attribs
  224. glEnableVertexAttribArray (this->a_TexCoord);
  225. glBindBuffer (GL_ARRAY_BUFFER, this->m_texCoordBuffer);
  226. glVertexAttribPointer (this->a_TexCoord, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
  227. glEnableVertexAttribArray (this->a_Position);
  228. glBindBuffer (GL_ARRAY_BUFFER, this->m_positionBuffer);
  229. glVertexAttribPointer (this->a_Position, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
  230. glUniform1i (this->g_Texture0, 0);
  231. // write the framebuffer as is to the screen
  232. glBindBuffer (GL_ARRAY_BUFFER, this->m_texCoordBuffer);
  233. glDrawArrays (GL_TRIANGLES, 0, 6);
  234. }
  235. void CWallpaper::pinpongFramebuffer (GLuint* drawTo, GLuint* inputTexture)
  236. {
  237. // temporarily store FBOs used
  238. CFBO* currentMainFBO = this->m_mainFBO;
  239. CFBO* currentSubFBO = this->m_subFBO;
  240. if (drawTo != nullptr)
  241. *drawTo = currentSubFBO->getFramebuffer ();
  242. if (inputTexture != nullptr)
  243. *inputTexture = currentMainFBO->getTextureID();
  244. // swap the FBOs
  245. this->m_mainFBO = currentSubFBO;
  246. this->m_subFBO = currentMainFBO;
  247. }
  248. void CWallpaper::setupFramebuffers ()
  249. {
  250. int windowWidth = 1920;
  251. int windowHeight = 1080;
  252. if (this->getWallpaperData ()->is <WallpaperEngine::Core::CScene> ())
  253. {
  254. auto projection = this->getWallpaperData ()->as <WallpaperEngine::Core::CScene> ()->getOrthogonalProjection ();
  255. windowWidth = projection->getWidth ();
  256. windowHeight = projection->getHeight ();
  257. }
  258. else if (this->is <WallpaperEngine::Render::CVideo> ())
  259. {
  260. auto video = this->as <WallpaperEngine::Render::CVideo> ();
  261. windowWidth = video->getWidth ();
  262. windowHeight = video->getHeight ();
  263. }
  264. this->m_mainFBO = this->createFBO ("_rt_FullFrameBuffera", ITexture::TextureFormat::ARGB8888, 1.0, windowWidth, windowHeight, windowWidth, windowHeight);
  265. this->m_subFBO = this->createFBO ("_rt_FullFrameBuffera", ITexture::TextureFormat::ARGB8888, 1.0, windowWidth, windowHeight, windowWidth, windowHeight);
  266. // create framebuffer for the scene
  267. this->m_sceneFBO = this->createFBO ("_rt_FullFrameBuffer", ITexture::TextureFormat::ARGB8888, 1.0, windowWidth, windowHeight, windowWidth, windowHeight);
  268. }
  269. CFBO* CWallpaper::createFBO (const std::string& name, ITexture::TextureFormat format, float scale, uint32_t realWidth, uint32_t realHeight, uint32_t textureWidth, uint32_t textureHeight)
  270. {
  271. CFBO* fbo = new CFBO (name, format, scale, realWidth, realHeight, textureWidth, textureHeight);
  272. this->m_fbos.insert (std::make_pair (name, fbo));
  273. return fbo;
  274. }
  275. const std::map<std::string, CFBO*>& CWallpaper::getFBOs () const
  276. {
  277. return this->m_fbos;
  278. }
  279. const CFBO* CWallpaper::findFBO (const std::string& name) const
  280. {
  281. auto it = this->m_fbos.find (name);
  282. if (it == this->m_fbos.end ())
  283. throw std::runtime_error ("Cannot find given FBO");
  284. return it->second;
  285. }