can't draw to screen after drawing to framebuffer

Started by
0 comments, last by JohnnyCode 9 years, 6 months ago

Hey!

i am having some issues with my framebuffers, i have successfully implemented deferred rendering and now instead of drawing the result of the light calculations on to screen i created a second frame buffer with a single texture that holds the results of the calculations, this works fine as i can see the texture getting the correct data in the OpenGL debugger. However when i try to draw to a quad on the screen with the result from the second frame buffer all i get is black, even when i try to draw without the texture just using the uv coordinates as the colors, this works fine if i skip the entire deferred rendering pass and just draw the screen quad colorized so the quad itself works fine.


{
    // Create the FBO
    glGenFramebuffers(1, &m_fbo);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);
    
    // Create the gbuffer textures
    glGenTextures(ARRAY_SIZE_IN_ELEMENTS(m_textures), m_textures);
    glGenTextures(1, &m_depthTexture);
    
    for (unsigned int i = 0 ; i < ARRAY_SIZE_IN_ELEMENTS(m_textures) ; i++) {
        glBindTexture(GL_TEXTURE_2D, m_textures[i]);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, WindowWidth, WindowHeight, 0, GL_RGB, GL_FLOAT, NULL);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, m_textures[i], 0);
    }
    
    // depth
    glBindTexture(GL_TEXTURE_2D, m_depthTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, WindowWidth, WindowHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT,
                 NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthTexture, 0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    
    
    GLenum DrawBuffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
    glDrawBuffers(ARRAY_SIZE_IN_ELEMENTS(DrawBuffers), DrawBuffers);
    
    GLenum Status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    
    if (Status != GL_FRAMEBUFFER_COMPLETE) {
        printf("FB error, status: 0x%x\n", Status);
        return false;
    }
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    glBindTexture(GL_TEXTURE_2D, 0);
    // restore default FBO
    
    glGenFramebuffers(1, &m_fbopost);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbopost);

    glGenTextures(1, &postTexture);
  
    glBindTexture(GL_TEXTURE_2D, postTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, WindowWidth, WindowHeight, 0, GL_RGB, GL_FLOAT, NULL);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , GL_TEXTURE_2D, postTexture, 0);
    

    glBindTexture(GL_TEXTURE_2D, 0);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

    return true;
}


void GBuffer::BindForWritingPost()
{
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbopost);
}
void GBuffer::BindForWriting()
{
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);
}
void GBuffer::BindFortReadingPost()
{
    glBindFramebuffer(GL_READ_FRAMEBUFFER,m_fbopost);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, postTexture);
}
void GBuffer::BindForReading()
{
    //glBindFramebuffer(GL_READ_FRAMEBUFFER,0);
    glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo);
    for (unsigned int i = 0 ; i < ARRAY_SIZE_IN_ELEMENTS(m_textures); i++) {
        glActiveTexture(GL_TEXTURE0 + i);
        glBindTexture(GL_TEXTURE_2D, m_textures[GBUFFER_TEXTURE_TYPE_POSITION + i]);
    }
}


while (!window.ShouldClose()) { 
_update_fps_counter(window.GetWindow()); 
DSGeometryPass(); 
BeginLightPasses(); 
DSPointLightsPass(); 
PostEffects(); 
glfwSwapInterval(0); 
glfwSwapBuffers(window.GetWindow()); 
glfwPollEvents (); 
glClearColor(0.f, 0.f, 0.f, 0.f); 
}


void DSGeometryPass()
{
    //retina
  //  glViewport(0, 0, window.GetFrameBufferWidth()/2, window.GetFrameBufferHeight()/2);

    gBuffer.BindForWriting();
  
    glDepthMask(GL_TRUE);
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glEnable(GL_DEPTH_TEST);
    
    glDisable(GL_BLEND);

    camera.ComputeMatricesFromInputs(window.GetWindow());
    ViewMatrix = camera.GetViewMatrix();
    ProjectionMatrix = camera.GetProjectionMatrix();
    heightMap.Update(ViewMatrix, ProjectionMatrix);
    heightMap.Draw();
//    for (int i = 0; i < spheres.size(); i++) {
//        spheres[i].Update(&window, ViewMatrix, ProjectionMatrix);
//        spheres[i].Draw(&camera, shadow.GetShadowMatrix());
//    }
    glDepthMask(GL_FALSE);
    
    glDisable(GL_DEPTH_TEST);
    
}

void BeginLightPasses()
{
    glEnable(GL_BLEND);
    glBlendEquation(GL_FUNC_ADD);
    glBlendFunc(GL_ONE, GL_ONE);

    gBuffer.BindForReading();
    gBuffer.BindForWritingPost();

    glClear(GL_COLOR_BUFFER_BIT);
}


void DSPointLightsPass()
{
  //  glViewport(0, 0, window.GetFrameBufferWidth(), window.GetFrameBufferHeight());

    angle +=0.005;
    angle2 -= 0.03f;
   // pLight.setPosition(pLight.GetPosition().x+(glm::vec3(((float)cos(angle))*100,0.f,pLight.GetPosition().z+((float)sin(angle)*100))));
    pLight2.setPosition((glm::vec3(((float)cos(angle2))*100,0.f,((float)sin(angle2)*100))));

    sphere2.SetPosition(pLight2.GetPosition());
    sphere2.Update(&window,  camera.GetViewMatrix(), camera.GetProjectionMatrix());
    float BSphereScale2 = CalcPointLightBSphere(pLight2);
    sphere2.Scale(glm::vec3(BSphereScale2,BSphereScale2,BSphereScale2));
    sphere2.Draw(&camera, shadow.GetShadowMatrix());
    
    sphere.SetPosition(pLight.GetPosition());
    sphere.Update(&window, camera.GetViewMatrix(), camera.GetProjectionMatrix());
    float BSphereScale = CalcPointLightBSphere(pLight);
    sphere.Scale(glm::vec3(BSphereScale,BSphereScale,BSphereScale));
    sphere.Draw(&camera, shadow.GetShadowMatrix());
}


void PostEffects()
{
    if(glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER)==0)
    {
        int i = 0;
    }
  //  gBuffer.BindFortReadingPost();
    screenQuad.Draw();

}

//
//  ScreenQuad.cpp
//  3D2 projekt scratch
//

#include "GBuffer.h"
#include "ScreenQuad.h"
static const GLfloat g_quad_vertex_buffer_data[] = {
    -1.0f, -1.0f, 0.0f,
    1.0f, -1.0f, 0.0f,
    -1.0f,  1.0f, 0.0f,
    -1.0f,  1.0f, 0.0f,
    1.0f, -1.0f, 0.0f,
    1.0f,  1.0f, 0.0f,
};

void _print_shader_info_log (unsigned int shader_index) {
    int max_length = 2048;
    int actual_length = 0;
    char log[2048];
    glGetShaderInfoLog (shader_index, max_length, &actual_length, log);
    printf ("shader info log for GL index %i:\n%s\n", shader_index, log);
}
void ScreenQuad::Init()
{
    glGenVertexArrays(1,&VAO);
    glBindVertexArray(VAO);
    
    glGenBuffers(1,&Buffer);
    glBindBuffer(GL_ARRAY_BUFFER,Buffer);
    glBufferData(GL_ARRAY_BUFFER,sizeof( g_quad_vertex_buffer_data), g_quad_vertex_buffer_data,GL_STATIC_DRAW);
    
   
    
    ShaderInfo shaders[] = {
        {GL_VERTEX_SHADER,"resources/PostProcess.vs"},
        {GL_FRAGMENT_SHADER,"resources/PostProcess.fs"},
        {GL_NONE,NULL}
    };

    glVertexAttribPointer(
                          0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
                          3,                  // size
                          GL_FLOAT,           // type
                          GL_FALSE,           // normalized?
                          0,                  // stride
                          (void*)0            // array buffer offset
                          );
    
    program = LoadShaders(shaders);
    glUseProgram(program);
    glEnableVertexAttribArray(0);


}
void ScreenQuad::Draw()
{
    glUseProgram(program);
    
    int test = 6;
    glActiveTexture(GL_TEXTURE0+test);
    glBindTexture(GL_TEXTURE_2D,test);
    glUniform1i(glGetUniformLocation(program, "screen"), 6);

  // glUniform1i(glGetUniformLocation(program, "screen"),test);

       // ensure the proper arrays are enabled
    glBindBuffer(GL_ARRAY_BUFFER, Buffer);
  
    glEnableVertexAttribArray(0);

    
    
    glBindVertexArray(VAO);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    
    glDisableVertexAttribArray(0);
    
}

#version 410

uniform sampler2D screen;
in vec2 UV;

out vec4 fragColor;
void main()
{
    fragColor = vec4(UV.x,UV.y,1,1);//texture(screen,UV);//
}

thanks for your time smile.png

Advertisement

glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);

You need to cal this after the buffer is bound

glBindBuffer(GL_ARRAY_BUFFER, Buffer);

And also your vertex attribute pointer does not match yout attribute declaration in shader, that is

in vec2 UV;

you should use vec3 , and also, apropriately transform projected X,Y to texture space, you cannot just use them right away

This topic is closed to new replies.

Advertisement