Skip to content

Commit 87e2698

Browse files
committed
Use Mesh class in Blur effect
1 parent dd9a4b4 commit 87e2698

File tree

3 files changed

+23
-35
lines changed

3 files changed

+23
-35
lines changed

src/libprojectM/MilkdropPreset/BlurTexture.cpp

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,34 @@
55

66
#include "MilkdropStaticShaders.hpp"
77

8-
#include "Renderer/ShaderCache.hpp"
8+
#include <Renderer/Point.hpp>
9+
#include <Renderer/ShaderCache.hpp>
910

1011
#include <array>
1112

1213
namespace libprojectM {
1314
namespace MilkdropPreset {
1415

1516
BlurTexture::BlurTexture()
16-
: m_blurSampler(std::make_shared<Renderer::Sampler>(GL_CLAMP_TO_EDGE, GL_LINEAR))
17+
: m_blurMesh(Renderer::VertexBufferUsage::StaticDraw, false, true)
18+
, m_blurSampler(std::make_shared<Renderer::Sampler>(GL_CLAMP_TO_EDGE, GL_LINEAR))
1719
{
1820
m_blurFramebuffer.CreateColorAttachment(0, 0);
1921

20-
// Initialize Blur VAO/VBO with a single fullscreen quad.
21-
static constexpr std::array<float, 16> pointsBlur{
22-
-1.0, -1.0, 0.0, 0.0,
23-
1.0, -1.0, 1.0, 0.0,
24-
-1.0, 1.0, 0.0, 1.0,
25-
1.0, 1.0, 1.0, 1.0};
22+
// Initialize blur mesh with a single fullscreen quad.
23+
m_blurMesh.SetRenderPrimitiveType(Renderer::Mesh::PrimitiveType::TriangleStrip);
2624

27-
glGenBuffers(1, &m_vboBlur);
28-
glGenVertexArrays(1, &m_vaoBlur);
25+
m_blurMesh.Vertices().Set({{-1.0f, -1.0f},
26+
{1.0f, -1.0f},
27+
{-1.0f, 1.0f},
28+
{1.0f, 1.0f}});
2929

30-
glBindVertexArray(m_vaoBlur);
31-
glBindBuffer(GL_ARRAY_BUFFER, m_vboBlur);
30+
m_blurMesh.UVs().Set({{0.0, 0.0},
31+
{1.0, 0.0},
32+
{0.0, 1.0},
33+
{1.0, 1.0}});
3234

33-
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * pointsBlur.size(), pointsBlur.data(), GL_STATIC_DRAW);
34-
35-
glEnableVertexAttribArray(0);
36-
glEnableVertexAttribArray(1);
37-
38-
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, nullptr); // Position at index 0 and 1
39-
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, reinterpret_cast<void*>(sizeof(float) * 2)); // Texture coord at index 2 and 3
40-
41-
glBindVertexArray(0);
42-
glBindBuffer(GL_ARRAY_BUFFER, 0);
35+
m_blurMesh.Update();
4336

4437
// Initialize with empty textures.
4538
for (size_t i = 0; i < m_blurTextures.size(); i++)
@@ -54,12 +47,6 @@ BlurTexture::BlurTexture()
5447
}
5548
}
5649

57-
BlurTexture::~BlurTexture()
58-
{
59-
glDeleteBuffers(1, &m_vboBlur);
60-
glDeleteVertexArrays(1, &m_vaoBlur);
61-
}
62-
6350
void BlurTexture::Initialize(const Renderer::RenderContext& renderContext)
6451
{
6552
auto staticShaders = libprojectM::MilkdropPreset::MilkdropStaticShaders::Get();
@@ -164,7 +151,6 @@ void BlurTexture::Update(const Renderer::Texture& sourceTexture, const PerFrameC
164151
m_blurFramebuffer.Bind(0);
165152

166153
glBlendFunc(GL_ONE, GL_ZERO);
167-
glBindVertexArray(m_vaoBlur);
168154

169155
for (unsigned int pass = 0; pass < passes; pass++)
170156
{
@@ -268,15 +254,15 @@ void BlurTexture::Update(const Renderer::Texture& sourceTexture, const PerFrameC
268254
}
269255

270256
// Draw fullscreen quad
271-
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
257+
m_blurMesh.Draw();
272258

273259
// Save to blur texture
274260
m_blurTextures[pass]->Bind(0);
275261
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_blurTextures[pass]->Width(), m_blurTextures[pass]->Height());
276262
m_blurTextures[pass]->Unbind(0);
277263
}
278264

279-
glBindVertexArray(0);
265+
Renderer::Mesh::Unbind();
280266
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
281267

282268
// Bind previous framebuffer and reset viewport size

src/libprojectM/MilkdropPreset/BlurTexture.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
*/
55
#pragma once
66

7+
#include "Renderer/Mesh.hpp"
8+
9+
710
#include <Renderer/Framebuffer.hpp>
811
#include <Renderer/RenderContext.hpp>
912
#include <Renderer/Shader.hpp>
@@ -48,7 +51,7 @@ class BlurTexture
4851
/**
4952
* Destructor.
5053
*/
51-
~BlurTexture();
54+
virtual ~BlurTexture() = default;
5255

5356
/**
5457
* @brief Initializes the blur texture.
@@ -105,8 +108,7 @@ class BlurTexture
105108
*/
106109
void AllocateTextures(const Renderer::Texture& sourceTexture);
107110

108-
GLuint m_vboBlur; //!< Vertex buffer object for the fullscreen blur quad.
109-
GLuint m_vaoBlur; //!< Vertex array object for the fullscreen blur quad.
111+
Renderer::Mesh m_blurMesh; //!< The blur mesh (a simple quad).
110112

111113
std::weak_ptr<Renderer::Shader> m_blur1Shader; //!< The shader used on the first blur pass.
112114
std::weak_ptr<Renderer::Shader> m_blur2Shader; //!< The shader used for subsequent blur passes after the initial pass.

src/libprojectM/MilkdropPreset/Shaders/BlurVertexShaderGlsl330.vert

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
precision mediump float;
22

33
layout(location = 0) in vec2 vertex_position;
4-
layout(location = 1) in vec2 vertex_texture;
4+
layout(location = 2) in vec2 vertex_texture;
55

66
uniform int flipVertical;
77

0 commit comments

Comments
 (0)