From caf79766dc747a44c6e3df20cb4c82d27c3916ae Mon Sep 17 00:00:00 2001 From: Hynak Date: Sat, 26 Feb 2022 17:40:16 -0500 Subject: [PATCH] Consolidated the renderImage function into render. --- src/WallpaperEngine/Render/CContext.cpp | 5 +- src/WallpaperEngine/Render/CWallpaper.cpp | 176 ++++++---------------- src/WallpaperEngine/Render/CWallpaper.h | 7 +- 3 files changed, 52 insertions(+), 136 deletions(-) diff --git a/src/WallpaperEngine/Render/CContext.cpp b/src/WallpaperEngine/Render/CContext.cpp index e531726..be1062b 100644 --- a/src/WallpaperEngine/Render/CContext.cpp +++ b/src/WallpaperEngine/Render/CContext.cpp @@ -101,7 +101,10 @@ void CContext::render () GC gc = XCreateGC(display, pm, 0, NULL); XFillRectangle(display, pm, gc, 0, 0, fullWidth, fullHeight); - char* image_data = this->m_wallpaper->renderImage (); + char* image_data; + image_data = new char[windowWidth*windowHeight*4]; + + this->m_wallpaper->render (this->m_defaultViewport, true, image_data); XImage* image = XCreateImage(display, CopyFromParent, 24, ZPixmap, 0, (char *)image_data, windowWidth, windowHeight, 32, 0); for (; cur != end; cur ++) { diff --git a/src/WallpaperEngine/Render/CWallpaper.cpp b/src/WallpaperEngine/Render/CWallpaper.cpp index f206fa9..b87bd9b 100644 --- a/src/WallpaperEngine/Render/CWallpaper.cpp +++ b/src/WallpaperEngine/Render/CWallpaper.cpp @@ -185,116 +185,12 @@ void CWallpaper::setupShaders () this->a_TexCoord = glGetAttribLocation (this->m_shader, "a_TexCoord"); } -void CWallpaper::render (glm::vec4 viewport, bool renderFrame, bool newFrame) +void CWallpaper::render (glm::vec4 viewport, bool drawToBackground, char* image_data) { - if (renderFrame == true) - this->renderFrame (viewport); - - int windowWidth = 1920; - int windowHeight = 1080; - - if (this->getWallpaperData ()->is ()) - { - auto projection = this->getWallpaperData ()->as ()->getOrthogonalProjection (); - - windowWidth = projection->getWidth (); - windowHeight = projection->getHeight (); - } - else if (this->is ()) - { - auto video = this->as (); - - windowWidth = video->getWidth (); - windowHeight = video->getHeight (); - } - - float widthRatio = windowWidth / viewport.z; - float heightRatio = windowHeight / viewport.w; - - if (widthRatio > 1.0f) - { - float diff = widthRatio - 1.0f; - - widthRatio -= diff; - heightRatio -= diff; - } - - if (heightRatio > 1.0f) - { - float diff = heightRatio - 1.0f; - - widthRatio -= diff; - heightRatio -= diff; - } - - if (widthRatio < 1.0f) - { - float diff = 1.0f - widthRatio; - - widthRatio += diff; - heightRatio += diff; - } - - if (heightRatio < 1.0f) - { - float diff = 1.0f - heightRatio; - - widthRatio += diff; - heightRatio += diff; - } - - if (widthRatio < 0.0f) widthRatio = -widthRatio; - if (heightRatio < 0.0f) heightRatio = -heightRatio; - - GLfloat position [] = { - -widthRatio, -heightRatio, 0.0f, - widthRatio, -heightRatio, 0.0f, - -widthRatio, heightRatio, 0.0f, - -widthRatio, heightRatio, 0.0f, - widthRatio, -heightRatio, 0.0f, - widthRatio, heightRatio, 0.0f - }; - - glBindBuffer (GL_ARRAY_BUFFER, this->m_positionBuffer); - glBufferData (GL_ARRAY_BUFFER, sizeof (position), position, GL_STATIC_DRAW); - - glViewport (viewport.x, viewport.y, viewport.z, viewport.w); - - // write to default's framebuffer - glBindFramebuffer (GL_FRAMEBUFFER, GL_NONE); - - if (newFrame == true) - glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glDisable (GL_BLEND); - glDisable (GL_DEPTH_TEST); - // do not use any shader - glUseProgram (this->m_shader); - // activate scene texture - glActiveTexture (GL_TEXTURE0); - glBindTexture (GL_TEXTURE_2D, this->getWallpaperTexture ()); - // set uniforms and attribs - glEnableVertexAttribArray (this->a_TexCoord); - glBindBuffer (GL_ARRAY_BUFFER, this->m_texCoordBuffer); - glVertexAttribPointer (this->a_TexCoord, 2, GL_FLOAT, GL_FALSE, 0, nullptr); - - glEnableVertexAttribArray (this->a_Position); - glBindBuffer (GL_ARRAY_BUFFER, this->m_positionBuffer); - glVertexAttribPointer (this->a_Position, 3, GL_FLOAT, GL_FALSE, 0, nullptr); - - glUniform1i (this->g_Texture0, 0); - // write the framebuffer as is to the screen - glBindBuffer (GL_ARRAY_BUFFER, this->m_texCoordBuffer); - glDrawArrays (GL_TRIANGLES, 0, 6); -} - -char* CWallpaper::renderImage () -{ - int windowWidth = 1920; - int windowHeight = 1080; - glm::vec4 viewport = {0, 0, windowWidth, windowHeight}; - this->renderFrame (viewport); + int windowWidth = 1920; + int windowHeight = 1080; if (this->getWallpaperData ()->is ()) { @@ -361,26 +257,52 @@ char* CWallpaper::renderImage () glBindBuffer (GL_ARRAY_BUFFER, this->m_positionBuffer); glBufferData (GL_ARRAY_BUFFER, sizeof (position), position, GL_STATIC_DRAW); - // Need to flip the image (FB stores the image upside down) - GLfloat texCoords [] = { - 0.0f, 1.0f, - 1.0f, 1.0f, - 0.0f, 0.0f, - 0.0f, 0.0f, - 1.0f, 1.0f, - 1.0f, 0.0f - }; - - glBindBuffer (GL_ARRAY_BUFFER, this->m_texCoordBuffer); - glBufferData (GL_ARRAY_BUFFER, sizeof (texCoords), texCoords, GL_STATIC_DRAW); + // we only want texCoords to be set once + static bool setTexCoords = true; + if (setTexCoords) + { + setTexCoords = false; + if (drawToBackground) + { + // Need to flip the image (FB stores the image upside down) + GLfloat texCoords [] = { + 0.0f, 1.0f, + 1.0f, 1.0f, + 0.0f, 0.0f, + 0.0f, 0.0f, + 1.0f, 1.0f, + 1.0f, 0.0f + }; + glBindBuffer (GL_ARRAY_BUFFER, this->m_texCoordBuffer); + glBufferData (GL_ARRAY_BUFFER, sizeof (texCoords), texCoords, GL_STATIC_DRAW); + } + else + { + GLfloat texCoords [] = { + 0.0f, 0.0f, + 1.0f, 0.0f, + 0.0f, 1.0f, + 0.0f, 1.0f, + 1.0f, 0.0f, + 1.0f, 1.0f + }; + glBindBuffer (GL_ARRAY_BUFFER, this->m_texCoordBuffer); + glBufferData (GL_ARRAY_BUFFER, sizeof (texCoords), texCoords, GL_STATIC_DRAW); + } + } glViewport (viewport.x, viewport.y, viewport.z, viewport.w); - // A fbo for flipping the image. Without this call the image will be flipped. - static CFBO* screen_fbo = this->createFBO ("_sc_FullFrameBuffer", ITexture::TextureFormat::ARGB8888, 1.0, windowWidth, windowHeight, windowWidth, windowHeight); - - glBindFramebuffer (GL_FRAMEBUFFER, screen_fbo->getFramebuffer()); + static CFBO* screen_fbo = new CFBO("_sc_FullFrameBuffer", ITexture::TextureFormat::ARGB8888, 1.0, windowWidth, windowHeight, windowWidth, windowHeight); + + if (drawToBackground) + // write to screen buffer + glBindFramebuffer (GL_FRAMEBUFFER, screen_fbo->getFramebuffer()); + else + // write to default's framebuffer + glBindFramebuffer (GL_FRAMEBUFFER, GL_NONE); + glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDisable (GL_BLEND); glDisable (GL_DEPTH_TEST); @@ -403,13 +325,9 @@ char* CWallpaper::renderImage () glBindBuffer (GL_ARRAY_BUFFER, this->m_texCoordBuffer); glDrawArrays (GL_TRIANGLES, 0, 6); - glBindTexture (GL_TEXTURE_2D, screen_fbo->getTextureID ()); - // Get FB data from OpenGL, X11 will free this pointer when it is created into an XImage. - char* image; - image = (char*)malloc(windowWidth*windowHeight*4); - glReadPixels(0, 0, 1920, 1080, GL_BGRA, GL_UNSIGNED_BYTE, (void*)(image)); - return image; + if (image_data) + glReadPixels(0, 0, 1920, 1080, GL_BGRA, GL_UNSIGNED_BYTE, (void*)(image_data)); } void CWallpaper::setupFramebuffers () diff --git a/src/WallpaperEngine/Render/CWallpaper.h b/src/WallpaperEngine/Render/CWallpaper.h index 9454251..c4d1b39 100644 --- a/src/WallpaperEngine/Render/CWallpaper.h +++ b/src/WallpaperEngine/Render/CWallpaper.h @@ -31,12 +31,7 @@ namespace WallpaperEngine::Render /** * Performs a render pass of the wallpaper */ - void render (glm::vec4 viewport, bool renderFrame = true, bool newFrame = true); - - /** - * Performs a render pass of the wallpaper and returns the char* of the image - */ - char* renderImage (); + void render (glm::vec4 viewport, bool drawToBackground = false, char* image_data = nullptr); /** * @return The container to resolve files for this wallpaper