如何像Canvas globalCompositeOperation那样模拟更轻的效果呢?

时间:2011-12-09 22:06:23

标签: c++ html5-canvas sfml

每年年底我都会制作烟花汇演画布效果(Javascript),最后一次是:

http://js1k.com/2011-dysentery/demo/955

这种效果很简单:很多圆圈都填充了createRadialGradient()方法,并且有一些透明度。这个秘密就是财产:

globalCompositeOperation = "lighter";

使用时,所有颜色都混合在一起,给人以光的感觉。今年,我想做同样的事情,但是在SFML / C ++中。我是SFML的初学者,不知道从哪里开始制作这个效果。谁能告诉我如何在SFML中模拟Canvas globalCompositeOperation()?

1 个答案:

答案 0 :(得分:5)

您可以使用sf::Blend::Mode获得此效果。见这个例子:

    #include <SFML/Graphics.hpp>

    int main()
    {
        sf::RenderWindow window(sf::VideoMode(400, 400), "Blend::Mode Sample");
        sf::Shape square = sf::Shape::Rectangle(100, 100, 200, 200, sf::Color(250, 30, 30));
        sf::Shape circle1 = sf::Shape::Circle(100, 100, 50, sf::Color(30, 30, 250));
        sf::Shape circle2 = sf::Shape::Circle(300, 100, 50, sf::Color(30, 30, 250));
        sf::Shape circle3 = sf::Shape::Circle(100, 300, 50, sf::Color(30, 30, 250));
        sf::Shape circle4 = sf::Shape::Circle(300, 300, 50, sf::Color(30, 30, 250));
        circle1.SetBlendMode(sf::Blend::Alpha);
        circle2.SetBlendMode(sf::Blend::Add);
        circle3.SetBlendMode(sf::Blend::Multiply);
        circle4.SetBlendMode(sf::Blend::None);
        while (window.IsOpened())
        {
            sf::Event event;
            while (window.PollEvent(event))
                if (event.Type == sf::Event::Closed)
                    window.Close();

            window.Clear();
            window.Draw(square);
            window.Draw(circle1);
            window.Draw(circle2);
            window.Draw(circle3);
            window.Draw(circle4);
            window.Display();
        }
    }

输出:

我认为你想要的效果是SetBlendMode(sf::Blend::Add)