关于纹理的多重采样

时间:2013-01-02 09:52:56

标签: opengl textures fragment-shader multisampling

我在非默认FBO中渲染带有绿色背景的白色立方体,并将创建的多重采样纹理附加到此FBO。 当我使用上面的纹理在默认FBO中渲染立方体时,它会在纹理中产生损坏。

这是我的代码:

viewport_width=32;
viewport_height=32;
        glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textureId);
        nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindTexture(*target, textureId);"));     

        glUniform1i(glGetUniformLocation(shader_data.psId,"tk_diffuseMap"), 0);
        nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glUniform1i(glGetUniformLocation(shader_data.psId,\"basetexture\"), 0);"));

        glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4 ,GL_RGBA, viewport_width, viewport_height ,true);  
        nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4 ,GL_RGBA, 32, 32,true);"));       

        glBindFramebuffer(GL_DRAW_FRAMEBUFFER, Fboid);
        nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindFramebuffer(GL_FRAMEBUFFER, Fboid);"));

        glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D_MULTISAMPLE,textureId,0);    
        nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D_MULTISAMPLE,textureId,0);"));

        glEnable(GL_MULTISAMPLE);
        nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glEnable(GL_MULTISAMPLE);"));

        draw_cube(viewport_width, viewport_height);

                glBindFramebuffer(GL_FRAMEBUFFER, 0);
        nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindFramebuffer(GL_FRAMEBUFFER, Fboid);"));

        glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textureId);
        nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindTexture(*target, textureId);"));     

                glDrawElements ( GL_TRIANGLES, 36,GL_UNSIGNED_INT, indices );

片段着色器:

uniform sampler2DMS tk_diffuseMap;
in vec3 ps_texCoord;
out vec4 fragColor;

void main(void)
{

    vec2 iTmp = textureSize(tk_diffuseMap);
    vec2 tmp = iTmp * ps_texCoord.xy;

    vec4 color;
    for(int i = 0; i < 4; ++i)
    {
        color = color + texelFetch(tk_diffuseMap, ivec2(tmp), i);
    }

fragColor=vec4(color/4);
}

让我知道我哪里出错了。

1 个答案:

答案 0 :(得分:3)

用作FBO的渲染目标附件的纹理如果附加到其上的FBO,则不得绑定为采样源,也必须绑定为渲染目标。它要么绑定纹理要么绑定FBO。

很明显为什么必须如此:如果你可以绑定一个也是渲染目标的纹理,你就会创建一个循环依赖循环。