将模糊着色器应用于sf :: RectangleShape [英] Applying a blur shader to sf::RectangleShape

查看:82
本文介绍了将模糊着色器应用于sf :: RectangleShape的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,它是sf :: RectangleShapes的二维数组(用于基于图块的游戏).它应该看起来像云,所以我想给它添加一些模糊效果.这是现在的样子:

I have an object that is a 2d array of sf::RectangleShapes (for a tile-based game). It's supposed to look like a cloud, so I want to add some blur to it. Here is what it looks like now:

我希望它看起来像这样:

And I want it to look like this:

本能地,似乎可以在较低的级别上实现这种模糊效果,我将不得不将云对象绘制到缓冲区,然后将模糊对象应用于缓冲区.但是我不确定SFML是否已经这样做了.

Instinctively, it seems to achieve this blur effect on the low level, I would have to draw the cloud object to a buffer, and then apply the blur object to the buffer. But I'm not sure if SFML is doing this already.

在我的主循环中,我有这个:

In my main loop, I have this:

    for( CloudIterator it = clouds.begin(); it != clouds.end(); it++ ) {
        window.draw(**it);
    }

我希望替换为:

    for( CloudIterator it = clouds.begin(); it != clouds.end(); it++ ) {
        window.draw(**it, &blurShader);
    }

从以下GLSL文件中加载blurShader的位置:

Where blurShader is loaded from the following GLSL file:

    uniform sampler2D texture;
    uniform float blur_radius;

    void main()
    {
        vec2 offx = vec2(blur_radius, 0.0);
        vec2 offy = vec2(0.0, blur_radius);

        vec4 pixel = texture2D(texture, gl_TexCoord[0].xy)               * 4.0 +
                     texture2D(texture, gl_TexCoord[0].xy - offx)        * 2.0 +
                     texture2D(texture, gl_TexCoord[0].xy + offx)        * 2.0 +
                     texture2D(texture, gl_TexCoord[0].xy - offy)        * 2.0 +
                     texture2D(texture, gl_TexCoord[0].xy + offy)        * 2.0 +
                     texture2D(texture, gl_TexCoord[0].xy - offx - offy) * 1.0 +
                     texture2D(texture, gl_TexCoord[0].xy - offx + offy) * 1.0 +
                     texture2D(texture, gl_TexCoord[0].xy + offx - offy) * 1.0 +
                     texture2D(texture, gl_TexCoord[0].xy + offx + offy) * 1.0;

        gl_FragColor =  gl_Color * (pixel / 16.0);
    }

但是,结果是乌云完全变成黑色.我必须加载GLSL文件中引用的纹理吗?

However, the result is clouds that are completely black. Is the texture referred to in the GLSL file something I have to load?

我为这些云对象绘制的代码如下所示,从sf :: Drawable重载:

My draw code for these cloud objects looks like this, overloaded from sf::Drawable :

void Cloud::draw(sf::RenderTarget& target, sf::RenderStates states) const {
    states.transform *= getTransform();

    ...loop to draw various sf::RectangleShape's in the Cloud...

}

所以,我也许天真地认为window.draw(**it, &blurShader)可以正常工作.我应该在Cloud :: draw函数中获取并应用着色器吗?

So, I may be a little naive that window.draw(**it, &blurShader) would just work. Should I be fetching and applying the shader in the Cloud::draw function?

推荐答案

但是,结果是乌云完全变成黑色.我必须加载GLSL文件中引用的纹理吗?

However, the result is clouds that are completely black. Is the texture referred to in the GLSL file something I have to load?

我很确定您必须自己设置纹理.如果查看着色器示例,则在加载每个着色器时,作者将当前纹理绑定为texture参数.您可能必须做同样的事情.

I'm pretty sure you'll have to set the texture yourself. If you look at the shader example, when each shader is loaded the author binds the current texture as the texture parameter. You'll probably have to do the same thing.

m_shader.setParameter("texture", sf::Shader::CurrentTexture);

如果sf::Shader::CurrentTexture引用了不同的纹理,则必须显式获得正确的纹理.如果您的sf :: RectangleShapes没有纹理,那么您当然不能模糊那些不存在的东西.

If sf::Shader::CurrentTexture refers to a different texture, then you'll have to explicitly obtain the correct texture. If your sf::RectangleShapes don't have a texture, then of course you can't blur something that isn't there.

如果形状没有自己的纹理,则可以通过将形状渲染为sf::RenderTexture,然后使用sf::RenderTexturesf::RenderTexture渲染sf::Sprite来获得所需的效果.将着色器应用于该绘制调用.

In the case where your shapes do not have a texture of their own, you may be able to achieve the desired effect by rendering the shapes to an sf::RenderTexture, and then rendering an sf::Sprite using the sf::RenderTexture and applying the shader to that draw call.

这篇关于将模糊着色器应用于sf :: RectangleShape的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆