1

我已经实现了一个简单的高斯模糊并将其与延迟着色相结合,并且它可以工作。当我尝试将另一个对象添加到场景中时,该对象也会受到模糊的影响,这不是我想要的。是否可以使模糊仅影响目标对象的图像,还是应该影响整个场景?

对于模糊,我使用了 2 个顶点着色器,一个用于计算水平纹理坐标,另一个用于计算垂直纹理坐标。两个顶点着色器都使用相同的片段着色器,它只计算这些预先计算的纹理坐标上纹素颜色的高斯加权平均值。

模糊对象的片段着色器:

vec3 color;
    color += texture(gColorSpec, v_blurTexCoords[ 0]).rgb *0.0044299121055113265;
    color += texture(gColorSpec, v_blurTexCoords[ 1]).rgb*0.00895781211794;
    color += texture(gColorSpec, v_blurTexCoords[ 2]).rgb*0.0215963866053;
    color += texture(gColorSpec, v_blurTexCoords[ 3]).rgb*0.0443683338718;
    color += texture(gColorSpec, v_blurTexCoords[ 4]).rgb*0.0776744219933;
    color += texture(gColorSpec, v_blurTexCoords[ 5]).rgb*0.115876621105;
    color += texture(gColorSpec, v_blurTexCoords[ 6]).rgb*0.147308056121;
    color += texture(gColorSpec, v_texCoord         ).rgb*0.159576912161;
    color += texture(gColorSpec, v_blurTexCoords[ 7]).rgb*0.147308056121;
    color += texture(gColorSpec, v_blurTexCoords[ 8]).rgb*0.115876621105;
    color += texture(gColorSpec, v_blurTexCoords[ 9]).rgb*0.0776744219933;
    color += texture(gColorSpec, v_blurTexCoords[10]).rgb*0.0443683338718;
    color += texture(gColorSpec, v_blurTexCoords[11]).rgb*0.0215963866053;
    color += texture(gColorSpec, v_blurTexCoords[12]).rgb*0.00895781211794;
    color += texture(gColorSpec, v_blurTexCoords[13]).rgb*0.0044299121055113265;
    ...

在这种情况下gColorSpec是延迟着色的颜色样本(图像)。

在延迟着色的闪电通道中,我激活了第一个和第二个顶点着色器。

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        //object without blur
        ShadersWithoutBlur.Use();
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, gPosition);
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, gNormal);
        glActiveTexture(GL_TEXTURE2);
        glBindTexture(GL_TEXTURE_2D, gColorSpec);
        // Also send light relevant uniforms
        glUniform3fv(glGetUniformLocation(ShadersWithoutBlur.shaderProgram, "lightPos"), 1, &lightPos[0]);
        glUniform3fv(glGetUniformLocation(ShadersWithoutBlur.shaderProgram, "lightColor"), 1, &lightColor[0]);
        glUniform3fv(glGetUniformLocation(ShadersWithoutBlur.shaderProgram, "viewPos"), 1, &camera.Position[0]);
        glUniform3fv(glGetUniformLocation(ShadersWithoutBlur.shaderProgram, "kd"), 1, &kd[0]);

        //Blur object
        FirstLightningPass.Use();
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, gPosition);
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, gNormal);
        glActiveTexture(GL_TEXTURE2);
        glBindTexture(GL_TEXTURE_2D, gColorSpec);
        // Also send light relevant uniforms
        glUniform3fv(glGetUniformLocation(FirstLightningPass.shaderProgram, "lightPos"), 1, &lightPos[0]);
        glUniform3fv(glGetUniformLocation(FirstLightningPass.shaderProgram, "lightColor"), 1, &lightColor[0]);
        glUniform3fv(glGetUniformLocation(FirstLightningPass.shaderProgram, "viewPos"), 1, &camera.Position[0]);
        glUniform3fv(glGetUniformLocation(FirstLightningPass.shaderProgram, "kd"), 1, &kd[0]);

        SecondLightningPass.Use();
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, gPosition);
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, gNormal);
        glActiveTexture(GL_TEXTURE2);
        glBindTexture(GL_TEXTURE_2D, gColorSpec);
        glUniform3fv(glGetUniformLocation(SecondLightningPass.shaderProgram, "lightPos"), 1, &lightPos[0]);
        glUniform3fv(glGetUniformLocation(SecondLightningPass.shaderProgram, "lightColor"), 1, &lightColor[0]);
        glUniform3fv(glGetUniformLocation(SecondLightningPass.shaderProgram, "viewPos"), 1, &camera.Position[0]);
        glUniform3fv(glGetUniformLocation(SecondLightningPass.shaderProgram, "kd"), 1, &kd[0]);
        // Finally render quad
        RenderQuad();   

已经尝试过:当我使用延迟着色时,我尝试制作另一个帧缓冲区来渲染几何图形,然后仅用于模糊图像对象。这不起作用,因为我似乎只需要一个帧缓冲区来加起来整个场景的几何图形。我还尝试添加另一个附件来替换gColorSpec,只是为了检查它是否与gColorSpec另一个图像对象的另一个连接。

4

0 回答 0