OpenGL(GLSL)不写入正确的FrameBuffer纹理

时间:2015-09-15 11:30:57

标签: opengl glsl framebuffer

我有一个附加了两个纹理的帧缓冲区,并且读取调用:

Occlusion = texture2D(OcclusionTexture, TexCoord);
FragColor = texture2D(ColourTexture, TexCoord);

返回同样的事情。它们返回驻留在纹理OcclusionTexture中的数据。请帮忙,对不起代码墙。

着色器代码: 附件

 subroutine void RenderPassType();
    subroutine uniform RenderPassType RenderPass;

    uniform sampler2D ColourTexture;
    uniform sampler2D OcclusionTexture;
    layout(location = 0) out vec4 FragColor; // Actual fragment colour;
    layout(location = 1) out vec3 OutColour;
    layout(location = 2) out vec3 OcclusionColour;

写作和阅读

    subroutine (RenderPassType) void Write()
    {
        // Writing
        OcclusionColour = vec3(m_Light[0].Intensity);
        OutColour = vec3(ApplyFog(ViewPos, Colour, fogColour,                   fogDistance)); 
    } 

    subroutine (RenderPassType) void Read()
    {
        // Reading
        vec4 Occlusion = texture2D(OcclusionTexture, TexCoord);
        FragColor = texture2D(ColourTexture, TexCoord);
    }

用于生成帧缓冲区和纹理的代码:

// Create and bind the FBO
glGenFramebuffers(1, &FBO);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);

// The depth buffer
glGenRenderbuffers(1, &DepthBuff);
glBindRenderbuffer(GL_RENDERBUFFER, DepthBuff);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 
    settings::GameSettings::GetSingleton()->Screen.x, settings::GameSettings::GetSingleton()->Screen.y);

// The color buffer
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &ScreenTex);
glBindTexture(GL_TEXTURE_2D, ScreenTex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, settings::GameSettings::GetSingleton()->Screen.x
    , settings::GameSettings::GetSingleton()->Screen.y, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

// The occlusion buffer
glActiveTexture(GL_TEXTURE1);
glGenTextures(1, &OcclusionTex);
glBindTexture(GL_TEXTURE_2D, OcclusionTex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, settings::GameSettings::GetSingleton()->Screen.x
    , settings::GameSettings::GetSingleton()->Screen.y, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

// Attach the images to the framebuffer
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, DepthBuff);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ScreenTex, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, OcclusionTex, 0);

GLenum drawBuffers[] = { GL_NONE, GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};

glDrawBuffers(3, drawBuffers);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

SetUniform("ColourTexture", ScreenTex);
SetUniform("OcclusionTexture", OcclusionTex);

/////////////////////////////////////////////// /////// 编辑 ////////////////////////////////////////////////// ////

首先我将场景渲染到纹理。在第二遍中,我只读取纹理并进行体积光照计算,然后将最终的碎片颜色推送到附件0.

2 个答案:

答案 0 :(得分:3)

您的纹理绑定错误。看起来应该是这样的:

        <key>NSAppTransportSecurity</key>
        <dict>
            <key>NSExceptionDomains</key>
            <dict>
                <key>cloudfront.net</key>
                    <dict>
                    <!--Include to allow subdomains-->
                    <key>NSIncludesSubdomains</key>
                    <true/>
                    <key>NSExceptionRequiresForwardSecrecy</key>
                    <false/>
                </dict>
            </dict>
        </dict>

注意:重要的部分是,活动纹理单元的数量传递给制服而不是纹理ID。

答案 1 :(得分:-1)

纹理的问题在于我绑定并解除了着色器程序的绑定。

void StandardLightingShader::Begin() 
{
    glUseProgram(m_shaderProgramHandle);

    // Set up Pass
    glBindFramebuffer(GL_FRAMEBUFFER, FBO);
    glEnable(GL_DEPTH_TEST);
    glUniformSubroutinesuiv(GL_FRAGMENT_SHADER, 1, &pass1Index);
    SetUniform("ScreenSize", settings::GameSettings::GetSingleton()->Screen);
    SetUniform("ScreenOffSet", settings::GameSettings::GetSingleton()->ScreenPosition);

    // Bind the textures to texture units.
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, ScreenTex);

    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, OcclusionTex);

}

启动着色器时,纹理仅绑定到纹理单元,因为着色器未绑定,它也解除了纹理单元的绑定。修复是:

SetUniform("ColourTexture", 0);

所以基本上如果你取消绑定你的程序,你需要将纹理重新绑定到纹理单位,但实际的统一变量将通过着色器更改持续存在。

Labels <- vector(mode = "list") # Make a list called "Labels"
Labels[[1]] <- c(1,3,5,7) # Assign value to the first list element
names(Labels)[1] <- "Name" # Assign the name "Name" to the first list element
print(Labels)
print(Labels$Name)
# [1] 1 3 5 7

# Now I have the name of the first list element "Name"
# in the variable "ElementName"
ElementName <- "Name"

# How R will understand in the next line
# that I refer to the value "Name" of the variable "ElementName"
# in order to get 
# [1] 1 3 5 7

print(Labels$ElementName)