From da45497a3dc91a756a4460388a97b14c3b09b1d0 Mon Sep 17 00:00:00 2001 From: reuk Date: Tue, 14 Jan 2025 19:59:36 +0000 Subject: [PATCH] GraphicsDemo: Add new demo showing blur and transparency effects --- examples/GUI/GraphicsDemo.h | 77 +++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/examples/GUI/GraphicsDemo.h b/examples/GUI/GraphicsDemo.h index 8ca9a15339..d28bebf797 100644 --- a/examples/GUI/GraphicsDemo.h +++ b/examples/GUI/GraphicsDemo.h @@ -516,6 +516,82 @@ public: std::unique_ptr svgDrawable; }; +class BlurDemo final : public GraphicsDemoBase +{ +public: + BlurDemo (ControllersComponent& cc) + : GraphicsDemoBase (cc, "Blur") + { + image.setBackupEnabled (false); + } + + void drawDemo (Graphics& g) override + { + const auto a = lopassA.next (jmap (frequencyA.getValue(), 0.09f, 0.12f)); + const auto b = lopassB.next (jmap (frequencyB.getValue(), 0.09f, 0.12f)); + + initialPhase += 0.01f; + initialPhase -= (float) (int) initialPhase; + const auto startAngle = initialPhase * MathConstants::twoPi; + const auto centreSquare = image.getBounds().reduced (100); + + { + Graphics g2 { image }; + g2.setColour (Colours::transparentBlack); + g2.excludeClipRegion (centreSquare); + g2.getInternalContext().fillRect (image.getBounds(), true); + } + + if (auto ptr = image.getClippedImage (centreSquare).getPixelData()) + { + ptr->applyGaussianBlurEffect (7.0f); + ptr->multiplyAllAlphas (0.98f); + } + + { + Graphics g2 { image }; + const auto baseColour = Colours::cyan; + const auto destColour = Colours::magenta; + const auto offset = image.getBounds().getCentre().toFloat(); + const auto numSegments = 200; + + for (auto i = 0; i < numSegments; ++i) + { + g2.setColour (baseColour.interpolatedWith (destColour, (float) i / numSegments)); + + const auto getPoint = [&] (auto ind) + { + return offset + Point { 200 * std::sin (startAngle + a * (float) ind), + 200 * std::cos (startAngle + b * (float) ind) }; + }; + + g2.drawLine ({ getPoint (i), getPoint (i + 1) }, 2.0f); + } + } + + AffineTransform transform (AffineTransform::translation ((float) (-image.getWidth() / 2), + (float) (-image.getHeight() / 2)) + .followedBy (getTransform())); + + g.setOpacity (getAlpha()); + g.drawImageTransformed (image, transform, false); + } + + class Lopass + { + public: + float next (float f) { return value += (f - value) * 0.05f; } + + private: + float value{}; + }; + + Image image { Image::ARGB, 512, 512, true }; + SlowerBouncingNumber frequencyA, frequencyB; + Lopass lopassA, lopassB; + float initialPhase = 0.0f; +}; + //============================================================================== class LinesDemo final : public GraphicsDemoBase { @@ -687,6 +763,7 @@ public: demos.add (new ImagesRenderingDemo (controls, false, true)); demos.add (new ImagesRenderingDemo (controls, true, false)); demos.add (new ImagesRenderingDemo (controls, true, true)); + demos.add (new BlurDemo (controls)); demos.add (new GlyphsDemo (controls)); demos.add (new SVGDemo (controls)); demos.add (new LinesDemo (controls));