C ++ OpenGL尝试超级采样时没有渲染

时间:2016-12-14 20:53:58

标签: c++ opengl antialiasing fbo

我正在尝试在我的场景中使用SSAA。以前,我已将整个场景设置为FBO,并且工作正常(它在我的屏幕上呈现完美)。但是,一旦我尝试将SSAA应用到它,屏幕总是黑色的,我不确定为什么。我已经遵循了基于iOS的教程,但复制到可以找到here的窗口非常简单  然而正如我在尝试使用类似技术时说的那样它不起作用。我理解SSAA的理论/概念,但似乎无法使其发挥作用。任何帮助我都非常感激,因为我找不到太多的材料(主要是MSAA或其他A.A.技术)

FBO初始化

void InitSSAAFBO()
{
    // Create the on-screen FBO.

    glGenFramebuffers(1, &fbo);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER,
        GL_COLOR_ATTACHMENT0,
        GL_RENDERBUFFER,
        fboColTex);

    glGenRenderbuffers(1, &bigColour);
    glBindRenderbuffer(GL_RENDERBUFFER, bigColour);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8,
        windowWidth * supersamplingLevel, windowHeight * supersamplingLevel);

    glGenRenderbuffers(1, &bigDepth);
    glBindRenderbuffer(GL_RENDERBUFFER, bigDepth);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24,
        windowWidth * supersamplingLevel, windowHeight * supersamplingLevel);

    glGenFramebuffers(1, &fboBig);
    glBindFramebuffer(GL_FRAMEBUFFER, fboBig);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER,
        GL_COLOR_ATTACHMENT0,
        GL_RENDERBUFFER,
        bigColour);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER,
        GL_DEPTH_ATTACHMENT,
        GL_RENDERBUFFER,
        bigDepth);

    // Create a texture object and associate it with the big FBO.

    glGenTextures(1, &offscreenTexture);
    glBindTexture(GL_TEXTURE_2D, offscreenTexture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, windowWidth * supersamplingLevel, windowHeight * supersamplingLevel, 0,
        GL_RGBA, GL_UNSIGNED_BYTE, 0);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
        GL_TEXTURE_2D, offscreenTexture, 0);

    GLenum demoFBOStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);

    if (demoFBOStatus != GL_FRAMEBUFFER_COMPLETE) {

        fboOkay = false;
        cout << "Could not successfully create framebuffer object to render texture!" << endl;

    }
    else {

        fboOkay = true;
        cout << "FBO successfully created" << endl;
    }

    // Unbind FBO for now! (Plug main framebuffer back in as rendering destination)
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

渲染

void display(void) {

    //Bind Large FBO
    glBindFramebuffer(GL_FRAMEBUFFER, fboBig);
    glBindRenderbuffer(GL_RENDERBUFFER, bigColour);
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glViewport(0, 0, windowWidth * supersamplingLevel, windowHeight * supersamplingLevel);
    glEnable(GL_DEPTH_TEST);
    DrawScene();


    //Bind small/Onscreen FBO
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    glBindRenderbuffer(GL_RENDERBUFFER, fboColTex);

    glViewport(0, 0, windowWidth, windowHeight);
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glDisable(GL_DEPTH_TEST);
    glUseProgram(fboShader);
    glBindVertexArray(quadVAO);
    glBindTexture(GL_TEXTURE_2D, offscreenTexture);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glBindVertexArray(0);

    glutSwapBuffers();
}

fboShader(顶点着色器)

#version 330 core
layout (location = 0) in vec2 position;
layout (location = 1) in vec2 texCoords;

out vec2 TexCoords;

void main()
{
    gl_Position = vec4(position.x, position.y, 0.0f, 1.0f); 
    TexCoords = texCoords;
}  

fboShader(片段着色器)

#version 330 core
in vec2 TexCoords;
out vec4 color;

uniform sampler2D screenTexture;

void main()
{ 
    color = texture(screenTexture, TexCoords);
}

0 个答案:

没有答案