glsl - fragment-shader(blur) - >黑屏

时间:2011-04-23 08:30:25

标签: c++ opengl openframeworks fragment-shader

我一直试图让这个工作在最后几天工作,我真的开始变得绝望了。我真的很感激你的建议。

我一直在努力做的事情:

  • 将场景渲染为FBO
  • 使用glsl着色器模糊附加纹理
  • 将生成的纹理渲染到对齐的四边形屏幕

问题:

如果我启用片段着色器(水平模糊),我会在第一帧左右渲染到四边形的模糊图像,之后它全部变黑。

我怀疑我将纹理传递到着色器的方式有问题:

    horizontalBlurVertex.enable();

    horizontalBlurFragment.enable();

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, fboTexture);
    glUniform1i(glGetUniformLocation(horizontalBlurFragment.program, "RTScene"), 0);

修改 还有一条信息: 如果我删除glUniform1i(glGetUniformLocation(horizo​​ntalBlurFragment.program,“RTScene”),0);我得到完全相同的结果。我猜这表明我的着色器从未获得任何纹理数据。

来源:

顶点着色器:

varying vec2 vTexCoord;

void main(void)
{
   gl_FrontColor = gl_Color;
   gl_Position = ftransform();

   vec2 Pos;
   Pos = sign(gl_Vertex.xy);

   gl_Position = vec4(Pos, 0.0, 1.0);

   vTexCoord = Pos * 0.5 + 0.5;
}

片段着色器:

uniform sampler2D RTScene;
varying vec2 vTexCoord;

const float blurSize = 1.0/800.0;
const float weightSum = 70.0 + 2.0 * (1.0 + 8.0 + 28.0 + 56.0);

void main(void)
{
   vec4 sum = vec4(0.0);

   sum += texture2D(RTScene, vec2(vTexCoord.x - 4.0*blurSize, vTexCoord.y)) * 1.0 / weightSum;
   sum += texture2D(RTScene, vec2(vTexCoord.x - 3.0*blurSize, vTexCoord.y)) * 8.0 / weightSum;
   sum += texture2D(RTScene, vec2(vTexCoord.x - 2.0*blurSize, vTexCoord.y)) * 28.0 / weightSum;
   sum += texture2D(RTScene, vec2(vTexCoord.x - blurSize, vTexCoord.y)) * 56.0 / weightSum;
   sum += texture2D(RTScene, vec2(vTexCoord.x, vTexCoord.y)) * 70.0 / weightSum;
   sum += texture2D(RTScene, vec2(vTexCoord.x + blurSize, vTexCoord.y)) * 56.0 / weightSum;
   sum += texture2D(RTScene, vec2(vTexCoord.x + 2.0*blurSize, vTexCoord.y)) * 28.0 / weightSum;
   sum += texture2D(RTScene, vec2(vTexCoord.x + 3.0*blurSize, vTexCoord.y)) * 8.0 / weightSum;
   sum += texture2D(RTScene, vec2(vTexCoord.x + 4.0*blurSize, vTexCoord.y)) * 1.0 / weightSum;

   gl_FragColor = sum;
}

渲染到fbo:

//handels
    GLuint fbo, fboTexture, fboDepthbuffer;

    // generate namespace for the frame buffer, colorbuffer and depthbuffer
    glGenFramebuffersEXT(1, &fbo);
    glGenTextures(1, &fboTexture);
    glGenRenderbuffersEXT(1, &fboDepthbuffer);

    //switch to our fbo so we can bind stuff to it
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);

    //create the colorbuffer texture and attach it to the frame buffer
    glGenTextures(1, &fboTexture);
    glBindTexture(GL_TEXTURE_2D, fboTexture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, fboTexture, 0);

    // create a render buffer as our depthbuffer and attach it
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fboDepthbuffer);
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height);
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, fboDepthbuffer);

    // Go back to regular frame buffer rendering
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

    glBindTexture(GL_TEXTURE_2D, 0);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
        glPushAttrib(GL_VIEWPORT_BIT);
            glViewport(0, 0, width, height);
            glPushMatrix();
                glBegin(GL_QUADS);
                    glNormal3f(0.0, 0.0, 1.0);

                    glColor3f(1.0f, 1.0f, 0.0f);
                    glVertex3f(-0.1, 0.1, -1.0);

                    glColor3f(1.0f, 0.0f, 0.0f);
                    glVertex3f(0.1, 0.1, -1.0);

                    glColor3f(0.0f, 1.0f, 0.0f);
                    glVertex3f(0.1, -0.1, -1.0);

                    glColor3f(0.0f, 0.0f, 1.0f);
                    glVertex3f(-0.1, -0.1, -1.0);
                glEnd();
            glPopMatrix();
        glPopAttrib();
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

(因为使用更简单的着色器(切换颜色和诸如此类),所以不在此处发布我的ShaderLoader源)

如果您认为错误位于其他地方,我会很乐意发布一些额外的代码。

1 个答案:

答案 0 :(得分:1)

问题解决了!

原来我愚蠢地每一帧都创造了我的fbo ......现在效果很好。