c++ - GLSL 径向模糊的奇怪问题

标签 c++ opengl glsl blur

使用的着色器:http://www.gamerendering.com/2008/12/20/radial-blur-filter/

我的问题是:整个场景只占据了四分之一的屏幕空间(这是一个由这些坐标组成的矩形:width/2,0 width,height/2),而且它似乎已经被字面剪裁了(未缩放)。它还用倒置的 Y 渲染它(但我能够通过从第 9 行的 gl_Position.y 中删除 - 符号来解决这个问题)。

我用了this tutorial进行后处理,除了使用基本的 glBegin(GL_QUADS) 而不是顶点数组。


编辑 2:我尝试使用另一个着色器 ( http://www.geeks3d.com/20110428/shader-library-swirl-post-processing-filter-in-glsl/ ),它工作得很好。无剪裁。

编辑:这是着色器的来源(我做了一些小的编辑):

片段着色器:

#version 140

// This texture should hold the image to blur.
// This can perhaps be a previous rendered scene.
uniform sampler2D tex;

// texture coordinates passed from the vertex shader
varying vec2 uv;

// some const, tweak for best look
const float sampleDist = 1.0;
const float sampleStrength = 20.2; 

void main(void)
{
   // some sample positions
   float samples[10] =
   float[](-0.08,-0.05,-0.03,-0.02,-0.01,0.01,0.02,0.03,0.05,0.08);

    // 0.5,0.5 is the center of the screen
    // so substracting uv from it will result in
    // a vector pointing to the middle of the screen
    vec2 dir = 0.5 - uv; 

    // calculate the distance to the center of the screen
    float dist = sqrt(dir.x*dir.x + dir.y*dir.y); 

    // normalize the direction (reuse the distance)
    //dir = dir/dist; 
    dir /= dist;

    // this is the original colour of this fragment
    // using only this would result in a nonblurred version
    vec4 color = texture2D(tex,uv); 

    vec4 sum = color;

    // take 10 additional blur samples in the direction towards
    // the center of the screen
    for (int i = 0; i < 10; i++)
    {
      sum += texture2D( tex, uv + dir * samples[i] * sampleDist );
    }

    // we have taken eleven samples
    sum *= 1.0/11.0;

    // weighten the blur effect with the distance to the
    // center of the screen ( further out is blurred more)
    float t = dist * sampleStrength;
    t = clamp( t ,0.0,1.0); //0 <= t <= 1

    //Blend the original color with the averaged pixels
    gl_FragColor = mix( color, sum, t );
}

顶点着色器:

#version 140

varying vec2  uv;
// this vertex shader is from AMD RenderMonkey
void main(void)
{
   gl_Position = vec4( gl_Vertex.xy, 0.0, 1.0 );
   gl_Position = sign( gl_Position );

   // Texture coordinate for screen aligned (in correct range):
   uv = (vec2( gl_Position.x, gl_Position.y ) + vec2(1.0 )) / vec2( 2.0 );
}

后处理器:

#include "std/opengl.h"

#include "postprocess.h"

Post::Post(Pos2D res) {
        this->res = res;
        glActiveTexture(GL_TEXTURE0);
        glGenTextures(1, &this->texture);
        glBindTexture(GL_TEXTURE_2D, this->texture);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, res.x, res.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
        glBindTexture(GL_TEXTURE_2D, 0);
        glGenRenderbuffers(1, &this->rbo);
        glBindRenderbuffer(GL_RENDERBUFFER, this->rbo);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, res.x, res.y);
        glBindRenderbuffer(GL_RENDERBUFFER, 0);
        glGenFramebuffers(1, &this->fbo);
        glBindFramebuffer(GL_FRAMEBUFFER, this->fbo);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->texture, 0);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, this->rbo);
        GLenum status;
        if ((status = glCheckFramebufferStatus(GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE) {
                fprintf(stderr, "Framebuffer error! %i", status);
        }
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

Post::~Post() {
        glDeleteRenderbuffers(1, &this->rbo);
        glDeleteTextures(1, &this->texture);
        glDeleteFramebuffers(1, &this->fbo);
}

void Post::update(Pos2D res) {
        this->res = res;
        glBindTexture(GL_TEXTURE_2D, this->texture);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, res.x, res.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
        glBindTexture(GL_TEXTURE_2D, 0);
        glBindRenderbuffer(GL_RENDERBUFFER, this->rbo);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, res.x, res.y);
        glBindRenderbuffer(GL_RENDERBUFFER, 0);
}

void Post::bind() {
        glBindFramebuffer(GL_FRAMEBUFFER, this->fbo);
}

void Post::unbind() {
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void Post::render(Shader* shader) {
        glClear(GL_COLOR_BUFFER_BIT);
        glClearColor(0.0, 0.0, 0.0, 1.0);

        shader->run();
        auto tex = glGetUniformLocation(shader->program_handle, "tex");
        glBindTexture(GL_TEXTURE_2D, this->texture);
        glUniform1i(tex, 0);
        glBegin(GL_QUADS);
          glTexCoord2f(0.0, 0.0);
          glVertex2f(0, 0);
          glTexCoord2f(0.0, 1.0);
          glVertex2f(0, this->res.y);
          glTexCoord2f(1.0, 1.0);
          glVertex2f(this->res.x, this->res.y);
          glTexCoord2f(1.0, 0.0);
          glVertex2f(this->res.x, 0);
        glEnd();
        glBindTexture(GL_TEXTURE_2D, 0);
        glUseProgram(0);
}

问题截图(如你所见,截掉了):buggy blur

我禁用着色器时的屏幕截图(如您所见,它可以渲染到整个窗口,没有被裁剪):no blur no bug

最佳答案

问题出在您定义顶点坐标的方式上:

    glTexCoord2f(0.0, 0.0);
    glVertex2f(0, 0);
    glTexCoord2f(0.0, 1.0);
    glVertex2f(0, this->res.y);
    glTexCoord2f(1.0, 1.0);
    glVertex2f(this->res.x, this->res.y);
    glTexCoord2f(1.0, 0.0);
    glVertex2f(this->res.x, 0);

不要使用窗口分辨率作为顶点坐标,而是尝试使用以下顶点绘制四边形:( -1, -1 )、( -1, 1 )、( 1, 1 ) 和 ( 1, -1 ) -这将导致 opengl 的 native 坐标系中的全屏四边形。在你的顶点着色器中,在我看来你正在尝试使用符号函数将顶点坐标转换为这个坐标系:

   gl_Position = vec4( gl_Vertex.xy, 0.0, 1.0 );
   gl_Position = sign( gl_Position );

但当然你会得到一个角点为 ( 0, 0 )、( 0, 1 )、( 1, 1 ) 和 ( 1, 0 ) 的四边形,这解释了为什么它只填充了部分屏幕。 结果,您在着色器中使用此行计算的纹理坐标:

uv = (vec2( gl_Position.x, gl_Position.y ) + vec2(1.0 )) / vec2( 2.0 );

也是错误的,导致你正在经历的“剪裁”外观 - 你的 texcoords 范围从 0.5 到 1,而不是 0 到 1。顺便说一句,你明确定义 texcoords 无论如何,那些 是正确的 - 因此无需在着色器中再次计算它们,只需使用内置的 gl_MultiTexcoord。

关于c++ - GLSL 径向模糊的奇怪问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16432297/

相关文章:

opengl - 在opengl中围绕兴趣点旋转相机

c++ - 如何在 OpenGL(使用 GLUT)上绘制像圆锥体一样的填充包络?

c++ - OpenGL GL_TEXTURE1 不显示

OpenGL 视锥教学法

c++ - 在 C++ 中使用 const 引用参数访问函数成员的最佳实践

c++ - 从 C-Style 字符串 C++ 中删除字符

c++ - 引用不完整的类类型 - C++

c++指针的内存分配过程

opengl - 实时图形中的骨骼动画插值

opengl-es - 从 FBO 设置和获取数据