1

我在非默认 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);
}

让我知道我哪里出错了。

4

1 回答 1

3

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

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

于 2013-01-02T10:28:30.543 回答