From 04e1f2ccfa1417119601eef0a486ac533df5676c Mon Sep 17 00:00:00 2001 From: Kai Blaschke Date: Tue, 30 Sep 2025 19:37:58 +0200 Subject: [PATCH] Use Mesh class in CopyTexture class --- src/libprojectM/Renderer/CopyTexture.cpp | 67 +++++++++--------------- src/libprojectM/Renderer/CopyTexture.hpp | 23 ++++---- 2 files changed, 35 insertions(+), 55 deletions(-) diff --git a/src/libprojectM/Renderer/CopyTexture.cpp b/src/libprojectM/Renderer/CopyTexture.cpp index 6e6ec383c..36602b08e 100644 --- a/src/libprojectM/Renderer/CopyTexture.cpp +++ b/src/libprojectM/Renderer/CopyTexture.cpp @@ -1,8 +1,5 @@ #include "CopyTexture.hpp" -#include -#include - namespace libprojectM { namespace Renderer { @@ -16,7 +13,7 @@ static constexpr char CopyTextureVertexShader[] = R"( precision mediump float; layout(location = 0) in vec2 position; -layout(location = 1) in vec2 tex_coord; +layout(location = 2) in vec2 tex_coord; out vec2 fragment_tex_coord; @@ -52,48 +49,33 @@ void main(){ )"; CopyTexture::CopyTexture() + : m_mesh(VertexBufferUsage::StaticDraw, false, true) { - RenderItem::Init(); - m_framebuffer.CreateColorAttachment(0, 0); - std::string vertexShader(static_cast(ShaderVersion)); - std::string fragmentShader(static_cast(ShaderVersion)); - vertexShader.append(static_cast(CopyTextureVertexShader)); - fragmentShader.append(static_cast(CopyTextureFragmentShader)); + std::string vertexShader(ShaderVersion); + std::string fragmentShader(ShaderVersion); + vertexShader.append(CopyTextureVertexShader); + fragmentShader.append(CopyTextureFragmentShader); m_shader.CompileProgram(vertexShader, fragmentShader); -} -void CopyTexture::InitVertexAttrib() -{ - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); + m_mesh.SetRenderPrimitiveType(Mesh::PrimitiveType::TriangleStrip); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedPoint), reinterpret_cast(offsetof(TexturedPoint, x))); // Position - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedPoint), reinterpret_cast(offsetof(TexturedPoint, u))); // Texture coordinate + m_mesh.SetVertexCount(4); + m_mesh.Vertices().Set({{-1.0, 1.0}, + {1.0, 1.0}, + {-1.0, -1.0}, + {1.0, -1.0}}); - std::array points; + m_mesh.UVs().Set({{0.0, 1.0}, + {1.0, 1.0}, + {0.0, 0.0}, + {1.0, 0.0}}); - points[0].x = -1.0; - points[0].y = 1.0; - points[1].x = 1.0; - points[1].y = 1.0; - points[2].x = -1.0; - points[2].y = -1.0; - points[3].x = 1.0; - points[3].y = -1.0; + m_mesh.Indices().Set({0, 1, 2, 3}); - points[0].u = 0.0; - points[0].v = 1.0; - points[1].u = 1.0; - points[1].v = 1.0; - points[2].u = 0.0; - points[2].v = 0.0; - points[3].u = 1.0; - points[3].v = 0.0; - - glBufferData(GL_ARRAY_BUFFER, sizeof(points), points.data(), GL_STATIC_DRAW); + m_mesh.Update(); } void CopyTexture::Draw(const std::shared_ptr& originalTexture, bool flipVertical, bool flipHorizontal) @@ -160,9 +142,9 @@ void CopyTexture::Draw(const std::shared_ptr& originalTexture, co void CopyTexture::Draw(const std::shared_ptr& originalTexture, Framebuffer& framebuffer, int framebufferIndex, bool flipVertical, bool flipHorizontal) { - if (originalTexture == nullptr - || originalTexture->Empty() - || framebuffer.GetColorAttachmentTexture(framebufferIndex, 0) == nullptr + if (originalTexture == nullptr // + || originalTexture->Empty() // + || framebuffer.GetColorAttachmentTexture(framebufferIndex, 0) == nullptr // || framebuffer.GetColorAttachmentTexture(framebufferIndex, 0)->Empty()) { return; @@ -211,7 +193,7 @@ void CopyTexture::UpdateTextureSize(int width, int height) m_framebuffer.SetSize(m_width, m_height); } -void CopyTexture::Copy(bool flipVertical, bool flipHorizontal) const +void CopyTexture::Copy(bool flipVertical, bool flipHorizontal) { m_shader.Bind(); m_shader.SetUniformInt("texture_sampler", 0); @@ -219,11 +201,10 @@ void CopyTexture::Copy(bool flipVertical, bool flipHorizontal) const m_sampler.Bind(0); - glBindVertexArray(m_vaoID); - glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - glBindVertexArray(0); + m_mesh.Draw(); glBindTexture(GL_TEXTURE_2D, 0); + Mesh::Unbind(); Sampler::Unbind(0); Shader::Unbind(); } diff --git a/src/libprojectM/Renderer/CopyTexture.hpp b/src/libprojectM/Renderer/CopyTexture.hpp index e8911d3ca..9a7ea0cec 100644 --- a/src/libprojectM/Renderer/CopyTexture.hpp +++ b/src/libprojectM/Renderer/CopyTexture.hpp @@ -1,7 +1,7 @@ #pragma once #include "Renderer/Framebuffer.hpp" -#include "Renderer/RenderItem.hpp" +#include "Renderer/Mesh.hpp" #include "Renderer/Shader.hpp" namespace libprojectM { @@ -13,18 +13,16 @@ namespace Renderer { * * Optionally flips the image horizontally or vertically during the copy operation. */ -class CopyTexture : public RenderItem +class CopyTexture { public: CopyTexture(); - void InitVertexAttrib() override; - /** * @brief Copies the original texture into the currently bound framebuffer. * @param originalTexture The texture to be copied. - * @param flipVertical Flip image on the y axis when copying. - * @param flipHorizontal Flip image on the x axis when copying. + * @param flipVertical Flip image on the y-axis when copying. + * @param flipHorizontal Flip image on the x-axis when copying. */ void Draw(const std::shared_ptr& originalTexture, bool flipVertical = false, bool flipHorizontal = false); @@ -34,8 +32,8 @@ public: * The original and target textures must not be the same. * @param originalTexture The texture to be copied. * @param targetTexture Optional target texture to draw onto. - * @param flipVertical Flip image on the y axis when copying. - * @param flipHorizontal Flip image on the x axis when copying. + * @param flipVertical Flip image on the y-axis when copying. + * @param flipHorizontal Flip image on the x-axis when copying. */ void Draw(const std::shared_ptr& originalTexture, const std::shared_ptr& targetTexture = {}, bool flipVertical = false, bool flipHorizontal = false); @@ -47,8 +45,8 @@ public: * @param originalTexture The texture to be copied. * @param targetFramebuffer Optional target texture to draw onto. * @param framebufferIndex The index of the framebuffer to use. - * @param flipVertical Flip image on the y axis when copying. - * @param flipHorizontal Flip image on the x axis when copying. + * @param flipVertical Flip image on the y-axis when copying. + * @param flipHorizontal Flip image on the x-axis when copying. */ void Draw(const std::shared_ptr& originalTexture, Framebuffer& framebuffer, int framebufferIndex, bool flipVertical = false, bool flipHorizontal = false); @@ -58,7 +56,7 @@ public: * * @return The flipped texture. */ - auto Texture() -> std::shared_ptr; + auto Texture() -> std::shared_ptr; private: /** @@ -66,8 +64,9 @@ private: */ void UpdateTextureSize(int width, int height); - void Copy(bool flipVertical, bool flipHorizontal) const; + void Copy(bool flipVertical, bool flipHorizontal); + Mesh m_mesh; Shader m_shader; //!< Simple textured shader Framebuffer m_framebuffer{1}; //!< Framebuffer for drawing the flipped texture Sampler m_sampler{GL_CLAMP_TO_EDGE, GL_NEAREST}; //!< Texture sampler settings