Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 20 additions & 33 deletions src/libprojectM/MilkdropPreset/BlurTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,35 @@

#include "MilkdropStaticShaders.hpp"

#include "Renderer/ShaderCache.hpp"
#include <Renderer/BlendMode.hpp>
#include <Renderer/Point.hpp>
#include <Renderer/ShaderCache.hpp>

#include <array>

namespace libprojectM {
namespace MilkdropPreset {

BlurTexture::BlurTexture()
: m_blurSampler(std::make_shared<Renderer::Sampler>(GL_CLAMP_TO_EDGE, GL_LINEAR))
: m_blurMesh(Renderer::VertexBufferUsage::StaticDraw, false, true)
, m_blurSampler(std::make_shared<Renderer::Sampler>(GL_CLAMP_TO_EDGE, GL_LINEAR))
{
m_blurFramebuffer.CreateColorAttachment(0, 0);

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

glGenBuffers(1, &m_vboBlur);
glGenVertexArrays(1, &m_vaoBlur);
m_blurMesh.Vertices().Set({{-1.0f, -1.0f},
{1.0f, -1.0f},
{-1.0f, 1.0f},
{1.0f, 1.0f}});

glBindVertexArray(m_vaoBlur);
glBindBuffer(GL_ARRAY_BUFFER, m_vboBlur);
m_blurMesh.UVs().Set({{0.0, 0.0},
{1.0, 0.0},
{0.0, 1.0},
{1.0, 1.0}});

glBufferData(GL_ARRAY_BUFFER, sizeof(float) * pointsBlur.size(), pointsBlur.data(), GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, nullptr); // Position at index 0 and 1
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, reinterpret_cast<void*>(sizeof(float) * 2)); // Texture coord at index 2 and 3

glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
m_blurMesh.Update();

// Initialize with empty textures.
for (size_t i = 0; i < m_blurTextures.size(); i++)
Expand All @@ -54,12 +48,6 @@ BlurTexture::BlurTexture()
}
}

BlurTexture::~BlurTexture()
{
glDeleteBuffers(1, &m_vboBlur);
glDeleteVertexArrays(1, &m_vaoBlur);
}

void BlurTexture::Initialize(const Renderer::RenderContext& renderContext)
{
auto staticShaders = libprojectM::MilkdropPreset::MilkdropStaticShaders::Get();
Expand Down Expand Up @@ -163,8 +151,7 @@ void BlurTexture::Update(const Renderer::Texture& sourceTexture, const PerFrameC

m_blurFramebuffer.Bind(0);

glBlendFunc(GL_ONE, GL_ZERO);
glBindVertexArray(m_vaoBlur);
Renderer::BlendMode::Set(true, Renderer::BlendMode::Function::One, Renderer::BlendMode::Function::Zero);

for (unsigned int pass = 0; pass < passes; pass++)
{
Expand Down Expand Up @@ -268,16 +255,16 @@ void BlurTexture::Update(const Renderer::Texture& sourceTexture, const PerFrameC
}

// Draw fullscreen quad
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
m_blurMesh.Draw();

// Save to blur texture
m_blurTextures[pass]->Bind(0);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_blurTextures[pass]->Width(), m_blurTextures[pass]->Height());
m_blurTextures[pass]->Unbind(0);
}

glBindVertexArray(0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Renderer::Mesh::Unbind();
Renderer::BlendMode::Set(false, Renderer::BlendMode::Function::SourceAlpha, Renderer::BlendMode::Function::OneMinusSourceAlpha);

// Bind previous framebuffer and reset viewport size
glBindFramebuffer(GL_READ_FRAMEBUFFER, origReadFramebuffer);
Expand Down
8 changes: 5 additions & 3 deletions src/libprojectM/MilkdropPreset/BlurTexture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
*/
#pragma once

#include "Renderer/Mesh.hpp"


#include <Renderer/Framebuffer.hpp>
#include <Renderer/RenderContext.hpp>
#include <Renderer/Shader.hpp>
Expand Down Expand Up @@ -48,7 +51,7 @@ class BlurTexture
/**
* Destructor.
*/
~BlurTexture();
virtual ~BlurTexture() = default;

/**
* @brief Initializes the blur texture.
Expand Down Expand Up @@ -105,8 +108,7 @@ class BlurTexture
*/
void AllocateTextures(const Renderer::Texture& sourceTexture);

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

std::weak_ptr<Renderer::Shader> m_blur1Shader; //!< The shader used on the first blur pass.
std::weak_ptr<Renderer::Shader> m_blur2Shader; //!< The shader used for subsequent blur passes after the initial pass.
Expand Down
80 changes: 33 additions & 47 deletions src/libprojectM/MilkdropPreset/Border.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
#include "Border.hpp"

#include <Renderer/BlendMode.hpp>

namespace libprojectM {
namespace MilkdropPreset {

Border::Border(PresetState& presetState)
: RenderItem()
, m_presetState(presetState)
: m_presetState(presetState)
, m_borderMesh(Renderer::VertexBufferUsage::StreamDraw)
{
RenderItem::Init();
}
m_borderMesh.SetRenderPrimitiveType(Renderer::Mesh::PrimitiveType::Triangles);
m_borderMesh.SetVertexCount(8);

void Border::InitVertexAttrib()
{
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glDisableVertexAttribArray(1);

std::array<Point, 4> vertices{};
glBufferData(GL_ARRAY_BUFFER, sizeof(Point) * 4, vertices.data(), GL_STREAM_DRAW);
m_borderMesh.Indices().Set({
{
0, 1, 4,
1, 4, 5,
2, 3, 6,
3, 7, 6,
2, 0, 6,
0, 4, 6,
3, 7, 5,
1, 3, 5
}});
}

void Border::Draw(const PerFrameContext& presetPerFrameContext)
Expand All @@ -26,18 +31,15 @@ void Border::Draw(const PerFrameContext& presetPerFrameContext)
float const outerBorderSize = static_cast<float>(*presetPerFrameContext.ob_size);
float const innerBorderSize = static_cast<float>(*presetPerFrameContext.ib_size);

glBindVertexArray(m_vaoID);
glBindBuffer(GL_ARRAY_BUFFER, m_vboID);

// No additive drawing for borders
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Renderer::BlendMode::Set(true, Renderer::BlendMode::Function::SourceAlpha, Renderer::BlendMode::Function::OneMinusSourceAlpha);

auto shader = m_presetState.untexturedShader.lock();
shader->Bind();
shader->SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);

std::array<Point, 4> vertices{};
m_borderMesh.Bind();

for (int border = 0; border < 2; border++)
{
float r = (border == 0) ? static_cast<float>(*presetPerFrameContext.ob_r) : static_cast<float>(*presetPerFrameContext.ib_r);
Expand All @@ -52,39 +54,23 @@ void Border::Draw(const PerFrameContext& presetPerFrameContext)
float innerRadius = (border == 0) ? 1.0f - outerBorderSize : 1.0f - outerBorderSize - innerBorderSize;
float outerRadius = (border == 0) ? 1.0f : 1.0f - outerBorderSize;

vertices[0].x = innerRadius;
vertices[1].x = outerRadius;
vertices[2].x = outerRadius;
vertices[3].x = innerRadius;
vertices[0].y = innerRadius;
vertices[1].y = outerRadius;
vertices[2].y = -outerRadius;
vertices[3].y = -innerRadius;

for (int rot = 0; rot < 4; rot++)
{
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Point) * 4, vertices.data());
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

// Rotate 90 degrees
// Milkdrop code calculates cos(PI/2) and sin(PI/2), which is 0 and 1 respectively.
// Our code here simplifies the expressions accordingly.
for (int vertex = 0; vertex < 4; vertex++)
{
float const x = vertices[vertex].x;
float const y = vertices[vertex].y;
vertices[vertex].x = -y; // x * cos(PI/2) - y * sin(PI/2) == x * 0 - y * 1
vertices[vertex].y = x; // x * sin(PI/2) + y * cos(PI/2) == x * 1 + y * 0
}
}
m_borderMesh.Vertices().Set({{outerRadius, outerRadius},
{outerRadius, -outerRadius},
{-outerRadius, outerRadius},
{-outerRadius, -outerRadius},
{innerRadius, innerRadius},
{innerRadius, -innerRadius},
{-innerRadius, innerRadius},
{-innerRadius, -innerRadius}});

m_borderMesh.Update();
m_borderMesh.Draw();
}
}

Renderer::Mesh::Unbind();
Renderer::Shader::Unbind();

glDisable(GL_BLEND);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
Renderer::BlendMode::SetBlendActive(false);
}

} // namespace MilkdropPreset
Expand Down
7 changes: 3 additions & 4 deletions src/libprojectM/MilkdropPreset/Border.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "PerFrameContext.hpp"
#include "PresetState.hpp"

#include "Renderer/RenderItem.hpp"
#include <Renderer/Mesh.hpp>

namespace libprojectM {
namespace MilkdropPreset {
Expand All @@ -12,15 +12,13 @@ namespace MilkdropPreset {
/**
* @brief Renders a border around the screen.
*/
class Border : public Renderer::RenderItem
class Border
{
public:
Border() = delete;

explicit Border(PresetState& presetState);

void InitVertexAttrib() override;

/**
* Draws the border.
* @param presetPerFrameContext The per-frame context variables.
Expand All @@ -29,6 +27,7 @@ class Border : public Renderer::RenderItem

private:
PresetState& m_presetState; //!< The global preset state.
Renderer::Mesh m_borderMesh; //!< The border geometry.
};

} // namespace MilkdropPreset
Expand Down
Loading
Loading